Skip to content

Commit

Permalink
RPC: Fix locks when reading/writing attributes (#16720)
Browse files Browse the repository at this point in the history
- Add RAII lock for RPCs which use the ember API to read and write
  attributes.
  • Loading branch information
rgoliver authored and pull[bot] committed Jan 25, 2024
1 parent ef33670 commit 1165668
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions examples/common/pigweed/rpc_services/Attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <app-common/zap-generated/attribute-id.h>
#include <app-common/zap-generated/attribute-type.h>
#include <app-common/zap-generated/cluster-id.h>
#include <platform/PlatformManager.h>

namespace chip {
namespace rpc {
Expand All @@ -35,6 +36,8 @@ class Attributes : public pw_rpc::nanopb::Attributes::Service<Attributes>
::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:
Expand Down Expand Up @@ -74,6 +77,8 @@ class Attributes : public pw_rpc::nanopb::Attributes::Service<Attributes>
{
void * data;
size_t size = 0;
DeviceLayer::StackLock lock;

switch (request.type)
{
case chip_rpc_AttributeType_ZCL_BOOLEAN_ATTRIBUTE_TYPE:
Expand Down
5 changes: 5 additions & 0 deletions examples/common/pigweed/rpc_services/Descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <app-common/zap-generated/cluster-objects.h>
#include <app-common/zap-generated/ids/Attributes.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <platform/PlatformManager.h>

namespace chip {
namespace rpc {
Expand All @@ -35,6 +36,7 @@ class Descriptor : public pw_rpc::nanopb::Descriptor::Service<Descriptor>

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)
Expand All @@ -50,16 +52,19 @@ class Descriptor : public pw_rpc::nanopb::Descriptor::Service<Descriptor>

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++)
Expand Down
5 changes: 5 additions & 0 deletions examples/common/pigweed/rpc_services/Lighting.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <app-common/zap-generated/attribute-id.h>
#include <app-common/zap-generated/attribute-type.h>
#include <app-common/zap-generated/cluster-id.h>
#include <platform/PlatformManager.h>

namespace chip {
namespace rpc {
Expand All @@ -37,6 +38,8 @@ class Lighting : public pw_rpc::nanopb::Lighting::Service<Lighting>

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));
Expand Down Expand Up @@ -65,6 +68,8 @@ class Lighting : public pw_rpc::nanopb::Lighting::Service<Lighting>

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;
Expand Down
3 changes: 3 additions & 0 deletions examples/common/pigweed/rpc_services/Locking.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "pigweed/rpc_services/internal/StatusUtils.h"
#include <app-common/zap-generated/attribute-type.h>
#include <app-common/zap-generated/attributes/Accessors.h>
#include <platform/PlatformManager.h>

namespace chip {
namespace rpc {
Expand All @@ -35,13 +36,15 @@ class Locking final : public pw_rpc::nanopb::Locking::Service<Locking>
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();
}

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();
Expand Down

0 comments on commit 1165668

Please sign in to comment.