forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoteDataModelLogger.cpp
279 lines (226 loc) · 9.4 KB
/
RemoteDataModelLogger.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
/*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* 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 "RemoteDataModelLogger.h"
#include <lib/support/SafeInt.h>
#include <lib/support/jsontlv/TlvJson.h>
constexpr char kEventNumberKey[] = "eventNumber";
constexpr char kDataVersionKey[] = "dataVersion";
constexpr char kClusterIdKey[] = "clusterId";
constexpr char kEndpointIdKey[] = "endpointId";
constexpr char kAttributeIdKey[] = "attributeId";
constexpr char kEventIdKey[] = "eventId";
constexpr char kCommandIdKey[] = "commandId";
constexpr char kErrorIdKey[] = "error";
constexpr char kClusterErrorIdKey[] = "clusterError";
constexpr char kValueKey[] = "value";
constexpr char kNodeIdKey[] = "nodeId";
constexpr char kNOCKey[] = "NOC";
constexpr char kICACKey[] = "ICAC";
constexpr char kRCACKey[] = "RCAC";
constexpr char kIPKKey[] = "IPK";
namespace {
RemoteDataModelLoggerDelegate * gDelegate;
CHIP_ERROR LogError(Json::Value & value, const chip::app::StatusIB & status)
{
if (status.mClusterStatus.HasValue())
{
auto statusValue = status.mClusterStatus.Value();
value[kClusterErrorIdKey] = statusValue;
}
#if CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT
auto statusName = chip::Protocols::InteractionModel::StatusName(status.mStatus);
value[kErrorIdKey] = statusName;
#else
auto statusName = status.mStatus;
value[kErrorIdKey] = chip::to_underlying(statusName);
#endif // CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT
auto valueStr = chip::JsonToString(value);
return gDelegate->LogJSON(valueStr.c_str());
}
} // namespace
namespace RemoteDataModelLogger {
CHIP_ERROR LogAttributeAsJSON(const chip::app::ConcreteDataAttributePath & path, chip::TLV::TLVReader * data)
{
VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
Json::Value value;
value[kClusterIdKey] = path.mClusterId;
value[kEndpointIdKey] = path.mEndpointId;
value[kAttributeIdKey] = path.mAttributeId;
if (path.mDataVersion.HasValue())
{
value[kDataVersionKey] = path.mDataVersion.Value();
}
chip::TLV::TLVReader reader;
reader.Init(*data);
ReturnErrorOnFailure(chip::TlvToJson(reader, value));
auto valueStr = chip::JsonToString(value);
return gDelegate->LogJSON(valueStr.c_str());
}
CHIP_ERROR LogErrorAsJSON(const chip::app::ConcreteDataAttributePath & path, const chip::app::StatusIB & status)
{
VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
Json::Value value;
value[kClusterIdKey] = path.mClusterId;
value[kEndpointIdKey] = path.mEndpointId;
value[kAttributeIdKey] = path.mAttributeId;
return LogError(value, status);
}
CHIP_ERROR LogCommandAsJSON(const chip::app::ConcreteCommandPath & path, chip::TLV::TLVReader * data)
{
VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
Json::Value value;
value[kClusterIdKey] = path.mClusterId;
value[kEndpointIdKey] = path.mEndpointId;
value[kCommandIdKey] = path.mCommandId;
chip::TLV::TLVReader reader;
reader.Init(*data);
ReturnErrorOnFailure(chip::TlvToJson(reader, value));
auto valueStr = chip::JsonToString(value);
return gDelegate->LogJSON(valueStr.c_str());
}
CHIP_ERROR LogErrorAsJSON(const chip::app::ConcreteCommandPath & path, const chip::app::StatusIB & status)
{
VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
Json::Value value;
value[kClusterIdKey] = path.mClusterId;
value[kEndpointIdKey] = path.mEndpointId;
value[kCommandIdKey] = path.mCommandId;
return LogError(value, status);
}
CHIP_ERROR LogEventAsJSON(const chip::app::EventHeader & header, chip::TLV::TLVReader * data)
{
VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
Json::Value value;
value[kClusterIdKey] = header.mPath.mClusterId;
value[kEndpointIdKey] = header.mPath.mEndpointId;
value[kEventIdKey] = header.mPath.mEventId;
value[kEventNumberKey] = header.mEventNumber;
chip::TLV::TLVReader reader;
reader.Init(*data);
ReturnErrorOnFailure(chip::TlvToJson(reader, value));
auto valueStr = chip::JsonToString(value);
return gDelegate->LogJSON(valueStr.c_str());
}
CHIP_ERROR LogErrorAsJSON(const chip::app::EventHeader & header, const chip::app::StatusIB & status)
{
VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
Json::Value value;
value[kClusterIdKey] = header.mPath.mClusterId;
value[kEndpointIdKey] = header.mPath.mEndpointId;
value[kEventIdKey] = header.mPath.mEventId;
return LogError(value, status);
}
CHIP_ERROR LogErrorAsJSON(const CHIP_ERROR & error)
{
VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
Json::Value value;
chip::app::StatusIB status;
status.InitFromChipError(error);
return LogError(value, status);
}
CHIP_ERROR LogGetCommissionerNodeId(chip::NodeId value)
{
VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
Json::Value rootValue;
rootValue[kValueKey] = Json::Value();
rootValue[kValueKey][kNodeIdKey] = value;
auto valueStr = chip::JsonToString(rootValue);
return gDelegate->LogJSON(valueStr.c_str());
}
CHIP_ERROR LogGetCommissionerRootCertificate(const char * value)
{
VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
Json::Value rootValue;
rootValue[kValueKey] = Json::Value();
rootValue[kValueKey][kRCACKey] = value;
auto valueStr = chip::JsonToString(rootValue);
return gDelegate->LogJSON(valueStr.c_str());
}
CHIP_ERROR LogIssueNOCChain(const char * noc, const char * icac, const char * rcac, const char * ipk)
{
VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
Json::Value rootValue;
rootValue[kValueKey] = Json::Value();
rootValue[kValueKey][kNOCKey] = noc;
rootValue[kValueKey][kICACKey] = icac;
rootValue[kValueKey][kRCACKey] = rcac;
rootValue[kValueKey][kIPKKey] = ipk;
auto valueStr = chip::JsonToString(rootValue);
return gDelegate->LogJSON(valueStr.c_str());
}
CHIP_ERROR LogDiscoveredNodeData(const chip::Dnssd::CommissionNodeData & nodeData)
{
VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
auto & commissionData = nodeData;
auto & resolutionData = commissionData;
if (!chip::CanCastTo<uint8_t>(resolutionData.numIPs))
{
ChipLogError(chipTool, "Too many ips.");
return CHIP_ERROR_INVALID_ARGUMENT;
}
if (!chip::CanCastTo<uint64_t>(commissionData.rotatingIdLen))
{
ChipLogError(chipTool, "Can not convert rotatingId to json format.");
return CHIP_ERROR_INVALID_ARGUMENT;
}
char rotatingId[chip::Dnssd::kMaxRotatingIdLen * 2 + 1] = "";
ReturnErrorOnFailure(chip::Encoding::BytesToUppercaseHexString(commissionData.rotatingId, commissionData.rotatingIdLen,
rotatingId, sizeof(rotatingId)));
Json::Value value;
value["hostName"] = resolutionData.hostName;
value["instanceName"] = commissionData.instanceName;
value["longDiscriminator"] = commissionData.longDiscriminator;
value["shortDiscriminator"] = ((commissionData.longDiscriminator >> 8) & 0x0F);
value["vendorId"] = commissionData.vendorId;
value["productId"] = commissionData.productId;
value["commissioningMode"] = commissionData.commissioningMode;
value["deviceType"] = commissionData.deviceType;
value["deviceName"] = commissionData.deviceName;
value["rotatingId"] = rotatingId;
value["rotatingIdLen"] = static_cast<uint64_t>(commissionData.rotatingIdLen);
value["pairingHint"] = commissionData.pairingHint;
value["pairingInstruction"] = commissionData.pairingInstruction;
value["supportsTcp"] = resolutionData.supportsTcp;
value["port"] = resolutionData.port;
value["numIPs"] = static_cast<uint8_t>(resolutionData.numIPs);
if (resolutionData.mrpRetryIntervalIdle.HasValue())
{
value["mrpRetryIntervalIdle"] = resolutionData.mrpRetryIntervalIdle.Value().count();
}
if (resolutionData.mrpRetryIntervalActive.HasValue())
{
value["mrpRetryIntervalActive"] = resolutionData.mrpRetryIntervalActive.Value().count();
}
if (resolutionData.mrpRetryActiveThreshold.HasValue())
{
value["mrpRetryActiveThreshold"] = resolutionData.mrpRetryActiveThreshold.Value().count();
}
if (resolutionData.isICDOperatingAsLIT.HasValue())
{
value["isICDOperatingAsLIT"] = resolutionData.isICDOperatingAsLIT.Value();
}
Json::Value rootValue;
rootValue[kValueKey] = value;
auto valueStr = chip::JsonToString(rootValue);
return gDelegate->LogJSON(valueStr.c_str());
}
void SetDelegate(RemoteDataModelLoggerDelegate * delegate)
{
gDelegate = delegate;
}
}; // namespace RemoteDataModelLogger