Marc Lepage | 2c4de1b | 2022-01-27 13:25:46 -0500 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) 2022 Project CHIP Authors |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include "ConcreteAttributePath.h" |
| 22 | #include "ConcreteCommandPath.h" |
| 23 | #include "ConcreteEventPath.h" |
| 24 | |
Marc Lepage | c7b4913 | 2022-03-23 17:28:47 -0400 | [diff] [blame] | 25 | #include <app/util/privilege-storage.h> |
| 26 | |
Marc Lepage | 2c4de1b | 2022-01-27 13:25:46 -0500 | [diff] [blame] | 27 | #include <access/Privilege.h> |
| 28 | |
| 29 | #include <lib/core/CHIPCore.h> |
| 30 | #include <lib/core/DataModelTypes.h> |
| 31 | |
| 32 | namespace chip { |
| 33 | namespace app { |
| 34 | |
Marc Lepage | 2c4de1b | 2022-01-27 13:25:46 -0500 | [diff] [blame] | 35 | class RequiredPrivilege |
| 36 | { |
| 37 | using Privilege = Access::Privilege; |
| 38 | |
Marc Lepage | c7b4913 | 2022-03-23 17:28:47 -0400 | [diff] [blame] | 39 | static constexpr Privilege kPrivilegeMapper[] = { Privilege::kView, Privilege::kOperate, Privilege::kManage, |
| 40 | Privilege::kAdminister }; |
| 41 | |
| 42 | static_assert(ArraySize(kPrivilegeMapper) > kMatterAccessPrivilegeView && |
| 43 | kPrivilegeMapper[kMatterAccessPrivilegeView] == Privilege::kView, |
| 44 | "Must map privilege correctly"); |
| 45 | static_assert(ArraySize(kPrivilegeMapper) > kMatterAccessPrivilegeOperate && |
| 46 | kPrivilegeMapper[kMatterAccessPrivilegeOperate] == Privilege::kOperate, |
| 47 | "Must map privilege correctly"); |
| 48 | static_assert(ArraySize(kPrivilegeMapper) > kMatterAccessPrivilegeManage && |
| 49 | kPrivilegeMapper[kMatterAccessPrivilegeManage] == Privilege::kManage, |
| 50 | "Must map privilege correctly"); |
| 51 | static_assert(ArraySize(kPrivilegeMapper) > kMatterAccessPrivilegeAdminister && |
| 52 | kPrivilegeMapper[kMatterAccessPrivilegeAdminister] == Privilege::kAdminister, |
| 53 | "Must map privilege correctly"); |
| 54 | static_assert(ArraySize(kPrivilegeMapper) > kMatterAccessPrivilegeMaxValue, "Must map all privileges"); |
| 55 | |
Marc Lepage | 2c4de1b | 2022-01-27 13:25:46 -0500 | [diff] [blame] | 56 | public: |
Marc Lepage | c7b4913 | 2022-03-23 17:28:47 -0400 | [diff] [blame] | 57 | static Privilege ForReadAttribute(const ConcreteAttributePath & path) |
| 58 | { |
| 59 | return kPrivilegeMapper[MatterGetAccessPrivilegeForReadAttribute(path.mClusterId, path.mAttributeId)]; |
| 60 | } |
| 61 | |
| 62 | static Privilege ForWriteAttribute(const ConcreteAttributePath & path) |
| 63 | { |
| 64 | return kPrivilegeMapper[MatterGetAccessPrivilegeForWriteAttribute(path.mClusterId, path.mAttributeId)]; |
| 65 | } |
| 66 | |
| 67 | static Privilege ForInvokeCommand(const ConcreteCommandPath & path) |
| 68 | { |
| 69 | return kPrivilegeMapper[MatterGetAccessPrivilegeForInvokeCommand(path.mClusterId, path.mCommandId)]; |
| 70 | } |
| 71 | |
| 72 | static Privilege ForReadEvent(const ConcreteEventPath & path) |
| 73 | { |
| 74 | return kPrivilegeMapper[MatterGetAccessPrivilegeForReadEvent(path.mClusterId, path.mEventId)]; |
| 75 | } |
Marc Lepage | 2c4de1b | 2022-01-27 13:25:46 -0500 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | } // namespace app |
| 79 | } // namespace chip |