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 "parcel.h"
19
20 #include "distributed_want_v2.h"
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 cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str());
221
222 if (!MarshalInner(rootValue)) {
223 cJSON_Delete(rootValue);
224 return INVALID_PARAMETERS_ERR;
225 }
226
227 char *data = cJSON_Print(rootValue);
228 if (data == nullptr) {
229 cJSON_Delete(rootValue);
230 return INVALID_PARAMETERS_ERR;
231 }
232
233 jsonStr = std::string(data);
234 cJSON_Delete(rootValue);
235 cJSON_free(data);
236 return ERR_OK;
237 }
238
MarshalInner(cJSON * rootValue)239 bool DSchedContinueDataCmd::MarshalInner(cJSON* rootValue)
240 {
241 if (rootValue == nullptr) {
242 return false;
243 }
244
245 Parcel dtbWantParcel;
246 DistributedWantV2 dtbWant(want_);
247 if (!dtbWant.Marshalling(dtbWantParcel)) {
248 return false;
249 }
250 std::string dtbWantStr = ParcelToBase64Str(dtbWantParcel);
251 cJSON_AddStringToObject(rootValue, "Want", dtbWantStr.c_str());
252
253 Parcel abilityParcel;
254 if (!abilityInfo_.Marshalling(abilityParcel)) {
255 return false;
256 }
257 std::string abilityInfoStr = ParcelToBase64Str(abilityParcel);
258 cJSON_AddStringToObject(rootValue, "AbilityInfo", abilityInfoStr.c_str());
259
260 cJSON_AddNumberToObject(rootValue, "RequestCode", requestCode_);
261
262 std::string callerInfoStr;
263 if (MarshalCallerInfo(callerInfoStr) != ERR_OK) {
264 return false;
265 }
266 cJSON_AddStringToObject(rootValue, "CallerInfo", callerInfoStr.c_str());
267
268 std::string accountInfoStr;
269 if (MarshalAccountInfo(accountInfoStr) != ERR_OK) {
270 return false;
271 }
272 cJSON_AddStringToObject(rootValue, "AccountInfo", accountInfoStr.c_str());
273
274 return true;
275 }
276
277
MarshalCallerInfo(std::string & jsonStr)278 int32_t DSchedContinueDataCmd::MarshalCallerInfo(std::string &jsonStr)
279 {
280 cJSON *callerInfoJson = cJSON_CreateObject();
281 if (callerInfoJson == nullptr) {
282 return INVALID_PARAMETERS_ERR;
283 }
284 cJSON_AddNumberToObject(callerInfoJson, "Uid", callerInfo_.uid);
285 cJSON_AddNumberToObject(callerInfoJson, "Pid", callerInfo_.pid);
286 cJSON_AddNumberToObject(callerInfoJson, "CallerType", callerInfo_.callerType);
287 cJSON_AddStringToObject(callerInfoJson, "SourceDeviceId", callerInfo_.sourceDeviceId.c_str());
288 cJSON_AddNumberToObject(callerInfoJson, "Duid", callerInfo_.duid);
289 cJSON_AddStringToObject(callerInfoJson, "CallerAppId", callerInfo_.callerAppId.c_str());
290
291 const auto bundleNamesSize = static_cast<int32_t>(callerInfo_.bundleNames.size());
292 cJSON *bundleNames = cJSON_CreateArray();
293 if (bundleNames == nullptr) {
294 cJSON_Delete(callerInfoJson);
295 return INVALID_PARAMETERS_ERR;
296 }
297 for (auto i = 0; i < bundleNamesSize; i++) {
298 cJSON *bundleName = cJSON_CreateString(callerInfo_.bundleNames[i].c_str());
299 if (bundleName == nullptr) {
300 cJSON_Delete(callerInfoJson);
301 cJSON_Delete(bundleNames);
302 return INVALID_PARAMETERS_ERR;
303 }
304 cJSON_AddItemToArray(bundleNames, bundleName);
305 }
306 cJSON_AddItemToObject(callerInfoJson, "BundleNames", bundleNames);
307
308 std::string extraInfo = callerInfo_.extraInfoJson.dump();
309 cJSON_AddStringToObject(callerInfoJson, "ExtraInfo", extraInfo.c_str());
310
311 char *data = cJSON_Print(callerInfoJson);
312 if (data == nullptr) {
313 cJSON_Delete(callerInfoJson);
314 return INVALID_PARAMETERS_ERR;
315 }
316 jsonStr = std::string(data);
317 cJSON_Delete(callerInfoJson);
318 cJSON_free(data);
319 return ERR_OK;
320 }
321
MarshalAccountInfo(std::string & jsonStr)322 int32_t DSchedContinueDataCmd::MarshalAccountInfo(std::string &jsonStr)
323 {
324 cJSON *accountInfoJson = cJSON_CreateObject();
325 if (accountInfoJson == nullptr) {
326 return INVALID_PARAMETERS_ERR;
327 }
328
329 cJSON_AddNumberToObject(accountInfoJson, "AccountType", accountInfo_.accountType);
330
331 const auto groupIdListSize = static_cast<int32_t>(accountInfo_.groupIdList.size());
332 cJSON *groupIdList = cJSON_CreateArray();
333 if (groupIdList == nullptr) {
334 cJSON_Delete(accountInfoJson);
335 return INVALID_PARAMETERS_ERR;
336 }
337 for (auto i = 0; i < groupIdListSize; i++) {
338 cJSON *groupId = cJSON_CreateString(accountInfo_.groupIdList[i].c_str());
339 if (groupId == nullptr) {
340 cJSON_Delete(accountInfoJson);
341 cJSON_Delete(groupIdList);
342 return INVALID_PARAMETERS_ERR;
343 }
344 cJSON_AddItemToArray(groupIdList, groupId);
345 }
346 cJSON_AddItemToObject(accountInfoJson, "GroupIdList", groupIdList);
347
348 cJSON_AddStringToObject(accountInfoJson, Constants::EXTRO_INFO_JSON_KEY_ACCOUNT_ID.c_str(),
349 accountInfo_.activeAccountId.c_str());
350 cJSON_AddNumberToObject(accountInfoJson, Constants::EXTRO_INFO_JSON_KEY_USERID_ID.c_str(), accountInfo_.userId);
351
352 char *data = cJSON_Print(accountInfoJson);
353 if (data == nullptr) {
354 cJSON_Delete(accountInfoJson);
355 return INVALID_PARAMETERS_ERR;
356 }
357 jsonStr = std::string(data);
358 cJSON_Delete(accountInfoJson);
359 cJSON_free(data);
360 return ERR_OK;
361 }
362
Unmarshal(const std::string & jsonStr)363 int32_t DSchedContinueDataCmd::Unmarshal(const std::string &jsonStr)
364 {
365 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
366 if (rootValue == nullptr) {
367 return INVALID_PARAMETERS_ERR;
368 }
369
370 cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd");
371 if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) {
372 cJSON_Delete(rootValue);
373 return INVALID_PARAMETERS_ERR;
374 }
375 std::string baseCmdStr = baseCmd->valuestring;
376 if (DSchedContinueCmdBase::Unmarshal(baseCmdStr) != ERR_OK) {
377 cJSON_Delete(rootValue);
378 return INVALID_PARAMETERS_ERR;
379 }
380
381 if (UnmarshalParcel(jsonStr) != ERR_OK) {
382 cJSON_Delete(rootValue);
383 return INVALID_PARAMETERS_ERR;
384 }
385
386 cJSON *requestCode = cJSON_GetObjectItemCaseSensitive(rootValue, "RequestCode");
387 if (requestCode == nullptr || !cJSON_IsNumber(requestCode)) {
388 cJSON_Delete(rootValue);
389 return INVALID_PARAMETERS_ERR;
390 }
391 requestCode_ = requestCode->valueint;
392
393 cJSON *callerInfoJson = cJSON_GetObjectItemCaseSensitive(rootValue, "CallerInfo");
394 if (callerInfoJson == nullptr || !cJSON_IsString(callerInfoJson) || (callerInfoJson->valuestring == nullptr)) {
395 cJSON_Delete(rootValue);
396 return INVALID_PARAMETERS_ERR;
397 }
398 std::string callerInfoStr = callerInfoJson->valuestring;
399 if (UnmarshalCallerInfo(callerInfoStr) != ERR_OK) {
400 cJSON_Delete(rootValue);
401 return INVALID_PARAMETERS_ERR;
402 }
403
404 cJSON *accountInfoJson = cJSON_GetObjectItemCaseSensitive(rootValue, "AccountInfo");
405 if (accountInfoJson == nullptr || !cJSON_IsString(accountInfoJson) || (accountInfoJson->valuestring == nullptr)) {
406 cJSON_Delete(rootValue);
407 return INVALID_PARAMETERS_ERR;
408 }
409 std::string accountInfoStr = accountInfoJson->valuestring;
410 if (UnmarshalAccountInfo(accountInfoStr) != ERR_OK) {
411 cJSON_Delete(rootValue);
412 return INVALID_PARAMETERS_ERR;
413 }
414
415 cJSON_Delete(rootValue);
416 return ERR_OK;
417 }
418
UnmarshalParcel(const std::string & jsonStr)419 int32_t DSchedContinueDataCmd::UnmarshalParcel(const std::string &jsonStr)
420 {
421 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
422 if (rootValue == nullptr) {
423 HILOGE("Want and AbilityInfo json string parse to cjson fail.");
424 return INVALID_PARAMETERS_ERR;
425 }
426
427 if (!UnmarshalWantParcel(rootValue)) {
428 cJSON_Delete(rootValue);
429 return INVALID_PARAMETERS_ERR;
430 }
431
432 cJSON *abilityInfoStr = cJSON_GetObjectItemCaseSensitive(rootValue, "AbilityInfo");
433 if (abilityInfoStr == nullptr || !cJSON_IsString(abilityInfoStr) || (abilityInfoStr->valuestring == nullptr)) {
434 cJSON_Delete(rootValue);
435 HILOGE("AbilityInfo term is null or not string.");
436 return INVALID_PARAMETERS_ERR;
437 }
438 Parcel abilityParcel;
439 int32_t ret = Base64StrToParcel(abilityInfoStr->valuestring, abilityParcel);
440 if (ret != ERR_OK) {
441 cJSON_Delete(rootValue);
442 HILOGE("AbilityInfo parcel Base64Str unmarshal fail, ret %{public}d.", ret);
443 return INVALID_PARAMETERS_ERR;
444 }
445 auto abilityInfoPtr = AppExecFwk::CompatibleAbilityInfo::Unmarshalling(abilityParcel);
446 if (abilityInfoPtr == nullptr) {
447 cJSON_Delete(rootValue);
448 HILOGE("AppExecFwk CompatibleAbilityInfo unmarshalling fail, check return null.");
449 return INVALID_PARAMETERS_ERR;
450 }
451 abilityInfo_ = *abilityInfoPtr;
452
453 cJSON_Delete(rootValue);
454 return ERR_OK;
455 }
456
UnmarshalWantParcel(cJSON * rootValue)457 bool DSchedContinueDataCmd::UnmarshalWantParcel(cJSON* rootValue)
458 {
459 if (rootValue == nullptr) {
460 return false;
461 }
462 cJSON *wantStr = cJSON_GetObjectItemCaseSensitive(rootValue, "Want");
463 if (wantStr == nullptr|| !cJSON_IsString(wantStr) || (wantStr->valuestring == nullptr)) {
464 HILOGE("Want term is null or not string.");
465 return false;
466 }
467 Parcel wantParcel;
468 int32_t ret = Base64StrToParcel(wantStr->valuestring, wantParcel);
469 if (ret != ERR_OK) {
470 HILOGE("Want parcel Base64Str unmarshal fail, ret %{public}d.", ret);
471 return false;
472 }
473
474 OHOS::AAFwk::Want* wantPtr;
475 DistributedWantV2* dtbWant = DistributedSchedule::DistributedWantV2::Unmarshalling(wantParcel);
476 if (dtbWant == nullptr) {
477 HILOGE("DistributedWantV2 Unmarshall failed, try to Unmarshall by amsWant");
478 wantPtr = AAFwk::Want::Unmarshalling(wantParcel);
479 } else {
480 HILOGI("DistributedWantV2 Unmarshall success");
481 std::shared_ptr<AAFwk::Want> amsWant = dtbWant->ToWant();
482 wantPtr = new(std::nothrow) AAFwk::Want();
483 wantPtr->SetOperation(amsWant->GetOperation());
484 wantPtr->SetParams(amsWant->GetParams());
485 }
486 if (wantPtr == nullptr) {
487 HILOGE("Want unmarshalling fail, check return null.");
488 return false;
489 }
490 want_ = *wantPtr;
491 return true;
492 }
493
UnmarshalCallerInfo(const std::string & jsonStr)494 int32_t DSchedContinueDataCmd::UnmarshalCallerInfo(const std::string &jsonStr)
495 {
496 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
497 if (rootValue == nullptr) {
498 HILOGE("Caller info json string parse to cjson fail.");
499 return INVALID_PARAMETERS_ERR;
500 }
501
502 const char *strKeys[] = {
503 "SourceDeviceId", "CallerAppId"
504 };
505 std::string *strValues[] = {
506 &callerInfo_.sourceDeviceId, &callerInfo_.callerAppId
507 };
508 int32_t strLength = sizeof(strKeys) / sizeof(strKeys[0]);
509 for (int32_t i = 0; i < strLength; i++) {
510 cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, strKeys[i]);
511 if (item == nullptr || !cJSON_IsString(item) || (item->valuestring == nullptr)) {
512 cJSON_Delete(rootValue);
513 HILOGE("Caller info json %{public}s term is null or not string.", strKeys[i]);
514 return INVALID_PARAMETERS_ERR;
515 }
516 *strValues[i] = item->valuestring;
517 }
518
519 const char *numKeys[] = {
520 "Uid", "Pid", "CallerType", "Duid"
521 };
522 int32_t *numValues[] = {
523 &callerInfo_.uid, &callerInfo_.pid, &callerInfo_.callerType, &callerInfo_.duid
524 };
525 int32_t numLength = sizeof(numKeys) / sizeof(numKeys[0]);
526 for (int32_t i = 0; i < numLength; i++) {
527 cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, numKeys[i]);
528 if (item == nullptr || !cJSON_IsNumber(item)) {
529 cJSON_Delete(rootValue);
530 HILOGE("Caller info json %{public}s term is null or not number.", numKeys[i]);
531 return INVALID_PARAMETERS_ERR;
532 }
533 *numValues[i] = item->valueint;
534 }
535
536 if (UnmarshalCallerInfoExtra(jsonStr) != ERR_OK) {
537 cJSON_Delete(rootValue);
538 HILOGE("Unmarshal CallerInfoExtra term from caller info json string fail.");
539 return INVALID_PARAMETERS_ERR;
540 }
541
542 cJSON_Delete(rootValue);
543 return ERR_OK;
544 }
545
UnmarshalCallerInfoExtra(const std::string & jsonStr)546 int32_t DSchedContinueDataCmd::UnmarshalCallerInfoExtra(const std::string &jsonStr)
547 {
548 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
549 if (rootValue == nullptr) {
550 HILOGE("Caller info extra json string parse to cjson fail.");
551 return INVALID_PARAMETERS_ERR;
552 }
553
554 cJSON *bundleName = nullptr;
555 std::vector<std::string> bundleNameList;
556 cJSON *bundleNames = cJSON_GetObjectItemCaseSensitive(rootValue, "BundleNames");
557 cJSON_ArrayForEach(bundleName, bundleNames) {
558 if (bundleName == nullptr || !cJSON_IsString(bundleName) || (bundleName->valuestring == nullptr)) {
559 cJSON_Delete(rootValue);
560 HILOGE("BundleNames term in CallerInfoExtra json is null or not string.");
561 return INVALID_PARAMETERS_ERR;
562 }
563 bundleNameList.push_back(bundleName->valuestring);
564 }
565 callerInfo_.bundleNames = bundleNameList;
566
567 cJSON *extraInfo = cJSON_GetObjectItemCaseSensitive(rootValue, "ExtraInfo");
568 if (extraInfo == nullptr || !cJSON_IsString(extraInfo) || (extraInfo->valuestring == nullptr)) {
569 cJSON_Delete(rootValue);
570 HILOGE("ExtraInfo term in CallerInfoExtra json is null or not string.");
571 return INVALID_PARAMETERS_ERR;
572 }
573 cJSON *extraInfoValue = cJSON_Parse(extraInfo->valuestring);
574 if (extraInfoValue == nullptr) {
575 cJSON_Delete(rootValue);
576 HILOGE("ExtraInfo term json string parse to cjson fail in CallerInfoExtra json.");
577 return INVALID_PARAMETERS_ERR;
578 }
579
580 cJSON *accessToken = cJSON_GetObjectItemCaseSensitive(extraInfoValue, EXTRO_INFO_JSON_KEY_ACCESS_TOKEN);
581 if (accessToken != nullptr && cJSON_IsNumber(accessToken)) {
582 callerInfo_.accessToken = static_cast<unsigned int>(accessToken->valueint);
583 }
584
585 cJSON *dmsVersion = cJSON_GetObjectItemCaseSensitive(extraInfoValue, DMS_VERSION_ID);
586 if (dmsVersion != nullptr && !cJSON_IsString(dmsVersion) && (dmsVersion->valuestring != nullptr)) {
587 callerInfo_.extraInfoJson[DMS_VERSION_ID] = dmsVersion->valuestring;
588 }
589 cJSON_Delete(extraInfoValue);
590 cJSON_Delete(rootValue);
591 return ERR_OK;
592 }
593
UnmarshalAccountInfo(const std::string & jsonStr)594 int32_t DSchedContinueDataCmd::UnmarshalAccountInfo(const std::string &jsonStr)
595 {
596 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
597 if (rootValue == nullptr) {
598 HILOGE("Account info json string parse to cjson fail.");
599 return INVALID_PARAMETERS_ERR;
600 }
601
602 cJSON *accountType = cJSON_GetObjectItemCaseSensitive(rootValue, "AccountType");
603 if (accountType == nullptr || !cJSON_IsNumber(accountType)) {
604 cJSON_Delete(rootValue);
605 HILOGE("AccountType term in account info json is null or not number.");
606 return INVALID_PARAMETERS_ERR;
607 }
608 accountInfo_.accountType = accountType->valueint;
609
610 cJSON *groupId = nullptr;
611 std::vector<std::string> groupIdList;
612 cJSON *groupIdListStr = cJSON_GetObjectItemCaseSensitive(rootValue, "groupIdList");
613 cJSON_ArrayForEach(groupId, groupIdListStr) {
614 if (groupId == nullptr || !cJSON_IsString(groupId) || (groupId->valuestring == nullptr)) {
615 cJSON_Delete(rootValue);
616 HILOGE("groupId term in account info json is null or not string.");
617 return INVALID_PARAMETERS_ERR;
618 }
619 groupIdList.push_back(groupId->valuestring);
620 }
621 accountInfo_.groupIdList = groupIdList;
622
623 cJSON *accountId = cJSON_GetObjectItemCaseSensitive(rootValue, Constants::EXTRO_INFO_JSON_KEY_ACCOUNT_ID.c_str());
624 if (accountId == nullptr || !cJSON_IsString(accountId)) {
625 HILOGE("accountId term in account info json is null or not string.");
626 } else {
627 accountInfo_.activeAccountId = accountId->valuestring;
628 }
629 cJSON *userId = cJSON_GetObjectItemCaseSensitive(rootValue, Constants::EXTRO_INFO_JSON_KEY_USERID_ID.c_str());
630 if (userId == nullptr || !cJSON_IsNumber(userId)) {
631 HILOGE("userId term in account info json is null or not number.");
632 } else {
633 accountInfo_.userId = userId->valueint;
634 }
635
636 cJSON_Delete(rootValue);
637 return ERR_OK;
638 }
639
Marshal(std::string & jsonStr)640 int32_t DSchedContinueReplyCmd::Marshal(std::string &jsonStr)
641 {
642 cJSON *rootValue = cJSON_CreateObject();
643 if (rootValue == nullptr) {
644 return INVALID_PARAMETERS_ERR;
645 }
646
647 std::string baseJsonStr;
648 if (DSchedContinueCmdBase::Marshal(baseJsonStr) != ERR_OK) {
649 cJSON_Delete(rootValue);
650 return INVALID_PARAMETERS_ERR;
651 }
652 cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str());
653
654 cJSON_AddNumberToObject(rootValue, "ReplyCmd", replyCmd_);
655 cJSON_AddNumberToObject(rootValue, "AppVersion", appVersion_);
656 cJSON_AddNumberToObject(rootValue, "Result", result_);
657 cJSON_AddStringToObject(rootValue, "Reason", reason_.c_str());
658
659 char *data = cJSON_Print(rootValue);
660 if (data == nullptr) {
661 cJSON_Delete(rootValue);
662 return INVALID_PARAMETERS_ERR;
663 }
664 jsonStr = std::string(data);
665 cJSON_Delete(rootValue);
666 cJSON_free(data);
667 return ERR_OK;
668 }
669
Unmarshal(const std::string & jsonStr)670 int32_t DSchedContinueReplyCmd::Unmarshal(const std::string &jsonStr)
671 {
672 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
673 if (rootValue == nullptr) {
674 return INVALID_PARAMETERS_ERR;
675 }
676
677 cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd");
678 if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) {
679 cJSON_Delete(rootValue);
680 return INVALID_PARAMETERS_ERR;
681 }
682 std::string baseCmdStr = baseCmd->valuestring;
683 if (DSchedContinueCmdBase::Unmarshal(baseCmdStr) != ERR_OK) {
684 cJSON_Delete(rootValue);
685 return INVALID_PARAMETERS_ERR;
686 }
687
688 const char *numKeys[] = {
689 "ReplyCmd", "AppVersion", "Result"
690 };
691 int32_t *numValues[] = {
692 &replyCmd_, &appVersion_, &result_
693 };
694 int32_t numLength = sizeof(numKeys) / sizeof(numKeys[0]);
695 for (int32_t i = 0; i < numLength; i++) {
696 cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, numKeys[i]);
697 if (item == nullptr || !cJSON_IsNumber(item)) {
698 cJSON_Delete(rootValue);
699 return INVALID_PARAMETERS_ERR;
700 }
701 *numValues[i] = item->valueint;
702 }
703
704 cJSON *reason = cJSON_GetObjectItemCaseSensitive(rootValue, "Reason");
705 if (reason == nullptr || !cJSON_IsString(reason) || (reason->valuestring == nullptr)) {
706 cJSON_Delete(rootValue);
707 return INVALID_PARAMETERS_ERR;
708 }
709 reason_ = reason->valuestring;
710
711 cJSON_Delete(rootValue);
712 return ERR_OK;
713 }
714
Marshal(std::string & jsonStr)715 int32_t DSchedContinueEndCmd::Marshal(std::string &jsonStr)
716 {
717 cJSON *rootValue = cJSON_CreateObject();
718 if (rootValue == nullptr) {
719 return INVALID_PARAMETERS_ERR;
720 }
721
722 std::string baseJsonStr;
723 if (DSchedContinueCmdBase::Marshal(baseJsonStr) != ERR_OK) {
724 cJSON_Delete(rootValue);
725 return INVALID_PARAMETERS_ERR;
726 }
727 cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str());
728
729 cJSON_AddNumberToObject(rootValue, "Result", result_);
730
731 char *data = cJSON_Print(rootValue);
732 if (data == nullptr) {
733 cJSON_Delete(rootValue);
734 return INVALID_PARAMETERS_ERR;
735 }
736 jsonStr = std::string(data);
737 cJSON_Delete(rootValue);
738 cJSON_free(data);
739 return ERR_OK;
740 }
741
Unmarshal(const std::string & jsonStr)742 int32_t DSchedContinueEndCmd::Unmarshal(const std::string &jsonStr)
743 {
744 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
745 if (rootValue == nullptr) {
746 return INVALID_PARAMETERS_ERR;
747 }
748
749 cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd");
750 if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) {
751 cJSON_Delete(rootValue);
752 return INVALID_PARAMETERS_ERR;
753 }
754 std::string baseCmdStr = baseCmd->valuestring;
755 if (DSchedContinueCmdBase::Unmarshal(baseCmdStr) != ERR_OK) {
756 cJSON_Delete(rootValue);
757 return INVALID_PARAMETERS_ERR;
758 }
759
760 cJSON *result = cJSON_GetObjectItemCaseSensitive(rootValue, "Result");
761 if (result == nullptr || !cJSON_IsNumber(result)) {
762 cJSON_Delete(rootValue);
763 return INVALID_PARAMETERS_ERR;
764 }
765 result_ = result->valueint;
766
767 cJSON_Delete(rootValue);
768 return ERR_OK;
769 }
770 } // namespace DistributedSchedule
771 } // namespace OHOS
772