RemoteDataModelLogger.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (c) 2023 Project CHIP Authors
  3. * All rights reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include "RemoteDataModelLogger.h"
  19. #include <lib/support/SafeInt.h>
  20. #include <lib/support/jsontlv/TlvJson.h>
  21. constexpr const char * kEventNumberKey = "eventNumber";
  22. constexpr const char * kDataVersionKey = "dataVersion";
  23. constexpr const char * kClusterIdKey = "clusterId";
  24. constexpr const char * kEndpointIdKey = "endpointId";
  25. constexpr const char * kAttributeIdKey = "attributeId";
  26. constexpr const char * kEventIdKey = "eventId";
  27. constexpr const char * kCommandIdKey = "commandId";
  28. constexpr const char * kErrorIdKey = "error";
  29. constexpr const char * kClusterErrorIdKey = "clusterError";
  30. constexpr const char * kValueKey = "value";
  31. constexpr const char * kNodeIdKey = "nodeId";
  32. constexpr const char * kNOCKey = "NOC";
  33. constexpr const char * kICACKey = "ICAC";
  34. constexpr const char * kRCACKey = "RCAC";
  35. constexpr const char * kIPKKey = "IPK";
  36. namespace {
  37. RemoteDataModelLoggerDelegate * gDelegate;
  38. CHIP_ERROR LogError(Json::Value & value, const chip::app::StatusIB & status)
  39. {
  40. if (status.mClusterStatus.HasValue())
  41. {
  42. auto statusValue = status.mClusterStatus.Value();
  43. value[kClusterErrorIdKey] = statusValue;
  44. }
  45. #if CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT
  46. auto statusName = chip::Protocols::InteractionModel::StatusName(status.mStatus);
  47. value[kErrorIdKey] = statusName;
  48. #else
  49. auto statusName = status.mStatus;
  50. value[kErrorIdKey] = chip::to_underlying(statusName);
  51. #endif // CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT
  52. auto valueStr = chip::JsonToString(value);
  53. return gDelegate->LogJSON(valueStr.c_str());
  54. }
  55. } // namespace
  56. namespace RemoteDataModelLogger {
  57. CHIP_ERROR LogAttributeAsJSON(const chip::app::ConcreteDataAttributePath & path, chip::TLV::TLVReader * data)
  58. {
  59. VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
  60. Json::Value value;
  61. value[kClusterIdKey] = path.mClusterId;
  62. value[kEndpointIdKey] = path.mEndpointId;
  63. value[kAttributeIdKey] = path.mAttributeId;
  64. if (path.mDataVersion.HasValue())
  65. {
  66. value[kDataVersionKey] = path.mDataVersion.Value();
  67. }
  68. chip::TLV::TLVReader reader;
  69. reader.Init(*data);
  70. ReturnErrorOnFailure(chip::TlvToJson(reader, value));
  71. auto valueStr = chip::JsonToString(value);
  72. return gDelegate->LogJSON(valueStr.c_str());
  73. }
  74. CHIP_ERROR LogErrorAsJSON(const chip::app::ConcreteDataAttributePath & path, const chip::app::StatusIB & status)
  75. {
  76. VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
  77. Json::Value value;
  78. value[kClusterIdKey] = path.mClusterId;
  79. value[kEndpointIdKey] = path.mEndpointId;
  80. value[kAttributeIdKey] = path.mAttributeId;
  81. return LogError(value, status);
  82. }
  83. CHIP_ERROR LogCommandAsJSON(const chip::app::ConcreteCommandPath & path, chip::TLV::TLVReader * data)
  84. {
  85. VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
  86. Json::Value value;
  87. value[kClusterIdKey] = path.mClusterId;
  88. value[kEndpointIdKey] = path.mEndpointId;
  89. value[kCommandIdKey] = path.mCommandId;
  90. chip::TLV::TLVReader reader;
  91. reader.Init(*data);
  92. ReturnErrorOnFailure(chip::TlvToJson(reader, value));
  93. auto valueStr = chip::JsonToString(value);
  94. return gDelegate->LogJSON(valueStr.c_str());
  95. }
  96. CHIP_ERROR LogErrorAsJSON(const chip::app::ConcreteCommandPath & path, const chip::app::StatusIB & status)
  97. {
  98. VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
  99. Json::Value value;
  100. value[kClusterIdKey] = path.mClusterId;
  101. value[kEndpointIdKey] = path.mEndpointId;
  102. value[kCommandIdKey] = path.mCommandId;
  103. return LogError(value, status);
  104. }
  105. CHIP_ERROR LogEventAsJSON(const chip::app::EventHeader & header, chip::TLV::TLVReader * data)
  106. {
  107. VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
  108. Json::Value value;
  109. value[kClusterIdKey] = header.mPath.mClusterId;
  110. value[kEndpointIdKey] = header.mPath.mEndpointId;
  111. value[kEventIdKey] = header.mPath.mEventId;
  112. value[kEventNumberKey] = header.mEventNumber;
  113. chip::TLV::TLVReader reader;
  114. reader.Init(*data);
  115. ReturnErrorOnFailure(chip::TlvToJson(reader, value));
  116. auto valueStr = chip::JsonToString(value);
  117. return gDelegate->LogJSON(valueStr.c_str());
  118. }
  119. CHIP_ERROR LogErrorAsJSON(const chip::app::EventHeader & header, const chip::app::StatusIB & status)
  120. {
  121. VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
  122. Json::Value value;
  123. value[kClusterIdKey] = header.mPath.mClusterId;
  124. value[kEndpointIdKey] = header.mPath.mEndpointId;
  125. value[kEventIdKey] = header.mPath.mEventId;
  126. return LogError(value, status);
  127. }
  128. CHIP_ERROR LogErrorAsJSON(const CHIP_ERROR & error)
  129. {
  130. VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
  131. Json::Value value;
  132. chip::app::StatusIB status;
  133. status.InitFromChipError(error);
  134. return LogError(value, status);
  135. }
  136. CHIP_ERROR LogGetCommissionerNodeId(chip::NodeId value)
  137. {
  138. VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
  139. Json::Value rootValue;
  140. rootValue[kValueKey] = Json::Value();
  141. rootValue[kValueKey][kNodeIdKey] = value;
  142. auto valueStr = chip::JsonToString(rootValue);
  143. return gDelegate->LogJSON(valueStr.c_str());
  144. }
  145. CHIP_ERROR LogGetCommissionerRootCertificate(const char * value)
  146. {
  147. VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
  148. Json::Value rootValue;
  149. rootValue[kValueKey] = Json::Value();
  150. rootValue[kValueKey][kRCACKey] = value;
  151. auto valueStr = chip::JsonToString(rootValue);
  152. return gDelegate->LogJSON(valueStr.c_str());
  153. }
  154. CHIP_ERROR LogIssueNOCChain(const char * noc, const char * icac, const char * rcac, const char * ipk)
  155. {
  156. VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
  157. Json::Value rootValue;
  158. rootValue[kValueKey] = Json::Value();
  159. rootValue[kValueKey][kNOCKey] = noc;
  160. rootValue[kValueKey][kICACKey] = icac;
  161. rootValue[kValueKey][kRCACKey] = rcac;
  162. rootValue[kValueKey][kIPKKey] = ipk;
  163. auto valueStr = chip::JsonToString(rootValue);
  164. return gDelegate->LogJSON(valueStr.c_str());
  165. }
  166. CHIP_ERROR LogDiscoveredNodeData(const chip::Dnssd::DiscoveredNodeData & nodeData)
  167. {
  168. VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);
  169. auto & resolutionData = nodeData.resolutionData;
  170. auto & commissionData = nodeData.commissionData;
  171. if (!chip::CanCastTo<uint8_t>(resolutionData.numIPs))
  172. {
  173. ChipLogError(chipTool, "Too many ips.");
  174. return CHIP_ERROR_INVALID_ARGUMENT;
  175. }
  176. if (!chip::CanCastTo<uint64_t>(commissionData.rotatingIdLen))
  177. {
  178. ChipLogError(chipTool, "Can not convert rotatingId to json format.");
  179. return CHIP_ERROR_INVALID_ARGUMENT;
  180. }
  181. char rotatingId[chip::Dnssd::kMaxRotatingIdLen * 2 + 1] = "";
  182. ReturnErrorOnFailure(chip::Encoding::BytesToUppercaseHexString(commissionData.rotatingId, commissionData.rotatingIdLen,
  183. rotatingId, sizeof(rotatingId)));
  184. Json::Value value;
  185. value["hostName"] = resolutionData.hostName;
  186. value["instanceName"] = commissionData.instanceName;
  187. value["longDiscriminator"] = commissionData.longDiscriminator;
  188. value["shortDiscriminator"] = ((commissionData.longDiscriminator >> 8) & 0x0F);
  189. value["vendorId"] = commissionData.vendorId;
  190. value["productId"] = commissionData.productId;
  191. value["commissioningMode"] = commissionData.commissioningMode;
  192. value["deviceType"] = commissionData.deviceType;
  193. value["deviceName"] = commissionData.deviceName;
  194. value["rotatingId"] = rotatingId;
  195. value["rotatingIdLen"] = static_cast<uint64_t>(commissionData.rotatingIdLen);
  196. value["pairingHint"] = commissionData.pairingHint;
  197. value["pairingInstruction"] = commissionData.pairingInstruction;
  198. value["supportsTcp"] = resolutionData.supportsTcp;
  199. value["port"] = resolutionData.port;
  200. value["numIPs"] = static_cast<uint8_t>(resolutionData.numIPs);
  201. if (resolutionData.mrpRetryIntervalIdle.HasValue())
  202. {
  203. value["mrpRetryIntervalIdle"] = resolutionData.mrpRetryIntervalIdle.Value().count();
  204. }
  205. if (resolutionData.mrpRetryIntervalActive.HasValue())
  206. {
  207. value["mrpRetryIntervalActive"] = resolutionData.mrpRetryIntervalActive.Value().count();
  208. }
  209. if (resolutionData.mrpRetryActiveThreshold.HasValue())
  210. {
  211. value["mrpRetryActiveThreshold"] = resolutionData.mrpRetryActiveThreshold.Value().count();
  212. }
  213. Json::Value rootValue;
  214. rootValue[kValueKey] = value;
  215. auto valueStr = chip::JsonToString(rootValue);
  216. return gDelegate->LogJSON(valueStr.c_str());
  217. }
  218. void SetDelegate(RemoteDataModelLoggerDelegate * delegate)
  219. {
  220. gDelegate = delegate;
  221. }
  222. }; // namespace RemoteDataModelLogger