• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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     cJSON_AddNumberToObject(jsonObject, COMM_MSG_USERID_KEY, commMsg.userId);
78     cJSON_AddNumberToObject(jsonObject, COMM_MSG_TOKENID_KEY, commMsg.tokenId);
79     const char *msg = commMsg.msg.c_str();
80     cJSON_AddStringToObject(jsonObject, COMM_MSG_MSG_KEY, msg);
81     cJSON_AddStringToObject(jsonObject, COMM_MSG_ACCOUNTID_KEY, commMsg.accountId.c_str());
82 }
83 
FromJson(const cJSON * jsonObject,CommMsg & commMsg)84 void FromJson(const cJSON *jsonObject, CommMsg &commMsg)
85 {
86     if (jsonObject == nullptr) {
87         DHLOGE("Json pointer is nullptr!");
88         return;
89     }
90     cJSON *commMsgCodeJson = cJSON_GetObjectItem(jsonObject, COMM_MSG_CODE_KEY);
91     if (IsInt32(commMsgCodeJson)) {
92         commMsg.code = commMsgCodeJson->valueint;
93     }
94     cJSON *commMsgUserIdJson = cJSON_GetObjectItem(jsonObject, COMM_MSG_USERID_KEY);
95     if (commMsgUserIdJson != NULL && cJSON_IsNumber(commMsgUserIdJson)) {
96         commMsg.userId = commMsgUserIdJson->valueint;
97     }
98     cJSON *commMsgTokenIdJson = cJSON_GetObjectItem(jsonObject, COMM_MSG_TOKENID_KEY);
99     if (commMsgTokenIdJson != NULL && cJSON_IsNumber(commMsgTokenIdJson)) {
100         commMsg.tokenId = static_cast<uint64_t>(commMsgTokenIdJson->valueint);
101     }
102     cJSON *commMsgeJson = cJSON_GetObjectItem(jsonObject, COMM_MSG_MSG_KEY);
103     if (commMsgeJson != NULL && cJSON_IsString(commMsgeJson)) {
104         commMsg.msg = commMsgeJson->valuestring;
105     }
106     cJSON *commMsgeAccountIdJson = cJSON_GetObjectItem(jsonObject, COMM_MSG_ACCOUNTID_KEY);
107     if (commMsgeAccountIdJson != NULL && cJSON_IsString(commMsgeAccountIdJson)) {
108         commMsg.accountId = commMsgeAccountIdJson->valuestring;
109     }
110 }
111 
GetCommMsgString(const CommMsg & commMsg)112 std::string GetCommMsgString(const CommMsg &commMsg)
113 {
114     cJSON *rootMsg = cJSON_CreateObject();
115     if (rootMsg == nullptr) {
116         DHLOGE("Create cJSON object failed.");
117         return "";
118     }
119     ToJson(rootMsg, commMsg);
120     char *msg = cJSON_PrintUnformatted(rootMsg);
121     if (msg == nullptr) {
122         cJSON_Delete(rootMsg);
123         return "";
124     }
125     std::string msgStr = std::string(msg);
126     cJSON_free(msg);
127     cJSON_Delete(rootMsg);
128 
129     return msgStr;
130 }
131 } // DistributedHardware
132 } // OHOS