Skip to content

Commit

Permalink
Use pointer for subject descriptor in datamodel provider instead of s…
Browse files Browse the repository at this point in the history
…td::optional (project-chip#36246)

* Use a pointer for the subject descriptor.

This seems to save about 88 bytes of flash on a test NRF board.

* Restyle

* Fix include

* Fix include

* Also fix PW rpc

---------

Co-authored-by: Andrei Litvin <[email protected]>
  • Loading branch information
andy31415 and andreilitvin authored Oct 28, 2024
1 parent 77b4780 commit 2142870
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 20 deletions.
2 changes: 1 addition & 1 deletion examples/common/pigweed/rpc_services/Attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class Attributes : public pw_rpc::nanopb::Attributes::Service<Attributes>
app::DataModel::ReadAttributeRequest request;
request.path = path;
request.operationFlags.Set(app::DataModel::OperationFlags::kInternal);
request.subjectDescriptor = subjectDescriptor;
request.subjectDescriptor = &subjectDescriptor;

std::optional<app::DataModel::ClusterInfo> info = provider->GetClusterInfo(path);
if (!info.has_value())
Expand Down
7 changes: 5 additions & 2 deletions src/app/CommandHandlerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <app/CommandHandlerImpl.h>

#include <access/AccessControl.h>
#include <access/SubjectDescriptor.h>
#include <app-common/zap-generated/cluster-objects.h>
#include <app/MessageDef/StatusIB.h>
#include <app/RequiredPrivilege.h>
Expand Down Expand Up @@ -391,10 +392,11 @@ Status CommandHandlerImpl::ProcessCommandDataIB(CommandDataIB::Parser & aCommand
VerifyOrReturnError(err == CHIP_NO_ERROR, Status::InvalidAction);

{
Access::SubjectDescriptor subjectDescriptor = GetSubjectDescriptor();
DataModel::InvokeRequest request;

request.path = concretePath;
request.subjectDescriptor = GetSubjectDescriptor();
request.subjectDescriptor = &subjectDescriptor;
request.invokeFlags.Set(DataModel::InvokeFlags::kTimed, IsTimedInvoke());

Status preCheckStatus = mpCallback->ValidateCommandCanBeDispatched(request);
Expand Down Expand Up @@ -513,10 +515,11 @@ Status CommandHandlerImpl::ProcessGroupCommandDataIB(CommandDataIB::Parser & aCo
const ConcreteCommandPath concretePath(mapping.endpoint_id, clusterId, commandId);

{
Access::SubjectDescriptor subjectDescriptor = GetSubjectDescriptor();
DataModel::InvokeRequest request;

request.path = concretePath;
request.subjectDescriptor = GetSubjectDescriptor();
request.subjectDescriptor = &subjectDescriptor;
request.invokeFlags.Set(DataModel::InvokeFlags::kTimed, IsTimedInvoke());

Status preCheckStatus = mpCallback->ValidateCommandCanBeDispatched(request);
Expand Down
6 changes: 4 additions & 2 deletions src/app/InteractionModelEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1646,10 +1646,12 @@ void InteractionModelEngine::DispatchCommand(CommandHandlerImpl & apCommandObj,
{
#if CHIP_CONFIG_USE_DATA_MODEL_INTERFACE

Access::SubjectDescriptor subjectDescriptor = apCommandObj.GetSubjectDescriptor();

DataModel::InvokeRequest request;
request.path = aCommandPath;
request.invokeFlags.Set(DataModel::InvokeFlags::kTimed, apCommandObj.IsTimedInvoke());
request.subjectDescriptor = apCommandObj.GetSubjectDescriptor();
request.subjectDescriptor = &subjectDescriptor;

std::optional<DataModel::ActionReturnStatus> status = GetDataModelProvider()->Invoke(request, apPayload, &apCommandObj);

Expand Down Expand Up @@ -1702,7 +1704,7 @@ Protocols::InteractionModel::Status InteractionModelEngine::ValidateCommandCanBe

Protocols::InteractionModel::Status InteractionModelEngine::CheckCommandAccess(const DataModel::InvokeRequest & aRequest)
{
if (!aRequest.subjectDescriptor.has_value())
if (aRequest.subjectDescriptor == nullptr)
{
return Status::UnsupportedAccess; // we require a subject for invoke
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/WriteHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ CHIP_ERROR WriteHandler::WriteClusterData(const Access::SubjectDescriptor & aSub
DataModel::WriteAttributeRequest request;

request.path = aPath;
request.subjectDescriptor = aSubject;
request.subjectDescriptor = &aSubject;
request.previousSuccessPath = mLastSuccessfullyWrittenPath;
request.writeFlags.Set(DataModel::WriteFlags::kTimed, IsTimedWrite());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ DataModel::ActionReturnStatus CodegenDataModelProvider::ReadAttribute(const Data
// ACL check for non-internal requests
if (!request.operationFlags.Has(DataModel::OperationFlags::kInternal))
{
VerifyOrReturnError(request.subjectDescriptor.has_value(), CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(request.subjectDescriptor != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

Access::RequestPath requestPath{ .cluster = request.path.mClusterId,
.endpoint = request.path.mEndpointId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ DataModel::ActionReturnStatus CodegenDataModelProvider::WriteAttribute(const Dat

if (checkAcl)
{
VerifyOrReturnError(request.subjectDescriptor.has_value(), Status::UnsupportedAccess);
VerifyOrReturnError(request.subjectDescriptor != nullptr, Status::UnsupportedAccess);

Access::RequestPath requestPath{ .cluster = request.path.mClusterId,
.endpoint = request.path.mEndpointId,
Expand Down
5 changes: 3 additions & 2 deletions src/app/data-model-provider/OperationTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,16 @@ struct OperationRequest
/// - operationFlags.Has(OperationFlags::kInternal) MUST NOT have this set
///
/// NOTE: once kInternal flag is removed, this will become non-optional
std::optional<chip::Access::SubjectDescriptor> subjectDescriptor;
const chip::Access::SubjectDescriptor * subjectDescriptor = nullptr;

/// Accessing fabric index is the subjectDescriptor fabric index (if any).
/// This is a readability convenience function.
///
/// Returns kUndefinedFabricIndex if no subject descriptor is available
FabricIndex GetAccessingFabricIndex() const
{
return subjectDescriptor.has_value() ? subjectDescriptor->fabricIndex : kUndefinedFabricIndex;
VerifyOrReturnValue(subjectDescriptor != nullptr, kUndefinedFabricIndex);
return subjectDescriptor->fabricIndex;
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/app/data-model-provider/tests/ReadTesting.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class ReadOperation
ReadOperation(const ConcreteAttributePath & path)
{
mRequest.path = path;
mRequest.subjectDescriptor = kDenySubjectDescriptor;
mRequest.subjectDescriptor = &kDenySubjectDescriptor;
}

ReadOperation(EndpointId endpoint, ClusterId cluster, AttributeId attribute) :
Expand All @@ -146,7 +146,7 @@ class ReadOperation
ReadOperation & SetSubjectDescriptor(const chip::Access::SubjectDescriptor & descriptor)
{
VerifyOrDie(mState == State::kInitializing);
mRequest.subjectDescriptor = descriptor;
mRequest.subjectDescriptor = &descriptor;
return *this;
}

Expand Down
10 changes: 7 additions & 3 deletions src/app/data-model-provider/tests/WriteTesting.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class WriteOperation
WriteOperation(const ConcreteDataAttributePath & path)
{
mRequest.path = path;
mRequest.subjectDescriptor = kDenySubjectDescriptor;
mRequest.subjectDescriptor = &kDenySubjectDescriptor;
}

WriteOperation(EndpointId endpoint, ClusterId cluster, AttributeId attribute) :
Expand All @@ -56,7 +56,7 @@ class WriteOperation

WriteOperation & SetSubjectDescriptor(const chip::Access::SubjectDescriptor & descriptor)
{
mRequest.subjectDescriptor = descriptor;
mRequest.subjectDescriptor = &descriptor;
return *this;
}

Expand Down Expand Up @@ -123,7 +123,11 @@ class WriteOperation
AttributeValueDecoder DecoderFor(const T & value)
{
mTLVReader = ReadEncodedValue(value);
return AttributeValueDecoder(mTLVReader, mRequest.subjectDescriptor.value_or(kDenySubjectDescriptor));
if (mRequest.subjectDescriptor == nullptr)
{
AttributeValueDecoder(mTLVReader, kDenySubjectDescriptor);
}
return AttributeValueDecoder(mTLVReader, *mRequest.subjectDescriptor);
}

private:
Expand Down
2 changes: 1 addition & 1 deletion src/app/reporting/Read-DataModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ DataModel::ActionReturnStatus RetrieveClusterData(DataModel::Provider * dataMode
{
readRequest.readFlags.Set(DataModel::ReadFlags::kFabricFiltered);
}
readRequest.subjectDescriptor = subjectDescriptor;
readRequest.subjectDescriptor = &subjectDescriptor;
readRequest.path = path;

DataVersion version = 0;
Expand Down
10 changes: 8 additions & 2 deletions src/app/tests/test-interaction-model-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "access/SubjectDescriptor.h"
#include <app/tests/test-interaction-model-api.h>

#include <app/InteractionModelEngine.h>
Expand Down Expand Up @@ -171,8 +172,13 @@ ActionReturnStatus TestImCustomDataModel::ReadAttribute(const ReadAttributeReque
{
AttributeEncodeState mutableState(&encoder.GetState()); // provide a state copy to start.

CHIP_ERROR err = ReadSingleClusterData(request.subjectDescriptor.value_or(Access::SubjectDescriptor()),
request.readFlags.Has(ReadFlags::kFabricFiltered), request.path,
Access::SubjectDescriptor subjectDescriptor;
if (request.subjectDescriptor != nullptr)
{
subjectDescriptor = *request.subjectDescriptor;
}

CHIP_ERROR err = ReadSingleClusterData(subjectDescriptor, request.readFlags.Has(ReadFlags::kFabricFiltered), request.path,
TestOnlyAttributeValueEncoderAccessor(encoder).Builder(), &mutableState);

// state must survive CHIP_ERRORs as it is used for chunking
Expand Down
10 changes: 8 additions & 2 deletions src/controller/tests/data_model/DataModelFixtures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "DataModelFixtures.h"

#include <access/SubjectDescriptor.h>
#include <app-common/zap-generated/cluster-objects.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app/AttributeValueDecoder.h>
Expand Down Expand Up @@ -522,8 +523,13 @@ ActionReturnStatus CustomDataModel::ReadAttribute(const ReadAttributeRequest & r
}
#endif // CHIP_CONFIG_USE_EMBER_DATA_MODEL && CHIP_CONFIG_USE_DATA_MODEL_INTERFACE

CHIP_ERROR err = ReadSingleClusterData(request.subjectDescriptor.value_or(Access::SubjectDescriptor()),
request.readFlags.Has(ReadFlags::kFabricFiltered), request.path,
Access::SubjectDescriptor subjectDescriptor;
if (request.subjectDescriptor != nullptr)
{
subjectDescriptor = *request.subjectDescriptor;
}

CHIP_ERROR err = ReadSingleClusterData(subjectDescriptor, request.readFlags.Has(ReadFlags::kFabricFiltered), request.path,
TestOnlyAttributeValueEncoderAccessor(encoder).Builder(), &mutableState);

// state must survive CHIP_ERRORs as it is used for chunking
Expand Down

0 comments on commit 2142870

Please sign in to comment.