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 delete wantPtr;
492 wantPtr = nullptr;
493 return true;
494 }
495
UnmarshalCallerInfo(const std::string & jsonStr)496 int32_t DSchedContinueDataCmd::UnmarshalCallerInfo(const std::string &jsonStr)
497 {
498 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
499 if (rootValue == nullptr) {
500 HILOGE("Caller info json string parse to cjson fail.");
501 return INVALID_PARAMETERS_ERR;
502 }
503
504 const char *strKeys[] = {
505 "SourceDeviceId", "CallerAppId"
506 };
507 std::string *strValues[] = {
508 &callerInfo_.sourceDeviceId, &callerInfo_.callerAppId
509 };
510 int32_t strLength = sizeof(strKeys) / sizeof(strKeys[0]);
511 for (int32_t i = 0; i < strLength; i++) {
512 cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, strKeys[i]);
513 if (item == nullptr || !cJSON_IsString(item) || (item->valuestring == nullptr)) {
514 cJSON_Delete(rootValue);
515 HILOGE("Caller info json %{public}s term is null or not string.", strKeys[i]);
516 return INVALID_PARAMETERS_ERR;
517 }
518 *strValues[i] = item->valuestring;
519 }
520
521 const char *numKeys[] = {
522 "Uid", "Pid", "CallerType", "Duid"
523 };
524 int32_t *numValues[] = {
525 &callerInfo_.uid, &callerInfo_.pid, &callerInfo_.callerType, &callerInfo_.duid
526 };
527 int32_t numLength = sizeof(numKeys) / sizeof(numKeys[0]);
528 for (int32_t i = 0; i < numLength; i++) {
529 cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, numKeys[i]);
530 if (item == nullptr || !cJSON_IsNumber(item)) {
531 cJSON_Delete(rootValue);
532 HILOGE("Caller info json %{public}s term is null or not number.", numKeys[i]);
533 return INVALID_PARAMETERS_ERR;
534 }
535 *numValues[i] = item->valueint;
536 }
537
538 if (UnmarshalCallerInfoExtra(jsonStr) != ERR_OK) {
539 cJSON_Delete(rootValue);
540 HILOGE("Unmarshal CallerInfoExtra term from caller info json string fail.");
541 return INVALID_PARAMETERS_ERR;
542 }
543
544 cJSON_Delete(rootValue);
545 return ERR_OK;
546 }
547
UnmarshalCallerInfoExtra(const std::string & jsonStr)548 int32_t DSchedContinueDataCmd::UnmarshalCallerInfoExtra(const std::string &jsonStr)
549 {
550 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
551 if (rootValue == nullptr) {
552 HILOGE("Caller info extra json string parse to cjson fail.");
553 return INVALID_PARAMETERS_ERR;
554 }
555
556 cJSON *bundleName = nullptr;
557 std::vector<std::string> bundleNameList;
558 cJSON *bundleNames = cJSON_GetObjectItemCaseSensitive(rootValue, "BundleNames");
559 cJSON_ArrayForEach(bundleName, bundleNames) {
560 if (bundleName == nullptr || !cJSON_IsString(bundleName) || (bundleName->valuestring == nullptr)) {
561 cJSON_Delete(rootValue);
562 HILOGE("BundleNames term in CallerInfoExtra json is null or not string.");
563 return INVALID_PARAMETERS_ERR;
564 }
565 bundleNameList.push_back(bundleName->valuestring);
566 }
567 callerInfo_.bundleNames = bundleNameList;
568
569 cJSON *extraInfo = cJSON_GetObjectItemCaseSensitive(rootValue, "ExtraInfo");
570 if (extraInfo == nullptr || !cJSON_IsString(extraInfo) || (extraInfo->valuestring == nullptr)) {
571 cJSON_Delete(rootValue);
572 HILOGE("ExtraInfo term in CallerInfoExtra json is null or not string.");
573 return INVALID_PARAMETERS_ERR;
574 }
575 cJSON *extraInfoValue = cJSON_Parse(extraInfo->valuestring);
576 if (extraInfoValue == nullptr) {
577 cJSON_Delete(rootValue);
578 HILOGE("ExtraInfo term json string parse to cjson fail in CallerInfoExtra json.");
579 return INVALID_PARAMETERS_ERR;
580 }
581
582 cJSON *accessToken = cJSON_GetObjectItemCaseSensitive(extraInfoValue, EXTRO_INFO_JSON_KEY_ACCESS_TOKEN);
583 if (accessToken != nullptr && cJSON_IsNumber(accessToken)) {
584 callerInfo_.accessToken = static_cast<unsigned int>(accessToken->valueint);
585 }
586
587 cJSON *dmsVersion = cJSON_GetObjectItemCaseSensitive(extraInfoValue, DMS_VERSION_ID);
588 if (dmsVersion != nullptr && cJSON_IsNumber(dmsVersion)) {
589 HILOGI("get dmsVersion information: %{public}d", dmsVersion->valueint);
590 callerInfo_.extraInfoJson[DMS_VERSION_ID] = dmsVersion->valueint;
591 }
592 cJSON_Delete(extraInfoValue);
593 cJSON_Delete(rootValue);
594 return ERR_OK;
595 }
596
UnmarshalAccountInfo(const std::string & jsonStr)597 int32_t DSchedContinueDataCmd::UnmarshalAccountInfo(const std::string &jsonStr)
598 {
599 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
600 if (rootValue == nullptr) {
601 HILOGE("Account info json string parse to cjson fail.");
602 return INVALID_PARAMETERS_ERR;
603 }
604
605 cJSON *accountType = cJSON_GetObjectItemCaseSensitive(rootValue, "AccountType");
606 if (accountType == nullptr || !cJSON_IsNumber(accountType)) {
607 cJSON_Delete(rootValue);
608 HILOGE("AccountType term in account info json is null or not number.");
609 return INVALID_PARAMETERS_ERR;
610 }
611 accountInfo_.accountType = accountType->valueint;
612
613 cJSON *groupId = nullptr;
614 std::vector<std::string> groupIdList;
615 cJSON *groupIdListStr = cJSON_GetObjectItemCaseSensitive(rootValue, "groupIdList");
616 cJSON_ArrayForEach(groupId, groupIdListStr) {
617 if (groupId == nullptr || !cJSON_IsString(groupId) || (groupId->valuestring == nullptr)) {
618 cJSON_Delete(rootValue);
619 HILOGE("groupId term in account info json is null or not string.");
620 return INVALID_PARAMETERS_ERR;
621 }
622 groupIdList.push_back(groupId->valuestring);
623 }
624 accountInfo_.groupIdList = groupIdList;
625
626 cJSON *accountId = cJSON_GetObjectItemCaseSensitive(rootValue, Constants::EXTRO_INFO_JSON_KEY_ACCOUNT_ID.c_str());
627 if (accountId == nullptr || !cJSON_IsString(accountId)) {
628 HILOGE("accountId term in account info json is null or not string.");
629 } else {
630 accountInfo_.activeAccountId = accountId->valuestring;
631 }
632 cJSON *userId = cJSON_GetObjectItemCaseSensitive(rootValue, Constants::EXTRO_INFO_JSON_KEY_USERID_ID.c_str());
633 if (userId == nullptr || !cJSON_IsNumber(userId)) {
634 HILOGE("userId term in account info json is null or not number.");
635 } else {
636 accountInfo_.userId = userId->valueint;
637 }
638
639 cJSON_Delete(rootValue);
640 return ERR_OK;
641 }
642
Marshal(std::string & jsonStr)643 int32_t DSchedContinueReplyCmd::Marshal(std::string &jsonStr)
644 {
645 cJSON *rootValue = cJSON_CreateObject();
646 if (rootValue == nullptr) {
647 return INVALID_PARAMETERS_ERR;
648 }
649
650 std::string baseJsonStr;
651 if (DSchedContinueCmdBase::Marshal(baseJsonStr) != ERR_OK) {
652 cJSON_Delete(rootValue);
653 return INVALID_PARAMETERS_ERR;
654 }
655 cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str());
656
657 cJSON_AddNumberToObject(rootValue, "ReplyCmd", replyCmd_);
658 cJSON_AddNumberToObject(rootValue, "AppVersion", appVersion_);
659 cJSON_AddNumberToObject(rootValue, "Result", result_);
660 cJSON_AddStringToObject(rootValue, "Reason", reason_.c_str());
661
662 char *data = cJSON_Print(rootValue);
663 if (data == nullptr) {
664 cJSON_Delete(rootValue);
665 return INVALID_PARAMETERS_ERR;
666 }
667 jsonStr = std::string(data);
668 cJSON_Delete(rootValue);
669 cJSON_free(data);
670 return ERR_OK;
671 }
672
Unmarshal(const std::string & jsonStr)673 int32_t DSchedContinueReplyCmd::Unmarshal(const std::string &jsonStr)
674 {
675 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
676 if (rootValue == nullptr) {
677 return INVALID_PARAMETERS_ERR;
678 }
679
680 cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd");
681 if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) {
682 cJSON_Delete(rootValue);
683 return INVALID_PARAMETERS_ERR;
684 }
685 std::string baseCmdStr = baseCmd->valuestring;
686 if (DSchedContinueCmdBase::Unmarshal(baseCmdStr) != ERR_OK) {
687 cJSON_Delete(rootValue);
688 return INVALID_PARAMETERS_ERR;
689 }
690
691 const char *numKeys[] = {
692 "ReplyCmd", "AppVersion", "Result"
693 };
694 int32_t *numValues[] = {
695 &replyCmd_, &appVersion_, &result_
696 };
697 int32_t numLength = sizeof(numKeys) / sizeof(numKeys[0]);
698 for (int32_t i = 0; i < numLength; i++) {
699 cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, numKeys[i]);
700 if (item == nullptr || !cJSON_IsNumber(item)) {
701 cJSON_Delete(rootValue);
702 return INVALID_PARAMETERS_ERR;
703 }
704 *numValues[i] = item->valueint;
705 }
706
707 cJSON *reason = cJSON_GetObjectItemCaseSensitive(rootValue, "Reason");
708 if (reason == nullptr || !cJSON_IsString(reason) || (reason->valuestring == nullptr)) {
709 cJSON_Delete(rootValue);
710 return INVALID_PARAMETERS_ERR;
711 }
712 reason_ = reason->valuestring;
713
714 cJSON_Delete(rootValue);
715 return ERR_OK;
716 }
717
Marshal(std::string & jsonStr)718 int32_t DSchedContinueEndCmd::Marshal(std::string &jsonStr)
719 {
720 cJSON *rootValue = cJSON_CreateObject();
721 if (rootValue == nullptr) {
722 return INVALID_PARAMETERS_ERR;
723 }
724
725 std::string baseJsonStr;
726 if (DSchedContinueCmdBase::Marshal(baseJsonStr) != ERR_OK) {
727 cJSON_Delete(rootValue);
728 return INVALID_PARAMETERS_ERR;
729 }
730 cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str());
731
732 cJSON_AddNumberToObject(rootValue, "Result", result_);
733
734 char *data = cJSON_Print(rootValue);
735 if (data == nullptr) {
736 cJSON_Delete(rootValue);
737 return INVALID_PARAMETERS_ERR;
738 }
739 jsonStr = std::string(data);
740 cJSON_Delete(rootValue);
741 cJSON_free(data);
742 return ERR_OK;
743 }
744
Unmarshal(const std::string & jsonStr)745 int32_t DSchedContinueEndCmd::Unmarshal(const std::string &jsonStr)
746 {
747 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
748 if (rootValue == nullptr) {
749 return INVALID_PARAMETERS_ERR;
750 }
751
752 cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd");
753 if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) {
754 cJSON_Delete(rootValue);
755 return INVALID_PARAMETERS_ERR;
756 }
757 std::string baseCmdStr = baseCmd->valuestring;
758 if (DSchedContinueCmdBase::Unmarshal(baseCmdStr) != ERR_OK) {
759 cJSON_Delete(rootValue);
760 return INVALID_PARAMETERS_ERR;
761 }
762
763 cJSON *result = cJSON_GetObjectItemCaseSensitive(rootValue, "Result");
764 if (result == nullptr || !cJSON_IsNumber(result)) {
765 cJSON_Delete(rootValue);
766 return INVALID_PARAMETERS_ERR;
767 }
768 result_ = result->valueint;
769
770 cJSON_Delete(rootValue);
771 return ERR_OK;
772 }
773 } // namespace DistributedSchedule
774 } // namespace OHOS
775