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 "dsched_continue_event.h"
17
18 #include "cJSON.h"
19 #include "parcel.h"
20
21 #include "distributed_sched_utils.h"
22 #include "dms_constant.h"
23 #include "dtbschedmgr_log.h"
24
25 namespace OHOS {
26 namespace DistributedSchedule {
27 namespace {
28 const std::string TAG = "DSchedContinueCmd";
29 const char* EXTRO_INFO_JSON_KEY_ACCESS_TOKEN = "accessTokenID";
30 const char* DMS_VERSION_ID = "dmsVersion";
31 }
32
Marshal(std::string & jsonStr)33 int32_t DSchedContinueCmdBase::Marshal(std::string &jsonStr)
34 {
35 cJSON *rootValue = cJSON_CreateObject();
36 if (rootValue == nullptr) {
37 return INVALID_PARAMETERS_ERR;
38 }
39 cJSON_AddNumberToObject(rootValue, "Version", version_);
40 cJSON_AddNumberToObject(rootValue, "ServiceType", serviceType_);
41 cJSON_AddNumberToObject(rootValue, "SubServiceType", subServiceType_);
42 cJSON_AddNumberToObject(rootValue, "Command", command_);
43
44 cJSON_AddStringToObject(rootValue, "SrcDeviceId", srcDeviceId_.c_str());
45 cJSON_AddStringToObject(rootValue, "SrcBundleName", srcBundleName_.c_str());
46 cJSON_AddStringToObject(rootValue, "SrcDeveloperId", srcDeveloperId_.c_str());
47 cJSON_AddStringToObject(rootValue, "DstDeviceId", dstDeviceId_.c_str());
48 cJSON_AddStringToObject(rootValue, "DstBundleName", dstBundleName_.c_str());
49 cJSON_AddStringToObject(rootValue, "DstDeveloperId", dstDeveloperId_.c_str());
50 cJSON_AddStringToObject(rootValue, "ContinueType", continueType_.c_str());
51
52 cJSON_AddNumberToObject(rootValue, "ContinueByType", continueByType_);
53 cJSON_AddNumberToObject(rootValue, "SourceMissionId", sourceMissionId_);
54 cJSON_AddNumberToObject(rootValue, "DmsVersion", dmsVersion_);
55
56 char *data = cJSON_Print(rootValue);
57 if (data == nullptr) {
58 cJSON_Delete(rootValue);
59 return INVALID_PARAMETERS_ERR;
60 }
61 jsonStr = std::string(data);
62 cJSON_Delete(rootValue);
63 cJSON_free(data);
64 return ERR_OK;
65 }
66
Unmarshal(const std::string & jsonStr)67 int32_t DSchedContinueCmdBase::Unmarshal(const std::string &jsonStr)
68 {
69 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
70 if (rootValue == nullptr) {
71 HILOGE("Dms continue cmd base json string parse to cjson fail.");
72 return INVALID_PARAMETERS_ERR;
73 }
74
75 const char *numKeys[] = { "Version", "ServiceType", "SubServiceType", "ContinueByType", "SourceMissionId",
76 "DmsVersion" };
77 int32_t *numValues[] = { &version_, &serviceType_, &subServiceType_, &continueByType_, &sourceMissionId_,
78 &dmsVersion_ };
79 int32_t numLength = sizeof(numKeys) / sizeof(numKeys[0]);
80 for (int32_t i = 0; i < numLength; i++) {
81 cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, numKeys[i]);
82 if (item == nullptr || !cJSON_IsNumber(item)) {
83 cJSON_Delete(rootValue);
84 HILOGE("Dms continue cmd base %{public}s term is null or not number.", numKeys[i]);
85 return INVALID_PARAMETERS_ERR;
86 }
87 *numValues[i] = item->valueint;
88 }
89
90 const char *strKeys[] = {"SrcDeviceId", "SrcBundleName", "DstDeviceId", "DstBundleName", "ContinueType"};
91 std::string *strValues[] = {&srcDeviceId_, &srcBundleName_, &dstDeviceId_, &dstBundleName_, &continueType_};
92 int32_t strLength = sizeof(strKeys) / sizeof(strKeys[0]);
93 for (int32_t i = 0; i < strLength; i++) {
94 cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, strKeys[i]);
95 if (item == nullptr || !cJSON_IsString(item) || (item->valuestring == nullptr)) {
96 cJSON_Delete(rootValue);
97 HILOGE("Dms continue cmd base %{public}s term is null or not string.", strKeys[i]);
98 return INVALID_PARAMETERS_ERR;
99 }
100 *strValues[i] = item->valuestring;
101 }
102 const char *strNotRequiredKeys[] = {"SrcDeveloperId", "DstDeveloperId"};
103 std::string *strNotRequiredValues[] = { &srcDeveloperId_, &dstDeveloperId_};
104 int32_t strNotRequiredLength = sizeof(strNotRequiredKeys) / sizeof(strNotRequiredKeys[0]);
105 for (int32_t i = 0; i < strNotRequiredLength; i++) {
106 cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, strNotRequiredKeys[i]);
107 if (item == nullptr || !cJSON_IsString(item) || (item->valuestring == nullptr)) {
108 *strNotRequiredValues[i] = "";
109 } else {
110 *strNotRequiredValues[i] = item->valuestring;
111 }
112 }
113
114 cJSON_Delete(rootValue);
115 return ERR_OK;
116 }
117
Marshal(std::string & jsonStr)118 int32_t DSchedContinueStartCmd::Marshal(std::string &jsonStr)
119 {
120 cJSON *rootValue = cJSON_CreateObject();
121 if (rootValue == nullptr) {
122 return INVALID_PARAMETERS_ERR;
123 }
124
125 std::string baseJsonStr;
126 if (DSchedContinueCmdBase::Marshal(baseJsonStr) != ERR_OK) {
127 cJSON_Delete(rootValue);
128 return INVALID_PARAMETERS_ERR;
129 }
130 cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str());
131
132 cJSON_AddNumberToObject(rootValue, "Direction", direction_);
133 cJSON_AddNumberToObject(rootValue, "AppVersion", appVersion_);
134
135 Parcel parcel;
136 if (!wantParams_.Marshalling(parcel)) {
137 cJSON_Delete(rootValue);
138 return INVALID_PARAMETERS_ERR;
139 }
140 std::string wantParamsStr = ParcelToBase64Str(parcel);
141 cJSON_AddStringToObject(rootValue, "WantParams", wantParamsStr.c_str());
142
143 char *data = cJSON_Print(rootValue);
144 if (data == nullptr) {
145 cJSON_Delete(rootValue);
146 return INVALID_PARAMETERS_ERR;
147 }
148 jsonStr = std::string(data);
149 cJSON_Delete(rootValue);
150 cJSON_free(data);
151 return ERR_OK;
152 }
153
Unmarshal(const std::string & jsonStr)154 int32_t DSchedContinueStartCmd::Unmarshal(const std::string &jsonStr)
155 {
156 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
157 if (rootValue == nullptr) {
158 return INVALID_PARAMETERS_ERR;
159 }
160
161 cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd");
162 if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) {
163 cJSON_Delete(rootValue);
164 return INVALID_PARAMETERS_ERR;
165 }
166 std::string baseCmdStr = baseCmd->valuestring;
167 if (DSchedContinueCmdBase::Unmarshal(baseCmdStr) != ERR_OK) {
168 cJSON_Delete(rootValue);
169 return INVALID_PARAMETERS_ERR;
170 }
171
172 cJSON *direction = cJSON_GetObjectItemCaseSensitive(rootValue, "Direction");
173 if (direction == nullptr || !cJSON_IsNumber(direction)) {
174 cJSON_Delete(rootValue);
175 return INVALID_PARAMETERS_ERR;
176 }
177 direction_ = direction->valueint;
178
179 cJSON *appVersion = cJSON_GetObjectItemCaseSensitive(rootValue, "AppVersion");
180 if (appVersion == nullptr || !cJSON_IsNumber(appVersion)) {
181 cJSON_Delete(rootValue);
182 return INVALID_PARAMETERS_ERR;
183 }
184 appVersion_ = appVersion->valueint;
185
186 cJSON *wantParams = cJSON_GetObjectItemCaseSensitive(rootValue, "WantParams");
187 if (wantParams == nullptr || !cJSON_IsString(wantParams) || (wantParams->valuestring == nullptr)) {
188 cJSON_Delete(rootValue);
189 return INVALID_PARAMETERS_ERR;
190 }
191 Parcel parcel;
192 int32_t ret = Base64StrToParcel(wantParams->valuestring, parcel);
193 if (ret != ERR_OK) {
194 cJSON_Delete(rootValue);
195 return INVALID_PARAMETERS_ERR;
196 }
197 auto wantParamsPtr = DistributedWantParams::Unmarshalling(parcel);
198 if (wantParamsPtr == nullptr) {
199 cJSON_Delete(rootValue);
200 return INVALID_PARAMETERS_ERR;
201 }
202 wantParams_ = *wantParamsPtr;
203
204 cJSON_Delete(rootValue);
205 return ERR_OK;
206 }
207
Marshal(std::string & jsonStr)208 int32_t DSchedContinueDataCmd::Marshal(std::string &jsonStr)
209 {
210 cJSON *rootValue = cJSON_CreateObject();
211 if (rootValue == nullptr) {
212 return INVALID_PARAMETERS_ERR;
213 }
214
215 std::string baseJsonStr;
216 if (DSchedContinueCmdBase::Marshal(baseJsonStr) != ERR_OK) {
217 cJSON_Delete(rootValue);
218 return INVALID_PARAMETERS_ERR;
219 }
220
221 cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str());
222
223 Parcel wantParcel;
224 if (!want_.Marshalling(wantParcel)) {
225 cJSON_Delete(rootValue);
226 return INVALID_PARAMETERS_ERR;
227 }
228 std::string wantStr = ParcelToBase64Str(wantParcel);
229 cJSON_AddStringToObject(rootValue, "Want", wantStr.c_str());
230
231 Parcel abilityParcel;
232 if (!abilityInfo_.Marshalling(abilityParcel)) {
233 cJSON_Delete(rootValue);
234 return INVALID_PARAMETERS_ERR;
235 }
236 std::string abilityInfoStr = ParcelToBase64Str(abilityParcel);
237 cJSON_AddStringToObject(rootValue, "AbilityInfo", abilityInfoStr.c_str());
238
239 cJSON_AddNumberToObject(rootValue, "RequestCode", requestCode_);
240
241 std::string callerInfoStr;
242 if (MarshalCallerInfo(callerInfoStr) != ERR_OK) {
243 cJSON_Delete(rootValue);
244 return INVALID_PARAMETERS_ERR;
245 }
246 cJSON_AddStringToObject(rootValue, "CallerInfo", callerInfoStr.c_str());
247
248 std::string accountInfoStr;
249 if (MarshalAccountInfo(accountInfoStr) != ERR_OK) {
250 cJSON_Delete(rootValue);
251 return INVALID_PARAMETERS_ERR;
252 }
253 cJSON_AddStringToObject(rootValue, "AccountInfo", accountInfoStr.c_str());
254
255 char *data = cJSON_Print(rootValue);
256 if (data == nullptr) {
257 cJSON_Delete(rootValue);
258 return INVALID_PARAMETERS_ERR;
259 }
260 jsonStr = std::string(data);
261 cJSON_Delete(rootValue);
262 cJSON_free(data);
263 return ERR_OK;
264 }
265
MarshalCallerInfo(std::string & jsonStr)266 int32_t DSchedContinueDataCmd::MarshalCallerInfo(std::string &jsonStr)
267 {
268 cJSON *callerInfoJson = cJSON_CreateObject();
269 if (callerInfoJson == nullptr) {
270 return INVALID_PARAMETERS_ERR;
271 }
272 cJSON_AddNumberToObject(callerInfoJson, "Uid", callerInfo_.uid);
273 cJSON_AddNumberToObject(callerInfoJson, "Pid", callerInfo_.pid);
274 cJSON_AddNumberToObject(callerInfoJson, "CallerType", callerInfo_.callerType);
275 cJSON_AddStringToObject(callerInfoJson, "SourceDeviceId", callerInfo_.sourceDeviceId.c_str());
276 cJSON_AddNumberToObject(callerInfoJson, "Duid", callerInfo_.duid);
277 cJSON_AddStringToObject(callerInfoJson, "CallerAppId", callerInfo_.callerAppId.c_str());
278
279 const auto bundleNamesSize = static_cast<int32_t>(callerInfo_.bundleNames.size());
280 cJSON *bundleNames = cJSON_CreateArray();
281 if (bundleNames == nullptr) {
282 cJSON_Delete(callerInfoJson);
283 return INVALID_PARAMETERS_ERR;
284 }
285 for (auto i = 0; i < bundleNamesSize; i++) {
286 cJSON *bundleName = cJSON_CreateString(callerInfo_.bundleNames[i].c_str());
287 if (bundleName == nullptr) {
288 cJSON_Delete(callerInfoJson);
289 cJSON_Delete(bundleNames);
290 return INVALID_PARAMETERS_ERR;
291 }
292 cJSON_AddItemToArray(bundleNames, bundleName);
293 }
294 cJSON_AddItemToObject(callerInfoJson, "BundleNames", bundleNames);
295
296 std::string extraInfo = callerInfo_.extraInfoJson.dump();
297 cJSON_AddStringToObject(callerInfoJson, "ExtraInfo", extraInfo.c_str());
298
299 char *data = cJSON_Print(callerInfoJson);
300 if (data == nullptr) {
301 cJSON_Delete(callerInfoJson);
302 return INVALID_PARAMETERS_ERR;
303 }
304 jsonStr = std::string(data);
305 cJSON_Delete(callerInfoJson);
306 cJSON_free(data);
307 return ERR_OK;
308 }
309
MarshalAccountInfo(std::string & jsonStr)310 int32_t DSchedContinueDataCmd::MarshalAccountInfo(std::string &jsonStr)
311 {
312 cJSON *accountInfoJson = cJSON_CreateObject();
313 if (accountInfoJson == nullptr) {
314 return INVALID_PARAMETERS_ERR;
315 }
316
317 cJSON_AddNumberToObject(accountInfoJson, "AccountType", accountInfo_.accountType);
318
319 const auto groupIdListSize = static_cast<int32_t>(accountInfo_.groupIdList.size());
320 cJSON *groupIdList = cJSON_CreateArray();
321 if (groupIdList == nullptr) {
322 cJSON_Delete(accountInfoJson);
323 return INVALID_PARAMETERS_ERR;
324 }
325 for (auto i = 0; i < groupIdListSize; i++) {
326 cJSON *groupId = cJSON_CreateString(accountInfo_.groupIdList[i].c_str());
327 if (groupId == nullptr) {
328 cJSON_Delete(accountInfoJson);
329 cJSON_Delete(groupIdList);
330 return INVALID_PARAMETERS_ERR;
331 }
332 cJSON_AddItemToArray(groupIdList, groupId);
333 }
334 cJSON_AddItemToObject(accountInfoJson, "GroupIdList", groupIdList);
335
336 cJSON_AddStringToObject(accountInfoJson, Constants::EXTRO_INFO_JSON_KEY_ACCOUNT_ID.c_str(),
337 accountInfo_.activeAccountId.c_str());
338 cJSON_AddNumberToObject(accountInfoJson, Constants::EXTRO_INFO_JSON_KEY_USERID_ID.c_str(), accountInfo_.userId);
339
340 char *data = cJSON_Print(accountInfoJson);
341 if (data == nullptr) {
342 cJSON_Delete(accountInfoJson);
343 return INVALID_PARAMETERS_ERR;
344 }
345 jsonStr = std::string(data);
346 cJSON_Delete(accountInfoJson);
347 cJSON_free(data);
348 return ERR_OK;
349 }
350
Unmarshal(const std::string & jsonStr)351 int32_t DSchedContinueDataCmd::Unmarshal(const std::string &jsonStr)
352 {
353 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
354 if (rootValue == nullptr) {
355 return INVALID_PARAMETERS_ERR;
356 }
357
358 cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd");
359 if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) {
360 cJSON_Delete(rootValue);
361 return INVALID_PARAMETERS_ERR;
362 }
363 std::string baseCmdStr = baseCmd->valuestring;
364 if (DSchedContinueCmdBase::Unmarshal(baseCmdStr) != ERR_OK) {
365 cJSON_Delete(rootValue);
366 return INVALID_PARAMETERS_ERR;
367 }
368
369 if (UnmarshalParcel(jsonStr) != ERR_OK) {
370 cJSON_Delete(rootValue);
371 return INVALID_PARAMETERS_ERR;
372 }
373
374 cJSON *requestCode = cJSON_GetObjectItemCaseSensitive(rootValue, "RequestCode");
375 if (requestCode == nullptr || !cJSON_IsNumber(requestCode)) {
376 cJSON_Delete(rootValue);
377 return INVALID_PARAMETERS_ERR;
378 }
379 requestCode_ = requestCode->valueint;
380
381 cJSON *callerInfoJson = cJSON_GetObjectItemCaseSensitive(rootValue, "CallerInfo");
382 if (callerInfoJson == nullptr || !cJSON_IsString(callerInfoJson) || (callerInfoJson->valuestring == nullptr)) {
383 cJSON_Delete(rootValue);
384 return INVALID_PARAMETERS_ERR;
385 }
386 std::string callerInfoStr = callerInfoJson->valuestring;
387 if (UnmarshalCallerInfo(callerInfoStr) != ERR_OK) {
388 cJSON_Delete(rootValue);
389 return INVALID_PARAMETERS_ERR;
390 }
391
392 cJSON *accountInfoJson = cJSON_GetObjectItemCaseSensitive(rootValue, "AccountInfo");
393 if (accountInfoJson == nullptr || !cJSON_IsString(accountInfoJson) || (accountInfoJson->valuestring == nullptr)) {
394 cJSON_Delete(rootValue);
395 return INVALID_PARAMETERS_ERR;
396 }
397 std::string accountInfoStr = accountInfoJson->valuestring;
398 if (UnmarshalAccountInfo(accountInfoStr) != ERR_OK) {
399 cJSON_Delete(rootValue);
400 return INVALID_PARAMETERS_ERR;
401 }
402
403 cJSON_Delete(rootValue);
404 return ERR_OK;
405 }
406
UnmarshalParcel(const std::string & jsonStr)407 int32_t DSchedContinueDataCmd::UnmarshalParcel(const std::string &jsonStr)
408 {
409 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
410 if (rootValue == nullptr) {
411 HILOGE("Want and AbilityInfo json string parse to cjson fail.");
412 return INVALID_PARAMETERS_ERR;
413 }
414
415 cJSON *wantStr = cJSON_GetObjectItemCaseSensitive(rootValue, "Want");
416 if (wantStr == nullptr || !cJSON_IsString(wantStr) || (wantStr->valuestring == nullptr)) {
417 cJSON_Delete(rootValue);
418 HILOGE("Want term is null or not string.");
419 return INVALID_PARAMETERS_ERR;
420 }
421 Parcel wantParcel;
422 int32_t ret = Base64StrToParcel(wantStr->valuestring, wantParcel);
423 if (ret != ERR_OK) {
424 cJSON_Delete(rootValue);
425 HILOGE("Want parcel Base64Str unmarshal fail, ret %{public}d.", ret);
426 return INVALID_PARAMETERS_ERR;
427 }
428 auto wantPtr = AAFwk::Want::Unmarshalling(wantParcel);
429 if (wantPtr == nullptr) {
430 cJSON_Delete(rootValue);
431 HILOGE("AAFwk Want unmarshalling fail, check return null.");
432 return INVALID_PARAMETERS_ERR;
433 }
434 want_ = *wantPtr;
435
436 cJSON *abilityInfoStr = cJSON_GetObjectItemCaseSensitive(rootValue, "AbilityInfo");
437 if (abilityInfoStr == nullptr || !cJSON_IsString(abilityInfoStr) || (abilityInfoStr->valuestring == nullptr)) {
438 cJSON_Delete(rootValue);
439 HILOGE("AbilityInfo term is null or not string.");
440 return INVALID_PARAMETERS_ERR;
441 }
442 Parcel abilityParcel;
443 ret = Base64StrToParcel(abilityInfoStr->valuestring, abilityParcel);
444 if (ret != ERR_OK) {
445 cJSON_Delete(rootValue);
446 HILOGE("AbilityInfo parcel Base64Str unmarshal fail, ret %{public}d.", ret);
447 return INVALID_PARAMETERS_ERR;
448 }
449 auto abilityInfoPtr = AppExecFwk::CompatibleAbilityInfo::Unmarshalling(abilityParcel);
450 if (abilityInfoPtr == nullptr) {
451 cJSON_Delete(rootValue);
452 HILOGE("AppExecFwk CompatibleAbilityInfo unmarshalling fail, check return null.");
453 return INVALID_PARAMETERS_ERR;
454 }
455 abilityInfo_ = *abilityInfoPtr;
456
457 cJSON_Delete(rootValue);
458 return ERR_OK;
459 }
460
UnmarshalCallerInfo(std::string & jsonStr)461 int32_t DSchedContinueDataCmd::UnmarshalCallerInfo(std::string &jsonStr)
462 {
463 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
464 if (rootValue == nullptr) {
465 HILOGE("Caller info json string parse to cjson fail.");
466 return INVALID_PARAMETERS_ERR;
467 }
468
469 const char *strKeys[] = {
470 "SourceDeviceId", "CallerAppId"
471 };
472 std::string *strValues[] = {
473 &callerInfo_.sourceDeviceId, &callerInfo_.callerAppId
474 };
475 int32_t strLength = sizeof(strKeys) / sizeof(strKeys[0]);
476 for (int32_t i = 0; i < strLength; i++) {
477 cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, strKeys[i]);
478 if (item == nullptr || !cJSON_IsString(item) || (item->valuestring == nullptr)) {
479 cJSON_Delete(rootValue);
480 HILOGE("Caller info json %{public}s term is null or not string.", strKeys[i]);
481 return INVALID_PARAMETERS_ERR;
482 }
483 *strValues[i] = item->valuestring;
484 }
485
486 const char *numKeys[] = {
487 "Uid", "Pid", "CallerType", "Duid"
488 };
489 int32_t *numValues[] = {
490 &callerInfo_.uid, &callerInfo_.pid, &callerInfo_.callerType, &callerInfo_.duid
491 };
492 int32_t numLength = sizeof(numKeys) / sizeof(numKeys[0]);
493 for (int32_t i = 0; i < numLength; i++) {
494 cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, numKeys[i]);
495 if (item == nullptr || !cJSON_IsNumber(item)) {
496 cJSON_Delete(rootValue);
497 HILOGE("Caller info json %{public}s term is null or not number.", numKeys[i]);
498 return INVALID_PARAMETERS_ERR;
499 }
500 *numValues[i] = item->valueint;
501 }
502
503 if (UnmarshalCallerInfoExtra(jsonStr) != ERR_OK) {
504 cJSON_Delete(rootValue);
505 HILOGE("Unmarshal CallerInfoExtra term from caller info json string fail.");
506 return INVALID_PARAMETERS_ERR;
507 }
508
509 cJSON_Delete(rootValue);
510 return ERR_OK;
511 }
512
UnmarshalCallerInfoExtra(std::string & jsonStr)513 int32_t DSchedContinueDataCmd::UnmarshalCallerInfoExtra(std::string &jsonStr)
514 {
515 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
516 if (rootValue == nullptr) {
517 HILOGE("Caller info extra json string parse to cjson fail.");
518 return INVALID_PARAMETERS_ERR;
519 }
520
521 cJSON *bundleName = nullptr;
522 std::vector<std::string> bundleNameList;
523 cJSON *bundleNames = cJSON_GetObjectItemCaseSensitive(rootValue, "BundleNames");
524 cJSON_ArrayForEach(bundleName, bundleNames) {
525 if (bundleName == nullptr || !cJSON_IsString(bundleName) || (bundleName->valuestring == nullptr)) {
526 cJSON_Delete(rootValue);
527 HILOGE("BundleNames term in CallerInfoExtra json is null or not string.");
528 return INVALID_PARAMETERS_ERR;
529 }
530 bundleNameList.push_back(bundleName->valuestring);
531 }
532 callerInfo_.bundleNames = bundleNameList;
533
534 cJSON *extraInfo = cJSON_GetObjectItemCaseSensitive(rootValue, "ExtraInfo");
535 if (extraInfo == nullptr || !cJSON_IsString(extraInfo) || (extraInfo->valuestring == nullptr)) {
536 cJSON_Delete(rootValue);
537 HILOGE("ExtraInfo term in CallerInfoExtra json is null or not string.");
538 return INVALID_PARAMETERS_ERR;
539 }
540 cJSON *extraInfoValue = cJSON_Parse(extraInfo->valuestring);
541 if (extraInfoValue == nullptr) {
542 cJSON_Delete(rootValue);
543 HILOGE("ExtraInfo term json string parse to cjson fail in CallerInfoExtra json.");
544 return INVALID_PARAMETERS_ERR;
545 }
546
547 cJSON *accessToken = cJSON_GetObjectItemCaseSensitive(extraInfoValue, EXTRO_INFO_JSON_KEY_ACCESS_TOKEN);
548 if (accessToken != nullptr && cJSON_IsNumber(accessToken)) {
549 callerInfo_.accessToken = static_cast<unsigned int>(accessToken->valueint);
550 }
551
552 cJSON *dmsVersion = cJSON_GetObjectItemCaseSensitive(extraInfoValue, DMS_VERSION_ID);
553 if (dmsVersion != nullptr && !cJSON_IsString(dmsVersion) && (dmsVersion->valuestring != nullptr)) {
554 callerInfo_.extraInfoJson[DMS_VERSION_ID] = dmsVersion->valuestring;
555 }
556 cJSON_Delete(extraInfoValue);
557 cJSON_Delete(rootValue);
558 return ERR_OK;
559 }
560
UnmarshalAccountInfo(std::string & jsonStr)561 int32_t DSchedContinueDataCmd::UnmarshalAccountInfo(std::string &jsonStr)
562 {
563 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
564 if (rootValue == nullptr) {
565 HILOGE("Account info json string parse to cjson fail.");
566 return INVALID_PARAMETERS_ERR;
567 }
568
569 cJSON *accountType = cJSON_GetObjectItemCaseSensitive(rootValue, "AccountType");
570 if (accountType == nullptr || !cJSON_IsNumber(accountType)) {
571 cJSON_Delete(rootValue);
572 HILOGE("AccountType term in account info json is null or not number.");
573 return INVALID_PARAMETERS_ERR;
574 }
575 accountInfo_.accountType = accountType->valueint;
576
577 cJSON *groupId = nullptr;
578 std::vector<std::string> groupIdList;
579 cJSON *groupIdListStr = cJSON_GetObjectItemCaseSensitive(rootValue, "groupIdList");
580 cJSON_ArrayForEach(groupId, groupIdListStr) {
581 if (groupId == nullptr || !cJSON_IsString(groupId) || (groupId->valuestring == nullptr)) {
582 cJSON_Delete(rootValue);
583 HILOGE("groupId term in account info json is null or not string.");
584 return INVALID_PARAMETERS_ERR;
585 }
586 groupIdList.push_back(groupId->valuestring);
587 }
588 accountInfo_.groupIdList = groupIdList;
589
590 cJSON *accountId = cJSON_GetObjectItemCaseSensitive(rootValue, Constants::EXTRO_INFO_JSON_KEY_ACCOUNT_ID.c_str());
591 if (accountId == nullptr || !cJSON_IsString(accountId)) {
592 HILOGE("accountId term in account info json is null or not string.");
593 } else {
594 accountInfo_.activeAccountId = accountId->valuestring;
595 }
596 cJSON *userId = cJSON_GetObjectItemCaseSensitive(rootValue, Constants::EXTRO_INFO_JSON_KEY_USERID_ID.c_str());
597 if (userId == nullptr || !cJSON_IsNumber(userId)) {
598 HILOGE("userId term in account info json is null or not number.");
599 } else {
600 accountInfo_.userId = userId->valueint;
601 }
602
603 cJSON_Delete(rootValue);
604 return ERR_OK;
605 }
606
Marshal(std::string & jsonStr)607 int32_t DSchedContinueReplyCmd::Marshal(std::string &jsonStr)
608 {
609 cJSON *rootValue = cJSON_CreateObject();
610 if (rootValue == nullptr) {
611 return INVALID_PARAMETERS_ERR;
612 }
613
614 std::string baseJsonStr;
615 if (DSchedContinueCmdBase::Marshal(baseJsonStr) != ERR_OK) {
616 cJSON_Delete(rootValue);
617 return INVALID_PARAMETERS_ERR;
618 }
619 cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str());
620
621 cJSON_AddNumberToObject(rootValue, "ReplyCmd", replyCmd_);
622 cJSON_AddNumberToObject(rootValue, "AppVersion", appVersion_);
623 cJSON_AddNumberToObject(rootValue, "Result", result_);
624 cJSON_AddStringToObject(rootValue, "Reason", reason_.c_str());
625
626 char *data = cJSON_Print(rootValue);
627 if (data == nullptr) {
628 cJSON_Delete(rootValue);
629 return INVALID_PARAMETERS_ERR;
630 }
631 jsonStr = std::string(data);
632 cJSON_Delete(rootValue);
633 cJSON_free(data);
634 return ERR_OK;
635 }
636
Unmarshal(const std::string & jsonStr)637 int32_t DSchedContinueReplyCmd::Unmarshal(const std::string &jsonStr)
638 {
639 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
640 if (rootValue == nullptr) {
641 return INVALID_PARAMETERS_ERR;
642 }
643
644 cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd");
645 if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) {
646 cJSON_Delete(rootValue);
647 return INVALID_PARAMETERS_ERR;
648 }
649 std::string baseCmdStr = baseCmd->valuestring;
650 if (DSchedContinueCmdBase::Unmarshal(baseCmdStr) != ERR_OK) {
651 cJSON_Delete(rootValue);
652 return INVALID_PARAMETERS_ERR;
653 }
654
655 const char *numKeys[] = {
656 "ReplyCmd", "AppVersion", "Result"
657 };
658 int32_t *numValues[] = {
659 &replyCmd_, &appVersion_, &result_
660 };
661 int32_t numLength = sizeof(numKeys) / sizeof(numKeys[0]);
662 for (int32_t i = 0; i < numLength; i++) {
663 cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, numKeys[i]);
664 if (item == nullptr || !cJSON_IsNumber(item)) {
665 cJSON_Delete(rootValue);
666 return INVALID_PARAMETERS_ERR;
667 }
668 *numValues[i] = item->valueint;
669 }
670
671 cJSON *reason = cJSON_GetObjectItemCaseSensitive(rootValue, "Reason");
672 if (reason == nullptr || !cJSON_IsString(reason) || (reason->valuestring == nullptr)) {
673 cJSON_Delete(rootValue);
674 return INVALID_PARAMETERS_ERR;
675 }
676 reason_ = reason->valuestring;
677
678 cJSON_Delete(rootValue);
679 return ERR_OK;
680 }
681
Marshal(std::string & jsonStr)682 int32_t DSchedContinueEndCmd::Marshal(std::string &jsonStr)
683 {
684 cJSON *rootValue = cJSON_CreateObject();
685 if (rootValue == nullptr) {
686 return INVALID_PARAMETERS_ERR;
687 }
688
689 std::string baseJsonStr;
690 if (DSchedContinueCmdBase::Marshal(baseJsonStr) != ERR_OK) {
691 cJSON_Delete(rootValue);
692 return INVALID_PARAMETERS_ERR;
693 }
694 cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str());
695
696 cJSON_AddNumberToObject(rootValue, "Result", result_);
697
698 char *data = cJSON_Print(rootValue);
699 if (data == nullptr) {
700 cJSON_Delete(rootValue);
701 return INVALID_PARAMETERS_ERR;
702 }
703 jsonStr = std::string(data);
704 cJSON_Delete(rootValue);
705 cJSON_free(data);
706 return ERR_OK;
707 }
708
Unmarshal(const std::string & jsonStr)709 int32_t DSchedContinueEndCmd::Unmarshal(const std::string &jsonStr)
710 {
711 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
712 if (rootValue == nullptr) {
713 return INVALID_PARAMETERS_ERR;
714 }
715
716 cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd");
717 if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) {
718 cJSON_Delete(rootValue);
719 return INVALID_PARAMETERS_ERR;
720 }
721 std::string baseCmdStr = baseCmd->valuestring;
722 if (DSchedContinueCmdBase::Unmarshal(baseCmdStr) != ERR_OK) {
723 cJSON_Delete(rootValue);
724 return INVALID_PARAMETERS_ERR;
725 }
726
727 cJSON *result = cJSON_GetObjectItemCaseSensitive(rootValue, "Result");
728 if (result == nullptr || !cJSON_IsNumber(result)) {
729 cJSON_Delete(rootValue);
730 return INVALID_PARAMETERS_ERR;
731 }
732 result_ = result->valueint;
733
734 cJSON_Delete(rootValue);
735 return ERR_OK;
736 }
737 } // namespace DistributedSchedule
738 } // namespace OHOS
739