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 std::string keyNetworkId(CAPS_RSP_NETWORKID_KEY);
55 if (IsString(jsonObject, keyNetworkId)) {
56 capsRsp.networkId = cJSON_GetObjectItem(jsonObject, CAPS_RSP_NETWORKID_KEY)->valuestring;
57 }
58 std::string keyCaps(CAPS_RSP_CAPS_KEY);
59 if (IsArray(jsonObject, keyCaps)) {
60 cJSON *capsArr = cJSON_GetObjectItem(jsonObject, CAPS_RSP_CAPS_KEY);
61 int32_t arrSize = cJSON_GetArraySize(capsArr);
62 for (int32_t i = 0; i < arrSize; i++) {
63 cJSON *cap = cJSON_GetArrayItem(capsArr, i);
64 std::shared_ptr<CapabilityInfo> capPtr = std::make_shared<CapabilityInfo>();
65 FromJson(cap, *capPtr);
66 capsRsp.caps.push_back(capPtr);
67 }
68 }
69 }
70
ToJson(cJSON * jsonObject,const CommMsg & commMsg)71 void ToJson(cJSON *jsonObject, const CommMsg &commMsg)
72 {
73 if (jsonObject == nullptr) {
74 DHLOGE("Json pointer is nullptr!");
75 return;
76 }
77 cJSON_AddNumberToObject(jsonObject, COMM_MSG_CODE_KEY, commMsg.code);
78 const char *msg = commMsg.msg.c_str();
79 cJSON_AddStringToObject(jsonObject, COMM_MSG_MSG_KEY, msg);
80 }
81
FromJson(const cJSON * jsonObject,CommMsg & commMsg)82 void FromJson(const cJSON *jsonObject, CommMsg &commMsg)
83 {
84 if (jsonObject == nullptr) {
85 DHLOGE("Json pointer is nullptr!");
86 return;
87 }
88 std::string keyCode(COMM_MSG_CODE_KEY);
89 if (IsInt32(jsonObject, keyCode)) {
90 commMsg.code = cJSON_GetObjectItem(jsonObject, COMM_MSG_CODE_KEY)->valueint;
91 }
92 std::string keyMsg(COMM_MSG_MSG_KEY);
93 if (IsString(jsonObject, keyMsg)) {
94 commMsg.msg = cJSON_GetObjectItem(jsonObject, COMM_MSG_MSG_KEY)->valuestring;
95 }
96 }
97
GetCommMsgString(const CommMsg & commMsg)98 std::string GetCommMsgString(const CommMsg &commMsg)
99 {
100 cJSON *rootMsg = cJSON_CreateObject();
101 if (rootMsg == nullptr) {
102 DHLOGE("Create cJSON object failed.");
103 return "";
104 }
105 ToJson(rootMsg, commMsg);
106 char *msg = cJSON_PrintUnformatted(rootMsg);
107 if (msg == nullptr) {
108 cJSON_Delete(rootMsg);
109 return "";
110 }
111 std::string msgStr = std::string(msg);
112 cJSON_free(msg);
113 cJSON_Delete(rootMsg);
114
115 return msgStr;
116 }
117 } // DistributedHardware
118 } // OHOS