Skip to content

Commit

Permalink
RPC improvements for example apps (#11285)
Browse files Browse the repository at this point in the history
* RPC: Fix lighting service

Was using the wrong cluster for on/off within lighting RPC service

* RPC: Add pairing info to device service

* RPC: Increase NRF Zephyr TTY Rx buffer

* RPC: Add attributes RPC service

Uses ember API to write/read matter attributes.

* RPC: Add locking RPC to all-clusters ESP app
  • Loading branch information
rgoliver authored and pull[bot] committed Apr 1, 2023
1 parent c64d925 commit c46a6b4
Show file tree
Hide file tree
Showing 19 changed files with 453 additions and 21 deletions.
26 changes: 26 additions & 0 deletions examples/all-clusters-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ include(${PIGWEED_ROOT}/pw_build/pigweed.cmake)
include(${PIGWEED_ROOT}/pw_protobuf_compiler/proto.cmake)
set(dir_pw_third_party_nanopb "${CHIP_ROOT}/third_party/nanopb/repo" CACHE STRING "" FORCE)

pw_proto_library(attributes_service
SOURCES
${CHIP_ROOT}/examples/common/pigweed/protos/attributes_service.proto
INPUTS
${CHIP_ROOT}/examples/common/pigweed/protos/attributes_service.options
PREFIX
attributes_service
STRIP_PREFIX
${CHIP_ROOT}/examples/common/pigweed/protos
DEPS
pw_protobuf.common_protos
)

pw_proto_library(button_service
SOURCES
${CHIP_ROOT}/examples/common/pigweed/protos/button_service.proto
Expand Down Expand Up @@ -160,6 +173,17 @@ pw_proto_library(lighting_service
pw_protobuf.common_protos
)

pw_proto_library(locking_service
SOURCES
${CHIP_ROOT}/examples/common/pigweed/protos/locking_service.proto
PREFIX
locking_service
STRIP_PREFIX
${CHIP_ROOT}/examples/common/pigweed/protos
DEPS
pw_protobuf.common_protos
)

pw_proto_library(wifi_service
SOURCES
${CHIP_ROOT}/examples/ipv6only-app/common/wifi_service/wifi_service.proto
Expand All @@ -174,9 +198,11 @@ pw_proto_library(wifi_service
)

target_link_libraries(${COMPONENT_LIB} PUBLIC
attributes_service.nanopb_rpc
button_service.nanopb_rpc
device_service.nanopb_rpc
lighting_service.nanopb_rpc
locking_service.nanopb_rpc
wifi_service.nanopb_rpc
pw_checksum
pw_hdlc
Expand Down
18 changes: 16 additions & 2 deletions examples/all-clusters-app/esp32/main/Rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
#include "pw_log/log.h"
#include "pw_rpc/server.h"
#include "pw_sys_io/sys_io.h"
#include "rpc_services/Attributes.h"
#include "rpc_services/Button.h"
#include "rpc_services/Device.h"
#include "rpc_services/Lighting.h"
#include "rpc_services/Locking.h"

#include <lib/support/logging/CHIPLogging.h>

Expand All @@ -41,6 +43,7 @@
#include "esp_wifi.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/timers.h"
#include "pw_containers/flat_map.h"
#include "pw_status/status.h"
#include "pw_status/try.h"
Expand Down Expand Up @@ -109,10 +112,17 @@ class Esp32Device final : public Device
public:
pw::Status Reboot(ServerContext & ctx, const pw_protobuf_Empty & request, pw_protobuf_Empty & response) override
{
esp_restart();
// WILL NOT RETURN
mRebootTimer = xTimerCreateStatic("Reboot", kRebootTimerPeriodTicks, false, nullptr, RebootHandler, &mRebootTimerBuffer);
xTimerStart(mRebootTimer, 0);
return pw::OkStatus();
}

private:
static constexpr TickType_t kRebootTimerPeriodTicks = 1000;
TimerHandle_t mRebootTimer;
StaticTimer_t mRebootTimerBuffer;

static void RebootHandler(TimerHandle_t) { esp_restart(); }
};

class Wifi final : public generated::Wifi<Wifi>
Expand Down Expand Up @@ -300,17 +310,21 @@ constexpr uint8_t kRpcTaskPriority = 5;

TaskHandle_t rpcTaskHandle;

Attributes attributes_service;
Esp32Button button_service;
Esp32Device device_service;
Lighting lighting_service;
Locking locking_service;
Wifi wifi_service;
pw::trace::TraceService trace_service;

void RegisterServices(pw::rpc::Server & server)
{
server.RegisterService(attributes_service);
server.RegisterService(button_service);
server.RegisterService(device_service);
server.RegisterService(lighting_service);
server.RegisterService(locking_service);
server.RegisterService(wifi_service);
server.RegisterService(trace_service);
PW_TRACE_SET_ENABLED(true);
Expand Down
8 changes: 8 additions & 0 deletions examples/common/pigweed/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ if (chip_enable_pw_rpc) {
prefix = "echo_service"
}

pw_proto_library("attributes_service") {
sources = [ "protos/attributes_service.proto" ]
inputs = [ "protos/attributes_service.options" ]
deps = [ "$dir_pw_protobuf:common_protos" ]
strip_prefix = "protos"
prefix = "attributes_service"
}

pw_proto_library("device_service") {
sources = [ "protos/device_service.proto" ]
inputs = [ "protos/device_service.options" ]
Expand Down
2 changes: 2 additions & 0 deletions examples/common/pigweed/protos/attributes_service.options
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

chip.rpc.AttributeData.data_bytes max_size:128
98 changes: 98 additions & 0 deletions examples/common/pigweed/protos/attributes_service.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
syntax = "proto3";

import 'pw_protobuf_protos/common.proto';

package chip.rpc;

enum AttributeType {
ZCL_NO_DATA_ATTRIBUTE_TYPE = 0x00; // No data
ZCL_BOOLEAN_ATTRIBUTE_TYPE = 0x10; // Boolean
ZCL_BITMAP8_ATTRIBUTE_TYPE = 0x18; // 8-bit bitmap
ZCL_BITMAP16_ATTRIBUTE_TYPE = 0x19; // 16-bit bitmap
ZCL_BITMAP32_ATTRIBUTE_TYPE = 0x1B; // 32-bit bitmap
ZCL_BITMAP64_ATTRIBUTE_TYPE = 0x1F; // 64-bit bitmap
ZCL_INT8U_ATTRIBUTE_TYPE = 0x20; // Unsigned 8-bit integer
ZCL_INT16U_ATTRIBUTE_TYPE = 0x21; // Unsigned 16-bit integer
ZCL_INT24U_ATTRIBUTE_TYPE = 0x22; // Unsigned 24-bit integer
ZCL_INT32U_ATTRIBUTE_TYPE = 0x23; // Unsigned 32-bit integer
ZCL_INT40U_ATTRIBUTE_TYPE = 0x24; // Unsigned 40-bit integer
ZCL_INT48U_ATTRIBUTE_TYPE = 0x25; // Unsigned 48-bit integer
ZCL_INT56U_ATTRIBUTE_TYPE = 0x26; // Unsigned 56-bit integer
ZCL_INT64U_ATTRIBUTE_TYPE = 0x27; // Unsigned 64-bit integer
ZCL_INT8S_ATTRIBUTE_TYPE = 0x28; // Signed 8-bit integer
ZCL_INT16S_ATTRIBUTE_TYPE = 0x29; // Signed 16-bit integer
ZCL_INT24S_ATTRIBUTE_TYPE = 0x2A; // Signed 24-bit integer
ZCL_INT32S_ATTRIBUTE_TYPE = 0x2B; // Signed 32-bit integer
ZCL_INT40S_ATTRIBUTE_TYPE = 0x2C; // Signed 40-bit integer
ZCL_INT48S_ATTRIBUTE_TYPE = 0x2D; // Signed 48-bit integer
ZCL_INT56S_ATTRIBUTE_TYPE = 0x2E; // Signed 56-bit integer
ZCL_INT64S_ATTRIBUTE_TYPE = 0x2F; // Signed 64-bit integer
ZCL_ENUM8_ATTRIBUTE_TYPE = 0x30; // 8-bit enumeration
ZCL_ENUM16_ATTRIBUTE_TYPE = 0x31; // 16-bit enumeration
ZCL_SINGLE_ATTRIBUTE_TYPE = 0x39; // Single precision
ZCL_DOUBLE_ATTRIBUTE_TYPE = 0x3A; // Double precision
ZCL_OCTET_STRING_ATTRIBUTE_TYPE = 0x41; // Octet String
ZCL_CHAR_STRING_ATTRIBUTE_TYPE = 0x42; // Character String
ZCL_LONG_OCTET_STRING_ATTRIBUTE_TYPE = 0x43; // Long Octet String
ZCL_LONG_CHAR_STRING_ATTRIBUTE_TYPE = 0x44; // Long Character String
ZCL_ARRAY_ATTRIBUTE_TYPE = 0x48; // List
ZCL_STRUCT_ATTRIBUTE_TYPE = 0x4C; // Structure
ZCL_TOD_ATTRIBUTE_TYPE = 0xE0; // Time of day
ZCL_DATE_ATTRIBUTE_TYPE = 0xE1; // Date
ZCL_UTC_ATTRIBUTE_TYPE = 0xE2; // UTC Time
ZCL_EPOCH_US_ATTRIBUTE_TYPE = 0xE3; // Epoch Microseconds
ZCL_EPOCH_S_ATTRIBUTE_TYPE = 0xE4; // Epoch Seconds
ZCL_SYSTIME_US_ATTRIBUTE_TYPE = 0xE5; // System Time Microseconds
ZCL_PERCENT_ATTRIBUTE_TYPE = 0xE6; // Percentage units 1%
ZCL_PERCENT100THS_ATTRIBUTE_TYPE = 0xE7; // Percentage units 0.01%
ZCL_CLUSTER_ID_ATTRIBUTE_TYPE = 0xE8; // Cluster ID
ZCL_ATTRIB_ID_ATTRIBUTE_TYPE = 0xE9; // Attribute ID
ZCL_FIELD_ID_ATTRIBUTE_TYPE = 0xEA; // Field ID
ZCL_EVENT_ID_ATTRIBUTE_TYPE = 0xEB; // Event ID
ZCL_COMMAND_ID_ATTRIBUTE_TYPE = 0xEC; // Command ID
ZCL_ACTION_ID_ATTRIBUTE_TYPE = 0xED; // Action ID
ZCL_TRANS_ID_ATTRIBUTE_TYPE = 0xEF; // Transaction ID
ZCL_NODE_ID_ATTRIBUTE_TYPE = 0xF0; // Node ID
ZCL_VENDOR_ID_ATTRIBUTE_TYPE = 0xF1; // Vendor ID
ZCL_DEVTYPE_ID_ATTRIBUTE_TYPE = 0xF2; // Device Type ID
ZCL_FABRIC_ID_ATTRIBUTE_TYPE = 0xF3; // Fabric ID
ZCL_GROUP_ID_ATTRIBUTE_TYPE = 0xF4; // Group ID
ZCL_STATUS_ATTRIBUTE_TYPE = 0xF5; // Status Code
ZCL_DATA_VER_ATTRIBUTE_TYPE = 0xF6; // Data Version
ZCL_EVENT_NO_ATTRIBUTE_TYPE = 0xF7; // Event Number
ZCL_ENDPOINT_NO_ATTRIBUTE_TYPE = 0xF8; // Endpoint Number
ZCL_FABRIC_IDX_ATTRIBUTE_TYPE = 0xF9; // Fabric Index
ZCL_IPADR_ATTRIBUTE_TYPE = 0xFA; // IP Address
ZCL_IPV4ADR_ATTRIBUTE_TYPE = 0xFB; // IPv4 Address
ZCL_IPV6ADR_ATTRIBUTE_TYPE = 0xFC; // IPv6 Address
ZCL_IPV6PRE_ATTRIBUTE_TYPE = 0xFD; // IPv6 Prefix
ZCL_HWADR_ATTRIBUTE_TYPE = 0xFE; // Hardware Address
ZCL_UNKNOWN_ATTRIBUTE_TYPE = 0xFF; // Unknown
}

message AttributeMetadata {
uint32 endpoint = 1;
uint32 cluster = 2;
uint32 attribute_id = 3;
AttributeType type = 4;
}

message AttributeData {
oneof data {
bool data_bool = 1;
uint32 data_uint8 = 2;
uint32 data_uint16 = 3;
uint32 data_uint32 = 4;
bytes data_bytes = 5;
};
}

message AttributeWrite {
AttributeMetadata metadata = 1;
AttributeData data = 2;
}

service Attributes {
rpc Write(AttributeWrite) returns (pw.protobuf.Empty){}
rpc Read(AttributeMetadata) returns (AttributeData){}
}
4 changes: 2 additions & 2 deletions examples/common/pigweed/protos/device_service.options
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

chip.rpc.DeviceInfo.serial_number max_size:32 // length defined in chip spec 8.2.3.1
chip.rpc.DeviceInfo.serial_number max_size:32 // length defined in chip spec 8.2.3.1
chip.rpc.DeviceState.fabric_info max_count:2
18 changes: 18 additions & 0 deletions examples/common/pigweed/protos/device_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,35 @@ package chip.rpc;

import 'pw_protobuf_protos/common.proto';

message PairingInfo {
uint32 code = 1;
uint32 discriminator = 2;
}

// type lengths defined in chip spec 8.2.3.1
message DeviceInfo {
uint32 vendor_id = 1;
uint32 product_id = 2;
uint32 software_version = 3;
string serial_number = 4;
PairingInfo pairing_info = 5;
}

message FabricInfo {
uint32 fabric_id = 1;
uint32 node_id = 2;
}

message DeviceState {
uint64 time_since_boot_millis = 1;
repeated FabricInfo fabric_info = 2;
}

service Device {
rpc FactoryReset(pw.protobuf.Empty) returns (pw.protobuf.Empty){}
rpc Reboot(pw.protobuf.Empty) returns (pw.protobuf.Empty){}
rpc TriggerOta(pw.protobuf.Empty) returns (pw.protobuf.Empty){}
rpc GetDeviceInfo(pw.protobuf.Empty) returns (DeviceInfo){}
rpc GetDeviceState(pw.protobuf.Empty) returns (DeviceState){}
rpc SetPairingInfo(PairingInfo) returns (pw.protobuf.Empty){}
}
1 change: 1 addition & 0 deletions examples/common/pigweed/rpc_console/py/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pw_python_package("chip_rpc") {
"$dir_pw_log_tokenized/py",
"$dir_pw_protobuf_compiler/py",
"$dir_pw_rpc/py",
"${chip_root}/examples/common/pigweed:attributes_service.python",
"${chip_root}/examples/common/pigweed:button_service.python",
"${chip_root}/examples/common/pigweed:device_service.python",
"${chip_root}/examples/common/pigweed:echo_service.python",
Expand Down
2 changes: 2 additions & 0 deletions examples/common/pigweed/rpc_console/py/chip_rpc/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from pw_hdlc.rpc import HdlcRpcClient, default_channels

# Protos
from attributes_service import attributes_service_pb2
from button_service import button_service_pb2
from device_service import device_service_pb2
from lighting_service import lighting_service_pb2
Expand All @@ -66,6 +67,7 @@
SOCKET_PORT = 33000

PROTOS = [button_service_pb2,
attributes_service_pb2,
lighting_service_pb2,
locking_service_pb2,
wifi_service_pb2,
Expand Down
Loading

0 comments on commit c46a6b4

Please sign in to comment.