• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "dsched_collab_event.h"
17 
18 #include "cJSON.h"
19 #include "distributed_sched_utils.h"
20 #include "dms_constant.h"
21 #include "dtbschedmgr_log.h"
22 #include "parcel.h"
23 
24 namespace OHOS {
25 namespace DistributedSchedule {
26 namespace {
27 const std::string TAG = "DSchedCollabCmd";
28 const char* EXTRO_INFO_JSON_KEY_ACCESS_TOKEN = "accessTokenID";
29 const char* DMS_VERSION_ID = "dmsVersion";
30 }
31 
Marshal(std::string & jsonStr)32 int32_t BaseCmd::Marshal(std::string &jsonStr)
33 {
34     cJSON *rootValue = cJSON_CreateObject();
35     if (rootValue == nullptr) {
36         return INVALID_PARAMETERS_ERR;
37     }
38     cJSON_AddNumberToObject(rootValue, "Command", command_);
39     cJSON_AddNumberToObject(rootValue, "CollabVersion", collabVersion_);
40     cJSON_AddNumberToObject(rootValue, "DmsVersion", dmsVersion_);
41     cJSON_AddNumberToObject(rootValue, "CollabSessionId", srcCollabSessionId_);
42     cJSON_AddStringToObject(rootValue, "CollabToken", collabToken_.c_str());
43     cJSON_AddStringToObject(rootValue, "SrcDeviceId", srcDeviceId_.c_str());
44     cJSON_AddStringToObject(rootValue, "SrcBundleName", srcBundleName_.c_str());
45     cJSON_AddStringToObject(rootValue, "SrcAbilityName", srcAbilityName_.c_str());
46     cJSON_AddStringToObject(rootValue, "SrcModuleName", srcModuleName_.c_str());
47     cJSON_AddStringToObject(rootValue, "SrcServiceId", srcServerId_.c_str());
48     cJSON_AddStringToObject(rootValue, "SinkDeviceId", sinkDeviceId_.c_str());
49     cJSON_AddStringToObject(rootValue, "SinkBundleName", sinkBundleName_.c_str());
50     cJSON_AddStringToObject(rootValue, "SinkAbilityName", sinkAbilityName_.c_str());
51     cJSON_AddStringToObject(rootValue, "SinkModuleName", sinkModuleName_.c_str());
52     cJSON_AddStringToObject(rootValue, "SinkServiceId", sinkServerId_.c_str());
53 
54     cJSON_AddBoolToObject(rootValue, "NeedSendBigData", needSendBigData_);
55     cJSON_AddBoolToObject(rootValue, "NeedSendStream_", needSendStream_);
56     cJSON_AddBoolToObject(rootValue, "NeedRecvStream", needRecvStream_);
57 
58     char *data = cJSON_Print(rootValue);
59     if (data == nullptr) {
60         cJSON_Delete(rootValue);
61         HILOGE("cJSON_Print failed.");
62         return INVALID_PARAMETERS_ERR;
63     }
64     jsonStr = std::string(data);
65     cJSON_Delete(rootValue);
66     cJSON_free(data);
67     return ERR_OK;
68 }
69 
Unmarshal(const std::string & jsonStr)70 int32_t BaseCmd::Unmarshal(const std::string &jsonStr)
71 {
72     cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
73     if (rootValue == nullptr) {
74         HILOGE("Dms collab cmd base json string parse to cjson fail.");
75         return INVALID_PARAMETERS_ERR;
76     }
77 
78     const char *numKeys[] = { "Command", "CollabVersion", "DmsVersion", "CollabSessionId" };
79     int32_t *numValues[] = { &command_, &collabVersion_, &dmsVersion_, &srcCollabSessionId_ };
80     int32_t numLength = sizeof(numKeys) / sizeof(numKeys[0]);
81     for (int32_t i = 0; i < numLength; i++) {
82         cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, numKeys[i]);
83         if (!cJSON_IsNumber(item)) {
84             cJSON_Delete(rootValue);
85             HILOGE("Dms collab cmd base %{public}s term is null or not number.", numKeys[i]);
86             return INVALID_PARAMETERS_ERR;
87         }
88         *numValues[i] = item->valueint;
89     }
90 
91     const char *strKeys[] = { "CollabToken", "SrcDeviceId", "SrcBundleName", "SrcAbilityName", "SrcModuleName",
92         "SrcServiceId", "SinkDeviceId", "SinkBundleName", "SinkAbilityName", "SinkModuleName", "SinkServiceId" };
93     std::string *strValues[] = {&collabToken_, &srcDeviceId_, &srcBundleName_, &srcAbilityName_, &srcModuleName_,
94         &srcServerId_, &sinkDeviceId_, &sinkBundleName_, &sinkAbilityName_, &sinkModuleName_, &sinkServerId_ };
95     int32_t strLength = sizeof(strKeys) / sizeof(strKeys[0]);
96     for (int32_t i = 0; i < strLength; i++) {
97         cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, strKeys[i]);
98         if (!cJSON_IsString(item) || (item->valuestring == nullptr)) {
99             cJSON_Delete(rootValue);
100             HILOGE("Dms collab cmd base %{public}s term is null or not string.", strKeys[i]);
101             return INVALID_PARAMETERS_ERR;
102         }
103         *strValues[i] = item->valuestring;
104     }
105 
106     const char *boolKeys[] = { "NeedSendBigData", "NeedSendStream_", "NeedRecvStream" };
107     bool *boolValues[] = { &needSendBigData_, &needSendStream_, &needRecvStream_ };
108     int32_t boolLength = sizeof(boolKeys) / sizeof(boolKeys[0]);
109     for (int32_t i = 0; i < boolLength; i++) {
110         cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, boolKeys[i]);
111         if (!cJSON_IsBool(item)) {
112             cJSON_Delete(rootValue);
113             HILOGE("Dms collab cmd base %{public}s term is null or not number.", boolKeys[i]);
114             return INVALID_PARAMETERS_ERR;
115         }
116         *boolValues[i] = item->valueint;
117     }
118     cJSON_Delete(rootValue);
119     return ERR_OK;
120 }
121 
Marshal(std::string & jsonStr)122 int32_t GetSinkCollabVersionCmd::Marshal(std::string &jsonStr)
123 {
124     HILOGD("called");
125     cJSON *rootValue = cJSON_CreateObject();
126     if (rootValue == nullptr) {
127         return INVALID_PARAMETERS_ERR;
128     }
129 
130     std::string baseJsonStr;
131     if (BaseCmd::Marshal(baseJsonStr) != ERR_OK) {
132         cJSON_Delete(rootValue);
133         return INVALID_PARAMETERS_ERR;
134     }
135     cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str());
136     cJSON_AddNumberToObject(rootValue, "SrcPid", srcPid_);
137     cJSON_AddNumberToObject(rootValue, "SrcUid", srcUid_);
138     cJSON_AddNumberToObject(rootValue, "SrcAccessToken", srcAccessToken_);
139     cJSON_AddNumberToObject(rootValue, "SinkCollabVersion", sinkCollabVersion_);
140 
141     char *data = cJSON_Print(rootValue);
142     if (data == nullptr) {
143         cJSON_Delete(rootValue);
144         return INVALID_PARAMETERS_ERR;
145     }
146     jsonStr = std::string(data);
147     cJSON_Delete(rootValue);
148     cJSON_free(data);
149     HILOGD("end");
150     return ERR_OK;
151 }
152 
Unmarshal(const std::string & jsonStr)153 int32_t GetSinkCollabVersionCmd::Unmarshal(const std::string &jsonStr)
154 {
155     HILOGD("called");
156     cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
157     if (rootValue == nullptr) {
158         return INVALID_PARAMETERS_ERR;
159     }
160     cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd");
161     if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) {
162         cJSON_Delete(rootValue);
163         return INVALID_PARAMETERS_ERR;
164     }
165     std::string baseCmdStr = baseCmd->valuestring;
166     if (BaseCmd::Unmarshal(baseCmdStr) != ERR_OK) {
167         cJSON_Delete(rootValue);
168         return INVALID_PARAMETERS_ERR;
169     }
170     const char *numKeys[] = { "SrcPid", "SrcUid", "SrcAccessToken", "SinkCollabVersion" };
171     int32_t *numValues[] = { &srcPid_, &srcUid_, &srcAccessToken_, &sinkCollabVersion_ };
172     int32_t numLength = sizeof(numKeys) / sizeof(numKeys[0]);
173     for (int32_t i = 0; i < numLength; i++) {
174         cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, numKeys[i]);
175         if (item == nullptr || !cJSON_IsNumber(item)) {
176             cJSON_Delete(rootValue);
177             HILOGE("Dms collab cmd base %{public}s term is null or not number.", numKeys[i]);
178             return INVALID_PARAMETERS_ERR;
179         }
180         *numValues[i] = item->valueint;
181     }
182     cJSON_Delete(rootValue);
183     HILOGD("end");
184     return ERR_OK;
185 }
186 
Marshal(std::string & jsonStr)187 int32_t SinkStartCmd::Marshal(std::string &jsonStr)
188 {
189     cJSON *rootValue = cJSON_CreateObject();
190     if (rootValue == nullptr) {
191         return INVALID_PARAMETERS_ERR;
192     }
193     std::string baseJsonStr;
194     if (BaseCmd::Marshal(baseJsonStr) != ERR_OK) {
195         cJSON_Delete(rootValue);
196         return INVALID_PARAMETERS_ERR;
197     }
198     cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str());
199     cJSON_AddNumberToObject(rootValue, "AppVersion", appVersion_);
200     cJSON_AddNumberToObject(rootValue, "SrcPid", srcPid_);
201     cJSON_AddNumberToObject(rootValue, "SrcUid", srcUid_);
202     cJSON_AddNumberToObject(rootValue, "SrcAccessToken", srcAccessToken_);
203     Parcel parcel;
204     if (!startParams_.Marshalling(parcel)) {
205         cJSON_Delete(rootValue);
206         return INVALID_PARAMETERS_ERR;
207     }
208     std::string startParamsStr = ParcelToBase64Str(parcel);
209     cJSON_AddStringToObject(rootValue, "StartParams", startParamsStr.c_str());
210     if (!messageParams_.Marshalling(parcel)) {
211         cJSON_Delete(rootValue);
212         return INVALID_PARAMETERS_ERR;
213     }
214     std::string wantParamsStr = ParcelToBase64Str(parcel);
215     cJSON_AddStringToObject(rootValue, "WantParams", wantParamsStr.c_str());
216     std::string callerInfoStr;
217     if (MarshalCallerInfo(callerInfoStr) != ERR_OK) {
218         cJSON_Delete(rootValue);
219         return INVALID_PARAMETERS_ERR;
220     }
221     cJSON_AddStringToObject(rootValue, "CallerInfo", callerInfoStr.c_str());
222     std::string accountInfoStr;
223     if (MarshalAccountInfo(accountInfoStr) != ERR_OK) {
224         cJSON_Delete(rootValue);
225         return INVALID_PARAMETERS_ERR;
226     }
227     cJSON_AddStringToObject(rootValue, "AccountInfo", accountInfoStr.c_str());
228     char *data = cJSON_Print(rootValue);
229     if (data == nullptr) {
230         cJSON_Delete(rootValue);
231         return INVALID_PARAMETERS_ERR;
232     }
233     jsonStr = std::string(data);
234     cJSON_Delete(rootValue);
235     cJSON_free(data);
236     return ERR_OK;
237 }
238 
Unmarshal(const std::string & jsonStr)239 int32_t SinkStartCmd::Unmarshal(const std::string &jsonStr)
240 {
241     HILOGD("called");
242     cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
243     if (rootValue == nullptr) {
244         return INVALID_PARAMETERS_ERR;
245     }
246     cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd");
247     if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) {
248         cJSON_Delete(rootValue);
249         return INVALID_PARAMETERS_ERR;
250     }
251     std::string baseCmdStr = baseCmd->valuestring;
252     if (BaseCmd::Unmarshal(baseCmdStr) != ERR_OK) {
253         cJSON_Delete(rootValue);
254         return INVALID_PARAMETERS_ERR;
255     }
256     const char *numKeys[] = { "AppVersion", "SrcPid", "SrcUid", "SrcAccessToken" };
257     int32_t *numValues[] = { &appVersion_, &srcPid_, &srcUid_, &srcAccessToken_ };
258     int32_t numLength = sizeof(numKeys) / sizeof(numKeys[0]);
259     for (int32_t i = 0; i < numLength; i++) {
260         cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, numKeys[i]);
261         if (!cJSON_IsNumber(item)) {
262             cJSON_Delete(rootValue);
263             HILOGE("%{public}s term is null or not number.", numKeys[i]);
264             return INVALID_PARAMETERS_ERR;
265         }
266         *numValues[i] = item->valueint;
267     }
268 
269     int32_t ret =  UnmarshalPartParcel(rootValue);
270     if (rootValue != nullptr) {
271         cJSON_Delete(rootValue);
272     }
273     return ret;
274 }
275 
UnmarshalPartParcel(cJSON * rootValue)276 int32_t SinkStartCmd::UnmarshalPartParcel(cJSON *rootValue)
277 {
278     if (UnmarshalOptParams(rootValue) != ERR_OK) {
279         HILOGE("get callerInfoJson failed.");
280         return INVALID_PARAMETERS_ERR;
281     }
282 
283     cJSON *callerInfoJson = cJSON_GetObjectItemCaseSensitive(rootValue, "CallerInfo");
284     if (callerInfoJson == nullptr || !cJSON_IsString(callerInfoJson) || (callerInfoJson->valuestring == nullptr)) {
285         HILOGE("get callerInfoJson failed.");
286         return INVALID_PARAMETERS_ERR;
287     }
288     std::string callerInfoStr = callerInfoJson->valuestring;
289     if (UnmarshalCallerInfo(callerInfoStr) != ERR_OK) {
290         HILOGE("unmarshal callerInfo failed.");
291         return INVALID_PARAMETERS_ERR;
292     }
293     cJSON *accountInfoJson = cJSON_GetObjectItemCaseSensitive(rootValue, "AccountInfo");
294     if (accountInfoJson == nullptr || !cJSON_IsString(accountInfoJson) || (accountInfoJson->valuestring == nullptr)) {
295         HILOGE("get accountInfoJson failed.");
296         return INVALID_PARAMETERS_ERR;
297     }
298     std::string accountInfoStr = accountInfoJson->valuestring;
299     if (UnmarshalAccountInfo(accountInfoStr) != ERR_OK) {
300         HILOGE("unmarshal accountInfo failed.");
301         return INVALID_PARAMETERS_ERR;
302     }
303     return ERR_OK;
304 }
305 
UnmarshalOptParams(cJSON * rootValue)306 int32_t SinkStartCmd::UnmarshalOptParams(cJSON *rootValue)
307 {
308     cJSON *startParamsJson = cJSON_GetObjectItemCaseSensitive(rootValue, "StartParams");
309     if (startParamsJson == nullptr || !cJSON_IsString(startParamsJson) || (startParamsJson->valuestring == nullptr)) {
310         HILOGE("get startParamsJson failed.");
311         return INVALID_PARAMETERS_ERR;
312     }
313     Parcel parcel;
314     int32_t ret = Base64StrToParcel(startParamsJson->valuestring, parcel);
315     if (ret != ERR_OK) {
316         HILOGE("base64StrToParcel failed.");
317         return INVALID_PARAMETERS_ERR;
318     }
319     auto startParamsPtr = AAFwk::WantParams::Unmarshalling(parcel);
320     if (startParamsPtr == nullptr) {
321         HILOGE("startParamsJson unmarshalling failed.");
322         return INVALID_PARAMETERS_ERR;
323     }
324     startParams_ = *startParamsPtr;
325 
326     cJSON *wantParamsJson = cJSON_GetObjectItemCaseSensitive(rootValue, "WantParams");
327     if (wantParamsJson == nullptr || !cJSON_IsString(wantParamsJson) || (wantParamsJson->valuestring == nullptr)) {
328         HILOGE("get wantParamsJson failed.");
329         return INVALID_PARAMETERS_ERR;
330     }
331     ret = Base64StrToParcel(wantParamsJson->valuestring, parcel);
332     if (ret != ERR_OK) {
333         HILOGE("base64StrToParcel failed.");
334         return INVALID_PARAMETERS_ERR;
335     }
336     auto wantParamsPtr = AAFwk::WantParams::Unmarshalling(parcel);
337     if (wantParamsPtr == nullptr) {
338         HILOGE("wantParamsJson unmarshalling failed.");
339         return INVALID_PARAMETERS_ERR;
340     }
341     messageParams_ = *wantParamsPtr;
342     return ERR_OK;
343 }
344 
MarshalCallerInfo(std::string & jsonStr)345 int32_t SinkStartCmd::MarshalCallerInfo(std::string &jsonStr)
346 {
347     HILOGD("called");
348     cJSON *callerInfoJson = cJSON_CreateObject();
349     if (callerInfoJson == nullptr) {
350         return INVALID_PARAMETERS_ERR;
351     }
352     cJSON_AddNumberToObject(callerInfoJson, "Uid", callerInfo_.uid);
353     cJSON_AddNumberToObject(callerInfoJson, "Pid", callerInfo_.pid);
354     cJSON_AddNumberToObject(callerInfoJson, "CallerType", callerInfo_.callerType);
355     cJSON_AddStringToObject(callerInfoJson, "SourceDeviceId", callerInfo_.sourceDeviceId.c_str());
356     cJSON_AddNumberToObject(callerInfoJson, "Duid", callerInfo_.duid);
357     cJSON_AddStringToObject(callerInfoJson, "CallerAppId", callerInfo_.callerAppId.c_str());
358 
359     const auto bundleNamesSize = static_cast<int32_t>(callerInfo_.bundleNames.size());
360     cJSON *bundleNames = cJSON_CreateArray();
361     if (bundleNames == nullptr) {
362         cJSON_Delete(callerInfoJson);
363         return INVALID_PARAMETERS_ERR;
364     }
365     for (auto i = 0; i < bundleNamesSize; i++) {
366         cJSON *bundleName = cJSON_CreateString(callerInfo_.bundleNames[i].c_str());
367         if (bundleName == nullptr) {
368             cJSON_Delete(callerInfoJson);
369             cJSON_Delete(bundleNames);
370             return INVALID_PARAMETERS_ERR;
371         }
372         cJSON_AddItemToArray(bundleNames, bundleName);
373     }
374     cJSON_AddItemToObject(callerInfoJson, "BundleNames", bundleNames);
375 
376     std::string extraInfo = callerInfo_.extraInfoJson.dump();
377     cJSON_AddStringToObject(callerInfoJson, "ExtraInfo", extraInfo.c_str());
378 
379     char *data = cJSON_Print(callerInfoJson);
380     if (data == nullptr) {
381         cJSON_Delete(callerInfoJson);
382         return INVALID_PARAMETERS_ERR;
383     }
384     jsonStr = std::string(data);
385     cJSON_Delete(callerInfoJson);
386     cJSON_free(data);
387     HILOGD("end");
388     return ERR_OK;
389 }
390 
MarshalAccountInfo(std::string & jsonStr)391 int32_t SinkStartCmd::MarshalAccountInfo(std::string &jsonStr)
392 {
393     HILOGD("called");
394     cJSON *accountInfoJson = cJSON_CreateObject();
395     if (accountInfoJson == nullptr) {
396         return INVALID_PARAMETERS_ERR;
397     }
398 
399     cJSON_AddNumberToObject(accountInfoJson, "AccountType", accountInfo_.accountType);
400 
401     const auto groupIdListSize = static_cast<int32_t>(accountInfo_.groupIdList.size());
402     cJSON *groupIdList = cJSON_CreateArray();
403     if (groupIdList == nullptr) {
404         cJSON_Delete(accountInfoJson);
405         return INVALID_PARAMETERS_ERR;
406     }
407     for (auto i = 0; i < groupIdListSize; i++) {
408         cJSON *groupId = cJSON_CreateString(accountInfo_.groupIdList[i].c_str());
409         if (groupId == nullptr) {
410             cJSON_Delete(accountInfoJson);
411             cJSON_Delete(groupIdList);
412             return INVALID_PARAMETERS_ERR;
413         }
414         cJSON_AddItemToArray(groupIdList, groupId);
415     }
416     cJSON_AddItemToObject(accountInfoJson, "GroupIdList", groupIdList);
417 
418     cJSON_AddStringToObject(accountInfoJson, Constants::EXTRO_INFO_JSON_KEY_ACCOUNT_ID.c_str(),
419         accountInfo_.activeAccountId.c_str());
420     cJSON_AddNumberToObject(accountInfoJson, Constants::EXTRO_INFO_JSON_KEY_USERID_ID.c_str(), accountInfo_.userId);
421 
422     char *data = cJSON_Print(accountInfoJson);
423     if (data == nullptr) {
424         cJSON_Delete(accountInfoJson);
425         return INVALID_PARAMETERS_ERR;
426     }
427     jsonStr = std::string(data);
428     cJSON_Delete(accountInfoJson);
429     cJSON_free(data);
430     HILOGD("end");
431     return ERR_OK;
432 }
433 
UnmarshalCallerInfo(std::string & jsonStr)434 int32_t SinkStartCmd::UnmarshalCallerInfo(std::string &jsonStr)
435 {
436     HILOGD("called");
437     cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
438     if (rootValue == nullptr) {
439         HILOGE("Caller info json string parse to cjson fail.");
440         return INVALID_PARAMETERS_ERR;
441     }
442 
443     const char *strKeys[] = { "SourceDeviceId", "CallerAppId" };
444     std::string *strValues[] = {
445         &callerInfo_.sourceDeviceId, &callerInfo_.callerAppId
446     };
447     int32_t strLength = sizeof(strKeys) / sizeof(strKeys[0]);
448     for (int32_t i = 0; i < strLength; i++) {
449         cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, strKeys[i]);
450         if (!cJSON_IsString(item) || (item->valuestring == nullptr)) {
451             cJSON_Delete(rootValue);
452             HILOGE("Caller info json %{public}s term is null or not string.", strKeys[i]);
453             return INVALID_PARAMETERS_ERR;
454         }
455         *strValues[i] = item->valuestring;
456     }
457 
458     const char *numKeys[] = { "Uid", "Pid", "CallerType", "Duid" };
459     int32_t *numValues[] = {
460         &callerInfo_.uid, &callerInfo_.pid, &callerInfo_.callerType, &callerInfo_.duid
461     };
462     int32_t numLength = sizeof(numKeys) / sizeof(numKeys[0]);
463     for (int32_t i = 0; i < numLength; i++) {
464         cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, numKeys[i]);
465         if (!cJSON_IsNumber(item)) {
466             cJSON_Delete(rootValue);
467             HILOGE("Caller info json %{public}s term is null or not number.", numKeys[i]);
468             return INVALID_PARAMETERS_ERR;
469         }
470         *numValues[i] = item->valueint;
471     }
472 
473     if (UnmarshalCallerInfoExtra(jsonStr) != ERR_OK) {
474         cJSON_Delete(rootValue);
475         HILOGE("Unmarshal CallerInfoExtra term from caller info json string fail.");
476         return INVALID_PARAMETERS_ERR;
477     }
478 
479     cJSON_Delete(rootValue);
480     HILOGD("end");
481     return ERR_OK;
482 }
483 
UnmarshalCallerInfoExtra(std::string & jsonStr)484 int32_t SinkStartCmd::UnmarshalCallerInfoExtra(std::string &jsonStr)
485 {
486     HILOGD("called");
487     cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
488     if (rootValue == nullptr) {
489         HILOGE("Caller info extra json string parse to cjson fail.");
490         return INVALID_PARAMETERS_ERR;
491     }
492 
493     cJSON *bundleName = nullptr;
494     std::vector<std::string> bundleNameList;
495     cJSON *bundleNames = cJSON_GetObjectItemCaseSensitive(rootValue, "BundleNames");
496     cJSON_ArrayForEach(bundleName, bundleNames) {
497         if (bundleName == nullptr || !cJSON_IsString(bundleName) || (bundleName->valuestring == nullptr)) {
498             cJSON_Delete(rootValue);
499             HILOGE("BundleNames term in CallerInfoExtra json is null or not string.");
500             return INVALID_PARAMETERS_ERR;
501         }
502         bundleNameList.push_back(bundleName->valuestring);
503     }
504     callerInfo_.bundleNames = bundleNameList;
505 
506     cJSON *extraInfo = cJSON_GetObjectItemCaseSensitive(rootValue, "ExtraInfo");
507     if (extraInfo == nullptr || !cJSON_IsString(extraInfo) || (extraInfo->valuestring == nullptr)) {
508         cJSON_Delete(rootValue);
509         HILOGE("ExtraInfo term in CallerInfoExtra json is null or not string.");
510         return INVALID_PARAMETERS_ERR;
511     }
512     cJSON *extraInfoValue = cJSON_Parse(extraInfo->valuestring);
513     if (extraInfoValue == nullptr) {
514         cJSON_Delete(rootValue);
515         HILOGE("ExtraInfo term json string parse to cjson fail in CallerInfoExtra json.");
516         return INVALID_PARAMETERS_ERR;
517     }
518 
519     cJSON *accessToken = cJSON_GetObjectItemCaseSensitive(extraInfoValue, EXTRO_INFO_JSON_KEY_ACCESS_TOKEN);
520     if (accessToken != nullptr && cJSON_IsNumber(accessToken)) {
521         callerInfo_.accessToken = static_cast<unsigned int>(accessToken->valueint);
522     }
523 
524     cJSON *dmsVersion = cJSON_GetObjectItemCaseSensitive(extraInfoValue, DMS_VERSION_ID);
525     if (dmsVersion != nullptr && cJSON_IsNumber(dmsVersion)) {
526         HILOGI("get dmsVersion information: %{public}d", dmsVersion->valueint);
527         callerInfo_.extraInfoJson[DMS_VERSION_ID] = dmsVersion->valueint;
528     }
529     cJSON_Delete(extraInfoValue);
530     cJSON_Delete(rootValue);
531     HILOGD("end");
532     return ERR_OK;
533 }
534 
UnmarshalAccountInfo(std::string & jsonStr)535 int32_t SinkStartCmd::UnmarshalAccountInfo(std::string &jsonStr)
536 {
537     HILOGD("called");
538     cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
539     if (rootValue == nullptr) {
540         HILOGE("Account info json string parse to cjson fail.");
541         return INVALID_PARAMETERS_ERR;
542     }
543 
544     cJSON *accountType = cJSON_GetObjectItemCaseSensitive(rootValue, "AccountType");
545     if (accountType == nullptr || !cJSON_IsNumber(accountType)) {
546         cJSON_Delete(rootValue);
547         HILOGE("AccountType term in account info json is null or not number.");
548         return INVALID_PARAMETERS_ERR;
549     }
550     accountInfo_.accountType = accountType->valueint;
551 
552     cJSON *groupId = nullptr;
553     std::vector<std::string> groupIdList;
554     cJSON *groupIdListStr = cJSON_GetObjectItemCaseSensitive(rootValue, "groupIdList");
555     cJSON_ArrayForEach(groupId, groupIdListStr) {
556         if (groupId == nullptr || !cJSON_IsString(groupId) || (groupId->valuestring == nullptr)) {
557             cJSON_Delete(rootValue);
558             HILOGE("groupId term in account info json is null or not string.");
559             return INVALID_PARAMETERS_ERR;
560         }
561         groupIdList.push_back(groupId->valuestring);
562     }
563     accountInfo_.groupIdList = groupIdList;
564 
565     cJSON *accountId = cJSON_GetObjectItemCaseSensitive(rootValue, Constants::EXTRO_INFO_JSON_KEY_ACCOUNT_ID.c_str());
566     if (accountId == nullptr || !cJSON_IsString(accountId)) {
567         HILOGE("accountId term in account info json is null or not string.");
568     } else {
569         accountInfo_.activeAccountId = accountId->valuestring;
570     }
571     cJSON *userId = cJSON_GetObjectItemCaseSensitive(rootValue, Constants::EXTRO_INFO_JSON_KEY_USERID_ID.c_str());
572     if (userId == nullptr || !cJSON_IsNumber(userId)) {
573         HILOGE("userId term in account info json is null or not number.");
574     } else {
575         accountInfo_.userId = userId->valueint;
576     }
577 
578     cJSON_Delete(rootValue);
579     HILOGD("end");
580     return ERR_OK;
581 }
582 
Marshal(std::string & jsonStr)583 int32_t NotifyResultCmd::Marshal(std::string &jsonStr)
584 {
585     HILOGD("called");
586     cJSON *rootValue = cJSON_CreateObject();
587     if (rootValue == nullptr) {
588         return INVALID_PARAMETERS_ERR;
589     }
590 
591     std::string baseJsonStr;
592     if (BaseCmd::Marshal(baseJsonStr) != ERR_OK) {
593         cJSON_Delete(rootValue);
594         return INVALID_PARAMETERS_ERR;
595     }
596     cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str());
597     cJSON_AddNumberToObject(rootValue, "SinkCollabSessionId", sinkCollabSessionId_);
598     cJSON_AddNumberToObject(rootValue, "Result", result_);
599     cJSON_AddStringToObject(rootValue, "SinkSocketName", sinkSocketName_.c_str());
600     cJSON_AddStringToObject(rootValue, "AbilityRejectReason", abilityRejectReason_.c_str());
601     cJSON_AddNumberToObject(rootValue, "SinkPid", sinkPid_);
602     cJSON_AddNumberToObject(rootValue, "SinkUserId", sinkUserId_);
603     cJSON_AddNumberToObject(rootValue, "SinkAccessToken", sinkAccessToken_);
604     cJSON_AddNumberToObject(rootValue, "SinkAccountId", sinkAccountId_);
605 
606     char *data = cJSON_Print(rootValue);
607     if (data == nullptr) {
608         cJSON_Delete(rootValue);
609         return INVALID_PARAMETERS_ERR;
610     }
611     jsonStr = std::string(data);
612     cJSON_Delete(rootValue);
613     cJSON_free(data);
614     HILOGD("end");
615     return ERR_OK;
616 }
617 
Unmarshal(const std::string & jsonStr)618 int32_t NotifyResultCmd::Unmarshal(const std::string &jsonStr)
619 {
620     HILOGD("called");
621     cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
622     if (rootValue == nullptr) {
623         return INVALID_PARAMETERS_ERR;
624     }
625     cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd");
626     if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) {
627         cJSON_Delete(rootValue);
628         return INVALID_PARAMETERS_ERR;
629     }
630     std::string baseCmdStr = baseCmd->valuestring;
631     if (BaseCmd::Unmarshal(baseCmdStr) != ERR_OK) {
632         cJSON_Delete(rootValue);
633         return INVALID_PARAMETERS_ERR;
634     }
635     cJSON *sinkCollabSessionId = cJSON_GetObjectItemCaseSensitive(rootValue, "SinkCollabSessionId");
636     if (sinkCollabSessionId == nullptr || !cJSON_IsNumber(sinkCollabSessionId)) {
637         cJSON_Delete(rootValue);
638         return INVALID_PARAMETERS_ERR;
639     }
640     sinkCollabSessionId_ = sinkCollabSessionId->valueint;
641     cJSON *result = cJSON_GetObjectItemCaseSensitive(rootValue, "Result");
642     if (result == nullptr || !cJSON_IsNumber(result)) {
643         cJSON_Delete(rootValue);
644         return INVALID_PARAMETERS_ERR;
645     }
646     result_ = result->valueint;
647     cJSON *sinkSocketName = cJSON_GetObjectItemCaseSensitive(rootValue, "SinkSocketName");
648     if (sinkSocketName == nullptr || !cJSON_IsString(sinkSocketName)) {
649         cJSON_Delete(rootValue);
650         return INVALID_PARAMETERS_ERR;
651     }
652     sinkSocketName_ = sinkSocketName->valuestring;
653     cJSON *abilityRejectReason = cJSON_GetObjectItemCaseSensitive(rootValue, "AbilityRejectReason");
654     if (abilityRejectReason != nullptr && cJSON_IsString(abilityRejectReason)) {
655         abilityRejectReason_ = abilityRejectReason->valuestring;
656     }
657     UnmarshalSinkInfo(rootValue);
658     if (rootValue != nullptr) {
659         cJSON_Delete(rootValue);
660     }
661     HILOGD("end");
662     return ERR_OK;
663 }
664 
UnmarshalSinkInfo(cJSON * rootValue)665 int32_t NotifyResultCmd::UnmarshalSinkInfo(cJSON *rootValue)
666 {
667     const char *numKeys[] = { "SinkPid", "SinkUserId", "SinkAccessToken", "SinkAccountId" };
668     int32_t *numValues[] = { &sinkPid_, &sinkUserId_, &sinkAccessToken_, &sinkAccountId_ };
669     int32_t numLength = sizeof(numKeys) / sizeof(numKeys[0]);
670     for (int32_t i = 0; i < numLength; i++) {
671         cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, numKeys[i]);
672         if (!cJSON_IsNumber(item)) {
673             HILOGE("%{public}s term is null or not number.", numKeys[i]);
674             return INVALID_PARAMETERS_ERR;
675         }
676         *numValues[i] = item->valueint;
677     }
678     return ERR_OK;
679 }
680 
Marshal(std::string & jsonStr)681 int32_t DisconnectCmd::Marshal(std::string &jsonStr)
682 {
683     HILOGD("called");
684     cJSON *rootValue = cJSON_CreateObject();
685     if (rootValue == nullptr) {
686         return INVALID_PARAMETERS_ERR;
687     }
688 
689     std::string baseJsonStr;
690     if (BaseCmd::Marshal(baseJsonStr) != ERR_OK) {
691         cJSON_Delete(rootValue);
692         return INVALID_PARAMETERS_ERR;
693     }
694     cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str());
695     char *data = cJSON_Print(rootValue);
696     if (data == nullptr) {
697         cJSON_Delete(rootValue);
698         return INVALID_PARAMETERS_ERR;
699     }
700     jsonStr = std::string(data);
701     cJSON_Delete(rootValue);
702     cJSON_free(data);
703     HILOGD("end");
704     return ERR_OK;
705 }
706 
Unmarshal(const std::string & jsonStr)707 int32_t DisconnectCmd::Unmarshal(const std::string &jsonStr)
708 {
709     HILOGD("called");
710     cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
711     if (rootValue == nullptr) {
712         return INVALID_PARAMETERS_ERR;
713     }
714 
715     cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd");
716     if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) {
717         cJSON_Delete(rootValue);
718         return INVALID_PARAMETERS_ERR;
719     }
720     std::string baseCmdStr = baseCmd->valuestring;
721     if (BaseCmd::Unmarshal(baseCmdStr) != ERR_OK) {
722         cJSON_Delete(rootValue);
723         return INVALID_PARAMETERS_ERR;
724     }
725 
726     cJSON_Delete(rootValue);
727     HILOGD("end");
728     return ERR_OK;
729 }
730 }  // namespace DistributedSchedule
731 }  // namespace OHOS
732