3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2015-2017 Nest Labs, Inc.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * This file contains macros to create static CHIP TLV data.
23 * These macros are designed to be used in statically initializing a byte array
24 * containing various TLV elements. TLVWriter can achieve similar functionality
25 * but only on RAM, while macros here can be used to create TLV elements in ROM.
33 #include <core/CHIPTLV.h>
36 * @brief Integral truncate L to the least significant 32-bit
38 * @note Integral truncate would take the least significant bits, regardless of hardware endianness.
40 #define CHIP_TLV_GetLower32From64(v) ((uint32_t)(((uint64_t)(v) >> 0) & 0xFFFFFFFFUL))
43 * @brief Integral truncate argument X to the next least significant 32-bit
45 * @note Right bit shift gets rid of the least significant bits, regardless of hardware endianness.
47 #define CHIP_TLV_GetUpper32From64(v) ((uint32_t)(((uint64_t)(v) >> 32) & 0xFFFFFFFFUL))
50 * @brief Integral truncate L to the least significant 16-bit
52 * @note Integral truncate would take the least significant bits, regardless of hardware endianness.
54 #define CHIP_TLV_GetLower16From32(v) ((uint16_t)(((uint32_t)(v) >> 0) & 0xFFFFU))
57 * @brief Integral truncate argument X to the next least significant 16-bit
59 * @note Right bit shift gets rid of the least significant bits, regardless of hardware endianness.
61 #define CHIP_TLV_GetUpper16From32(v) ((uint16_t)(((uint32_t)(v) >> 16) & 0xFFFFU))
64 * @brief Integral truncate L to the least significant 8-bit
66 * @note Integral truncate would take the least significant bits, regardless of hardware endianness.
68 #define CHIP_TLV_GetLower8From16(v) ((uint8_t)(((uint16_t)(v) >> 0) & 0xFFU))
71 * @brief Integral truncate argument X to the next least significant 8-bit
73 * @note Right bit shift gets rid of the least significant bits, regardless of hardware endianness.
75 #define CHIP_TLV_GetUpper8From16(v) ((uint8_t)(((uint16_t)(v) >> 8) & 0xFFU))
78 * @brief Integral truncate argument v to 8-bit
80 * @note Integral truncate would take the least significant bits, regardless of hardware endianness.
82 #define CHIP_TLV_Serialize8(v) ((uint8_t)(v))
85 * @brief Integral truncate argument v to 16-bit, and then serialize it using CHIP standard Little Endian order
87 * @note Integral truncate would preserve the least significant bits, regardless of hardware endianness.
88 * Right bit shift gets rid of the least significant bits, regardless of hardware endianness.
90 #define CHIP_TLV_Serialize16(v) CHIP_TLV_GetLower8From16(v), CHIP_TLV_GetUpper8From16(v)
93 * @brief Integral truncate argument v to 32-bit, and then serialize it using CHIP standard Little Endian order
95 #define CHIP_TLV_Serialize32(v) \
96 CHIP_TLV_Serialize16(CHIP_TLV_GetLower16From32(v)), CHIP_TLV_Serialize16(CHIP_TLV_GetUpper16From32(v))
99 * @brief Integral truncate argument v to 64-bit, and then serialize it using CHIP standard Little Endian order
101 #define CHIP_TLV_Serialize64(v) \
102 CHIP_TLV_Serialize32(CHIP_TLV_GetLower32From64(v)), CHIP_TLV_Serialize32(CHIP_TLV_GetUpper32From64(v))
105 * @brief Specifies an anonymous TLV element, which doesn't have any tag
107 #define CHIP_TLV_TAG_ANONYMOUS chip::TLV::TLVTagControl::Anonymous
110 * @brief Specifies a TLV element with a context-specific tag
111 * @param Tag The context-specific tag for this TLV element. Would be truncated to 8 bits.
113 #define CHIP_TLV_TAG_CONTEXT_SPECIFIC(Tag) chip::TLV::TLVTagControl::ContextSpecific, CHIP_TLV_Serialize8(Tag)
116 * @brief Specifies a TLV element with a Common Profile tag
117 * @param Tag The tag for this TLV element, defined under Common Profile.
118 * Would be truncated to 16 bites.
120 #define CHIP_TLV_TAG_COMMON_PROFILE_2Bytes(Tag) chip::TLV::TLVTagControl::CommonProfile_2Bytes, CHIP_TLV_Serialize16(Tag)
123 * @brief Specifies a TLV element with a Common Profile tag
124 * @param Tag The tag for this TLV element, defined under Common Profile.
125 * Would be truncated to 32 bites.
127 #define CHIP_TLV_TAG_COMMON_PROFILE_4Bytes(Tag) chip::TLV::TLVTagControl::CommonProfile_4Bytes, CHIP_TLV_Serialize32(Tag)
130 * @brief Specifies a TLV element with an Implicit Profile tag
131 * @param Tag The tag for this TLV element, defined under the current implicit profile.
132 * Would be truncated to 16 bits.
134 #define CHIP_TLV_TAG_IMPLICIT_PROFILE_2Bytes(Tag) chip::TLV::TLVTagControl::ImplicitProfile_2Bytes, CHIP_TLV_Serialize16(Tag)
137 * @brief Specifies a TLV element with an Implicit Profile tag
138 * @param Tag The tag for this TLV element, defined under the current implicit profile.
139 * Would be truncated to 32 bits.
141 #define CHIP_TLV_TAG_IMPLICIT_PROFILE_4Bytes(Tag) chip::TLV::TLVTagControl::ImplicitProfile_4Bytes, CHIP_TLV_Serialize32(Tag)
144 * @brief Specifies a TLV element with a Fully Qualified tag
145 * @param ProfileId {Vendor ID, Profile Number}, as in #CHIPProfileId
146 * @param Tag The tag for this TLV element, defined under ProfileId.
147 * Would be truncated to 16 bits.
149 #define CHIP_TLV_TAG_FULLY_QUALIFIED_6Bytes(ProfileId, Tag) \
150 chip::TLV::TLVTagControl::FullyQualified_6Bytes, CHIP_TLV_Serialize16(ProfileId >> 16), CHIP_TLV_Serialize16(ProfileId), \
151 CHIP_TLV_Serialize16(Tag)
154 * @brief Specifies a TLV element with a Fully Qualified tag
155 * @param ProfileId {Vendor ID, Profile Number}, as in #CHIPProfileId
156 * @param Tag The tag for this TLV element, defined under ProfileId.
157 * Would be truncated to 32 bits.
159 #define CHIP_TLV_TAG_FULLY_QUALIFIED_8Bytes(ProfileId, Tag) \
160 chip::TLV::TLVTagControl::FullyQualified_8Bytes, CHIP_TLV_Serialize16(ProfileId >> 16), CHIP_TLV_Serialize16(ProfileId), \
161 CHIP_TLV_Serialize32(Tag)
164 * @brief Specifies a NULL TLV element, which has just the tag but no value
165 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
167 #define CHIP_TLV_NULL(TagSpec) chip::TLV::TLVElementType::Null | TagSpec
170 * @brief Specifies a Structure TLV element, marking the beginning of a Structure
171 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
173 #define CHIP_TLV_STRUCTURE(TagSpec) chip::TLV::TLVElementType::Structure | TagSpec
176 * @brief Specifies a Array TLV element, marking the beginning of an Array
177 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
179 #define CHIP_TLV_ARRAY(TagSpec) chip::TLV::TLVElementType::Array | TagSpec
182 * @brief Specifies a Path TLV element, marking the beginning of a Path
183 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
185 #define CHIP_TLV_PATH(TagSpec) chip::TLV::TLVElementType::Path | TagSpec
188 * @brief Specifies a Boolean TLV element, which can be either true or false
189 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
190 * @param Value Should be either true or false
192 #define CHIP_TLV_BOOL(TagSpec, Value) \
193 ((Value) ? chip::TLV::TLVElementType::BooleanTrue : chip::TLV::TLVElementType::BooleanFalse) | TagSpec
197 * Specifies a Single Precision Floating Point TLV element, marking the beginning of 32-bit data
199 * @param TagSpec Should be filled with macros beginning with CHIP_TLV_TAG_
201 * @param ... Bytes representing the floating point value to serialize
203 #define CHIP_TLV_FLOAT32(TagSpec, ...) chip::TLV::TLVElementType::FloatingPointNumber32 | TagSpec, ##__VA_ARGS__
207 * Specifies a Double Precision Floating Point TLV element, marking the beginning of 64-bit data
209 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
211 * @param ... Bytes representing the floating point value to serialize
213 #define CHIP_TLV_FLOAT64(TagSpec, ...) chip::TLV::TLVElementType::FloatingPointNumber64 | TagSpec, ##__VA_ARGS__
216 * @brief Specifies a EndOfContainer TLV element, marking the end of a Structure, Array, or Path
218 #define CHIP_TLV_END_OF_CONTAINER chip::TLV::TLVElementType::EndOfContainer | chip::TLV::TLVTagControl::Anonymous
221 * @brief Specifies a EndOfContainer TLV element, marking the end of a Structure, Array, or Path
223 #define CHIP_TLV_END_OF_STRUCTURE CHIP_TLV_END_OF_CONTAINER
226 * @brief Specifies a EndOfContainer TLV element, marking the end of a Structure, Array, or Path
228 #define CHIP_TLV_END_OF_ARRAY CHIP_TLV_END_OF_CONTAINER
231 * @brief Specifies a EndOfContainer TLV element, marking the end of a Structure, Array, or Path
233 #define CHIP_TLV_END_OF_PATH CHIP_TLV_END_OF_CONTAINER
236 * @brief Specifies an 8-bit Signed Integer TLV element
237 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
238 * @param Value Would be first converted to int8_t, and then serialized
240 #define CHIP_TLV_INT8(TagSpec, Value) chip::TLV::TLVElementType::Int8 | TagSpec, CHIP_TLV_Serialize8(int8_t(Value))
243 * @brief Specifies a 16-bit Signed Integer TLV element
244 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
245 * @param Value Would be first converted to int16_t, and then serialized
247 #define CHIP_TLV_INT16(TagSpec, Value) chip::TLV::TLVElementType::Int16 | TagSpec, CHIP_TLV_Serialize16(int16_t(Value))
250 * @brief Specifies a 32-bit Signed Integer TLV element
251 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
252 * @param Value Would be first converted to int32_t, and then serialized
254 #define CHIP_TLV_INT32(TagSpec, Value) chip::TLV::TLVElementType::Int32 | TagSpec, CHIP_TLV_Serialize32(int32_t(Value))
257 * @brief Specifies a 32-bit Signed Integer TLV element
258 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
259 * @param Value Would be first converted to int64_t, and then serialized
261 #define CHIP_TLV_INT64(TagSpec, Value) chip::TLV::TLVElementType::Int64 | TagSpec, CHIP_TLV_Serialize64(int64_t(Value))
264 * @brief Specifies an 8-bit Unsigned Integer TLV element
265 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
266 * @param Value Would be first converted to (uint8_t), and then serialized
268 #define CHIP_TLV_UINT8(TagSpec, Value) chip::TLV::TLVElementType::UInt8 | TagSpec, CHIP_TLV_Serialize8((uint8_t)(Value))
271 * @brief Specifies a 16-bit Unsigned Integer TLV element
272 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
273 * @param Value Would be first converted to (uint16_t), and then serialized
275 #define CHIP_TLV_UINT16(TagSpec, Value) chip::TLV::TLVElementType::UInt16 | TagSpec, CHIP_TLV_Serialize16((uint16_t)(Value))
278 * @brief Specifies a 32-bit Unsigned Integer TLV element
279 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
280 * @param Value Would be first converted to (uint32_t), and then serialized
282 #define CHIP_TLV_UINT32(TagSpec, Value) chip::TLV::TLVElementType::UInt32 | TagSpec, CHIP_TLV_Serialize32((uint32_t)(Value))
285 * @brief Specifies a 64-bit Unsigned Integer TLV element
286 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
287 * @param Value Would be first converted to (uint64_t), and then serialized
289 #define CHIP_TLV_UINT64(TagSpec, Value) chip::TLV::TLVElementType::UInt64 | TagSpec, CHIP_TLV_Serialize64((uint64_t)(Value))
293 * Specifies an UTF8 String TLV element, marking the beginning of String data
295 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
297 * @param StringLength Number of bytes in this string, must be less than 0x100
299 * @param ... Bytes representing the string characters to serialize
301 #define CHIP_TLV_UTF8_STRING_1ByteLength(TagSpec, StringLength, ...) \
302 chip::TLV::TLVElementType::UTF8String_1ByteLength | TagSpec, CHIP_TLV_Serialize8((uint8_t)(StringLength)), ##__VA_ARGS__
306 * Specifies an UTF8 String TLV element, marking the beginning of String data
308 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
310 * @param StringLength Number of bytes in this string, must be less than 0x10000
312 * @param ... Bytes representing the string characters to serialize
314 #define CHIP_TLV_UTF8_STRING_2ByteLength(TagSpec, StringLength, ...) \
315 chip::TLV::TLVElementType::UTF8String_2ByteLength | TagSpec, CHIP_TLV_Serialize16((uint16_t)(StringLength)), ##__VA_ARGS__
319 * Specifies an UTF8 String TLV element, marking the beginning of String data
321 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
323 * @param StringLength Number of bytes in this string, must be less than 0x100000000
325 * @param ... Bytes representing the string characters to serialize
327 #define CHIP_TLV_UTF8_STRING_4ByteLength(TagSpec, StringLength, ...) \
328 chip::TLV::TLVElementType::UTF8String_4ByteLength | TagSpec, CHIP_TLV_Serialize32((uint32_t)(StringLength)), ##__VA_ARGS__
332 * Specifies an UTF8 String TLV element, marking the beginning of String data
334 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
336 * @param StringLength Number of bytes in this string, must be less than 0x10000000000000000
338 * @param ... Bytes representing the string characters to serialize
340 #define CHIP_TLV_UTF8_STRING_8ByteLength(TagSpec, StringLength, ...) \
341 chip::TLV::TLVElementType::UTF8String_8ByteLength | TagSpec, CHIP_TLV_Serialize64((uint64_t)(StringLength)), ##__VA_ARGS__
345 * Specifies a BYTE String TLV element, marking the beginning of String data
347 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
349 * @param StringLength Number of bytes in this string, must be less than 0x100
351 * @param ... Bytes to serialize
353 #define CHIP_TLV_BYTE_STRING_1ByteLength(TagSpec, StringLength, ...) \
354 chip::TLV::TLVElementType::ByteString_1ByteLength | TagSpec, CHIP_TLV_Serialize8((uint8_t)(StringLength)), ##__VA_ARGS__
358 * Specifies a BYTE String TLV element, marking the beginning of String data
360 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
362 * @param StringLength Number of bytes in this string, must be less than 0x10000
364 * @param ... Bytes to serialize
366 #define CHIP_TLV_BYTE_STRING_2ByteLength(TagSpec, StringLength, ...) \
367 chip::TLV::TLVElementType::ByteString_2ByteLength | TagSpec, CHIP_TLV_Serialize16((uint16_t)(StringLength)), ##__VA_ARGS__
371 * Specifies a BYTE String TLV element, marking the beginning of String data
373 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
375 * @param StringLength Number of bytes in this string, must be less than 0x100000000
377 * @param ... Bytes to serialize
379 #define CHIP_TLV_BYTE_STRING_4ByteLength(TagSpec, StringLength, ...) \
380 chip::TLV::TLVElementType::ByteString_4ByteLength | TagSpec, CHIP_TLV_Serialize32((uint32_t)(StringLength)), ##__VA_ARGS__
384 * Specifies a BYTE String TLV element, marking the beginning of String data
386 * @param TagSpec Should be filled with macros begin with CHIP_TLV_TAG_
388 * @param StringLength Number of bytes in this string, must be less than 0x10000000000000000
390 * @param ... Bytes to serialize
392 #define CHIP_TLV_BYTE_STRING_8ByteLength(TagSpec, StringLength, ...) \
393 chip::TLV::TLVElementType::ByteString_8ByteLength | TagSpec, CHIP_TLV_Serialize64((uint64_t)(StringLength)), ##__VA_ARGS__