blob: 9d61dae3b52ce9b6220af1bba82dda41dec7c4df [file] [log] [blame]
Marc Lepage2c4de1b2022-01-27 13:25:46 -05001/*
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 Lepagec7b49132022-03-23 17:28:47 -040025#include <app/util/privilege-storage.h>
26
Marc Lepage2c4de1b2022-01-27 13:25:46 -050027#include <access/Privilege.h>
28
29#include <lib/core/CHIPCore.h>
30#include <lib/core/DataModelTypes.h>
31
32namespace chip {
33namespace app {
34
Marc Lepage2c4de1b2022-01-27 13:25:46 -050035class RequiredPrivilege
36{
37 using Privilege = Access::Privilege;
38
Marc Lepagec7b49132022-03-23 17:28:47 -040039 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 Lepage2c4de1b2022-01-27 13:25:46 -050056public:
Marc Lepagec7b49132022-03-23 17:28:47 -040057 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 Lepage2c4de1b2022-01-27 13:25:46 -050076};
77
78} // namespace app
79} // namespace chip