Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2021-2023 Project CHIP Authors
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #pragma once
19 :
20 : #include <cstdint>
21 :
22 : #include <lib/core/CHIPConfig.h>
23 : #include <lib/core/CHIPVendorIdentifiers.hpp> // For VendorId
24 : #include <lib/core/GroupId.h>
25 : #include <lib/core/NodeId.h>
26 : #include <lib/core/PasscodeId.h>
27 :
28 : namespace chip {
29 :
30 : typedef uint8_t ActionId;
31 : typedef uint32_t AttributeId;
32 : typedef uint32_t ClusterId;
33 : typedef uint8_t ClusterStatus;
34 : typedef uint32_t CommandId;
35 : typedef uint16_t CommandRef;
36 : typedef uint64_t CompressedFabricId;
37 : typedef uint32_t DataVersion;
38 : typedef uint32_t DeviceTypeId;
39 : typedef uint32_t ElapsedS;
40 : typedef uint16_t EndpointId;
41 : typedef uint32_t EventId;
42 : typedef uint64_t EventNumber;
43 : typedef uint64_t FabricId;
44 : typedef uint8_t FabricIndex;
45 : typedef uint32_t FieldId;
46 : typedef uint16_t ListIndex;
47 : typedef uint16_t LocalizedStringIdentifier;
48 : typedef uint32_t TransactionId;
49 : typedef uint16_t KeysetId;
50 : typedef uint8_t InteractionModelRevision;
51 : typedef uint32_t SubscriptionId;
52 : typedef uint8_t SceneId;
53 :
54 : typedef uint8_t Percent;
55 : typedef uint16_t Percent100ths;
56 : typedef int64_t Energy_mWh;
57 : typedef int64_t Energy_mVAh;
58 : typedef int64_t Energy_mVARh;
59 : typedef int64_t Amperage_mA;
60 : typedef int64_t Power_mW;
61 : typedef int64_t Power_mVA;
62 : typedef int64_t Power_mVAR;
63 : typedef int64_t Voltage_mV;
64 : typedef int64_t Money;
65 :
66 : inline constexpr CompressedFabricId kUndefinedCompressedFabricId = 0ULL;
67 : inline constexpr FabricId kUndefinedFabricId = 0ULL;
68 :
69 : inline constexpr FabricIndex kUndefinedFabricIndex = 0;
70 : inline constexpr FabricIndex kMinValidFabricIndex = 1;
71 : inline constexpr FabricIndex kMaxValidFabricIndex = UINT8_MAX - 1;
72 :
73 : inline constexpr EndpointId kInvalidEndpointId = 0xFFFF;
74 : inline constexpr EndpointId kRootEndpointId = 0;
75 : inline constexpr ListIndex kInvalidListIndex = 0xFFFF; // List index is a uint16 thus 0xFFFF is a invalid list index.
76 : inline constexpr KeysetId kInvalidKeysetId = 0xFFFF;
77 :
78 : // Invalid IC identifier is provisional. Value will most likely change when identifying token is defined
79 : // https://github.com/project-chip/connectedhomeip/issues/24251
80 : inline constexpr uint64_t kInvalidIcId = 0;
81 :
82 : // These are MEIs, 0xFFFF is not a valid manufacturer code,
83 : // thus 0xFFFF'FFFF is not a valid MEI.
84 : static constexpr ClusterId kInvalidClusterId = 0xFFFF'FFFF;
85 : static constexpr AttributeId kInvalidAttributeId = 0xFFFF'FFFF;
86 : static constexpr CommandId kInvalidCommandId = 0xFFFF'FFFF;
87 : static constexpr EventId kInvalidEventId = 0xFFFF'FFFF;
88 : static constexpr FieldId kInvalidFieldId = 0xFFFF'FFFF;
89 :
90 : static constexpr uint16_t kMaxVendorId = VendorId::TestVendor4;
91 :
92 44061 : static constexpr uint16_t ExtractIdFromMEI(uint32_t aMEI)
93 : {
94 44061 : constexpr uint32_t kIdMask = 0x0000'FFFF;
95 44061 : return static_cast<uint16_t>(aMEI & kIdMask);
96 : }
97 :
98 44061 : static constexpr uint16_t ExtractVendorFromMEI(uint32_t aMEI)
99 : {
100 44061 : constexpr uint32_t kVendorMask = 0xFFFF'0000;
101 44061 : constexpr uint32_t kVendorShift = 16;
102 44061 : return static_cast<uint16_t>((aMEI & kVendorMask) >> kVendorShift);
103 : }
104 :
105 40501 : static constexpr bool IsValidVendorId(uint16_t aVendorId)
106 : {
107 40501 : return aVendorId <= kMaxVendorId;
108 : }
109 :
110 25762 : constexpr bool IsValidClusterId(ClusterId aClusterId)
111 : {
112 25762 : const auto id = ExtractIdFromMEI(aClusterId);
113 25762 : const auto vendor = ExtractVendorFromMEI(aClusterId);
114 : // Cluster id suffixes in the range 0x0000 to 0x7FFF indicate a standard
115 : // cluster.
116 : //
117 : // Cluster id suffixes in the range 0xFC00 to 0xFFFE indicate an MS cluster.
118 25762 : return IsValidVendorId(vendor) && ((vendor == 0x0000 && id <= 0x7FFF) || (vendor >= 0x0001 && id >= 0xFC00 && id <= 0xFFFE));
119 : }
120 :
121 1780 : constexpr bool IsGlobalAttribute(AttributeId aAttributeId)
122 : {
123 1780 : const auto id = ExtractIdFromMEI(aAttributeId);
124 1780 : const auto vendor = ExtractVendorFromMEI(aAttributeId);
125 : // Attribute id suffixes in the range 0xF000 to 0xFFFE indicate a standard
126 : // global attribute.
127 1780 : return (vendor == 0x0000 && id >= 0xF000 && id <= 0xFFFE);
128 : }
129 :
130 16444 : constexpr bool IsValidAttributeId(AttributeId aAttributeId)
131 : {
132 16444 : const auto id = ExtractIdFromMEI(aAttributeId);
133 16444 : const auto vendor = ExtractVendorFromMEI(aAttributeId);
134 : // Attribute id suffixes in the range 0x0000 to 0x4FFF indicate a non-global
135 : // attribute (standard or MS). The vendor id must not be wildcard in this
136 : // case.
137 16444 : return (id <= 0x4FFF && IsValidVendorId(vendor)) || IsGlobalAttribute(aAttributeId);
138 : }
139 :
140 75 : constexpr bool IsValidCommandId(CommandId aCommandId)
141 : {
142 75 : const auto id = ExtractIdFromMEI(aCommandId);
143 75 : const auto vendor = ExtractVendorFromMEI(aCommandId);
144 :
145 : // Command id suffixes must be in the range 0x00 to 0xFF, and the prefix
146 : // must be a valid prefix (standard or MC).
147 75 : return id <= 0xFF && IsValidVendorId(vendor);
148 : }
149 :
150 1360 : constexpr bool IsValidDeviceTypeId(DeviceTypeId aDeviceTypeId)
151 : {
152 1360 : const DeviceTypeId kIdMask = 0x0000'FFFF;
153 1360 : const DeviceTypeId kVendorMask = 0xFFFF'0000;
154 1360 : const auto id = aDeviceTypeId & kIdMask;
155 1360 : const auto vendor = aDeviceTypeId & kVendorMask;
156 1360 : return vendor <= 0xFFFE'0000 && id <= 0xBFFF;
157 : }
158 :
159 5822 : constexpr bool IsValidEndpointId(EndpointId aEndpointId)
160 : {
161 5822 : return aEndpointId != kInvalidEndpointId;
162 : }
163 :
164 9968 : constexpr bool IsValidFabricIndex(FabricIndex fabricIndex)
165 : {
166 9968 : return (fabricIndex >= kMinValidFabricIndex) && (fabricIndex <= kMaxValidFabricIndex);
167 : }
168 :
169 5812 : constexpr bool IsValidFabricId(FabricId fabricId)
170 : {
171 5812 : return fabricId != kUndefinedFabricId;
172 : }
173 :
174 : } // namespace chip
|