Skip to content

Commit

Permalink
Add extension attribute to access control cluster
Browse files Browse the repository at this point in the history
Includes attribute. Does not include events, or fabric removal.

Part of issue project-chip#10252
  • Loading branch information
mlepage-google committed Apr 4, 2022
1 parent 3666138 commit ebddcdd
Show file tree
Hide file tree
Showing 21 changed files with 120 additions and 29 deletions.
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
95 changes: 89 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)
{
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);
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,65 @@ 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);
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);
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;
}
}
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);
}

AccessControlCluster::Structs::ExtensionEntry::DecodableType item;
ReturnErrorOnFailure(aDecoder.Decode(item));
ChipLogProgress(DataManagement, "############################ storing item %u", (unsigned) item.data.size());
// TODO(#13590): generated code doesn't automatically handle max length so do it manually
ReturnErrorCodeIf(item.data.size() > kExtensionDataMaxLength, CHIP_ERROR_INVALID_ARGUMENT);
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));
}

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.

0 comments on commit ebddcdd

Please sign in to comment.