-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathTlvToJson.cpp
360 lines (312 loc) · 10.9 KB
/
TlvToJson.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
*
* Copyright (c) 2023 Project CHIP Authors
*
* 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 <json/json.h>
#include <lib/core/DataModelTypes.h>
#include <lib/support/Base64.h>
#include <lib/support/SafeInt.h>
#include <lib/support/jsontlv/ElementTypes.h>
#include <lib/support/jsontlv/TlvToJson.h>
namespace chip {
namespace {
// actual value of this does not actually matter, however we need
// a value to be able to read 32-bit implicit profile tags
//
// JSON format never has this and TLV payload contains "implicit profile"
// and this value is never stored.
constexpr uint32_t kTemporaryImplicitProfileId = 0xFF01;
/// RAII to switch the implicit profile id for a reader
class ImplicitProfileIdChange
{
public:
ImplicitProfileIdChange(TLV::TLVReader & reader, uint32_t id) : mReader(reader), mOldImplicitProfileId(reader.ImplicitProfileId)
{
reader.ImplicitProfileId = id;
}
~ImplicitProfileIdChange() { mReader.ImplicitProfileId = mOldImplicitProfileId; }
private:
TLV::TLVReader & mReader;
uint32_t mOldImplicitProfileId;
};
const char * GetJsonElementStrFromType(const ElementTypeContext & ctx)
{
switch (ctx.tlvType)
{
case TLV::kTLVType_UnsignedInteger:
return kElementTypeUInt;
case TLV::kTLVType_SignedInteger:
return kElementTypeInt;
case TLV::kTLVType_Boolean:
return kElementTypeBool;
case TLV::kTLVType_FloatingPointNumber:
return ctx.isDouble ? kElementTypeDouble : kElementTypeFloat;
case TLV::kTLVType_ByteString:
return kElementTypeBytes;
case TLV::kTLVType_UTF8String:
return kElementTypeString;
case TLV::kTLVType_Null:
return kElementTypeNull;
case TLV::kTLVType_Structure:
return kElementTypeStruct;
case TLV::kTLVType_Array:
return kElementTypeArray;
default:
return kElementTypeEmpty;
}
};
/*
* Encapsulates the element information required to construct a JSON element name string in a JSON object.
*
* The generated JSON element name string is constructed as:
* 'TagNumber:ElementType-SubElementType'.
*/
struct JsonObjectElementContext
{
JsonObjectElementContext(TLV::TLVReader & reader)
{
tag = reader.GetTag();
implicitProfileId = reader.ImplicitProfileId;
type.tlvType = reader.GetType();
if (type.tlvType == TLV::kTLVType_FloatingPointNumber)
{
type.isDouble = reader.IsElementDouble();
}
}
std::string GenerateJsonElementName() const
{
std::string str = "???";
if (TLV::IsContextTag(tag))
{
// common case for context tags: raw value
str = std::to_string(TLV::TagNumFromTag(tag));
}
else if (TLV::IsProfileTag(tag))
{
if (TLV::ProfileIdFromTag(tag) == implicitProfileId)
{
str = std::to_string(TLV::TagNumFromTag(tag));
}
else
{
uint32_t tagNumber = (static_cast<uint32_t>(TLV::VendorIdFromTag(tag)) << 16) | TLV::TagNumFromTag(tag);
str = std::to_string(tagNumber);
}
}
str = str + ":" + GetJsonElementStrFromType(type);
if (type.tlvType == TLV::kTLVType_Array)
{
str = str + "-" + GetJsonElementStrFromType(subType);
}
return str;
}
TLV::Tag tag;
uint32_t implicitProfileId;
ElementTypeContext type;
ElementTypeContext subType;
};
/*
* This templated function inserts a name/value pair into the Json object.
* The value is templated to be of type T and accepts any of the following types:
*
* bool, uint*_t, int*_t, char *, float, double, std::string, Json::Value
*
* This method uses the provided element context to generate Json name string.
*/
template <typename T>
void InsertJsonElement(Json::Value & json, const JsonObjectElementContext & ctx, T val)
{
if (json.isArray())
{
json.append(val);
}
else
{
json[ctx.GenerateJsonElementName()] = val;
}
}
static CHIP_ERROR TlvToJson(TLV::TLVReader & reader, Json::Value & jsonObj);
/*
* Given a TLVReader positioned at TLV structure this function:
* - enters structure
* - converts all elements of a structure into JSON object representation
* - exits structure
*/
CHIP_ERROR TlvStructToJson(TLV::TLVReader & reader, Json::Value & jsonObj)
{
CHIP_ERROR err;
TLV::TLVType containerType;
ReturnErrorOnFailure(reader.EnterContainer(containerType));
while ((err = reader.Next()) == CHIP_NO_ERROR)
{
TLV::Tag tag = reader.GetTag();
VerifyOrReturnError(TLV::IsContextTag(tag) || TLV::IsProfileTag(tag), CHIP_ERROR_INVALID_TLV_TAG);
if (TLV::IsProfileTag(tag) && TLV::VendorIdFromTag(tag) == 0)
{
VerifyOrReturnError(TLV::TagNumFromTag(tag) > UINT8_MAX, CHIP_ERROR_INVALID_TLV_TAG);
}
// Recursively convert to JSON the item within the struct.
ReturnErrorOnFailure(TlvToJson(reader, jsonObj));
}
VerifyOrReturnError(err == CHIP_END_OF_TLV, err);
return reader.ExitContainer(containerType);
}
CHIP_ERROR TlvToJson(TLV::TLVReader & reader, Json::Value & jsonObj)
{
JsonObjectElementContext context(reader);
switch (reader.GetType())
{
case TLV::kTLVType_UnsignedInteger: {
uint64_t v;
ReturnErrorOnFailure(reader.Get(v));
if (CanCastTo<uint32_t>(v))
{
InsertJsonElement(jsonObj, context, v);
}
else
{
InsertJsonElement(jsonObj, context, std::to_string(v));
}
break;
}
case TLV::kTLVType_SignedInteger: {
int64_t v;
ReturnErrorOnFailure(reader.Get(v));
if (CanCastTo<int32_t>(v))
{
InsertJsonElement(jsonObj, context, v);
}
else
{
InsertJsonElement(jsonObj, context, std::to_string(v));
}
break;
}
case TLV::kTLVType_Boolean: {
bool v;
ReturnErrorOnFailure(reader.Get(v));
InsertJsonElement(jsonObj, context, v);
break;
}
case TLV::kTLVType_FloatingPointNumber: {
double v;
ReturnErrorOnFailure(reader.Get(v));
if (v == std::numeric_limits<double>::infinity())
{
InsertJsonElement(jsonObj, context, kFloatingPointPositiveInfinity);
}
else if (v == -std::numeric_limits<double>::infinity())
{
InsertJsonElement(jsonObj, context, kFloatingPointNegativeInfinity);
}
else
{
InsertJsonElement(jsonObj, context, v);
}
break;
}
case TLV::kTLVType_ByteString: {
ByteSpan span;
ReturnErrorOnFailure(reader.Get(span));
Platform::ScopedMemoryBuffer<char> byteString;
byteString.Alloc(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());
byteString.Get()[encodedLen] = '\0';
InsertJsonElement(jsonObj, context, byteString.Get());
break;
}
case TLV::kTLVType_UTF8String: {
CharSpan span;
ReturnErrorOnFailure(reader.Get(span));
std::string str(span.data(), span.size());
InsertJsonElement(jsonObj, context, str);
break;
}
case TLV::kTLVType_Null: {
InsertJsonElement(jsonObj, context, Json::Value());
break;
}
case TLV::kTLVType_Structure: {
Json::Value jsonStruct(Json::objectValue);
ReturnErrorOnFailure(TlvStructToJson(reader, jsonStruct));
InsertJsonElement(jsonObj, context, jsonStruct);
break;
}
case TLV::kTLVType_Array: {
CHIP_ERROR err;
Json::Value jsonArray(Json::arrayValue);
ElementTypeContext prevSubType;
ElementTypeContext nextSubType;
TLV::TLVType containerType;
ReturnErrorOnFailure(reader.EnterContainer(containerType));
while ((err = reader.Next()) == CHIP_NO_ERROR)
{
VerifyOrReturnError(reader.GetTag() == TLV::AnonymousTag(), CHIP_ERROR_INVALID_TLV_TAG);
VerifyOrReturnError(reader.GetType() != TLV::kTLVType_Array, CHIP_ERROR_INVALID_TLV_ELEMENT);
nextSubType.tlvType = reader.GetType();
if (nextSubType.tlvType == TLV::kTLVType_FloatingPointNumber)
{
nextSubType.isDouble = reader.IsElementDouble();
}
if (jsonArray.empty())
{
prevSubType = nextSubType;
}
else
{
VerifyOrReturnError(prevSubType.tlvType == nextSubType.tlvType && prevSubType.isDouble == nextSubType.isDouble,
CHIP_ERROR_INVALID_TLV_ELEMENT);
}
// Recursively convert to JSON the encompassing item within the array.
ReturnErrorOnFailure(TlvToJson(reader, jsonArray));
}
VerifyOrReturnError(err == CHIP_END_OF_TLV, err);
ReturnErrorOnFailure(reader.ExitContainer(containerType));
context.subType = prevSubType;
InsertJsonElement(jsonObj, context, jsonArray);
break;
}
default:
return CHIP_ERROR_INVALID_TLV_ELEMENT;
break;
}
return CHIP_NO_ERROR;
}
} // namespace
CHIP_ERROR TlvToJson(const ByteSpan & tlv, std::string & jsonString)
{
TLV::TLVReader reader;
reader.Init(tlv);
reader.ImplicitProfileId = kTemporaryImplicitProfileId;
ReturnErrorOnFailure(reader.Next());
return TlvToJson(reader, jsonString);
}
CHIP_ERROR TlvToJson(TLV::TLVReader & reader, std::string & jsonString)
{
// The top level element must be a TLV Structure of Anonymous type.
VerifyOrReturnError(reader.GetType() == TLV::kTLVType_Structure, CHIP_ERROR_WRONG_TLV_TYPE);
VerifyOrReturnError(reader.GetTag() == TLV::AnonymousTag(), CHIP_ERROR_INVALID_TLV_TAG);
// During json conversion, a implicit profile ID is required
ImplicitProfileIdChange implicitProfileIdChange(reader, kTemporaryImplicitProfileId);
Json::Value jsonObject(Json::objectValue);
ReturnErrorOnFailure(TlvStructToJson(reader, jsonObject));
Json::StyledWriter writer;
jsonString = writer.write(jsonObject);
return CHIP_NO_ERROR;
}
} // namespace chip