Skip to content

Commit

Permalink
Remove privilege-constants.h in favor of using access/Privilege.h (
Browse files Browse the repository at this point in the history
…project-chip#32408)

* Start consolidating privilege constants

* Update mappings

* Updated names

* Code now compiles

* RequiredPrivilege should be just a namespace

* Fix darwin types as well

* Apparently without inline we get duplicate definitions. fixed

* Undo parts of a bad merge

* Apparently deps are needed

* Cleanup again

* Remove invalid todo ... this does not apply anymore now

* Restyle
  • Loading branch information
andy31415 authored Mar 5, 2024
1 parent 23a76a9 commit 8a1c7aa
Show file tree
Hide file tree
Showing 12 changed files with 257 additions and 332 deletions.
240 changes: 120 additions & 120 deletions scripts/tools/zap/tests/outputs/all-clusters-app/app-templates/access.h

Large diffs are not rendered by default.

122 changes: 61 additions & 61 deletions scripts/tools/zap/tests/outputs/lighting-app/app-templates/access.h

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions src/app/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ static_library("interaction-model") {
"PendingResponseTrackerImpl.h",
"ReadClient.h", # TODO: cpp is only included conditionally. Needs logic
# fixing
"RequiredPrivilege.cpp",
"RequiredPrivilege.h",
"StatusResponse.cpp",
"StatusResponse.h",
Expand Down Expand Up @@ -231,7 +230,6 @@ static_library("interaction-model") {
"dynamic_server/AccessControl.cpp",
"dynamic_server/AccessControl.h",
"dynamic_server/DynamicDispatcher.cpp",
"util/privilege-constants.h",
"util/privilege-storage.cpp",
"util/privilege-storage.h",
]
Expand Down
27 changes: 0 additions & 27 deletions src/app/RequiredPrivilege.cpp

This file was deleted.

57 changes: 17 additions & 40 deletions src/app/RequiredPrivilege.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,26 @@

namespace chip {
namespace app {

class RequiredPrivilege
namespace RequiredPrivilege {
inline chip::Access::Privilege ForReadAttribute(const ConcreteAttributePath & path)
{
using Privilege = Access::Privilege;

static constexpr Privilege kPrivilegeMapper[] = { Privilege::kView, Privilege::kOperate, Privilege::kManage,
Privilege::kAdminister };

static_assert(ArraySize(kPrivilegeMapper) > kMatterAccessPrivilegeView &&
kPrivilegeMapper[kMatterAccessPrivilegeView] == Privilege::kView,
"Must map privilege correctly");
static_assert(ArraySize(kPrivilegeMapper) > kMatterAccessPrivilegeOperate &&
kPrivilegeMapper[kMatterAccessPrivilegeOperate] == Privilege::kOperate,
"Must map privilege correctly");
static_assert(ArraySize(kPrivilegeMapper) > kMatterAccessPrivilegeManage &&
kPrivilegeMapper[kMatterAccessPrivilegeManage] == Privilege::kManage,
"Must map privilege correctly");
static_assert(ArraySize(kPrivilegeMapper) > kMatterAccessPrivilegeAdminister &&
kPrivilegeMapper[kMatterAccessPrivilegeAdminister] == Privilege::kAdminister,
"Must map privilege correctly");
static_assert(ArraySize(kPrivilegeMapper) > kMatterAccessPrivilegeMaxValue, "Must map all privileges");

public:
static Privilege ForReadAttribute(const ConcreteAttributePath & path)
{
return kPrivilegeMapper[MatterGetAccessPrivilegeForReadAttribute(path.mClusterId, path.mAttributeId)];
}
return MatterGetAccessPrivilegeForReadAttribute(path.mClusterId, path.mAttributeId);
}

static Privilege ForWriteAttribute(const ConcreteAttributePath & path)
{
return kPrivilegeMapper[MatterGetAccessPrivilegeForWriteAttribute(path.mClusterId, path.mAttributeId)];
}

static Privilege ForInvokeCommand(const ConcreteCommandPath & path)
{
return kPrivilegeMapper[MatterGetAccessPrivilegeForInvokeCommand(path.mClusterId, path.mCommandId)];
}
inline chip::Access::Privilege ForWriteAttribute(const ConcreteAttributePath & path)
{
return MatterGetAccessPrivilegeForWriteAttribute(path.mClusterId, path.mAttributeId);
}

static Privilege ForReadEvent(const ConcreteEventPath & path)
{
return kPrivilegeMapper[MatterGetAccessPrivilegeForReadEvent(path.mClusterId, path.mEventId)];
}
};
inline chip::Access::Privilege ForInvokeCommand(const ConcreteCommandPath & path)
{
return MatterGetAccessPrivilegeForInvokeCommand(path.mClusterId, path.mCommandId);
}

inline chip::Access::Privilege ForReadEvent(const ConcreteEventPath & path)
{
return MatterGetAccessPrivilegeForReadEvent(path.mClusterId, path.mEventId);
}
} // namespace RequiredPrivilege
} // namespace app
} // namespace chip
2 changes: 2 additions & 0 deletions src/app/chip_data_model.gni
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ template("chip_data_model") {
deps = [
":${_data_model_name}_codegen_generate",
":${_data_model_name}_zapgen_generate",
"${chip_root}/src/access",
"${chip_root}/src/lib/core:chip_config_header",
]
}
Expand Down Expand Up @@ -398,6 +399,7 @@ template("chip_data_model") {
public_deps += [
":${_data_model_name}_codegen",
":${_data_model_name}_zapgen",
"${chip_root}/src/access",
"${chip_root}/src/app",
"${chip_root}/src/app/common:attribute-type",
"${chip_root}/src/app/common:cluster-objects",
Expand Down
16 changes: 8 additions & 8 deletions src/app/tests/integration/RequiredPrivilegeStubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@

#include <app/util/privilege-storage.h>

int MatterGetAccessPrivilegeForReadAttribute(chip::ClusterId cluster, chip::AttributeId attribute)
chip::Access::Privilege MatterGetAccessPrivilegeForReadAttribute(chip::ClusterId cluster, chip::AttributeId attribute)
{
return kMatterAccessPrivilegeAdminister;
return chip::Access::Privilege::kAdminister;
}

int MatterGetAccessPrivilegeForWriteAttribute(chip::ClusterId cluster, chip::AttributeId attribute)
chip::Access::Privilege MatterGetAccessPrivilegeForWriteAttribute(chip::ClusterId cluster, chip::AttributeId attribute)
{
return kMatterAccessPrivilegeAdminister;
return chip::Access::Privilege::kAdminister;
}

int MatterGetAccessPrivilegeForInvokeCommand(chip::ClusterId cluster, chip::CommandId command)
chip::Access::Privilege MatterGetAccessPrivilegeForInvokeCommand(chip::ClusterId cluster, chip::CommandId command)
{
return kMatterAccessPrivilegeAdminister;
return chip::Access::Privilege::kAdminister;
}

int MatterGetAccessPrivilegeForReadEvent(chip::ClusterId cluster, chip::EventId event)
chip::Access::Privilege MatterGetAccessPrivilegeForReadEvent(chip::ClusterId cluster, chip::EventId event)
{
return kMatterAccessPrivilegeAdminister;
return chip::Access::Privilege::kAdminister;
}
23 changes: 0 additions & 23 deletions src/app/util/privilege-constants.h

This file was deleted.

42 changes: 20 additions & 22 deletions src/app/util/privilege-storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

#include <lib/support/CodeUtils.h>

#include <cstdint>

using chip::AttributeId;
using chip::ClusterId;
using chip::CommandId;
Expand All @@ -36,47 +34,47 @@ namespace {

#ifdef GENERATED_ACCESS_READ_ATTRIBUTE__CLUSTER
namespace GeneratedAccessReadAttribute {
constexpr ClusterId kCluster[] = GENERATED_ACCESS_READ_ATTRIBUTE__CLUSTER;
constexpr AttributeId kAttribute[] = GENERATED_ACCESS_READ_ATTRIBUTE__ATTRIBUTE;
constexpr uint8_t kPrivilege[] = GENERATED_ACCESS_READ_ATTRIBUTE__PRIVILEGE;
constexpr ClusterId kCluster[] = GENERATED_ACCESS_READ_ATTRIBUTE__CLUSTER;
constexpr AttributeId kAttribute[] = GENERATED_ACCESS_READ_ATTRIBUTE__ATTRIBUTE;
constexpr chip::Access::Privilege kPrivilege[] = GENERATED_ACCESS_READ_ATTRIBUTE__PRIVILEGE;
static_assert(ArraySize(kCluster) == ArraySize(kAttribute) && ArraySize(kAttribute) == ArraySize(kPrivilege),
"Generated parallel arrays must be same size");
} // namespace GeneratedAccessReadAttribute
#endif

#ifdef GENERATED_ACCESS_WRITE_ATTRIBUTE__CLUSTER
namespace GeneratedAccessWriteAttribute {
constexpr ClusterId kCluster[] = GENERATED_ACCESS_WRITE_ATTRIBUTE__CLUSTER;
constexpr AttributeId kAttribute[] = GENERATED_ACCESS_WRITE_ATTRIBUTE__ATTRIBUTE;
constexpr uint8_t kPrivilege[] = GENERATED_ACCESS_WRITE_ATTRIBUTE__PRIVILEGE;
constexpr ClusterId kCluster[] = GENERATED_ACCESS_WRITE_ATTRIBUTE__CLUSTER;
constexpr AttributeId kAttribute[] = GENERATED_ACCESS_WRITE_ATTRIBUTE__ATTRIBUTE;
constexpr chip::Access::Privilege kPrivilege[] = GENERATED_ACCESS_WRITE_ATTRIBUTE__PRIVILEGE;
static_assert(ArraySize(kCluster) == ArraySize(kAttribute) && ArraySize(kAttribute) == ArraySize(kPrivilege),
"Generated parallel arrays must be same size");
} // namespace GeneratedAccessWriteAttribute
#endif

#ifdef GENERATED_ACCESS_INVOKE_COMMAND__CLUSTER
namespace GeneratedAccessInvokeCommand {
constexpr ClusterId kCluster[] = GENERATED_ACCESS_INVOKE_COMMAND__CLUSTER;
constexpr CommandId kCommand[] = GENERATED_ACCESS_INVOKE_COMMAND__COMMAND;
constexpr uint8_t kPrivilege[] = GENERATED_ACCESS_INVOKE_COMMAND__PRIVILEGE;
constexpr ClusterId kCluster[] = GENERATED_ACCESS_INVOKE_COMMAND__CLUSTER;
constexpr CommandId kCommand[] = GENERATED_ACCESS_INVOKE_COMMAND__COMMAND;
constexpr chip::Access::Privilege kPrivilege[] = GENERATED_ACCESS_INVOKE_COMMAND__PRIVILEGE;
static_assert(ArraySize(kCluster) == ArraySize(kCommand) && ArraySize(kCommand) == ArraySize(kPrivilege),
"Generated parallel arrays must be same size");
} // namespace GeneratedAccessInvokeCommand
#endif

#ifdef GENERATED_ACCESS_READ_EVENT__CLUSTER
namespace GeneratedAccessReadEvent {
constexpr ClusterId kCluster[] = GENERATED_ACCESS_READ_EVENT__CLUSTER;
constexpr EventId kEvent[] = GENERATED_ACCESS_READ_EVENT__EVENT;
constexpr uint8_t kPrivilege[] = GENERATED_ACCESS_READ_EVENT__PRIVILEGE;
constexpr ClusterId kCluster[] = GENERATED_ACCESS_READ_EVENT__CLUSTER;
constexpr EventId kEvent[] = GENERATED_ACCESS_READ_EVENT__EVENT;
constexpr chip::Access::Privilege kPrivilege[] = GENERATED_ACCESS_READ_EVENT__PRIVILEGE;
static_assert(ArraySize(kCluster) == ArraySize(kEvent) && ArraySize(kEvent) == ArraySize(kPrivilege),
"Generated parallel arrays must be same size");
} // namespace GeneratedAccessReadEvent
#endif

} // anonymous namespace

int MatterGetAccessPrivilegeForReadAttribute(ClusterId cluster, AttributeId attribute)
chip::Access::Privilege MatterGetAccessPrivilegeForReadAttribute(ClusterId cluster, AttributeId attribute)
{
#ifdef GENERATED_ACCESS_READ_ATTRIBUTE__CLUSTER
using namespace GeneratedAccessReadAttribute;
Expand All @@ -88,10 +86,10 @@ int MatterGetAccessPrivilegeForReadAttribute(ClusterId cluster, AttributeId attr
}
}
#endif
return kMatterAccessPrivilegeView;
return chip::Access::Privilege::kView;
}

int MatterGetAccessPrivilegeForWriteAttribute(ClusterId cluster, AttributeId attribute)
chip::Access::Privilege MatterGetAccessPrivilegeForWriteAttribute(ClusterId cluster, AttributeId attribute)
{
#ifdef GENERATED_ACCESS_WRITE_ATTRIBUTE__CLUSTER
using namespace GeneratedAccessWriteAttribute;
Expand All @@ -103,10 +101,10 @@ int MatterGetAccessPrivilegeForWriteAttribute(ClusterId cluster, AttributeId att
}
}
#endif
return kMatterAccessPrivilegeOperate;
return chip::Access::Privilege::kOperate;
}

int MatterGetAccessPrivilegeForInvokeCommand(ClusterId cluster, CommandId command)
chip::Access::Privilege MatterGetAccessPrivilegeForInvokeCommand(ClusterId cluster, CommandId command)
{
#ifdef GENERATED_ACCESS_INVOKE_COMMAND__CLUSTER
using namespace GeneratedAccessInvokeCommand;
Expand All @@ -118,10 +116,10 @@ int MatterGetAccessPrivilegeForInvokeCommand(ClusterId cluster, CommandId comman
}
}
#endif
return kMatterAccessPrivilegeOperate;
return chip::Access::Privilege::kOperate;
}

int MatterGetAccessPrivilegeForReadEvent(ClusterId cluster, EventId event)
chip::Access::Privilege MatterGetAccessPrivilegeForReadEvent(ClusterId cluster, EventId event)
{
#ifdef GENERATED_ACCESS_READ_EVENT__CLUSTER
using namespace GeneratedAccessReadEvent;
Expand All @@ -133,5 +131,5 @@ int MatterGetAccessPrivilegeForReadEvent(ClusterId cluster, EventId event)
}
}
#endif
return kMatterAccessPrivilegeView;
return chip::Access::Privilege::kView;
}
10 changes: 5 additions & 5 deletions src/app/util/privilege-storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
*/
#pragma once

#include <app/util/privilege-constants.h>
#include <access/Privilege.h>
#include <lib/core/DataModelTypes.h>

int MatterGetAccessPrivilegeForReadAttribute(chip::ClusterId cluster, chip::AttributeId attribute);
int MatterGetAccessPrivilegeForWriteAttribute(chip::ClusterId cluster, chip::AttributeId attribute);
int MatterGetAccessPrivilegeForInvokeCommand(chip::ClusterId cluster, chip::CommandId command);
int MatterGetAccessPrivilegeForReadEvent(chip::ClusterId cluster, chip::EventId event);
chip::Access::Privilege MatterGetAccessPrivilegeForReadAttribute(chip::ClusterId cluster, chip::AttributeId attribute);
chip::Access::Privilege MatterGetAccessPrivilegeForWriteAttribute(chip::ClusterId cluster, chip::AttributeId attribute);
chip::Access::Privilege MatterGetAccessPrivilegeForInvokeCommand(chip::ClusterId cluster, chip::CommandId command);
chip::Access::Privilege MatterGetAccessPrivilegeForReadEvent(chip::ClusterId cluster, chip::EventId event);
22 changes: 11 additions & 11 deletions src/app/zap-templates/templates/app/access.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Prevent multiple inclusion
#pragma once

#include <app/util/privilege-constants.h>
#include <access/Privilege.h>

// Prevent changing generated format
// clang-format off
Expand Down Expand Up @@ -89,11 +89,11 @@
{{#if (isStrEqual role "view")}}
/* Cluster: {{parent.parent.name}}, Attribute: {{parent.name}}, Privilege: {{role}} */ \
{{else if (isStrEqual role "operate")}}
kMatterAccessPrivilegeOperate, /* Cluster: {{parent.parent.name}}, Attribute: {{parent.name}}, Privilege: {{role}} */ \
chip::Access::Privilege::kOperate, /* Cluster: {{parent.parent.name}}, Attribute: {{parent.name}}, Privilege: {{role}} */ \
{{else if (isStrEqual role "manage")}}
kMatterAccessPrivilegeManage, /* Cluster: {{parent.parent.name}}, Attribute: {{parent.name}}, Privilege: {{role}} */ \
chip::Access::Privilege::kManage, /* Cluster: {{parent.parent.name}}, Attribute: {{parent.name}}, Privilege: {{role}} */ \
{{else if (isStrEqual role "administer")}}
kMatterAccessPrivilegeAdminister, /* Cluster: {{parent.parent.name}}, Attribute: {{parent.name}}, Privilege: {{role}} */ \
chip::Access::Privilege::kAdminister, /* Cluster: {{parent.parent.name}}, Attribute: {{parent.name}}, Privilege: {{role}} */ \
{{else}}
ERROR: access has unrecognized role/privilege [ Cluster: {{parent.parent.name}}, Attribute: {{parent.name}} ]
{{/if}}
Expand Down Expand Up @@ -192,9 +192,9 @@
{{else if (isStrEqual role "operate")}}
/* Cluster: {{parent.parent.name}}, Attribute: {{parent.name}}, Privilege: {{role}} */ \
{{else if (isStrEqual role "manage")}}
kMatterAccessPrivilegeManage, /* Cluster: {{parent.parent.name}}, Attribute: {{parent.name}}, Privilege: {{role}} */ \
chip::Access::Privilege::kManage, /* Cluster: {{parent.parent.name}}, Attribute: {{parent.name}}, Privilege: {{role}} */ \
{{else if (isStrEqual role "administer")}}
kMatterAccessPrivilegeAdminister, /* Cluster: {{parent.parent.name}}, Attribute: {{parent.name}}, Privilege: {{role}} */ \
chip::Access::Privilege::kAdminister, /* Cluster: {{parent.parent.name}}, Attribute: {{parent.name}}, Privilege: {{role}} */ \
{{else}}
ERROR: access has unrecognized role/privilege [ Cluster: {{parent.parent.name}}, Attribute: {{parent.name}} ]
{{/if}}
Expand Down Expand Up @@ -293,9 +293,9 @@
{{else if (isStrEqual role "operate")}}
/* Cluster: {{parent.parent.name}}, Command: {{parent.commandName}}, Privilege: {{role}} */ \
{{else if (isStrEqual role "manage")}}
kMatterAccessPrivilegeManage, /* Cluster: {{parent.parent.name}}, Command: {{parent.commandName}}, Privilege: {{role}} */ \
chip::Access::Privilege::kManage, /* Cluster: {{parent.parent.name}}, Command: {{parent.commandName}}, Privilege: {{role}} */ \
{{else if (isStrEqual role "administer")}}
kMatterAccessPrivilegeAdminister, /* Cluster: {{parent.parent.name}}, Command: {{parent.commandName}}, Privilege: {{role}} */ \
chip::Access::Privilege::kAdminister, /* Cluster: {{parent.parent.name}}, Command: {{parent.commandName}}, Privilege: {{role}} */ \
{{else}}
ERROR: access has unrecognized role/privilege [ Cluster: {{parent.parent.name}}, Command: {{parent.commandName}} ]
{{/if}}
Expand Down Expand Up @@ -392,11 +392,11 @@
{{#if (isStrEqual role "view")}}
/* Cluster: {{parent.parent.name}}, Event: {{parent.name}}, Privilege: {{role}} */ \
{{else if (isStrEqual role "operate")}}
kMatterAccessPrivilegeOperate, /* Cluster: {{parent.parent.name}}, Event: {{parent.name}}, Privilege: {{role}} */ \
chip::Access::Privilege::kOperate, /* Cluster: {{parent.parent.name}}, Event: {{parent.name}}, Privilege: {{role}} */ \
{{else if (isStrEqual role "manage")}}
kMatterAccessPrivilegeManage, /* Cluster: {{parent.parent.name}}, Event: {{parent.name}}, Privilege: {{role}} */ \
chip::Access::Privilege::kManage, /* Cluster: {{parent.parent.name}}, Event: {{parent.name}}, Privilege: {{role}} */ \
{{else if (isStrEqual role "administer")}}
kMatterAccessPrivilegeAdminister, /* Cluster: {{parent.parent.name}}, Event: {{parent.name}}, Privilege: {{role}} */ \
chip::Access::Privilege::kAdminister, /* Cluster: {{parent.parent.name}}, Event: {{parent.name}}, Privilege: {{role}} */ \
{{else}}
ERROR: access has unrecognized role/privilege [ Cluster: {{parent.parent.name}}, Event: {{parent.name}} ]
{{/if}}
Expand Down
Loading

0 comments on commit 8a1c7aa

Please sign in to comment.