Skip to content

Commit

Permalink
Create an AAI registry for PwRPC services to allow registration of cu…
Browse files Browse the repository at this point in the history
…stom AAI classes (#37351)

* Allow custom AAI registration for PwRPC Reads/Writes. Update PwRPC Writes to look for custom AAIs before using datamodel provider

* Add succes log

* Make TryWriteViaAAI public

* Make TryWriteViaAAI public

* Fix compilation errors

* Fix compilation errors

* Fix compilation errors

* Update examples/common/pigweed/rpc_services/Attributes.h

Co-authored-by: Tennessee Carmel-Veilleux <tennessee.carmelveilleux@gmail.com>

* Add new attribute accessor and registry for PwRPC

* Fix compilation error

* Fix compilation error

* Fix compilation error

* Revert changes

* Revert changes

* Accessors will not be indexed by EndpointId/ClusterId. Caller queries all accessors to satisfy the request and returns as soon as a succes/failure is encountered. Accessors will return 'pw::Status::NotFound()' as a way of hinting the caller 'look for an alternative'. If no accessors were registered or all returned 'NotFound' the fallback path is taken.

* fix compilation bug

* Review comments

* Fix compilation

* Fix nits

* Fix ESP32 compilation

* Update examples/common/pigweed/rpc_services/AccessInterceptor.h

Nit : rename parameter names.

Co-authored-by: Andrei Litvin <andy314@gmail.com>

* Update examples/common/pigweed/rpc_services/AccessInterceptor.h

Review suggestion.

Co-authored-by: Andrei Litvin <andy314@gmail.com>

* Update examples/common/pigweed/rpc_services/AccessInterceptorRegistry.h

Review suggestion.

Co-authored-by: Andrei Litvin <andy314@gmail.com>

* Review comments

* Update examples/common/pigweed/rpc_services/AccessInterceptor.h

---------

Co-authored-by: Tennessee Carmel-Veilleux <tennessee.carmelveilleux@gmail.com>
Co-authored-by: Andrei Litvin <andy314@gmail.com>
  • Loading branch information
3 people authored Feb 7, 2025
1 parent cbe17ed commit ce47e22
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
55 changes: 55 additions & 0 deletions examples/common/pigweed/rpc_services/AccessInterceptor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
*
* Copyright (c) 2025 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "pw_status/status.h"
#include <app/AttributeReportBuilder.h>
#include <app/AttributeValueDecoder.h>
#include <app/AttributeValueEncoder.h>

namespace chip {
namespace rpc {

/**
* Callback class that clusters can implement in order to interpose custom
* interception logic.
*/
class PigweedDebugAccessInterceptor
{
public:
PigweedDebugAccessInterceptor() = default;
virtual ~PigweedDebugAccessInterceptor() = default;

/**
* Callback for writing attributes.
*
* The implementation can do one of three things:
*
* Returns:
* - `std::nullopt` if the `path` was not handled by this Interceptor.
* Interceptor MUST NOT have attepted to decode `decoder`.
* - A `::pw::Status` value that is considered the FINAL result of the
* write (i.e. write handled) either with success or failure.
*/
virtual std::optional<::pw::Status> Write(const chip::app::ConcreteDataAttributePath & path,
chip::app::AttributeValueDecoder & decoder) = 0;
};

} // namespace rpc
} // namespace chip
73 changes: 73 additions & 0 deletions examples/common/pigweed/rpc_services/AccessInterceptorRegistry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
*
* Copyright (c) 2025 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "pw_status/status.h"
#include <pigweed/rpc_services/AccessInterceptor.h>
#include <set>

namespace chip {
namespace rpc {

/** @brief Custom debug request interceptors.
*
* This class is specifically meant for registering custom Accessors that
* allow mini-handlers to process PigweedRPC read/writes separately from the cluster
* code. It is meant to be used by samples using this PigweedRPC services to allow the RPC
* interface to be used in more ways than simply for example, simulating writes from a Matter
* client at the IM level. Handlers registered here by applications should be attempted before any
* standard processing.
*/
class PigweedDebugAccessInterceptorRegistry
{
public:
/**
* Registers an attribute access inteceptor within this registry. Use `Unregister` to
* unregister an interceptor that was previously registered.
* @param attrOverride - the interceptor to be registered.
*/
void Register(PigweedDebugAccessInterceptor * attrOverride) { mAccessors.insert(attrOverride); }

void Unregister(PigweedDebugAccessInterceptor * attrOverride)
{
if (mAccessors.find(attrOverride) == mAccessors.end())
{
ChipLogError(Support, "Attempt to unregister accessor that is not registered.");
return;
}
mAccessors.erase(attrOverride);
}

const std::set<PigweedDebugAccessInterceptor *> & GetAllAccessors() const { return mAccessors; }

/**
* Returns the singleton instance of the attribute accessor registory.
*/
static PigweedDebugAccessInterceptorRegistry & Instance()
{
static PigweedDebugAccessInterceptorRegistry instance;
return instance;
}

private:
std::set<PigweedDebugAccessInterceptor *> mAccessors;
};

} // namespace rpc
} // namespace chip
36 changes: 36 additions & 0 deletions examples/common/pigweed/rpc_services/Attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,42 @@
#include <app/data-model-provider/Provider.h>
#include <app/util/attribute-storage.h>
#include <app/util/attribute-table.h>
#include <data-model-providers/codegen/CodegenDataModelProvider.h>
#include <lib/core/TLV.h>
#include <lib/core/TLVTags.h>
#include <lib/core/TLVTypes.h>
#include <pigweed/rpc_services/AccessInterceptor.h>
#include <pigweed/rpc_services/AccessInterceptorRegistry.h>
#include <platform/PlatformManager.h>
#include <set>

namespace chip {
namespace rpc {

std::optional<::pw::Status> TryWriteViaAccessor(const chip::app::ConcreteDataAttributePath & path,
chip::app::AttributeValueDecoder & decoder)
{
std::set<PigweedDebugAccessInterceptor *> accessors = PigweedDebugAccessInterceptorRegistry::Instance().GetAllAccessors();

for (PigweedDebugAccessInterceptor * accessor : accessors)
{
std::optional<::pw::Status> result = accessor->Write(path, decoder);
if (result.has_value()) // Write was either a success or failure.
{
return result;
}
else if (decoder.TriedDecode())
{
ChipLogError(Support, "Interceptor tried decode but did not return status.");
return ::pw::Status::FailedPrecondition();
}
}

VerifyOrReturnError(!decoder.TriedDecode(), ::pw::Status::FailedPrecondition());

return std::nullopt;
}

// Implementation class for chip.rpc.Attributes.
class Attributes : public pw_rpc::nanopb::Attributes::Service<Attributes>
{
Expand Down Expand Up @@ -207,6 +235,14 @@ class Attributes : public pw_rpc::nanopb::Attributes::Service<Attributes>
}

app::AttributeValueDecoder decoder(tlvReader.value(), subjectDescriptor);

std::optional<::pw::Status> interceptResult = TryWriteViaAccessor(write_request.path, decoder);
if (interceptResult.has_value())
{
return *interceptResult;
}
ChipLogProgress(Support, "No custom PigweedRPC Attribute Accessor registration found, using fake write access.");

app::DataModel::ActionReturnStatus result = provider->WriteAttribute(write_request, decoder);

if (!result.IsSuccess())
Expand Down

0 comments on commit ce47e22

Please sign in to comment.