• 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_IsString(dmsVersion) && (dmsVersion->valuestring != nullptr)) {
526         callerInfo_.extraInfoJson[DMS_VERSION_ID] = dmsVersion->valuestring;
527     }
528     cJSON_Delete(extraInfoValue);
529     cJSON_Delete(rootValue);
530     HILOGD("end");
531     return ERR_OK;
532 }
533 
UnmarshalAccountInfo(std::string & jsonStr)534 int32_t SinkStartCmd::UnmarshalAccountInfo(std::string &jsonStr)
535 {
536     HILOGD("called");
537     cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
538     if (rootValue == nullptr) {
539         HILOGE("Account info json string parse to cjson fail.");
540         return INVALID_PARAMETERS_ERR;
541     }
542 
543     cJSON *accountType = cJSON_GetObjectItemCaseSensitive(rootValue, "AccountType");
544     if (accountType == nullptr || !cJSON_IsNumber(accountType)) {
545         cJSON_Delete(rootValue);
546         HILOGE("AccountType term in account info json is null or not number.");
547         return INVALID_PARAMETERS_ERR;
548     }
549     accountInfo_.accountType = accountType->valueint;
550 
551     cJSON *groupId = nullptr;
552     std::vector<std::string> groupIdList;
553     cJSON *groupIdListStr = cJSON_GetObjectItemCaseSensitive(rootValue, "groupIdList");
554     cJSON_ArrayForEach(groupId, groupIdListStr) {
555         if (groupId == nullptr || !cJSON_IsString(groupId) || (groupId->valuestring == nullptr)) {
556             cJSON_Delete(rootValue);
557             HILOGE("groupId term in account info json is null or not string.");
558             return INVALID_PARAMETERS_ERR;
559         }
560         groupIdList.push_back(groupId->valuestring);
561     }
562     accountInfo_.groupIdList = groupIdList;
563 
564     cJSON *accountId = cJSON_GetObjectItemCaseSensitive(rootValue, Constants::EXTRO_INFO_JSON_KEY_ACCOUNT_ID.c_str());
565     if (accountId == nullptr || !cJSON_IsString(accountId)) {
566         HILOGE("accountId term in account info json is null or not string.");
567     } else {
568         accountInfo_.activeAccountId = accountId->valuestring;
569     }
570     cJSON *userId = cJSON_GetObjectItemCaseSensitive(rootValue, Constants::EXTRO_INFO_JSON_KEY_USERID_ID.c_str());
571     if (userId == nullptr || !cJSON_IsNumber(userId)) {
572         HILOGE("userId term in account info json is null or not number.");
573     } else {
574         accountInfo_.userId = userId->valueint;
575     }
576 
577     cJSON_Delete(rootValue);
578     HILOGD("end");
579     return ERR_OK;
580 }
581 
Marshal(std::string & jsonStr)582 int32_t NotifyResultCmd::Marshal(std::string &jsonStr)
583 {
584     HILOGD("called");
585     cJSON *rootValue = cJSON_CreateObject();
586     if (rootValue == nullptr) {
587         return INVALID_PARAMETERS_ERR;
588     }
589 
590     std::string baseJsonStr;
591     if (BaseCmd::Marshal(baseJsonStr) != ERR_OK) {
592         cJSON_Delete(rootValue);
593         return INVALID_PARAMETERS_ERR;
594     }
595     cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str());
596     cJSON_AddNumberToObject(rootValue, "SinkCollabSessionId", sinkCollabSessionId_);
597     cJSON_AddNumberToObject(rootValue, "Result", result_);
598     cJSON_AddStringToObject(rootValue, "SinkSocketName", sinkSocketName_.c_str());
599     cJSON_AddStringToObject(rootValue, "AbilityRejectReason", abilityRejectReason_.c_str());
600     cJSON_AddNumberToObject(rootValue, "SinkPid", sinkPid_);
601     cJSON_AddNumberToObject(rootValue, "SinkUserId", sinkUserId_);
602     cJSON_AddNumberToObject(rootValue, "SinkAccessToken", sinkAccessToken_);
603     cJSON_AddNumberToObject(rootValue, "SinkAccountId", sinkAccountId_);
604 
605     char *data = cJSON_Print(rootValue);
606     if (data == nullptr) {
607         cJSON_Delete(rootValue);
608         return INVALID_PARAMETERS_ERR;
609     }
610     jsonStr = std::string(data);
611     cJSON_Delete(rootValue);
612     cJSON_free(data);
613     HILOGD("end");
614     return ERR_OK;
615 }
616 
Unmarshal(const std::string & jsonStr)617 int32_t NotifyResultCmd::Unmarshal(const std::string &jsonStr)
618 {
619     HILOGD("called");
620     cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
621     if (rootValue == nullptr) {
622         return INVALID_PARAMETERS_ERR;
623     }
624     cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd");
625     if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) {
626         cJSON_Delete(rootValue);
627         return INVALID_PARAMETERS_ERR;
628     }
629     std::string baseCmdStr = baseCmd->valuestring;
630     if (BaseCmd::Unmarshal(baseCmdStr) != ERR_OK) {
631         cJSON_Delete(rootValue);
632         return INVALID_PARAMETERS_ERR;
633     }
634     cJSON *sinkCollabSessionId = cJSON_GetObjectItemCaseSensitive(rootValue, "SinkCollabSessionId");
635     if (sinkCollabSessionId == nullptr || !cJSON_IsNumber(sinkCollabSessionId)) {
636         cJSON_Delete(rootValue);
637         return INVALID_PARAMETERS_ERR;
638     }
639     sinkCollabSessionId_ = sinkCollabSessionId->valueint;
640     cJSON *result = cJSON_GetObjectItemCaseSensitive(rootValue, "Result");
641     if (result == nullptr || !cJSON_IsNumber(result)) {
642         cJSON_Delete(rootValue);
643         return INVALID_PARAMETERS_ERR;
644     }
645     result_ = result->valueint;
646     cJSON *sinkSocketName = cJSON_GetObjectItemCaseSensitive(rootValue, "SinkSocketName");
647     if (sinkSocketName == nullptr || !cJSON_IsString(sinkSocketName)) {
648         cJSON_Delete(rootValue);
649         return INVALID_PARAMETERS_ERR;
650     }
651     sinkSocketName_ = sinkSocketName->valuestring;
652     cJSON *abilityRejectReason = cJSON_GetObjectItemCaseSensitive(rootValue, "AbilityRejectReason");
653     if (abilityRejectReason != nullptr && cJSON_IsString(abilityRejectReason)) {
654         abilityRejectReason_ = abilityRejectReason->valuestring;
655     }
656     UnmarshalSinkInfo(rootValue);
657     if (rootValue != nullptr) {
658         cJSON_Delete(rootValue);
659     }
660     HILOGD("end");
661     return ERR_OK;
662 }
663 
UnmarshalSinkInfo(cJSON * rootValue)664 int32_t NotifyResultCmd::UnmarshalSinkInfo(cJSON *rootValue)
665 {
666     const char *numKeys[] = { "SinkPid", "SinkUserId", "SinkAccessToken", "SinkAccountId" };
667     int32_t *numValues[] = { &sinkPid_, &sinkUserId_, &sinkAccessToken_, &sinkAccountId_ };
668     int32_t numLength = sizeof(numKeys) / sizeof(numKeys[0]);
669     for (int32_t i = 0; i < numLength; i++) {
670         cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, numKeys[i]);
671         if (!cJSON_IsNumber(item)) {
672             HILOGE("%{public}s term is null or not number.", numKeys[i]);
673             return INVALID_PARAMETERS_ERR;
674         }
675         *numValues[i] = item->valueint;
676     }
677     return ERR_OK;
678 }
679 
Marshal(std::string & jsonStr)680 int32_t DisconnectCmd::Marshal(std::string &jsonStr)
681 {
682     HILOGD("called");
683     cJSON *rootValue = cJSON_CreateObject();
684     if (rootValue == nullptr) {
685         return INVALID_PARAMETERS_ERR;
686     }
687 
688     std::string baseJsonStr;
689     if (BaseCmd::Marshal(baseJsonStr) != ERR_OK) {
690         cJSON_Delete(rootValue);
691         return INVALID_PARAMETERS_ERR;
692     }
693     cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str());
694     char *data = cJSON_Print(rootValue);
695     if (data == nullptr) {
696         cJSON_Delete(rootValue);
697         return INVALID_PARAMETERS_ERR;
698     }
699     jsonStr = std::string(data);
700     cJSON_Delete(rootValue);
701     cJSON_free(data);
702     HILOGD("end");
703     return ERR_OK;
704 }
705 
Unmarshal(const std::string & jsonStr)706 int32_t DisconnectCmd::Unmarshal(const std::string &jsonStr)
707 {
708     HILOGD("called");
709     cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
710     if (rootValue == nullptr) {
711         return INVALID_PARAMETERS_ERR;
712     }
713 
714     cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd");
715     if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) {
716         cJSON_Delete(rootValue);
717         return INVALID_PARAMETERS_ERR;
718     }
719     std::string baseCmdStr = baseCmd->valuestring;
720     if (BaseCmd::Unmarshal(baseCmdStr) != ERR_OK) {
721         cJSON_Delete(rootValue);
722         return INVALID_PARAMETERS_ERR;
723     }
724 
725     cJSON_Delete(rootValue);
726     HILOGD("end");
727     return ERR_OK;
728 }
729 }  // namespace DistributedSchedule
730 }  // namespace OHOS
731