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 : inline constexpr CompressedFabricId kUndefinedCompressedFabricId = 0ULL;
55 : inline constexpr FabricId kUndefinedFabricId = 0ULL;
56 :
57 : inline constexpr FabricIndex kUndefinedFabricIndex = 0;
58 : inline constexpr FabricIndex kMinValidFabricIndex = 1;
59 : inline constexpr FabricIndex kMaxValidFabricIndex = UINT8_MAX - 1;
60 :
61 : inline constexpr EndpointId kInvalidEndpointId = 0xFFFF;
62 : inline constexpr EndpointId kRootEndpointId = 0;
63 : inline constexpr ListIndex kInvalidListIndex = 0xFFFF; // List index is a uint16 thus 0xFFFF is a invalid list index.
64 : inline constexpr KeysetId kInvalidKeysetId = 0xFFFF;
65 :
66 : // Invalid IC identifier is provisional. Value will most likely change when identifying token is defined
67 : // https://github.com/project-chip/connectedhomeip/issues/24251
68 : inline constexpr uint64_t kInvalidIcId = 0;
69 :
70 : // These are MEIs, 0xFFFF is not a valid manufacturer code,
71 : // thus 0xFFFF'FFFF is not a valid MEI.
72 : static constexpr ClusterId kInvalidClusterId = 0xFFFF'FFFF;
73 : static constexpr AttributeId kInvalidAttributeId = 0xFFFF'FFFF;
74 : static constexpr CommandId kInvalidCommandId = 0xFFFF'FFFF;
75 : static constexpr EventId kInvalidEventId = 0xFFFF'FFFF;
76 : static constexpr FieldId kInvalidFieldId = 0xFFFF'FFFF;
77 :
78 : static constexpr uint16_t kMaxVendorId = VendorId::TestVendor4;
79 :
80 40240 : static constexpr uint16_t ExtractIdFromMEI(uint32_t aMEI)
81 : {
82 40240 : constexpr uint32_t kIdMask = 0x0000'FFFF;
83 40240 : return static_cast<uint16_t>(aMEI & kIdMask);
84 : }
85 :
86 40240 : static constexpr uint16_t ExtractVendorFromMEI(uint32_t aMEI)
87 : {
88 40240 : constexpr uint32_t kVendorMask = 0xFFFF'0000;
89 40240 : constexpr uint32_t kVendorShift = 16;
90 40240 : return static_cast<uint16_t>((aMEI & kVendorMask) >> kVendorShift);
91 : }
92 :
93 36680 : static constexpr bool IsValidVendorId(uint16_t aVendorId)
94 : {
95 36680 : return aVendorId <= kMaxVendorId;
96 : }
97 :
98 23851 : constexpr bool IsValidClusterId(ClusterId aClusterId)
99 : {
100 23851 : const auto id = ExtractIdFromMEI(aClusterId);
101 23851 : const auto vendor = ExtractVendorFromMEI(aClusterId);
102 : // Cluster id suffixes in the range 0x0000 to 0x7FFF indicate a standard
103 : // cluster.
104 : //
105 : // Cluster id suffixes in the range 0xFC00 to 0xFFFE indicate an MS cluster.
106 23851 : return IsValidVendorId(vendor) && ((vendor == 0x0000 && id <= 0x7FFF) || (vendor >= 0x0001 && id >= 0xFC00 && id <= 0xFFFE));
107 : }
108 :
109 1780 : constexpr bool IsGlobalAttribute(AttributeId aAttributeId)
110 : {
111 1780 : const auto id = ExtractIdFromMEI(aAttributeId);
112 1780 : const auto vendor = ExtractVendorFromMEI(aAttributeId);
113 : // Attribute id suffixes in the range 0xF000 to 0xFFFE indicate a standard
114 : // global attribute.
115 1780 : return (vendor == 0x0000 && id >= 0xF000 && id <= 0xFFFE);
116 : }
117 :
118 14538 : constexpr bool IsValidAttributeId(AttributeId aAttributeId)
119 : {
120 14538 : const auto id = ExtractIdFromMEI(aAttributeId);
121 14538 : const auto vendor = ExtractVendorFromMEI(aAttributeId);
122 : // Attribute id suffixes in the range 0x0000 to 0x4FFF indicate a non-global
123 : // attribute (standard or MS). The vendor id must not be wildcard in this
124 : // case.
125 14538 : return (id <= 0x4FFF && IsValidVendorId(vendor)) || IsGlobalAttribute(aAttributeId);
126 : }
127 :
128 71 : constexpr bool IsValidCommandId(CommandId aCommandId)
129 : {
130 71 : const auto id = ExtractIdFromMEI(aCommandId);
131 71 : const auto vendor = ExtractVendorFromMEI(aCommandId);
132 :
133 : // Command id suffixes must be in the range 0x00 to 0xFF, and the prefix
134 : // must be a valid prefix (standard or MC).
135 71 : return id <= 0xFF && IsValidVendorId(vendor);
136 : }
137 :
138 1360 : constexpr bool IsValidDeviceTypeId(DeviceTypeId aDeviceTypeId)
139 : {
140 1360 : const DeviceTypeId kIdMask = 0x0000'FFFF;
141 1360 : const DeviceTypeId kVendorMask = 0xFFFF'0000;
142 1360 : const auto id = aDeviceTypeId & kIdMask;
143 1360 : const auto vendor = aDeviceTypeId & kVendorMask;
144 1360 : return vendor <= 0xFFFE'0000 && id <= 0xBFFF;
145 : }
146 :
147 5819 : constexpr bool IsValidEndpointId(EndpointId aEndpointId)
148 : {
149 5819 : return aEndpointId != kInvalidEndpointId;
150 : }
151 :
152 9596 : constexpr bool IsValidFabricIndex(FabricIndex fabricIndex)
153 : {
154 9596 : return (fabricIndex >= kMinValidFabricIndex) && (fabricIndex <= kMaxValidFabricIndex);
155 : }
156 :
157 5649 : constexpr bool IsValidFabricId(FabricId fabricId)
158 : {
159 5649 : return fabricId != kUndefinedFabricId;
160 : }
161 :
162 : } // namespace chip
|