1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "dh_transport_obj.h"
17
18 #include "dh_utils_tool.h"
19 #include "distributed_hardware_log.h"
20
21 namespace OHOS {
22 namespace DistributedHardware {
23
ToJson(cJSON * jsonObject,const FullCapsRsp & capsRsp)24 void ToJson(cJSON *jsonObject, const FullCapsRsp &capsRsp)
25 {
26 if (jsonObject == nullptr) {
27 DHLOGE("Json pointer is nullptr!");
28 return;
29 }
30 const char *networkId = capsRsp.networkId.c_str();
31 cJSON_AddStringToObject(jsonObject, CAPS_RSP_NETWORKID_KEY, networkId);
32 cJSON *capArr = cJSON_CreateArray();
33 if (capArr == nullptr) {
34 return;
35 }
36 for (auto const &cap : capsRsp.caps) {
37 cJSON *capValJson = cJSON_CreateObject();
38 if (capValJson == nullptr) {
39 cJSON_Delete(capArr);
40 return;
41 }
42 ToJson(capValJson, *cap);
43 cJSON_AddItemToArray(capArr, capValJson);
44 }
45 cJSON_AddItemToObject(jsonObject, CAPS_RSP_CAPS_KEY, capArr);
46 }
47
FromJson(const cJSON * jsonObject,FullCapsRsp & capsRsp)48 void FromJson(const cJSON *jsonObject, FullCapsRsp &capsRsp)
49 {
50 if (jsonObject == nullptr) {
51 DHLOGE("Json pointer is nullptr!");
52 return;
53 }
54 cJSON *capsRspJson = cJSON_GetObjectItem(jsonObject, CAPS_RSP_NETWORKID_KEY);
55 if (IsString(capsRspJson)) {
56 capsRsp.networkId = capsRspJson->valuestring;
57 }
58 cJSON *capsRspKeyJson = cJSON_GetObjectItem(jsonObject, CAPS_RSP_CAPS_KEY);
59 if (IsArray(capsRspKeyJson)) {
60 int32_t arrSize = cJSON_GetArraySize(capsRspKeyJson);
61 for (int32_t i = 0; i < arrSize; i++) {
62 cJSON *cap = cJSON_GetArrayItem(capsRspKeyJson, i);
63 std::shared_ptr<CapabilityInfo> capPtr = std::make_shared<CapabilityInfo>();
64 FromJson(cap, *capPtr);
65 capsRsp.caps.push_back(capPtr);
66 }
67 }
68 }
69
ToJson(cJSON * jsonObject,const CommMsg & commMsg)70 void ToJson(cJSON *jsonObject, const CommMsg &commMsg)
71 {
72 if (jsonObject == nullptr) {
73 DHLOGE("Json pointer is nullptr!");
74 return;
75 }
76 cJSON_AddNumberToObject(jsonObject, COMM_MSG_CODE_KEY, commMsg.code);
77 const char *msg = commMsg.msg.c_str();
78 cJSON_AddStringToObject(jsonObject, COMM_MSG_MSG_KEY, msg);
79 }
80
FromJson(const cJSON * jsonObject,CommMsg & commMsg)81 void FromJson(const cJSON *jsonObject, CommMsg &commMsg)
82 {
83 if (jsonObject == nullptr) {
84 DHLOGE("Json pointer is nullptr!");
85 return;
86 }
87 cJSON *commMsgCodeJson = cJSON_GetObjectItem(jsonObject, COMM_MSG_CODE_KEY);
88 if (IsInt32(commMsgCodeJson)) {
89 commMsg.code = commMsgCodeJson->valueint;
90 }
91 cJSON *commMsgeJson = cJSON_GetObjectItem(jsonObject, COMM_MSG_MSG_KEY);
92 if (IsString(commMsgeJson)) {
93 commMsg.msg = commMsgeJson->valuestring;
94 }
95 }
96
GetCommMsgString(const CommMsg & commMsg)97 std::string GetCommMsgString(const CommMsg &commMsg)
98 {
99 cJSON *rootMsg = cJSON_CreateObject();
100 if (rootMsg == nullptr) {
101 DHLOGE("Create cJSON object failed.");
102 return "";
103 }
104 ToJson(rootMsg, commMsg);
105 char *msg = cJSON_PrintUnformatted(rootMsg);
106 if (msg == nullptr) {
107 cJSON_Delete(rootMsg);
108 return "";
109 }
110 std::string msgStr = std::string(msg);
111 cJSON_free(msg);
112 cJSON_Delete(rootMsg);
113
114 return msgStr;
115 }
116 } // DistributedHardware
117 } // OHOS