1 /*
2 * Copyright (c) 2022 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 "json_utils.h"
17 #include "avsession_log.h"
18 #include "av_session.h"
19
20 namespace OHOS::AVSession {
ConvertSessionType(const std::string & typeString)21 int32_t JsonUtils::ConvertSessionType(const std::string& typeString)
22 {
23 int32_t typeNum = AVSession::SESSION_TYPE_INVALID;
24 if (typeString == "audio") {
25 typeNum = AVSession::SESSION_TYPE_AUDIO;
26 } else if (typeString == "video") {
27 typeNum = AVSession::SESSION_TYPE_VIDEO;
28 }
29 return typeNum;
30 }
31
ConvertSessionType(int32_t type)32 std::string JsonUtils::ConvertSessionType(int32_t type)
33 {
34 if (type == AVSession::SESSION_TYPE_AUDIO) {
35 return "audio";
36 } else if (type == AVSession::SESSION_TYPE_VIDEO) {
37 return "video";
38 } else {
39 return "";
40 }
41 }
42
JsonToVector(cJSON * object,std::vector<int32_t> & out)43 int32_t JsonUtils::JsonToVector(cJSON* object, std::vector<int32_t>& out)
44 {
45 CHECK_AND_RETURN_RET_LOG(object != nullptr, AVSESSION_ERROR, "json object is null");
46 CHECK_AND_RETURN_RET_LOG(!cJSON_IsInvalid(object), AVSESSION_ERROR, "json object is invalid");
47 CHECK_AND_RETURN_RET_LOG(cJSON_IsArray(object), AVSESSION_ERROR, "json object is not array");
48
49 const cJSON* numberItem = nullptr;
50 cJSON_ArrayForEach(numberItem, object) {
51 CHECK_AND_CONTINUE(numberItem != nullptr && !cJSON_IsInvalid(numberItem) && cJSON_IsNumber(numberItem));
52 out.push_back(numberItem->valueint);
53 }
54 return AVSESSION_SUCCESS;
55 }
56
SetIntVectorToCJSON(cJSON * jsonObject,std::string key,const std::vector<int32_t> & array)57 int32_t JsonUtils::SetIntVectorToCJSON(cJSON* jsonObject, std::string key, const std::vector<int32_t>& array)
58 {
59 CHECK_AND_RETURN_RET_LOG(jsonObject != nullptr, AVSESSION_ERROR, "json object is null");
60 CHECK_AND_RETURN_RET_LOG(!cJSON_IsInvalid(jsonObject), AVSESSION_ERROR, "json object is invalid");
61 cJSON* itemArray = cJSON_CreateArray();
62 CHECK_AND_RETURN_RET_LOG(itemArray != nullptr, AVSESSION_ERROR, "itemArray is null");
63 if (cJSON_IsInvalid(itemArray) || !cJSON_IsArray(itemArray)) {
64 SLOGE("itemArray is invalid");
65 cJSON_Delete(itemArray);
66 return AVSESSION_ERROR;
67 }
68 for (int32_t g_val : array) {
69 cJSON* valItem = cJSON_CreateNumber(g_val);
70 if (valItem == nullptr) {
71 SLOGE("fail create number");
72 cJSON_Delete(itemArray);
73 return AVSESSION_ERROR;
74 }
75 cJSON_AddItemToArray(itemArray, valItem);
76 }
77 cJSON_AddItemToObject(jsonObject, key.c_str(), itemArray);
78 return AVSESSION_SUCCESS;
79 }
80
GetJsonCapability(const std::vector<std::vector<int32_t>> & capability,std::string & jsonCapability)81 int32_t JsonUtils::GetJsonCapability(const std::vector<std::vector<int32_t>>& capability, std::string& jsonCapability)
82 {
83 cJSON* jsonObject = cJSON_CreateObject();
84 if (jsonObject == nullptr) {
85 SLOGE("create json object get nullptr");
86 return AVSESSION_ERROR;
87 }
88 if (cJSON_IsInvalid(jsonObject)) {
89 SLOGE("create json object get invalid");
90 cJSON_Delete(jsonObject);
91 return AVSESSION_ERROR;
92 }
93
94 // cjson jsonObject;
95 for (uint32_t g_i = 0; g_i < capability.size(); g_i++) {
96 if (g_i == SESSION_DATA_META) {
97 SetIntVectorToCJSON(jsonObject, "metaData", capability[g_i]);
98 continue;
99 }
100 if (g_i == SESSION_DATA_PLAYBACK_STATE) {
101 SetIntVectorToCJSON(jsonObject, "playbackState", capability[g_i]);
102 continue;
103 }
104 if (g_i == SESSION_DATA_CONTROL_COMMAND) {
105 SetIntVectorToCJSON(jsonObject, "controlCommand", capability[g_i]);
106 continue;
107 }
108 }
109 if (cJSON_IsInvalid(jsonObject)) {
110 SLOGE("get json object invalid aft addNumber");
111 cJSON_Delete(jsonObject);
112 return AVSESSION_ERROR;
113 }
114
115 char* g_jsonStr = cJSON_Print(jsonObject);
116 if (g_jsonStr != nullptr) {
117 jsonCapability = g_jsonStr;
118 cJSON_free(g_jsonStr);
119 } else {
120 SLOGE("get json str with nullptr");
121 }
122 cJSON_Delete(jsonObject);
123 return AVSESSION_SUCCESS;
124 }
125
IsInt32(cJSON * jsonObj,const std::string & key)126 bool JsonUtils::IsInt32(cJSON* jsonObj, const std::string& key)
127 {
128 if (!cJSON_HasObjectItem(jsonObj, key.c_str())) {
129 SLOGE("The key %{public}s of jsonObj is not found", key.c_str());
130 return false;
131 }
132 cJSON* numberItem = cJSON_GetObjectItem(jsonObj, key.c_str());
133 bool g_res = numberItem != nullptr && !cJSON_IsInvalid(numberItem) && cJSON_IsNumber(numberItem) &&
134 INT32_MIN <= numberItem->valueint && numberItem->valueint <= INT32_MAX;
135 if (!g_res) {
136 SLOGE("The key %{public}s of jsonObj is invalid", key.c_str());
137 }
138 return g_res;
139 }
140
IsString(cJSON * jsonObj,const std::string & key)141 bool JsonUtils::IsString(cJSON* jsonObj, const std::string& key)
142 {
143 if (!cJSON_HasObjectItem(jsonObj, key.c_str())) {
144 SLOGE("The key %{public}s of jsonObj is not found", key.c_str());
145 return false;
146 }
147 cJSON* stringItem = cJSON_GetObjectItem(jsonObj, key.c_str());
148 bool res = stringItem != nullptr && !cJSON_IsInvalid(stringItem) && cJSON_IsString(stringItem);
149 if (!res) {
150 SLOGE("The key %{public}s of jsonObj is invalid", key.c_str());
151 }
152 return res;
153 }
154
IsBool(cJSON * jsonObj,const std::string & key)155 bool JsonUtils::IsBool(cJSON* jsonObj, const std::string& key)
156 {
157 if (!cJSON_HasObjectItem(jsonObj, key.c_str())) {
158 SLOGE("The key %{public}s of jsonObj is not found", key.c_str());
159 return false;
160 }
161 cJSON* boolItem = cJSON_GetObjectItem(jsonObj, key.c_str());
162 bool res = boolItem != nullptr && !cJSON_IsInvalid(boolItem) && cJSON_IsBool(boolItem);
163 if (!res) {
164 SLOGE("The key %{public}s of jsonObj is invalid", key.c_str());
165 }
166 return res;
167 }
168
GetVectorCapability(const std::string & jsonCapability,std::vector<std::vector<int32_t>> & vectorCapability)169 int32_t JsonUtils::GetVectorCapability(const std::string& jsonCapability,
170 std::vector<std::vector<int32_t>>& vectorCapability)
171 {
172 CHECK_AND_RETURN_RET_LOG(!jsonCapability.empty(), AVSESSION_ERROR, "jsonCapability is empty");
173 cJSON* jsonObj = cJSON_Parse(jsonCapability.c_str());
174 CHECK_AND_RETURN_RET_LOG(jsonObj != nullptr, AVSESSION_ERROR, "parse json with nullptr");
175 if (cJSON_IsInvalid(jsonObj)) {
176 SLOGE("json obj is invalid");
177 cJSON_Delete(jsonObj);
178 return AVSESSION_ERROR;
179 }
180 cJSON* metaDataItemArray = cJSON_GetObjectItem(jsonObj, "metaData");
181 int32_t g_ret = JsonToVector(metaDataItemArray, vectorCapability[SESSION_DATA_META]);
182 CHECK_AND_CONTINUE_LOG(g_ret == AVSESSION_SUCCESS, "Get metaDataCapability error");
183
184 cJSON* playbackStateItemArray = cJSON_GetObjectItem(jsonObj, "playbackState");
185 g_ret = JsonToVector(playbackStateItemArray, vectorCapability[SESSION_DATA_PLAYBACK_STATE]);
186 CHECK_AND_CONTINUE_LOG(g_ret == AVSESSION_SUCCESS, "Get playbackStateCapability error");
187
188 cJSON* controlCommandArray = cJSON_GetObjectItem(jsonObj, "controlCommand");
189 g_ret = JsonToVector(controlCommandArray, vectorCapability[SESSION_DATA_CONTROL_COMMAND]);
190 CHECK_AND_CONTINUE_LOG(g_ret == AVSESSION_SUCCESS, "Get controlCommandCapability error");
191
192 cJSON_Delete(jsonObj);
193 return AVSESSION_SUCCESS;
194 }
195
GetAllCapability(const std::string & sessionInfo,std::string & jsonCapability)196 int32_t JsonUtils::GetAllCapability(const std::string& sessionInfo, std::string& jsonCapability)
197 {
198 CHECK_AND_RETURN_RET_LOG(!sessionInfo.empty(), AVSESSION_ERROR, "sessionInfo is empty");
199 cJSON* jsonSessionInfo = cJSON_Parse(sessionInfo.c_str());
200 CHECK_AND_RETURN_RET_LOG(jsonSessionInfo != nullptr, AVSESSION_ERROR, "json object is null");
201 if (cJSON_IsInvalid(jsonSessionInfo) || cJSON_IsNull(jsonSessionInfo)) {
202 SLOGE("jsonSessionInfo get invalid");
203 cJSON_Delete(jsonSessionInfo);
204 return AVSESSION_ERROR;
205 }
206 cJSON* compatibilityItem = cJSON_GetObjectItem(jsonSessionInfo, "compatibility");
207 if (compatibilityItem == nullptr || cJSON_IsInvalid(compatibilityItem) || cJSON_IsNull(compatibilityItem)) {
208 SLOGE("GetCompatibility error");
209 cJSON_Delete(jsonSessionInfo);
210 return AVSESSION_ERROR;
211 }
212 cJSON* capabilitySetItem = cJSON_GetObjectItem(compatibilityItem, "capabilitySet");
213 if (capabilitySetItem == nullptr || cJSON_IsInvalid(capabilitySetItem) || cJSON_IsNull(capabilitySetItem)) {
214 SLOGE("GetCapabilitySet error");
215 cJSON_Delete(jsonSessionInfo);
216 return AVSESSION_ERROR;
217 }
218
219 char* g_capabilitySetStr = cJSON_Print(capabilitySetItem);
220 if (g_capabilitySetStr != nullptr) {
221 jsonCapability = g_capabilitySetStr;
222 cJSON_free(g_capabilitySetStr);
223 } else {
224 SLOGE("get g_capabilitySetStr nullptr");
225 cJSON_Delete(jsonSessionInfo);
226 return AVSESSION_ERROR;
227 }
228 cJSON_Delete(jsonSessionInfo);
229 return AVSESSION_SUCCESS;
230 }
231
SetSessionCapabilitySet(cJSON * jsonObj,const AVSessionBasicInfo & basicInfo)232 int32_t JsonUtils::SetSessionCapabilitySet(cJSON* jsonObj, const AVSessionBasicInfo& basicInfo)
233 {
234 CHECK_AND_RETURN_RET_LOG(jsonObj != nullptr && !cJSON_IsInvalid(jsonObj), AVSESSION_ERROR, "json object is err");
235 cJSON* capabilitySetItem = cJSON_GetObjectItem(jsonObj, "capabilitySet");
236 if (capabilitySetItem == nullptr || cJSON_IsInvalid(capabilitySetItem) || cJSON_IsNull(capabilitySetItem)) {
237 capabilitySetItem = cJSON_CreateObject();
238 cJSON_AddItemToObject(jsonObj, "capabilitySet", capabilitySetItem);
239 }
240
241 cJSON* metaDataCapArray = cJSON_CreateArray();
242 for (int32_t g_val : basicInfo.metaDataCap_) {
243 cJSON* valItem = cJSON_CreateNumber(g_val);
244 if (valItem == nullptr) {
245 SLOGE("fail create number for metaData");
246 cJSON_Delete(metaDataCapArray);
247 return AVSESSION_ERROR;
248 }
249 cJSON_AddItemToArray(metaDataCapArray, valItem);
250 }
251 cJSON_AddItemToObject(capabilitySetItem, "metaData", metaDataCapArray);
252 cJSON* playBackStateCapArray = cJSON_CreateArray();
253 for (int32_t val : basicInfo.playBackStateCap_) {
254 cJSON* valItem = cJSON_CreateNumber(val);
255 if (valItem == nullptr) {
256 SLOGE("fail create number for playbackState");
257 cJSON_Delete(playBackStateCapArray);
258 return AVSESSION_ERROR;
259 }
260 cJSON_AddItemToArray(playBackStateCapArray, valItem);
261 }
262 cJSON_AddItemToObject(capabilitySetItem, "playbackState", playBackStateCapArray);
263 cJSON* controlCommandCapArray = cJSON_CreateArray();
264 for (int32_t val : basicInfo.controlCommandCap_) {
265 cJSON* valItem = cJSON_CreateNumber(val);
266 if (valItem == nullptr) {
267 SLOGE("fail create number for controlCommand");
268 cJSON_Delete(controlCommandCapArray);
269 return AVSESSION_ERROR;
270 }
271 cJSON_AddItemToArray(controlCommandCapArray, valItem);
272 }
273 cJSON_AddItemToObject(capabilitySetItem, "controlCommand", controlCommandCapArray);
274
275 return AVSESSION_SUCCESS;
276 }
277
SetSessionCompatibility(cJSON * jsonObj,const AVSessionBasicInfo & basicInfo)278 int32_t JsonUtils::SetSessionCompatibility(cJSON* jsonObj, const AVSessionBasicInfo& basicInfo)
279 {
280 CHECK_AND_RETURN_RET_LOG(jsonObj != nullptr && !cJSON_IsInvalid(jsonObj), AVSESSION_ERROR, "json object is err");
281 cJSON* compatibilityItem = cJSON_GetObjectItem(jsonObj, "compatibility");
282 if (compatibilityItem == nullptr || cJSON_IsInvalid(compatibilityItem) || cJSON_IsNull(compatibilityItem)) {
283 compatibilityItem = cJSON_CreateObject();
284 cJSON_AddItemToObject(jsonObj, "compatibility", compatibilityItem);
285 }
286 cJSON_AddStringToObject(compatibilityItem, "networkId", basicInfo.networkId_.c_str());
287 cJSON_AddStringToObject(compatibilityItem, "vendorId", basicInfo.vendorId_.c_str());
288 cJSON_AddStringToObject(compatibilityItem, "deviceType", basicInfo.deviceType_.c_str());
289 cJSON_AddStringToObject(compatibilityItem, "systemVersion", basicInfo.systemVersion_.c_str());
290 cJSON_AddNumberToObject(compatibilityItem, "avsessionVersion",
291 static_cast<long long>(basicInfo.sessionVersion_));
292 cJSON* reserveArray = cJSON_CreateArray();
293 for (int32_t val : basicInfo.reserve_) {
294 cJSON* valItem = cJSON_CreateNumber(val);
295 if (valItem == nullptr) {
296 SLOGE("fail create number for reserve");
297 cJSON_Delete(reserveArray);
298 return AVSESSION_ERROR;
299 }
300 cJSON_AddItemToArray(reserveArray, valItem);
301 }
302 cJSON_AddItemToObject(compatibilityItem, "reserve", reserveArray);
303 cJSON* featuresArray = cJSON_CreateArray();
304 for (int32_t val : basicInfo.feature_) {
305 cJSON* valItem = cJSON_CreateNumber(val);
306 if (valItem == nullptr) {
307 SLOGE("fail create number for features");
308 cJSON_Delete(featuresArray);
309 return AVSESSION_ERROR;
310 }
311 cJSON_AddItemToArray(featuresArray, valItem);
312 }
313 cJSON_AddItemToObject(compatibilityItem, "features", featuresArray);
314 SetSessionCapabilitySet(compatibilityItem, basicInfo);
315 cJSON* extendCapabilityArray = cJSON_CreateArray();
316 for (int32_t val : basicInfo.extendCapability_) {
317 cJSON* valItem = cJSON_CreateNumber(val);
318 if (valItem == nullptr) {
319 SLOGE("fail create number for extendCapability");
320 cJSON_Delete(extendCapabilityArray);
321 return AVSESSION_ERROR;
322 }
323 cJSON_AddItemToArray(extendCapabilityArray, valItem);
324 }
325 cJSON_AddItemToObject(compatibilityItem, "extendCapability", extendCapabilityArray);
326 return AVSESSION_SUCCESS;
327 }
328
SetSessionData(cJSON * jsonObj,const AVSessionBasicInfo & basicInfo)329 int32_t JsonUtils::SetSessionData(cJSON* jsonObj, const AVSessionBasicInfo& basicInfo)
330 {
331 CHECK_AND_RETURN_RET_LOG(jsonObj != nullptr && !cJSON_IsInvalid(jsonObj), AVSESSION_ERROR, "json object is err");
332 cJSON* dataItem = cJSON_GetObjectItem(jsonObj, "data");
333 if (dataItem == nullptr || cJSON_IsInvalid(dataItem) || cJSON_IsNull(dataItem)) {
334 dataItem = cJSON_CreateObject();
335 cJSON_AddItemToObject(jsonObj, "data", dataItem);
336 }
337 cJSON_AddNumberToObject(dataItem, "systemTime", static_cast<long long>(basicInfo.systemTime_));
338 cJSON* extendArray = cJSON_CreateArray();
339 for (int32_t val : basicInfo.extend_) {
340 cJSON* valItem = cJSON_CreateNumber(val);
341 if (valItem == nullptr) {
342 SLOGE("fail create number for extend");
343 cJSON_Delete(extendArray);
344 return AVSESSION_ERROR;
345 }
346 cJSON_AddItemToArray(extendArray, valItem);
347 }
348 cJSON_AddItemToObject(dataItem, "extend", extendArray);
349 return AVSESSION_SUCCESS;
350 }
351
SetSessionBasicInfo(std::string & sessionInfo,const AVSessionBasicInfo & basicInfo)352 int32_t JsonUtils::SetSessionBasicInfo(std::string& sessionInfo, const AVSessionBasicInfo& basicInfo)
353 {
354 cJSON* jsonObj = nullptr;
355 if (sessionInfo.empty()) {
356 jsonObj = cJSON_CreateObject();
357 } else {
358 jsonObj = cJSON_Parse(sessionInfo.c_str());
359 }
360 CHECK_AND_RETURN_RET_LOG(jsonObj != nullptr, AVSESSION_ERROR, "json object is null");
361 if (cJSON_IsInvalid(jsonObj)) {
362 SLOGE("parse json invalid");
363 cJSON_Delete(jsonObj);
364 return AVSESSION_ERROR;
365 }
366
367 if (SetSessionCompatibility(jsonObj, basicInfo) != AVSESSION_SUCCESS) {
368 SLOGE("SetSessionCompatibility fail");
369 cJSON_Delete(jsonObj);
370 return AVSESSION_ERROR;
371 }
372
373 if (SetSessionData(jsonObj, basicInfo) != AVSESSION_SUCCESS) {
374 SLOGE("SetSessionData fail");
375 cJSON_Delete(jsonObj);
376 return AVSESSION_ERROR;
377 }
378
379 if (cJSON_IsNull(jsonObj) || cJSON_IsInvalid(jsonObj)) {
380 SLOGE("parse json aft set with invalid");
381 cJSON_Delete(jsonObj);
382 return AVSESSION_ERROR;
383 }
384
385 char* jsonStr = cJSON_Print(jsonObj);
386 if (jsonStr != nullptr) {
387 sessionInfo = jsonStr;
388 cJSON_free(jsonStr);
389 } else {
390 SLOGE("get jsonStr nullptr");
391 cJSON_Delete(jsonObj);
392 return AVSESSION_ERROR;
393 }
394 cJSON_Delete(jsonObj);
395 return AVSESSION_SUCCESS;
396 }
397
GetSessionCapabilitySet(cJSON * jsonObj,AVSessionBasicInfo & basicInfo)398 int32_t JsonUtils::GetSessionCapabilitySet(cJSON* jsonObj, AVSessionBasicInfo& basicInfo)
399 {
400 CHECK_AND_RETURN_RET_LOG(jsonObj != nullptr && !cJSON_IsInvalid(jsonObj), AVSESSION_ERROR, "json object is err");
401
402 cJSON* metaDataItem = cJSON_GetObjectItem(jsonObj, "metaData");
403 CHECK_AND_RETURN_RET_LOG(!(metaDataItem == nullptr || cJSON_IsInvalid(metaDataItem) ||
404 !cJSON_IsArray(metaDataItem)), AVSESSION_ERROR, "get metaDataItem err");
405 int32_t ret = JsonToVector(metaDataItem, basicInfo.metaDataCap_);
406 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, AVSESSION_ERROR, "Get metaDataCap error");
407
408 cJSON* playbackStateItem = cJSON_GetObjectItem(jsonObj, "playbackState");
409 CHECK_AND_RETURN_RET_LOG(!(playbackStateItem == nullptr || cJSON_IsInvalid(playbackStateItem) ||
410 !cJSON_IsArray(playbackStateItem)), AVSESSION_ERROR, "get playbackStateItem err");
411 ret = JsonToVector(playbackStateItem, basicInfo.playBackStateCap_);
412 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, AVSESSION_ERROR, "Get playBackStateCap error");
413
414 cJSON* controlCommandItem = cJSON_GetObjectItem(jsonObj, "controlCommand");
415 CHECK_AND_RETURN_RET_LOG(!(controlCommandItem == nullptr || cJSON_IsInvalid(controlCommandItem) ||
416 !cJSON_IsArray(controlCommandItem)), AVSESSION_ERROR, "get controlCommandItem err");
417 ret = JsonToVector(controlCommandItem, basicInfo.controlCommandCap_);
418 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, AVSESSION_ERROR, "Get controlCommandCap error");
419
420 return AVSESSION_SUCCESS;
421 }
422
GetSessionCompatibility(cJSON * jsonObj,AVSessionBasicInfo & basicInfo)423 int32_t JsonUtils::GetSessionCompatibility(cJSON* jsonObj, AVSessionBasicInfo& basicInfo)
424 {
425 CHECK_AND_RETURN_RET_LOG(jsonObj != nullptr && !cJSON_IsInvalid(jsonObj), AVSESSION_ERROR, "json object is err");
426
427 cJSON* networkIdItem = cJSON_GetObjectItem(jsonObj, "networkId");
428 CHECK_AND_RETURN_RET_LOG(!(networkIdItem == nullptr || cJSON_IsInvalid(networkIdItem) ||
429 !cJSON_IsString(networkIdItem)), AVSESSION_ERROR, "get networkIdItem err");
430 basicInfo.networkId_ = std::string(networkIdItem->valuestring);
431 cJSON* vendorIdItem = cJSON_GetObjectItem(jsonObj, "vendorId");
432 CHECK_AND_RETURN_RET_LOG(!(vendorIdItem == nullptr || cJSON_IsInvalid(vendorIdItem) ||
433 !cJSON_IsString(vendorIdItem)), AVSESSION_ERROR, "get vendorIdItem err");
434 basicInfo.vendorId_ = std::string(vendorIdItem->valuestring);
435 cJSON* deviceTypeItem = cJSON_GetObjectItem(jsonObj, "deviceType");
436 CHECK_AND_RETURN_RET_LOG(!(deviceTypeItem == nullptr || cJSON_IsInvalid(deviceTypeItem) ||
437 !cJSON_IsString(deviceTypeItem)), AVSESSION_ERROR, "get deviceTypeItem err");
438 basicInfo.deviceType_ = std::string(deviceTypeItem->valuestring);
439 cJSON* systemVersionItem = cJSON_GetObjectItem(jsonObj, "systemVersion");
440 CHECK_AND_RETURN_RET_LOG(!(systemVersionItem == nullptr || cJSON_IsInvalid(systemVersionItem) ||
441 !cJSON_IsString(systemVersionItem)), AVSESSION_ERROR, "get systemVersionItem err");
442 basicInfo.systemVersion_ = std::string(systemVersionItem->valuestring);
443 cJSON* sessionVersionItem = cJSON_GetObjectItem(jsonObj, "avsessionVersion");
444 CHECK_AND_RETURN_RET_LOG(!(sessionVersionItem == nullptr || cJSON_IsInvalid(sessionVersionItem) ||
445 !cJSON_IsNumber(sessionVersionItem)), AVSESSION_ERROR, "get sessionVersionItem err");
446 basicInfo.sessionVersion_ = sessionVersionItem->valueint;
447
448 cJSON* reserveItem = cJSON_GetObjectItem(jsonObj, "reserve");
449 CHECK_AND_RETURN_RET_LOG(!(reserveItem == nullptr || cJSON_IsInvalid(reserveItem) ||
450 !cJSON_IsArray(reserveItem)), AVSESSION_ERROR, "get reserveItem err");
451 int32_t ret = JsonToVector(reserveItem, basicInfo.reserve_);
452 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, AVSESSION_ERROR, "Get reserve error");
453 cJSON* featuresItem = cJSON_GetObjectItem(jsonObj, "features");
454 CHECK_AND_RETURN_RET_LOG(!(featuresItem == nullptr || cJSON_IsInvalid(featuresItem) ||
455 !cJSON_IsArray(featuresItem)), AVSESSION_ERROR, "get featuresItem err");
456 ret = JsonToVector(featuresItem, basicInfo.feature_);
457 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, AVSESSION_ERROR, "Get features error");
458
459 cJSON* capabilitySetItem = cJSON_GetObjectItem(jsonObj, "capabilitySet");
460 CHECK_AND_RETURN_RET_LOG(!(capabilitySetItem == nullptr || cJSON_IsInvalid(capabilitySetItem)),
461 AVSESSION_ERROR, "get capabilitySetItem err");
462 if (GetSessionCapabilitySet(capabilitySetItem, basicInfo) != AVSESSION_SUCCESS) {
463 SLOGE("GetSessionCapabilitySet fail");
464 return AVSESSION_ERROR;
465 }
466
467 cJSON* extendCapabilityItem = cJSON_GetObjectItem(jsonObj, "extendCapability");
468 CHECK_AND_RETURN_RET_LOG(!(extendCapabilityItem == nullptr || cJSON_IsInvalid(extendCapabilityItem) ||
469 !cJSON_IsArray(extendCapabilityItem)), AVSESSION_ERROR, "get extendCapabilityItem err");
470 ret = JsonToVector(extendCapabilityItem, basicInfo.extendCapability_);
471 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, AVSESSION_ERROR, "Get extendCapability error");
472
473 return AVSESSION_SUCCESS;
474 }
475
GetSessionData(cJSON * jsonObj,AVSessionBasicInfo & basicInfo)476 int32_t JsonUtils::GetSessionData(cJSON* jsonObj, AVSessionBasicInfo& basicInfo)
477 {
478 CHECK_AND_RETURN_RET_LOG(jsonObj != nullptr && !cJSON_IsInvalid(jsonObj), AVSESSION_ERROR, "json object is err");
479
480 cJSON* systemTimeItem = cJSON_GetObjectItem(jsonObj, "systemTime");
481 CHECK_AND_RETURN_RET_LOG(!(systemTimeItem == nullptr || cJSON_IsInvalid(systemTimeItem) ||
482 !cJSON_IsNumber(systemTimeItem)), AVSESSION_ERROR, "get systemTimeItem err");
483 basicInfo.systemTime_ = systemTimeItem->valueint;
484
485 cJSON* extendItem = cJSON_GetObjectItem(jsonObj, "extend");
486 CHECK_AND_RETURN_RET_LOG(!(extendItem == nullptr || cJSON_IsInvalid(extendItem) ||
487 !cJSON_IsArray(extendItem)), AVSESSION_ERROR, "get extendItem err");
488 int32_t ret = JsonToVector(extendItem, basicInfo.extend_);
489 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, AVSESSION_ERROR, "Get extend error");
490
491 return AVSESSION_SUCCESS;
492 }
493
GetSessionBasicInfo(const std::string & sessionInfo,AVSessionBasicInfo & basicInfo)494 int32_t JsonUtils::GetSessionBasicInfo(const std::string& sessionInfo, AVSessionBasicInfo& basicInfo)
495 {
496 CHECK_AND_RETURN_RET_LOG(!sessionInfo.empty(), AVSESSION_ERROR, "sessionInfo is empty");
497 cJSON* sessionInfoItem = cJSON_Parse(sessionInfo.c_str());
498 CHECK_AND_RETURN_RET_LOG(sessionInfoItem != nullptr, AVSESSION_ERROR, "sessionInfoItem is null");
499 if (cJSON_IsInvalid(sessionInfoItem) || cJSON_IsNull(sessionInfoItem)) {
500 SLOGE("get sessionInfoItem invalid");
501 cJSON_Delete(sessionInfoItem);
502 return AVSESSION_ERROR;
503 }
504 if (!cJSON_HasObjectItem(sessionInfoItem, "compatibility") || !cJSON_HasObjectItem(sessionInfoItem, "data")) {
505 SLOGE("The key of jsonObj is invalid");
506 cJSON_Delete(sessionInfoItem);
507 return AVSESSION_ERROR;
508 }
509 cJSON* compatibilityItem = cJSON_GetObjectItem(sessionInfoItem, "compatibility");
510 if (GetSessionCompatibility(compatibilityItem, basicInfo) != AVSESSION_SUCCESS) {
511 SLOGE("GetSessionCompatibility fail");
512 cJSON_Delete(sessionInfoItem);
513 return AVSESSION_ERROR;
514 }
515
516 cJSON* dataItem = cJSON_GetObjectItem(sessionInfoItem, "data");
517 if (GetSessionData(dataItem, basicInfo) != AVSESSION_SUCCESS) {
518 SLOGE("GetSessionData fail");
519 cJSON_Delete(sessionInfoItem);
520 return AVSESSION_ERROR;
521 }
522 cJSON_Delete(sessionInfoItem);
523 return AVSESSION_SUCCESS;
524 }
525
SetSessionDescriptorByCJSON(cJSON * sessionDescriptorItem,const AVSessionDescriptor & descriptor)526 int32_t JsonUtils::SetSessionDescriptorByCJSON(cJSON* sessionDescriptorItem,
527 const AVSessionDescriptor& descriptor)
528 {
529 if (sessionDescriptorItem == nullptr || cJSON_IsInvalid(sessionDescriptorItem) ||
530 cJSON_IsNull(sessionDescriptorItem)) {
531 SLOGE("get sessionDescriptorItem invalid");
532 return AVSESSION_ERROR;
533 }
534 cJSON_AddStringToObject(sessionDescriptorItem, "sessionId", descriptor.sessionId_.c_str());
535 cJSON_AddStringToObject(sessionDescriptorItem, "type", ConvertSessionType(descriptor.sessionType_).c_str());
536 cJSON_AddStringToObject(sessionDescriptorItem, "bundleName", descriptor.elementName_.GetBundleName().c_str());
537 cJSON_AddStringToObject(sessionDescriptorItem, "abilityName", descriptor.elementName_.GetAbilityName().c_str());
538 cJSON_AddStringToObject(sessionDescriptorItem, "tag", descriptor.sessionTag_.c_str());
539 cJSON_AddBoolToObject(sessionDescriptorItem, "isThirdPartyApp", descriptor.isThirdPartyApp_);
540 if (cJSON_IsInvalid(sessionDescriptorItem)) {
541 SLOGE("get sessionDescriptorItem aft add descriptor invalid");
542 return AVSESSION_ERROR;
543 }
544 return AVSESSION_SUCCESS;
545 }
546
SetSessionDescriptors(std::string & sessionInfo,const std::vector<AVSessionDescriptor> & descriptors)547 int32_t JsonUtils::SetSessionDescriptors(std::string& sessionInfo, const std::vector<AVSessionDescriptor>& descriptors)
548 {
549 cJSON* sessionInfoItem = nullptr;
550 if (sessionInfo.empty()) {
551 sessionInfoItem = cJSON_CreateObject();
552 } else {
553 sessionInfoItem = cJSON_Parse(sessionInfo.c_str());
554 }
555 CHECK_AND_RETURN_RET_LOG(sessionInfoItem != nullptr, AVSESSION_ERROR, "json object is null");
556 if (cJSON_IsInvalid(sessionInfoItem)) {
557 SLOGE("parse json invalid");
558 cJSON_Delete(sessionInfoItem);
559 return AVSESSION_ERROR;
560 }
561 cJSON* dataItem = cJSON_GetObjectItem(sessionInfoItem, "data");
562 if (dataItem == nullptr || cJSON_IsInvalid(dataItem) || cJSON_IsNull(dataItem)) {
563 dataItem = cJSON_CreateObject();
564 cJSON_AddItemToObject(sessionInfoItem, "data", dataItem);
565 }
566 cJSON* descriptorsArray = cJSON_GetObjectItem(dataItem, "sessionDescriptors");
567 if (descriptorsArray == nullptr || cJSON_IsInvalid(descriptorsArray) || !cJSON_IsArray(descriptorsArray)) {
568 descriptorsArray = cJSON_CreateArray();
569 cJSON_AddItemToObject(dataItem, "sessionDescriptors", descriptorsArray);
570 }
571 for (uint32_t i = 0; i < descriptors.size(); i++) {
572 cJSON* sessionDescriptorItem = cJSON_CreateObject();
573 CHECK_AND_CONTINUE(sessionDescriptorItem != nullptr);
574 if (cJSON_IsInvalid(sessionDescriptorItem) ||
575 SetSessionDescriptorByCJSON(sessionDescriptorItem, descriptors[i]) != AVSESSION_SUCCESS) {
576 SLOGE("get sessionDescriptorItem and set descriptor fail");
577 cJSON_Delete(sessionDescriptorItem);
578 continue;
579 }
580 cJSON_InsertItemInArray(descriptorsArray, i, sessionDescriptorItem);
581 }
582 char* g_sessionInfoStr = cJSON_Print(sessionInfoItem);
583 if (g_sessionInfoStr != nullptr) {
584 sessionInfo = g_sessionInfoStr;
585 cJSON_free(g_sessionInfoStr);
586 }
587 cJSON_Delete(sessionInfoItem);
588 return AVSESSION_SUCCESS;
589 }
590
GetSessionDescriptorByCJSON(cJSON * sessionDescriptorItem,AVSessionDescriptor & descriptor)591 int32_t JsonUtils::GetSessionDescriptorByCJSON(cJSON* sessionDescriptorItem,
592 AVSessionDescriptor& descriptor)
593 {
594 CHECK_AND_RETURN_RET_LOG(sessionDescriptorItem != nullptr, AVSESSION_ERROR, "json object is null");
595 CHECK_AND_RETURN_RET_LOG(!cJSON_IsInvalid(sessionDescriptorItem), AVSESSION_ERROR, "json object is invalid");
596
597 CHECK_AND_RETURN_RET_LOG(IsString(sessionDescriptorItem, "sessionId") && IsString(sessionDescriptorItem, "type")
598 && IsString(sessionDescriptorItem, "bundleName") && IsString(sessionDescriptorItem, "abilityName")
599 && IsString(sessionDescriptorItem, "tag") && IsBool(sessionDescriptorItem, "isThirdPartyApp"), AVSESSION_ERROR,
600 "The key of cjson is invalid");
601
602 cJSON* sessionIdItem = cJSON_GetObjectItem(sessionDescriptorItem, "sessionId");
603 CHECK_AND_RETURN_RET_LOG(sessionIdItem != nullptr && !cJSON_IsInvalid(sessionIdItem) &&
604 cJSON_IsString(sessionIdItem), AVSESSION_ERROR, "sessionIdItem is invalid");
605 descriptor.sessionId_.assign(sessionIdItem->valuestring);
606
607 cJSON* sessionTypeItem = cJSON_GetObjectItem(sessionDescriptorItem, "type");
608 CHECK_AND_RETURN_RET_LOG(sessionTypeItem != nullptr && !cJSON_IsInvalid(sessionTypeItem) &&
609 cJSON_IsString(sessionTypeItem), AVSESSION_ERROR, "sessionTypeItem is invalid");
610 descriptor.sessionType_ = ConvertSessionType(std::string(sessionTypeItem->valuestring));
611
612 cJSON* bundleNameItem = cJSON_GetObjectItem(sessionDescriptorItem, "bundleName");
613 CHECK_AND_RETURN_RET_LOG(bundleNameItem != nullptr && !cJSON_IsInvalid(bundleNameItem) &&
614 cJSON_IsString(bundleNameItem), AVSESSION_ERROR, "bundleNameItem is invalid");
615 descriptor.elementName_.SetBundleName(std::string(bundleNameItem->valuestring));
616
617 cJSON* abilityNameItem = cJSON_GetObjectItem(sessionDescriptorItem, "abilityName");
618 CHECK_AND_RETURN_RET_LOG(abilityNameItem != nullptr && !cJSON_IsInvalid(abilityNameItem) &&
619 cJSON_IsString(abilityNameItem), AVSESSION_ERROR, "abilityNameItem is invalid");
620 descriptor.elementName_.SetAbilityName(std::string(abilityNameItem->valuestring));
621
622 cJSON* sessionTagItem = cJSON_GetObjectItem(sessionDescriptorItem, "tag");
623 CHECK_AND_RETURN_RET_LOG(sessionTagItem != nullptr && !cJSON_IsInvalid(sessionTagItem) &&
624 cJSON_IsString(sessionTagItem), AVSESSION_ERROR, "sessionTagItem is invalid");
625 descriptor.sessionTag_.assign(sessionTagItem->valuestring);
626
627 cJSON* isThirdPartyAppItem = cJSON_GetObjectItem(sessionDescriptorItem, "isThirdPartyApp");
628 CHECK_AND_RETURN_RET_LOG(isThirdPartyAppItem != nullptr && !cJSON_IsInvalid(isThirdPartyAppItem) &&
629 cJSON_IsBool(isThirdPartyAppItem), AVSESSION_ERROR, "isThirdPartyAppItem is invalid");
630 descriptor.isThirdPartyApp_ = cJSON_IsTrue(isThirdPartyAppItem);
631
632 return AVSESSION_SUCCESS;
633 }
634
GetSessionDescriptors(const std::string & sessionInfo,std::vector<AVSessionDescriptor> & descriptors)635 int32_t JsonUtils::GetSessionDescriptors(const std::string& sessionInfo, std::vector<AVSessionDescriptor>& descriptors)
636 {
637 CHECK_AND_RETURN_RET_LOG(!sessionInfo.empty(), AVSESSION_ERROR, "sessionInfo is empty");
638 cJSON* sessionInfoItem = cJSON_Parse(sessionInfo.c_str());
639 CHECK_AND_RETURN_RET_LOG(sessionInfoItem != nullptr, AVSESSION_ERROR, "json object is null");
640 if (cJSON_IsInvalid(sessionInfoItem)) {
641 SLOGE("parse json invalid");
642 cJSON_Delete(sessionInfoItem);
643 return AVSESSION_ERROR;
644 }
645 if (!cJSON_HasObjectItem(sessionInfoItem, "data")) {
646 SLOGE("json object data is null");
647 cJSON_Delete(sessionInfoItem);
648 return AVSESSION_ERROR;
649 }
650 cJSON* dataItem = cJSON_GetObjectItem(sessionInfoItem, "data");
651 if (dataItem == nullptr || cJSON_IsInvalid(dataItem)) {
652 SLOGE("get dataItem null or invalid");
653 cJSON_Delete(sessionInfoItem);
654 return AVSESSION_ERROR;
655 }
656 if (!cJSON_HasObjectItem(dataItem, "sessionDescriptors")) {
657 SLOGE("json object sessionDescriptors is null");
658 cJSON_Delete(sessionInfoItem);
659 return AVSESSION_ERROR;
660 }
661 cJSON* descriptorsArray = cJSON_GetObjectItem(dataItem, "sessionDescriptors");
662 if (descriptorsArray == nullptr || cJSON_IsInvalid(descriptorsArray) || !cJSON_IsArray(descriptorsArray)) {
663 SLOGE("get descriptorsArray null or invalid");
664 cJSON_Delete(sessionInfoItem);
665 return AVSESSION_ERROR;
666 }
667
668 cJSON* sessionDescriptorItem = nullptr;
669 cJSON_ArrayForEach(sessionDescriptorItem, descriptorsArray) {
670 AVSessionDescriptor descriptor;
671 if (GetSessionDescriptorByCJSON(sessionDescriptorItem, descriptor) != AVSESSION_SUCCESS) {
672 SLOGE("GetSessionDescriptorByCJSON fail");
673 cJSON_Delete(sessionInfoItem);
674 return AVSESSION_ERROR;
675 }
676 descriptors.push_back(descriptor);
677 }
678
679 cJSON_Delete(sessionInfoItem);
680 return AVSESSION_SUCCESS;
681 }
682
SetSessionDescriptor(std::string & sessionInfo,const AVSessionDescriptor & descriptor)683 int32_t JsonUtils::SetSessionDescriptor(std::string& sessionInfo, const AVSessionDescriptor& descriptor)
684 {
685 cJSON* sessionInfoItem = nullptr;
686 if (sessionInfo.empty()) {
687 sessionInfoItem = cJSON_CreateObject();
688 } else {
689 sessionInfoItem = cJSON_Parse(sessionInfo.c_str());
690 }
691 CHECK_AND_RETURN_RET_LOG(sessionInfoItem != nullptr, AVSESSION_ERROR, "json object is null");
692 if (cJSON_IsInvalid(sessionInfoItem)) {
693 SLOGE("parse json invalid");
694 cJSON_Delete(sessionInfoItem);
695 return AVSESSION_ERROR;
696 }
697 cJSON* dataItem = cJSON_GetObjectItem(sessionInfoItem, "data");
698 if (dataItem == nullptr || cJSON_IsInvalid(dataItem) || cJSON_IsNull(dataItem)) {
699 dataItem = cJSON_CreateObject();
700 cJSON_AddItemToObject(sessionInfoItem, "data", dataItem);
701 }
702 cJSON* descriptorItem = cJSON_GetObjectItem(dataItem, "sessionDescriptor");
703 if (descriptorItem == nullptr || cJSON_IsInvalid(descriptorItem) || cJSON_IsNull(descriptorItem)) {
704 descriptorItem = cJSON_CreateObject();
705 cJSON_AddItemToObject(dataItem, "sessionDescriptor", descriptorItem);
706 }
707 if (SetSessionDescriptorByCJSON(descriptorItem, descriptor) != AVSESSION_SUCCESS) {
708 SLOGE("get descriptorItem and set descriptor fail");
709 cJSON_Delete(sessionInfoItem);
710 return AVSESSION_ERROR;
711 }
712 char* sessionInfoStr = cJSON_Print(sessionInfoItem);
713 if (sessionInfoStr != nullptr) {
714 sessionInfo = sessionInfoStr;
715 cJSON_free(sessionInfoStr);
716 } else {
717 SLOGE("get sessionInfoStr with nullptr");
718 }
719
720 cJSON_Delete(sessionInfoItem);
721 return AVSESSION_SUCCESS;
722 }
723
GetSessionDescriptor(const std::string & sessionInfo,AVSessionDescriptor & descriptor)724 int32_t JsonUtils::GetSessionDescriptor(const std::string& sessionInfo, AVSessionDescriptor& descriptor)
725 {
726 CHECK_AND_RETURN_RET_LOG(!sessionInfo.empty(), AVSESSION_ERROR, "sessionInfo is empty");
727 cJSON* sessionInfoItem = cJSON_Parse(sessionInfo.c_str());
728 CHECK_AND_RETURN_RET_LOG(sessionInfoItem != nullptr, AVSESSION_ERROR, "json object is null");
729 if (cJSON_IsInvalid(sessionInfoItem)) {
730 SLOGE("parse json invalid");
731 cJSON_Delete(sessionInfoItem);
732 return AVSESSION_ERROR;
733 }
734 if (!cJSON_HasObjectItem(sessionInfoItem, "data")) {
735 SLOGE("json object data is null");
736 cJSON_Delete(sessionInfoItem);
737 return AVSESSION_ERROR;
738 }
739 cJSON* dataItem = cJSON_GetObjectItem(sessionInfoItem, "data");
740 if (dataItem == nullptr || cJSON_IsInvalid(dataItem)) {
741 SLOGE("get dataItem null or invalid");
742 cJSON_Delete(sessionInfoItem);
743 return AVSESSION_ERROR;
744 }
745 if (!cJSON_HasObjectItem(dataItem, "sessionDescriptor")) {
746 SLOGE("json object sessionDescriptor is null");
747 cJSON_Delete(sessionInfoItem);
748 return AVSESSION_ERROR;
749 }
750 cJSON* descriptorItem = cJSON_GetObjectItem(dataItem, "sessionDescriptor");
751 if (descriptorItem == nullptr || cJSON_IsInvalid(descriptorItem)) {
752 SLOGE("get descriptorItem null or invalid");
753 cJSON_Delete(sessionInfoItem);
754 return AVSESSION_ERROR;
755 }
756 if (GetSessionDescriptorByCJSON(descriptorItem, descriptor) != AVSESSION_SUCCESS) {
757 SLOGE("GetSessionDescriptorByCJSON fail");
758 cJSON_Delete(sessionInfoItem);
759 return AVSESSION_ERROR;
760 }
761
762 cJSON_Delete(sessionInfoItem);
763 return AVSESSION_SUCCESS;
764 }
765 } // namespace OHOS::AVSession
766