From e891630b28046734353200af6300cfa47c72fd3b Mon Sep 17 00:00:00 2001 From: rgoliver Date: Tue, 29 Mar 2022 09:14:44 -0400 Subject: [PATCH] RPC: Fix locks when reading/writing attributes (#16720) - Add RAII lock for RPCs which use the ember API to read and write attributes. --- examples/common/pigweed/rpc_services/Attributes.h | 5 +++++ examples/common/pigweed/rpc_services/Descriptor.h | 5 +++++ examples/common/pigweed/rpc_services/Lighting.h | 5 +++++ examples/common/pigweed/rpc_services/Locking.h | 3 +++ 4 files changed, 18 insertions(+) diff --git a/examples/common/pigweed/rpc_services/Attributes.h b/examples/common/pigweed/rpc_services/Attributes.h index 34d44b7f5c64a8..8350c285ff7740 100644 --- a/examples/common/pigweed/rpc_services/Attributes.h +++ b/examples/common/pigweed/rpc_services/Attributes.h @@ -24,6 +24,7 @@ #include #include #include +#include namespace chip { namespace rpc { @@ -35,6 +36,8 @@ class Attributes : public pw_rpc::nanopb::Attributes::Service ::pw::Status Write(const chip_rpc_AttributeWrite & request, pw_protobuf_Empty & response) { const void * data; + DeviceLayer::StackLock lock; + switch (request.data.which_data) { case chip_rpc_AttributeData_data_bool_tag: @@ -74,6 +77,8 @@ class Attributes : public pw_rpc::nanopb::Attributes::Service { void * data; size_t size = 0; + DeviceLayer::StackLock lock; + switch (request.type) { case chip_rpc_AttributeType_ZCL_BOOLEAN_ATTRIBUTE_TYPE: diff --git a/examples/common/pigweed/rpc_services/Descriptor.h b/examples/common/pigweed/rpc_services/Descriptor.h index 506362cbb748b7..e2033a17e7b75f 100644 --- a/examples/common/pigweed/rpc_services/Descriptor.h +++ b/examples/common/pigweed/rpc_services/Descriptor.h @@ -24,6 +24,7 @@ #include #include #include +#include namespace chip { namespace rpc { @@ -35,6 +36,7 @@ class Descriptor : public pw_rpc::nanopb::Descriptor::Service virtual void DeviceTypeList(const ::chip_rpc_Endpoint & request, ServerWriter<::chip_rpc_DeviceType> & writer) { + DeviceLayer::StackLock lock; constexpr uint16_t kInvalidEndpointIndex = 0xFFFF; uint16_t index = emberAfIndexFromEndpoint(request.endpoint); if (index == kInvalidEndpointIndex) @@ -50,16 +52,19 @@ class Descriptor : public pw_rpc::nanopb::Descriptor::Service void ServerList(const ::chip_rpc_Endpoint & request, ServerWriter<::chip_rpc_Cluster> & writer) { + DeviceLayer::StackLock lock; ClusterList(request.endpoint, true /*server*/, writer); } void ClientList(const ::chip_rpc_Endpoint & request, ServerWriter<::chip_rpc_Cluster> & writer) { + DeviceLayer::StackLock lock; ClusterList(request.endpoint, false /*server*/, writer); } void PartsList(const ::chip_rpc_Endpoint & request, ServerWriter<::chip_rpc_Endpoint> & writer) { + DeviceLayer::StackLock lock; if (request.endpoint == 0x00) { for (uint16_t index = 0; index < emberAfEndpointCount(); index++) diff --git a/examples/common/pigweed/rpc_services/Lighting.h b/examples/common/pigweed/rpc_services/Lighting.h index fb5055f0159ed2..e8a96c6f4bdec3 100644 --- a/examples/common/pigweed/rpc_services/Lighting.h +++ b/examples/common/pigweed/rpc_services/Lighting.h @@ -24,6 +24,7 @@ #include #include #include +#include namespace chip { namespace rpc { @@ -37,6 +38,8 @@ class Lighting : public pw_rpc::nanopb::Lighting::Service virtual pw::Status Set(const chip_rpc_LightingState & request, pw_protobuf_Empty & response) { + DeviceLayer::StackLock lock; + uint8_t on = request.on; RETURN_STATUS_IF_NOT_OK( emberAfWriteServerAttribute(1, ZCL_ON_OFF_CLUSTER_ID, ZCL_ON_OFF_ATTRIBUTE_ID, &on, ZCL_BOOLEAN_ATTRIBUTE_ID)); @@ -65,6 +68,8 @@ class Lighting : public pw_rpc::nanopb::Lighting::Service virtual pw::Status Get(const pw_protobuf_Empty & request, chip_rpc_LightingState & response) { + DeviceLayer::StackLock lock; + uint8_t on; uint8_t level; uint8_t hue; diff --git a/examples/common/pigweed/rpc_services/Locking.h b/examples/common/pigweed/rpc_services/Locking.h index 3d6df4df2e9680..5400da3479f849 100644 --- a/examples/common/pigweed/rpc_services/Locking.h +++ b/examples/common/pigweed/rpc_services/Locking.h @@ -23,6 +23,7 @@ #include "pigweed/rpc_services/internal/StatusUtils.h" #include #include +#include namespace chip { namespace rpc { @@ -35,6 +36,7 @@ class Locking final : public pw_rpc::nanopb::Locking::Service virtual pw::Status Set(const chip_rpc_LockingState & request, pw_protobuf_Empty & response) { bool locked = request.locked; + DeviceLayer::StackLock lock; RETURN_STATUS_IF_NOT_OK(app::Clusters::OnOff::Attributes::OnOff::Set(kEndpoint, locked)); return pw::OkStatus(); } @@ -42,6 +44,7 @@ class Locking final : public pw_rpc::nanopb::Locking::Service virtual pw::Status Get(const pw_protobuf_Empty & request, chip_rpc_LockingState & response) { bool locked; + DeviceLayer::StackLock lock; RETURN_STATUS_IF_NOT_OK(app::Clusters::OnOff::Attributes::OnOff::Get(kEndpoint, &locked)); response.locked = locked; return pw::OkStatus();