-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathTlvJson.cpp
249 lines (211 loc) · 7.32 KB
/
TlvJson.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2013-2017 Nest Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "lib/support/CHIPMemString.h"
#include "lib/support/ScopedBuffer.h"
#include <lib/core/DataModelTypes.h>
#include <lib/support/Base64.h>
#include <lib/support/jsontlv/TlvJson.h>
namespace {
/*
* Encapsulates the different types of keys permissible.
*
* Root Key = Key with a name of 'value'. This is the top-most key in a given JSON object generated from TLV.
* Struct Field = Key containing the 32-bit field ID of an item in a struct.
* Array Item = Key containing the 16-bit list index of an item in a list.
*
* In the latter two modes, the actual field ID/list index is encapsulated within the 'key' member.
*
*/
struct KeyContext
{
enum KeyType
{
kRoot,
kStructField,
kArrayItem
};
KeyContext() = default;
KeyContext(chip::FieldId fieldId)
{
keyType = kStructField;
key = fieldId;
}
KeyContext(chip::ListIndex listIndex)
{
keyType = kArrayItem;
key = listIndex;
}
KeyType keyType = kRoot;
unsigned int key = 0;
};
} // namespace
//
// For now, let's put a bound of the maximum length of a byte/char string to be the size of an IPv6
// MTU. While this is smaller than that of the limit defined in the data model specification,
// strings by virtue of not being chunked are intrinsically limited in size to the size of the encompassing packet.
//
static constexpr uint16_t kMaxStringLen = 1280;
constexpr char kBase64Header[] = "base64:";
constexpr size_t kBase64HeaderLen = ArraySize(kBase64Header) - 1;
namespace chip {
/*
* This templated function inserts a key/value pair into the Json value object.
* The value is templated to be of type T and accepts any of the following primitive
* types:
* bool, uint*_t, int*_t, char *, float, double.
*
* This method uses the provided key context to deduce the type of element being added.
*
*/
template <typename T>
void InsertKeyValue(Json::Value & json, const KeyContext & keyContext, T val)
{
//
// This needs to accomodate either the string 'value', or a 32-bit integer.
// The size of the largest 32-bit integer key represented as a string is 11 characters long.
// Tack on 1 byte for the null character.
//
char keyBuf[12];
if (keyContext.keyType == KeyContext::kRoot)
{
Platform::CopyString(keyBuf, sizeof(keyBuf), "value");
json[keyBuf] = val;
}
else if (keyContext.keyType == KeyContext::kStructField)
{
snprintf(keyBuf, sizeof(keyBuf), "%u", keyContext.key);
json[keyBuf] = val;
}
else
{
json[keyContext.key] = val;
}
}
std::string JsonToString(Json::Value & json)
{
Json::FastWriter writer;
writer.omitEndingLineFeed();
return writer.write(json);
}
CHIP_ERROR TlvToJson(TLV::TLVReader & reader, KeyContext context, Json::Value & parent)
{
switch (reader.GetType())
{
case TLV::kTLVType_UnsignedInteger: {
uint64_t v;
ReturnErrorOnFailure(reader.Get(v));
InsertKeyValue(parent, context, v);
break;
}
case TLV::kTLVType_SignedInteger: {
int64_t v;
ReturnErrorOnFailure(reader.Get(v));
InsertKeyValue(parent, context, v);
break;
}
case TLV::kTLVType_Boolean: {
bool v;
ReturnErrorOnFailure(reader.Get(v));
InsertKeyValue(parent, context, v);
break;
}
case TLV::kTLVType_FloatingPointNumber: {
double v;
ReturnErrorOnFailure(reader.Get(v));
InsertKeyValue(parent, context, v);
break;
}
case TLV::kTLVType_ByteString: {
ByteSpan span;
ReturnErrorOnFailure(reader.Get(span));
VerifyOrReturnError(span.size() < kMaxStringLen, CHIP_ERROR_INVALID_TLV_ELEMENT);
Platform::ScopedMemoryBuffer<char> byteString;
byteString.Alloc(kBase64HeaderLen + BASE64_ENCODED_LEN(span.size()) + 1);
VerifyOrReturnError(byteString.Get() != nullptr, CHIP_ERROR_NO_MEMORY);
auto encodedLen = Base64Encode(span.data(), static_cast<uint16_t>(span.size()), byteString.Get() + kBase64HeaderLen);
if (encodedLen)
{
memcpy(byteString.Get(), kBase64Header, kBase64HeaderLen);
encodedLen = static_cast<uint16_t>(encodedLen + kBase64HeaderLen);
}
byteString.Get()[encodedLen] = '\0';
InsertKeyValue(parent, context, byteString.Get());
break;
}
case TLV::kTLVType_UTF8String: {
CharSpan span;
ReturnErrorOnFailure(reader.Get(span));
VerifyOrReturnError(span.size() < kMaxStringLen, CHIP_ERROR_INVALID_TLV_ELEMENT);
Platform::ScopedMemoryString charString(span.data(), span.size());
InsertKeyValue(parent, context, charString.Get());
break;
}
case TLV::kTLVType_Null: {
InsertKeyValue(parent, context, Json::Value());
break;
}
case TLV::kTLVType_Structure: {
TLV::TLVType containerType;
ReturnErrorOnFailure(reader.EnterContainer(containerType));
CHIP_ERROR err;
Json::Value value;
while ((err = reader.Next()) == CHIP_NO_ERROR)
{
VerifyOrReturnError(TLV::IsContextTag(reader.GetTag()), CHIP_ERROR_INVALID_TLV_TAG);
KeyContext context2(static_cast<chip::FieldId>(TLV::TagNumFromTag(reader.GetTag())));
//
// Recursively convert to JSON the encompassing item within the struct.
//
ReturnErrorOnFailure(TlvToJson(reader, context2, value));
}
VerifyOrReturnError(err == CHIP_END_OF_TLV, err);
ReturnErrorOnFailure(reader.ExitContainer(containerType));
InsertKeyValue(parent, context, value);
break;
}
case TLV::kTLVType_Array: {
TLV::TLVType containerType;
ReturnErrorOnFailure(reader.EnterContainer(containerType));
CHIP_ERROR err;
Json::Value value = Json::Value(Json::arrayValue);
size_t listIndex = 0;
while ((err = reader.Next()) == CHIP_NO_ERROR)
{
KeyContext context2(static_cast<chip::ListIndex>(listIndex++));
//
// Recursively convert to JSON the encompassing item within the array.
//
ReturnErrorOnFailure(TlvToJson(reader, context2, value));
}
VerifyOrReturnError(err == CHIP_END_OF_TLV, err);
ReturnErrorOnFailure(reader.ExitContainer(containerType));
InsertKeyValue(parent, context, value);
break;
}
default:
return CHIP_ERROR_INVALID_TLV_ELEMENT;
break;
}
return CHIP_NO_ERROR;
}
CHIP_ERROR TlvToJson(TLV::TLVReader & reader, Json::Value & root)
{
KeyContext context;
return TlvToJson(reader, context, root);
}
} // namespace chip