Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020-2023 Project CHIP Authors
4 : * Copyright (c) 2013-2017 Nest Labs, Inc.
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 : /**
20 : * @file
21 : * This file contains common headers and definitions for working with data encoded in CHIP TLV format.
22 : *
23 : * CHIP TLV (Tag-Length-Value) is a generalized encoding method for simple structured data. It
24 : * shares many properties with the commonly used JSON serialization format while being considerably
25 : * more compact over the wire.
26 : */
27 :
28 : #pragma once
29 :
30 : #include <lib/core/CHIPError.h>
31 : #include <lib/core/TLVTags.h>
32 : #include <lib/core/TLVTypes.h>
33 :
34 : #include <lib/support/BitFlags.h>
35 : #include <lib/support/BitMask.h>
36 : #include <lib/support/DLLUtil.h>
37 : #include <lib/support/EnforceFormat.h>
38 : #include <lib/support/ScopedBuffer.h>
39 : #include <lib/support/Span.h>
40 : #include <lib/support/TypeTraits.h>
41 : #include <lib/support/logging/Constants.h>
42 :
43 : #include <stdarg.h>
44 : #include <stdlib.h>
45 : #include <type_traits>
46 :
47 : /**
48 : * @namespace chip::TLV
49 : *
50 : * Definitions for working with data encoded in CHIP TLV format.
51 : *
52 : * CHIP TLV is a generalized encoding method for simple structured data. It shares many properties
53 : * with the commonly used JSON serialization format while being considerably more compact over the wire.
54 : */
55 :
56 : namespace chip {
57 : namespace TLV {
58 :
59 : /* Forward declarations */
60 : class TLVReader;
61 : class TLVWriter;
62 : class TLVUpdater;
63 : class TLVBackingStore;
64 :
65 : constexpr inline uint8_t operator|(TLVElementType lhs, TLVTagControl rhs)
66 : {
67 : return static_cast<uint8_t>(lhs) | static_cast<uint8_t>(rhs);
68 : }
69 :
70 367365 : constexpr inline uint8_t operator|(TLVTagControl lhs, TLVElementType rhs)
71 : {
72 367365 : return static_cast<uint8_t>(lhs) | static_cast<uint8_t>(rhs);
73 : }
74 :
75 : enum
76 : {
77 : kTLVControlByte_NotSpecified = 0xFFFF
78 : };
79 :
80 1851 : constexpr size_t EstimateStructOverhead()
81 : {
82 : // The struct itself has a control byte and an end-of-struct marker.
83 1851 : return 2;
84 : }
85 :
86 : template <typename... FieldSizes>
87 6058 : constexpr size_t EstimateStructOverhead(size_t firstFieldSize, FieldSizes... otherFields)
88 : {
89 : // Estimate 4 bytes of overhead per field. This can happen for a large
90 : // octet string field: 1 byte control, 1 byte context tag, 2 bytes
91 : // length.
92 6058 : return firstFieldSize + 4u + EstimateStructOverhead(otherFields...);
93 : }
94 :
95 : } // namespace TLV
96 : } // namespace chip
|