Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extension attribute to access control cluster #17008

Merged
merged 6 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ server cluster AccessControl = 31 {
}

struct ExtensionEntry {
OCTET_STRING<254> data = 1;
OCTET_STRING<128> data = 1;
fabric_idx fabricIndex = 254;
}

Expand Down
4 changes: 2 additions & 2 deletions examples/bridge-app/bridge-common/bridge-app.matter
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ client cluster AccessControl = 31 {
}

struct ExtensionEntry {
OCTET_STRING<254> data = 1;
OCTET_STRING<128> data = 1;
fabric_idx fabricIndex = 254;
}

Expand Down Expand Up @@ -104,7 +104,7 @@ server cluster AccessControl = 31 {
}

struct ExtensionEntry {
OCTET_STRING<254> data = 1;
OCTET_STRING<128> data = 1;
fabric_idx fabricIndex = 254;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ server cluster AccessControl = 31 {
}

struct ExtensionEntry {
OCTET_STRING<254> data = 1;
OCTET_STRING<128> data = 1;
fabric_idx fabricIndex = 254;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ server cluster AccessControl = 31 {
}

struct ExtensionEntry {
OCTET_STRING<254> data = 1;
OCTET_STRING<128> data = 1;
fabric_idx fabricIndex = 254;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/lighting-app/lighting-common/lighting-app.matter
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ server cluster AccessControl = 31 {
}

struct ExtensionEntry {
OCTET_STRING<254> data = 1;
OCTET_STRING<128> data = 1;
fabric_idx fabricIndex = 254;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/lock-app/lock-common/lock-app.matter
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ server cluster AccessControl = 31 {
}

struct ExtensionEntry {
OCTET_STRING<254> data = 1;
OCTET_STRING<128> data = 1;
fabric_idx fabricIndex = 254;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ server cluster AccessControl = 31 {
}

struct ExtensionEntry {
OCTET_STRING<254> data = 1;
OCTET_STRING<128> data = 1;
fabric_idx fabricIndex = 254;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ client cluster AccessControl = 31 {
}

struct ExtensionEntry {
OCTET_STRING<254> data = 1;
OCTET_STRING<128> data = 1;
fabric_idx fabricIndex = 254;
}

Expand Down Expand Up @@ -104,7 +104,7 @@ server cluster AccessControl = 31 {
}

struct ExtensionEntry {
OCTET_STRING<254> data = 1;
OCTET_STRING<128> data = 1;
fabric_idx fabricIndex = 254;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ server cluster AccessControl = 31 {
}

struct ExtensionEntry {
OCTET_STRING<254> data = 1;
OCTET_STRING<128> data = 1;
fabric_idx fabricIndex = 254;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/pump-app/pump-common/pump-app.matter
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ server cluster AccessControl = 31 {
}

struct ExtensionEntry {
OCTET_STRING<254> data = 1;
OCTET_STRING<128> data = 1;
fabric_idx fabricIndex = 254;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ server cluster AccessControl = 31 {
}

struct ExtensionEntry {
OCTET_STRING<254> data = 1;
OCTET_STRING<128> data = 1;
fabric_idx fabricIndex = 254;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ server cluster AccessControl = 31 {
}

struct ExtensionEntry {
OCTET_STRING<254> data = 1;
OCTET_STRING<128> data = 1;
fabric_idx fabricIndex = 254;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/thermostat/thermostat-common/thermostat.matter
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ server cluster AccessControl = 31 {
}

struct ExtensionEntry {
OCTET_STRING<254> data = 1;
OCTET_STRING<128> data = 1;
fabric_idx fabricIndex = 254;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/tv-app/tv-common/tv-app.matter
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ server cluster AccessControl = 31 {
}

struct ExtensionEntry {
OCTET_STRING<254> data = 1;
OCTET_STRING<128> data = 1;
fabric_idx fabricIndex = 254;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ server cluster AccessControl = 31 {
}

struct ExtensionEntry {
OCTET_STRING<254> data = 1;
OCTET_STRING<128> data = 1;
fabric_idx fabricIndex = 254;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/window-app/common/window-app.matter
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ server cluster AccessControl = 31 {
}

struct ExtensionEntry {
OCTET_STRING<254> data = 1;
OCTET_STRING<128> data = 1;
fabric_idx fabricIndex = 254;
}

Expand Down
94 changes: 88 additions & 6 deletions src/app/clusters/access-control-server/access-control-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <app/ConcreteCommandPath.h>
#include <app/EventLogging.h>
#include <app/data-model/Encode.h>
#include <app/server/Server.h>
#include <app/util/af.h>
#include <app/util/attribute-storage.h>

Expand All @@ -35,6 +36,12 @@ using namespace chip::Access;

namespace AccessControlCluster = chip::app::Clusters::AccessControl;

// TODO(#13590): generated code doesn't automatically handle max length so do it manually
constexpr int kExtensionDataMaxLength = 128;

// Storage version used in keys.
constexpr int kStorageVersion = 1;

namespace {

struct Subject
Expand Down Expand Up @@ -355,7 +362,7 @@ class AccessControlAttribute : public chip::app::AttributeAccessInterface
CHIP_ERROR ReadAcl(AttributeValueEncoder & aEncoder);
CHIP_ERROR ReadExtension(AttributeValueEncoder & aEncoder);
CHIP_ERROR WriteAcl(const ConcreteDataAttributePath & aPath, AttributeValueDecoder & aDecoder);
CHIP_ERROR WriteExtension(AttributeValueDecoder & aDecoder);
CHIP_ERROR WriteExtension(const ConcreteDataAttributePath & aPath, AttributeValueDecoder & aDecoder);
};

constexpr uint16_t AccessControlAttribute::ClusterRevision;
Expand Down Expand Up @@ -476,7 +483,28 @@ CHIP_ERROR AccessControlAttribute::ReadAcl(AttributeValueEncoder & aEncoder)

CHIP_ERROR AccessControlAttribute::ReadExtension(AttributeValueEncoder & aEncoder)
{
return aEncoder.EncodeEmptyList();
auto & storage = Server::GetInstance().GetPersistentStorage();
DefaultStorageKeyAllocator key;

auto & fabrics = Server::GetInstance().GetFabricTable();

return aEncoder.EncodeList([&](const auto & encoder) -> CHIP_ERROR {
for (auto it = fabrics.begin(); it != fabrics.end(); ++it)
mlepage-google marked this conversation as resolved.
Show resolved Hide resolved
{
uint8_t buffer[kExtensionDataMaxLength] = { 0 };
uint16_t size = static_cast<uint16_t>(sizeof(buffer));
auto err =
storage.SyncGetKeyValue(key.AccessControlExtensionEntry(kStorageVersion, it->GetFabricIndex()), buffer, size);
ReturnErrorCodeIf(err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND, CHIP_NO_ERROR);
bzbarsky-apple marked this conversation as resolved.
Show resolved Hide resolved
ReturnErrorOnFailure(err);
AccessControlCluster::Structs::ExtensionEntry::Type item = {
.data = ByteSpan(buffer, size),
.fabricIndex = it->GetFabricIndex(),
};
ReturnErrorOnFailure(encoder.Encode(item));
}
return CHIP_NO_ERROR;
});
}

CHIP_ERROR AccessControlAttribute::Write(const ConcreteDataAttributePath & aPath, AttributeValueDecoder & aDecoder)
Expand All @@ -486,7 +514,7 @@ CHIP_ERROR AccessControlAttribute::Write(const ConcreteDataAttributePath & aPath
case AccessControlCluster::Attributes::Acl::Id:
return WriteAcl(aPath, aDecoder);
case AccessControlCluster::Attributes::Extension::Id:
return WriteExtension(aDecoder);
return WriteExtension(aPath, aDecoder);
}

return CHIP_NO_ERROR;
Expand Down Expand Up @@ -570,10 +598,64 @@ CHIP_ERROR AccessControlAttribute::WriteAcl(const ConcreteDataAttributePath & aP
return CHIP_NO_ERROR;
}

CHIP_ERROR AccessControlAttribute::WriteExtension(AttributeValueDecoder & aDecoder)
CHIP_ERROR AccessControlAttribute::WriteExtension(const ConcreteDataAttributePath & aPath, AttributeValueDecoder & aDecoder)
{
DataModel::DecodableList<AccessControlCluster::Structs::ExtensionEntry::DecodableType> list;
ReturnErrorOnFailure(aDecoder.Decode(list));
auto & storage = Server::GetInstance().GetPersistentStorage();
DefaultStorageKeyAllocator key;

FabricIndex accessingFabricIndex = aDecoder.AccessingFabricIndex();

if (!aPath.IsListItemOperation())
{
DataModel::DecodableList<AccessControlCluster::Structs::ExtensionEntry::DecodableType> list;
ReturnErrorOnFailure(aDecoder.Decode(list));

size_t count = 0;
ReturnErrorOnFailure(list.ComputeSize(&count));

if (count == 0)
{
auto err = storage.SyncDeleteKeyValue(key.AccessControlExtensionEntry(kStorageVersion, accessingFabricIndex));
ReturnErrorCodeIf(err != CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND, err);
}
else if (count == 1)
{
auto iterator = list.begin();
ReturnErrorCodeIf(!iterator.Next(), CHIP_ERROR_MISSING_TLV_ELEMENT);
bzbarsky-apple marked this conversation as resolved.
Show resolved Hide resolved
auto & item = iterator.GetValue();
// TODO(#13590): generated code doesn't automatically handle max length so do it manually
ReturnErrorCodeIf(item.data.size() > kExtensionDataMaxLength, CHIP_ERROR_INVALID_ARGUMENT);
bzbarsky-apple marked this conversation as resolved.
Show resolved Hide resolved
ReturnErrorOnFailure(storage.SyncSetKeyValue(key.AccessControlExtensionEntry(kStorageVersion, accessingFabricIndex),
item.data.data(), static_cast<uint16_t>(item.data.size())));
}
else
{
// Only one item supported per fabric.
return CHIP_ERROR_INVALID_ARGUMENT;
bzbarsky-apple marked this conversation as resolved.
Show resolved Hide resolved
}
}
else if (aPath.mListOp == ConcreteDataAttributePath::ListOperation::AppendItem)
{
{
uint8_t buffer[0];
uint16_t size = static_cast<uint16_t>(sizeof(buffer));
auto err =
storage.SyncGetKeyValue(key.AccessControlExtensionEntry(kStorageVersion, accessingFabricIndex), buffer, size);
ReturnErrorCodeIf(err != CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND, err);
bzbarsky-apple marked this conversation as resolved.
Show resolved Hide resolved
}

AccessControlCluster::Structs::ExtensionEntry::DecodableType item;
ReturnErrorOnFailure(aDecoder.Decode(item));
// TODO(#13590): generated code doesn't automatically handle max length so do it manually
ReturnErrorCodeIf(item.data.size() > kExtensionDataMaxLength, CHIP_ERROR_INVALID_ARGUMENT);
bzbarsky-apple marked this conversation as resolved.
Show resolved Hide resolved
ReturnErrorOnFailure(storage.SyncSetKeyValue(key.AccessControlExtensionEntry(kStorageVersion, accessingFabricIndex),
item.data.data(), static_cast<uint16_t>(item.data.size())));
}
else
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}

return CHIP_NO_ERROR;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ limitations under the License.

<struct name="ExtensionEntry">
<cluster code="0x001F"/>
<item fieldId="1" name="Data" type="OCTET_STRING" length="254"/>
<item fieldId="0xFE" name="FabricIndex" type="fabric_idx"/>
<item fieldId="1" name="Data" type="OCTET_STRING" length="128" isFabricSensitive="true"/>
<item fieldId="0xFE" name="FabricIndex" type="fabric_idx" isFabricSensitive="true"/>
</struct>

<cluster>
Expand Down
2 changes: 1 addition & 1 deletion src/controller/data_model/controller-clusters.matter
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ client cluster AccessControl = 31 {
}

struct ExtensionEntry {
OCTET_STRING<254> data = 1;
OCTET_STRING<128> data = 1;
fabric_idx fabricIndex = 254;
}

Expand Down
6 changes: 5 additions & 1 deletion src/lib/support/DefaultStorageKeyAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ class DefaultStorageKeyAllocator
// FailSafeContext
const char * FailSafeContextKey() { return Format("g/fsc"); }

// Access Control List
// Access Control
const char * AccessControlExtensionEntry(size_t version, FabricIndex fabric)
{
return Format("a/%x/1/%x", static_cast<unsigned>(version), static_cast<unsigned>(fabric));
mlepage-google marked this conversation as resolved.
Show resolved Hide resolved
mlepage-google marked this conversation as resolved.
Show resolved Hide resolved
}

const char * AccessControlList() { return Format("acl"); }
const char * AccessControlEntry(size_t index)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.