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