forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ZAP gen for Matter access required privileges
ZAPT file iterates over access definitions for app server clusters, for attributes/commands/events, to generate parallel arrays of custom privileges for read/write attribute, invoke command, and read event. These are generated and built per-app, and linked in by the library's RequiredPrivilege module. Weak implementations provide an empty default (needed for testing). Fixes project-chip#14419
- Loading branch information
1 parent
0c43841
commit e00ab9b
Showing
29 changed files
with
3,837 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/** | ||
* | ||
* Copyright (c) 2022 Project CHIP Authors | ||
* | ||
* 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. | ||
*/ | ||
|
||
#include "privilege-storage.h" | ||
|
||
#include <zap-generated/access.h> | ||
|
||
#include <lib/support/CodeUtils.h> | ||
|
||
#include <cstdint> | ||
|
||
using chip::AttributeId; | ||
using chip::ClusterId; | ||
using chip::CommandId; | ||
using chip::EventId; | ||
|
||
namespace { | ||
|
||
static_assert(GENERATED_ACCESS_PRIVILEGE__VIEW == kMatterAccessPrivilegeView, | ||
"Generated privilege value must match privilege value in code"); | ||
static_assert(GENERATED_ACCESS_PRIVILEGE__OPERATE == kMatterAccessPrivilegeOperate, | ||
"Generated privilege value must match privilege value in code"); | ||
static_assert(GENERATED_ACCESS_PRIVILEGE__MANAGE == kMatterAccessPrivilegeManage, | ||
"Generated privilege value must match privilege value in code"); | ||
static_assert(GENERATED_ACCESS_PRIVILEGE__ADMINISTER == kMatterAccessPrivilegeAdminister, | ||
"Generated privilege value must match privilege value in code"); | ||
|
||
#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; | ||
static_assert(ArraySize(kCluster) == ArraySize(kAttribute) && ArraySize(kAttribute) == ArraySize(kPrivilege), | ||
"Generated parallel arrays must be same size"); | ||
} // namespace GeneratedAccessReadAttribute | ||
#else | ||
#error "Undefined generated access for read attribute" | ||
#endif | ||
|
||
#ifdef GENERATED_ACCESS_WRITE_ATTRIBUTE__CLUSTER | ||
namespace GeneratedAccessWriteAttribute { | ||
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; | ||
static_assert(ArraySize(kCluster) == ArraySize(kAttribute) && ArraySize(kAttribute) == ArraySize(kPrivilege), | ||
"Generated parallel arrays must be same size"); | ||
} // namespace GeneratedAccessWriteAttribute | ||
#else | ||
#error "Undefined generated access for read attribute" | ||
#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; | ||
static_assert(ArraySize(kCluster) == ArraySize(kCommand) && ArraySize(kCommand) == ArraySize(kPrivilege), | ||
"Generated parallel arrays must be same size"); | ||
} // namespace GeneratedAccessInvokeCommand | ||
#else | ||
#error "Undefined generated access for read attribute" | ||
#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; | ||
static_assert(ArraySize(kCluster) == ArraySize(kEvent) && ArraySize(kEvent) == ArraySize(kPrivilege), | ||
"Generated parallel arrays must be same size"); | ||
} // namespace GeneratedAccessReadEvent | ||
#else | ||
#error "Undefined generated access for read attribute" | ||
#endif | ||
|
||
} // anonymous namespace | ||
|
||
int MatterGetAccessPrivilegeForReadAttribute(ClusterId cluster, AttributeId attribute) | ||
{ | ||
using namespace GeneratedAccessReadAttribute; | ||
for (size_t i = 0; i < ArraySize(kCluster); ++i) | ||
{ | ||
if (kCluster[i] == cluster && kAttribute[i] == attribute) | ||
{ | ||
return kPrivilege[i]; | ||
} | ||
} | ||
return GENERATED_ACCESS_PRIVILEGE__VIEW; | ||
} | ||
|
||
int MatterGetAccessPrivilegeForWriteAttribute(ClusterId cluster, AttributeId attribute) | ||
{ | ||
using namespace GeneratedAccessWriteAttribute; | ||
for (size_t i = 0; i < ArraySize(kCluster); ++i) | ||
{ | ||
if (kCluster[i] == cluster && kAttribute[i] == attribute) | ||
{ | ||
return kPrivilege[i]; | ||
} | ||
} | ||
return GENERATED_ACCESS_PRIVILEGE__OPERATE; | ||
} | ||
|
||
int MatterGetAccessPrivilegeForInvokeCommand(ClusterId cluster, CommandId command) | ||
{ | ||
using namespace GeneratedAccessInvokeCommand; | ||
for (size_t i = 0; i < ArraySize(kCluster); ++i) | ||
{ | ||
if (kCluster[i] == cluster && kCommand[i] == command) | ||
{ | ||
return kPrivilege[i]; | ||
} | ||
} | ||
return GENERATED_ACCESS_PRIVILEGE__OPERATE; | ||
} | ||
|
||
int MatterGetAccessPrivilegeForReadEvent(ClusterId cluster, EventId event) | ||
{ | ||
using namespace GeneratedAccessReadEvent; | ||
for (size_t i = 0; i < ArraySize(kCluster); ++i) | ||
{ | ||
if (kCluster[i] == cluster && kEvent[i] == event) | ||
{ | ||
return kPrivilege[i]; | ||
} | ||
} | ||
return GENERATED_ACCESS_PRIVILEGE__VIEW; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* | ||
* Copyright (c) 2022 Project CHIP Authors | ||
* | ||
* 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 <lib/core/DataModelTypes.h> | ||
|
||
constexpr int kMatterAccessPrivilegeView = 0; | ||
constexpr int kMatterAccessPrivilegeOperate = 1; | ||
constexpr int kMatterAccessPrivilegeManage = 2; | ||
constexpr int kMatterAccessPrivilegeAdminister = 3; | ||
constexpr int kMatterAccessPrivilegeMaxValue = kMatterAccessPrivilegeAdminister; | ||
|
||
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.