1686862fbSopenharmony_ci/* 2686862fbSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3686862fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4686862fbSopenharmony_ci * you may not use this file except in compliance with the License. 5686862fbSopenharmony_ci * You may obtain a copy of the License at 6686862fbSopenharmony_ci * 7686862fbSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8686862fbSopenharmony_ci * 9686862fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10686862fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11686862fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12686862fbSopenharmony_ci * See the License for the specific language governing permissions and 13686862fbSopenharmony_ci * limitations under the License. 14686862fbSopenharmony_ci */ 15686862fbSopenharmony_ci 16686862fbSopenharmony_ci#include "dsched_continue_event.h" 17686862fbSopenharmony_ci 18686862fbSopenharmony_ci#include "cJSON.h" 19686862fbSopenharmony_ci#include "parcel.h" 20686862fbSopenharmony_ci 21686862fbSopenharmony_ci#include "distributed_sched_utils.h" 22686862fbSopenharmony_ci#include "dms_constant.h" 23686862fbSopenharmony_ci#include "dtbschedmgr_log.h" 24686862fbSopenharmony_ci 25686862fbSopenharmony_cinamespace OHOS { 26686862fbSopenharmony_cinamespace DistributedSchedule { 27686862fbSopenharmony_cinamespace { 28686862fbSopenharmony_ciconst std::string TAG = "DSchedContinueCmd"; 29686862fbSopenharmony_ciconst char* EXTRO_INFO_JSON_KEY_ACCESS_TOKEN = "accessTokenID"; 30686862fbSopenharmony_ciconst char* DMS_VERSION_ID = "dmsVersion"; 31686862fbSopenharmony_ci} 32686862fbSopenharmony_ci 33686862fbSopenharmony_ciint32_t DSchedContinueCmdBase::Marshal(std::string &jsonStr) 34686862fbSopenharmony_ci{ 35686862fbSopenharmony_ci cJSON *rootValue = cJSON_CreateObject(); 36686862fbSopenharmony_ci if (rootValue == nullptr) { 37686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 38686862fbSopenharmony_ci } 39686862fbSopenharmony_ci cJSON_AddNumberToObject(rootValue, "Version", version_); 40686862fbSopenharmony_ci cJSON_AddNumberToObject(rootValue, "ServiceType", serviceType_); 41686862fbSopenharmony_ci cJSON_AddNumberToObject(rootValue, "SubServiceType", subServiceType_); 42686862fbSopenharmony_ci cJSON_AddNumberToObject(rootValue, "Command", command_); 43686862fbSopenharmony_ci 44686862fbSopenharmony_ci cJSON_AddStringToObject(rootValue, "SrcDeviceId", srcDeviceId_.c_str()); 45686862fbSopenharmony_ci cJSON_AddStringToObject(rootValue, "SrcBundleName", srcBundleName_.c_str()); 46686862fbSopenharmony_ci cJSON_AddStringToObject(rootValue, "SrcDeveloperId", srcDeveloperId_.c_str()); 47686862fbSopenharmony_ci cJSON_AddStringToObject(rootValue, "DstDeviceId", dstDeviceId_.c_str()); 48686862fbSopenharmony_ci cJSON_AddStringToObject(rootValue, "DstBundleName", dstBundleName_.c_str()); 49686862fbSopenharmony_ci cJSON_AddStringToObject(rootValue, "DstDeveloperId", dstDeveloperId_.c_str()); 50686862fbSopenharmony_ci cJSON_AddStringToObject(rootValue, "ContinueType", continueType_.c_str()); 51686862fbSopenharmony_ci 52686862fbSopenharmony_ci cJSON_AddNumberToObject(rootValue, "ContinueByType", continueByType_); 53686862fbSopenharmony_ci cJSON_AddNumberToObject(rootValue, "SourceMissionId", sourceMissionId_); 54686862fbSopenharmony_ci cJSON_AddNumberToObject(rootValue, "DmsVersion", dmsVersion_); 55686862fbSopenharmony_ci 56686862fbSopenharmony_ci char *data = cJSON_Print(rootValue); 57686862fbSopenharmony_ci if (data == nullptr) { 58686862fbSopenharmony_ci cJSON_Delete(rootValue); 59686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 60686862fbSopenharmony_ci } 61686862fbSopenharmony_ci jsonStr = std::string(data); 62686862fbSopenharmony_ci cJSON_Delete(rootValue); 63686862fbSopenharmony_ci cJSON_free(data); 64686862fbSopenharmony_ci return ERR_OK; 65686862fbSopenharmony_ci} 66686862fbSopenharmony_ci 67686862fbSopenharmony_ciint32_t DSchedContinueCmdBase::Unmarshal(const std::string &jsonStr) 68686862fbSopenharmony_ci{ 69686862fbSopenharmony_ci cJSON *rootValue = cJSON_Parse(jsonStr.c_str()); 70686862fbSopenharmony_ci if (rootValue == nullptr) { 71686862fbSopenharmony_ci HILOGE("Dms continue cmd base json string parse to cjson fail."); 72686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 73686862fbSopenharmony_ci } 74686862fbSopenharmony_ci 75686862fbSopenharmony_ci const char *numKeys[] = { "Version", "ServiceType", "SubServiceType", "ContinueByType", "SourceMissionId", 76686862fbSopenharmony_ci "DmsVersion" }; 77686862fbSopenharmony_ci int32_t *numValues[] = { &version_, &serviceType_, &subServiceType_, &continueByType_, &sourceMissionId_, 78686862fbSopenharmony_ci &dmsVersion_ }; 79686862fbSopenharmony_ci int32_t numLength = sizeof(numKeys) / sizeof(numKeys[0]); 80686862fbSopenharmony_ci for (int32_t i = 0; i < numLength; i++) { 81686862fbSopenharmony_ci cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, numKeys[i]); 82686862fbSopenharmony_ci if (item == nullptr || !cJSON_IsNumber(item)) { 83686862fbSopenharmony_ci cJSON_Delete(rootValue); 84686862fbSopenharmony_ci HILOGE("Dms continue cmd base %{public}s term is null or not number.", numKeys[i]); 85686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 86686862fbSopenharmony_ci } 87686862fbSopenharmony_ci *numValues[i] = item->valueint; 88686862fbSopenharmony_ci } 89686862fbSopenharmony_ci 90686862fbSopenharmony_ci const char *strKeys[] = {"SrcDeviceId", "SrcBundleName", "DstDeviceId", "DstBundleName", "ContinueType"}; 91686862fbSopenharmony_ci std::string *strValues[] = {&srcDeviceId_, &srcBundleName_, &dstDeviceId_, &dstBundleName_, &continueType_}; 92686862fbSopenharmony_ci int32_t strLength = sizeof(strKeys) / sizeof(strKeys[0]); 93686862fbSopenharmony_ci for (int32_t i = 0; i < strLength; i++) { 94686862fbSopenharmony_ci cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, strKeys[i]); 95686862fbSopenharmony_ci if (item == nullptr || !cJSON_IsString(item) || (item->valuestring == nullptr)) { 96686862fbSopenharmony_ci cJSON_Delete(rootValue); 97686862fbSopenharmony_ci HILOGE("Dms continue cmd base %{public}s term is null or not string.", strKeys[i]); 98686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 99686862fbSopenharmony_ci } 100686862fbSopenharmony_ci *strValues[i] = item->valuestring; 101686862fbSopenharmony_ci } 102686862fbSopenharmony_ci const char *strNotRequiredKeys[] = {"SrcDeveloperId", "DstDeveloperId"}; 103686862fbSopenharmony_ci std::string *strNotRequiredValues[] = { &srcDeveloperId_, &dstDeveloperId_}; 104686862fbSopenharmony_ci int32_t strNotRequiredLength = sizeof(strNotRequiredKeys) / sizeof(strNotRequiredKeys[0]); 105686862fbSopenharmony_ci for (int32_t i = 0; i < strNotRequiredLength; i++) { 106686862fbSopenharmony_ci cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, strNotRequiredKeys[i]); 107686862fbSopenharmony_ci if (item == nullptr || !cJSON_IsString(item) || (item->valuestring == nullptr)) { 108686862fbSopenharmony_ci *strNotRequiredValues[i] = ""; 109686862fbSopenharmony_ci } else { 110686862fbSopenharmony_ci *strNotRequiredValues[i] = item->valuestring; 111686862fbSopenharmony_ci } 112686862fbSopenharmony_ci } 113686862fbSopenharmony_ci 114686862fbSopenharmony_ci cJSON_Delete(rootValue); 115686862fbSopenharmony_ci return ERR_OK; 116686862fbSopenharmony_ci} 117686862fbSopenharmony_ci 118686862fbSopenharmony_ciint32_t DSchedContinueStartCmd::Marshal(std::string &jsonStr) 119686862fbSopenharmony_ci{ 120686862fbSopenharmony_ci cJSON *rootValue = cJSON_CreateObject(); 121686862fbSopenharmony_ci if (rootValue == nullptr) { 122686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 123686862fbSopenharmony_ci } 124686862fbSopenharmony_ci 125686862fbSopenharmony_ci std::string baseJsonStr; 126686862fbSopenharmony_ci if (DSchedContinueCmdBase::Marshal(baseJsonStr) != ERR_OK) { 127686862fbSopenharmony_ci cJSON_Delete(rootValue); 128686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 129686862fbSopenharmony_ci } 130686862fbSopenharmony_ci cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str()); 131686862fbSopenharmony_ci 132686862fbSopenharmony_ci cJSON_AddNumberToObject(rootValue, "Direction", direction_); 133686862fbSopenharmony_ci cJSON_AddNumberToObject(rootValue, "AppVersion", appVersion_); 134686862fbSopenharmony_ci 135686862fbSopenharmony_ci Parcel parcel; 136686862fbSopenharmony_ci if (!wantParams_.Marshalling(parcel)) { 137686862fbSopenharmony_ci cJSON_Delete(rootValue); 138686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 139686862fbSopenharmony_ci } 140686862fbSopenharmony_ci std::string wantParamsStr = ParcelToBase64Str(parcel); 141686862fbSopenharmony_ci cJSON_AddStringToObject(rootValue, "WantParams", wantParamsStr.c_str()); 142686862fbSopenharmony_ci 143686862fbSopenharmony_ci char *data = cJSON_Print(rootValue); 144686862fbSopenharmony_ci if (data == nullptr) { 145686862fbSopenharmony_ci cJSON_Delete(rootValue); 146686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 147686862fbSopenharmony_ci } 148686862fbSopenharmony_ci jsonStr = std::string(data); 149686862fbSopenharmony_ci cJSON_Delete(rootValue); 150686862fbSopenharmony_ci cJSON_free(data); 151686862fbSopenharmony_ci return ERR_OK; 152686862fbSopenharmony_ci} 153686862fbSopenharmony_ci 154686862fbSopenharmony_ciint32_t DSchedContinueStartCmd::Unmarshal(const std::string &jsonStr) 155686862fbSopenharmony_ci{ 156686862fbSopenharmony_ci cJSON *rootValue = cJSON_Parse(jsonStr.c_str()); 157686862fbSopenharmony_ci if (rootValue == nullptr) { 158686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 159686862fbSopenharmony_ci } 160686862fbSopenharmony_ci 161686862fbSopenharmony_ci cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd"); 162686862fbSopenharmony_ci if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) { 163686862fbSopenharmony_ci cJSON_Delete(rootValue); 164686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 165686862fbSopenharmony_ci } 166686862fbSopenharmony_ci std::string baseCmdStr = baseCmd->valuestring; 167686862fbSopenharmony_ci if (DSchedContinueCmdBase::Unmarshal(baseCmdStr) != ERR_OK) { 168686862fbSopenharmony_ci cJSON_Delete(rootValue); 169686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 170686862fbSopenharmony_ci } 171686862fbSopenharmony_ci 172686862fbSopenharmony_ci cJSON *direction = cJSON_GetObjectItemCaseSensitive(rootValue, "Direction"); 173686862fbSopenharmony_ci if (direction == nullptr || !cJSON_IsNumber(direction)) { 174686862fbSopenharmony_ci cJSON_Delete(rootValue); 175686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 176686862fbSopenharmony_ci } 177686862fbSopenharmony_ci direction_ = direction->valueint; 178686862fbSopenharmony_ci 179686862fbSopenharmony_ci cJSON *appVersion = cJSON_GetObjectItemCaseSensitive(rootValue, "AppVersion"); 180686862fbSopenharmony_ci if (appVersion == nullptr || !cJSON_IsNumber(appVersion)) { 181686862fbSopenharmony_ci cJSON_Delete(rootValue); 182686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 183686862fbSopenharmony_ci } 184686862fbSopenharmony_ci appVersion_ = appVersion->valueint; 185686862fbSopenharmony_ci 186686862fbSopenharmony_ci cJSON *wantParams = cJSON_GetObjectItemCaseSensitive(rootValue, "WantParams"); 187686862fbSopenharmony_ci if (wantParams == nullptr || !cJSON_IsString(wantParams) || (wantParams->valuestring == nullptr)) { 188686862fbSopenharmony_ci cJSON_Delete(rootValue); 189686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 190686862fbSopenharmony_ci } 191686862fbSopenharmony_ci Parcel parcel; 192686862fbSopenharmony_ci int32_t ret = Base64StrToParcel(wantParams->valuestring, parcel); 193686862fbSopenharmony_ci if (ret != ERR_OK) { 194686862fbSopenharmony_ci cJSON_Delete(rootValue); 195686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 196686862fbSopenharmony_ci } 197686862fbSopenharmony_ci auto wantParamsPtr = DistributedWantParams::Unmarshalling(parcel); 198686862fbSopenharmony_ci if (wantParamsPtr == nullptr) { 199686862fbSopenharmony_ci cJSON_Delete(rootValue); 200686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 201686862fbSopenharmony_ci } 202686862fbSopenharmony_ci wantParams_ = *wantParamsPtr; 203686862fbSopenharmony_ci 204686862fbSopenharmony_ci cJSON_Delete(rootValue); 205686862fbSopenharmony_ci return ERR_OK; 206686862fbSopenharmony_ci} 207686862fbSopenharmony_ci 208686862fbSopenharmony_ciint32_t DSchedContinueDataCmd::Marshal(std::string &jsonStr) 209686862fbSopenharmony_ci{ 210686862fbSopenharmony_ci cJSON *rootValue = cJSON_CreateObject(); 211686862fbSopenharmony_ci if (rootValue == nullptr) { 212686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 213686862fbSopenharmony_ci } 214686862fbSopenharmony_ci 215686862fbSopenharmony_ci std::string baseJsonStr; 216686862fbSopenharmony_ci if (DSchedContinueCmdBase::Marshal(baseJsonStr) != ERR_OK) { 217686862fbSopenharmony_ci cJSON_Delete(rootValue); 218686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 219686862fbSopenharmony_ci } 220686862fbSopenharmony_ci 221686862fbSopenharmony_ci cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str()); 222686862fbSopenharmony_ci 223686862fbSopenharmony_ci Parcel wantParcel; 224686862fbSopenharmony_ci if (!want_.Marshalling(wantParcel)) { 225686862fbSopenharmony_ci cJSON_Delete(rootValue); 226686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 227686862fbSopenharmony_ci } 228686862fbSopenharmony_ci std::string wantStr = ParcelToBase64Str(wantParcel); 229686862fbSopenharmony_ci cJSON_AddStringToObject(rootValue, "Want", wantStr.c_str()); 230686862fbSopenharmony_ci 231686862fbSopenharmony_ci Parcel abilityParcel; 232686862fbSopenharmony_ci if (!abilityInfo_.Marshalling(abilityParcel)) { 233686862fbSopenharmony_ci cJSON_Delete(rootValue); 234686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 235686862fbSopenharmony_ci } 236686862fbSopenharmony_ci std::string abilityInfoStr = ParcelToBase64Str(abilityParcel); 237686862fbSopenharmony_ci cJSON_AddStringToObject(rootValue, "AbilityInfo", abilityInfoStr.c_str()); 238686862fbSopenharmony_ci 239686862fbSopenharmony_ci cJSON_AddNumberToObject(rootValue, "RequestCode", requestCode_); 240686862fbSopenharmony_ci 241686862fbSopenharmony_ci std::string callerInfoStr; 242686862fbSopenharmony_ci if (MarshalCallerInfo(callerInfoStr) != ERR_OK) { 243686862fbSopenharmony_ci cJSON_Delete(rootValue); 244686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 245686862fbSopenharmony_ci } 246686862fbSopenharmony_ci cJSON_AddStringToObject(rootValue, "CallerInfo", callerInfoStr.c_str()); 247686862fbSopenharmony_ci 248686862fbSopenharmony_ci std::string accountInfoStr; 249686862fbSopenharmony_ci if (MarshalAccountInfo(accountInfoStr) != ERR_OK) { 250686862fbSopenharmony_ci cJSON_Delete(rootValue); 251686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 252686862fbSopenharmony_ci } 253686862fbSopenharmony_ci cJSON_AddStringToObject(rootValue, "AccountInfo", accountInfoStr.c_str()); 254686862fbSopenharmony_ci 255686862fbSopenharmony_ci char *data = cJSON_Print(rootValue); 256686862fbSopenharmony_ci if (data == nullptr) { 257686862fbSopenharmony_ci cJSON_Delete(rootValue); 258686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 259686862fbSopenharmony_ci } 260686862fbSopenharmony_ci jsonStr = std::string(data); 261686862fbSopenharmony_ci cJSON_Delete(rootValue); 262686862fbSopenharmony_ci cJSON_free(data); 263686862fbSopenharmony_ci return ERR_OK; 264686862fbSopenharmony_ci} 265686862fbSopenharmony_ci 266686862fbSopenharmony_ciint32_t DSchedContinueDataCmd::MarshalCallerInfo(std::string &jsonStr) 267686862fbSopenharmony_ci{ 268686862fbSopenharmony_ci cJSON *callerInfoJson = cJSON_CreateObject(); 269686862fbSopenharmony_ci if (callerInfoJson == nullptr) { 270686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 271686862fbSopenharmony_ci } 272686862fbSopenharmony_ci cJSON_AddNumberToObject(callerInfoJson, "Uid", callerInfo_.uid); 273686862fbSopenharmony_ci cJSON_AddNumberToObject(callerInfoJson, "Pid", callerInfo_.pid); 274686862fbSopenharmony_ci cJSON_AddNumberToObject(callerInfoJson, "CallerType", callerInfo_.callerType); 275686862fbSopenharmony_ci cJSON_AddStringToObject(callerInfoJson, "SourceDeviceId", callerInfo_.sourceDeviceId.c_str()); 276686862fbSopenharmony_ci cJSON_AddNumberToObject(callerInfoJson, "Duid", callerInfo_.duid); 277686862fbSopenharmony_ci cJSON_AddStringToObject(callerInfoJson, "CallerAppId", callerInfo_.callerAppId.c_str()); 278686862fbSopenharmony_ci 279686862fbSopenharmony_ci const auto bundleNamesSize = static_cast<int32_t>(callerInfo_.bundleNames.size()); 280686862fbSopenharmony_ci cJSON *bundleNames = cJSON_CreateArray(); 281686862fbSopenharmony_ci if (bundleNames == nullptr) { 282686862fbSopenharmony_ci cJSON_Delete(callerInfoJson); 283686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 284686862fbSopenharmony_ci } 285686862fbSopenharmony_ci for (auto i = 0; i < bundleNamesSize; i++) { 286686862fbSopenharmony_ci cJSON *bundleName = cJSON_CreateString(callerInfo_.bundleNames[i].c_str()); 287686862fbSopenharmony_ci if (bundleName == nullptr) { 288686862fbSopenharmony_ci cJSON_Delete(callerInfoJson); 289686862fbSopenharmony_ci cJSON_Delete(bundleNames); 290686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 291686862fbSopenharmony_ci } 292686862fbSopenharmony_ci cJSON_AddItemToArray(bundleNames, bundleName); 293686862fbSopenharmony_ci } 294686862fbSopenharmony_ci cJSON_AddItemToObject(callerInfoJson, "BundleNames", bundleNames); 295686862fbSopenharmony_ci 296686862fbSopenharmony_ci std::string extraInfo = callerInfo_.extraInfoJson.dump(); 297686862fbSopenharmony_ci cJSON_AddStringToObject(callerInfoJson, "ExtraInfo", extraInfo.c_str()); 298686862fbSopenharmony_ci 299686862fbSopenharmony_ci char *data = cJSON_Print(callerInfoJson); 300686862fbSopenharmony_ci if (data == nullptr) { 301686862fbSopenharmony_ci cJSON_Delete(callerInfoJson); 302686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 303686862fbSopenharmony_ci } 304686862fbSopenharmony_ci jsonStr = std::string(data); 305686862fbSopenharmony_ci cJSON_Delete(callerInfoJson); 306686862fbSopenharmony_ci cJSON_free(data); 307686862fbSopenharmony_ci return ERR_OK; 308686862fbSopenharmony_ci} 309686862fbSopenharmony_ci 310686862fbSopenharmony_ciint32_t DSchedContinueDataCmd::MarshalAccountInfo(std::string &jsonStr) 311686862fbSopenharmony_ci{ 312686862fbSopenharmony_ci cJSON *accountInfoJson = cJSON_CreateObject(); 313686862fbSopenharmony_ci if (accountInfoJson == nullptr) { 314686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 315686862fbSopenharmony_ci } 316686862fbSopenharmony_ci 317686862fbSopenharmony_ci cJSON_AddNumberToObject(accountInfoJson, "AccountType", accountInfo_.accountType); 318686862fbSopenharmony_ci 319686862fbSopenharmony_ci const auto groupIdListSize = static_cast<int32_t>(accountInfo_.groupIdList.size()); 320686862fbSopenharmony_ci cJSON *groupIdList = cJSON_CreateArray(); 321686862fbSopenharmony_ci if (groupIdList == nullptr) { 322686862fbSopenharmony_ci cJSON_Delete(accountInfoJson); 323686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 324686862fbSopenharmony_ci } 325686862fbSopenharmony_ci for (auto i = 0; i < groupIdListSize; i++) { 326686862fbSopenharmony_ci cJSON *groupId = cJSON_CreateString(accountInfo_.groupIdList[i].c_str()); 327686862fbSopenharmony_ci if (groupId == nullptr) { 328686862fbSopenharmony_ci cJSON_Delete(accountInfoJson); 329686862fbSopenharmony_ci cJSON_Delete(groupIdList); 330686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 331686862fbSopenharmony_ci } 332686862fbSopenharmony_ci cJSON_AddItemToArray(groupIdList, groupId); 333686862fbSopenharmony_ci } 334686862fbSopenharmony_ci cJSON_AddItemToObject(accountInfoJson, "GroupIdList", groupIdList); 335686862fbSopenharmony_ci 336686862fbSopenharmony_ci cJSON_AddStringToObject(accountInfoJson, Constants::EXTRO_INFO_JSON_KEY_ACCOUNT_ID.c_str(), 337686862fbSopenharmony_ci accountInfo_.activeAccountId.c_str()); 338686862fbSopenharmony_ci cJSON_AddNumberToObject(accountInfoJson, Constants::EXTRO_INFO_JSON_KEY_USERID_ID.c_str(), accountInfo_.userId); 339686862fbSopenharmony_ci 340686862fbSopenharmony_ci char *data = cJSON_Print(accountInfoJson); 341686862fbSopenharmony_ci if (data == nullptr) { 342686862fbSopenharmony_ci cJSON_Delete(accountInfoJson); 343686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 344686862fbSopenharmony_ci } 345686862fbSopenharmony_ci jsonStr = std::string(data); 346686862fbSopenharmony_ci cJSON_Delete(accountInfoJson); 347686862fbSopenharmony_ci cJSON_free(data); 348686862fbSopenharmony_ci return ERR_OK; 349686862fbSopenharmony_ci} 350686862fbSopenharmony_ci 351686862fbSopenharmony_ciint32_t DSchedContinueDataCmd::Unmarshal(const std::string &jsonStr) 352686862fbSopenharmony_ci{ 353686862fbSopenharmony_ci cJSON *rootValue = cJSON_Parse(jsonStr.c_str()); 354686862fbSopenharmony_ci if (rootValue == nullptr) { 355686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 356686862fbSopenharmony_ci } 357686862fbSopenharmony_ci 358686862fbSopenharmony_ci cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd"); 359686862fbSopenharmony_ci if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) { 360686862fbSopenharmony_ci cJSON_Delete(rootValue); 361686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 362686862fbSopenharmony_ci } 363686862fbSopenharmony_ci std::string baseCmdStr = baseCmd->valuestring; 364686862fbSopenharmony_ci if (DSchedContinueCmdBase::Unmarshal(baseCmdStr) != ERR_OK) { 365686862fbSopenharmony_ci cJSON_Delete(rootValue); 366686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 367686862fbSopenharmony_ci } 368686862fbSopenharmony_ci 369686862fbSopenharmony_ci if (UnmarshalParcel(jsonStr) != ERR_OK) { 370686862fbSopenharmony_ci cJSON_Delete(rootValue); 371686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 372686862fbSopenharmony_ci } 373686862fbSopenharmony_ci 374686862fbSopenharmony_ci cJSON *requestCode = cJSON_GetObjectItemCaseSensitive(rootValue, "RequestCode"); 375686862fbSopenharmony_ci if (requestCode == nullptr || !cJSON_IsNumber(requestCode)) { 376686862fbSopenharmony_ci cJSON_Delete(rootValue); 377686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 378686862fbSopenharmony_ci } 379686862fbSopenharmony_ci requestCode_ = requestCode->valueint; 380686862fbSopenharmony_ci 381686862fbSopenharmony_ci cJSON *callerInfoJson = cJSON_GetObjectItemCaseSensitive(rootValue, "CallerInfo"); 382686862fbSopenharmony_ci if (callerInfoJson == nullptr || !cJSON_IsString(callerInfoJson) || (callerInfoJson->valuestring == nullptr)) { 383686862fbSopenharmony_ci cJSON_Delete(rootValue); 384686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 385686862fbSopenharmony_ci } 386686862fbSopenharmony_ci std::string callerInfoStr = callerInfoJson->valuestring; 387686862fbSopenharmony_ci if (UnmarshalCallerInfo(callerInfoStr) != ERR_OK) { 388686862fbSopenharmony_ci cJSON_Delete(rootValue); 389686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 390686862fbSopenharmony_ci } 391686862fbSopenharmony_ci 392686862fbSopenharmony_ci cJSON *accountInfoJson = cJSON_GetObjectItemCaseSensitive(rootValue, "AccountInfo"); 393686862fbSopenharmony_ci if (accountInfoJson == nullptr || !cJSON_IsString(accountInfoJson) || (accountInfoJson->valuestring == nullptr)) { 394686862fbSopenharmony_ci cJSON_Delete(rootValue); 395686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 396686862fbSopenharmony_ci } 397686862fbSopenharmony_ci std::string accountInfoStr = accountInfoJson->valuestring; 398686862fbSopenharmony_ci if (UnmarshalAccountInfo(accountInfoStr) != ERR_OK) { 399686862fbSopenharmony_ci cJSON_Delete(rootValue); 400686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 401686862fbSopenharmony_ci } 402686862fbSopenharmony_ci 403686862fbSopenharmony_ci cJSON_Delete(rootValue); 404686862fbSopenharmony_ci return ERR_OK; 405686862fbSopenharmony_ci} 406686862fbSopenharmony_ci 407686862fbSopenharmony_ciint32_t DSchedContinueDataCmd::UnmarshalParcel(const std::string &jsonStr) 408686862fbSopenharmony_ci{ 409686862fbSopenharmony_ci cJSON *rootValue = cJSON_Parse(jsonStr.c_str()); 410686862fbSopenharmony_ci if (rootValue == nullptr) { 411686862fbSopenharmony_ci HILOGE("Want and AbilityInfo json string parse to cjson fail."); 412686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 413686862fbSopenharmony_ci } 414686862fbSopenharmony_ci 415686862fbSopenharmony_ci cJSON *wantStr = cJSON_GetObjectItemCaseSensitive(rootValue, "Want"); 416686862fbSopenharmony_ci if (wantStr == nullptr || !cJSON_IsString(wantStr) || (wantStr->valuestring == nullptr)) { 417686862fbSopenharmony_ci cJSON_Delete(rootValue); 418686862fbSopenharmony_ci HILOGE("Want term is null or not string."); 419686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 420686862fbSopenharmony_ci } 421686862fbSopenharmony_ci Parcel wantParcel; 422686862fbSopenharmony_ci int32_t ret = Base64StrToParcel(wantStr->valuestring, wantParcel); 423686862fbSopenharmony_ci if (ret != ERR_OK) { 424686862fbSopenharmony_ci cJSON_Delete(rootValue); 425686862fbSopenharmony_ci HILOGE("Want parcel Base64Str unmarshal fail, ret %{public}d.", ret); 426686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 427686862fbSopenharmony_ci } 428686862fbSopenharmony_ci auto wantPtr = AAFwk::Want::Unmarshalling(wantParcel); 429686862fbSopenharmony_ci if (wantPtr == nullptr) { 430686862fbSopenharmony_ci cJSON_Delete(rootValue); 431686862fbSopenharmony_ci HILOGE("AAFwk Want unmarshalling fail, check return null."); 432686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 433686862fbSopenharmony_ci } 434686862fbSopenharmony_ci want_ = *wantPtr; 435686862fbSopenharmony_ci 436686862fbSopenharmony_ci cJSON *abilityInfoStr = cJSON_GetObjectItemCaseSensitive(rootValue, "AbilityInfo"); 437686862fbSopenharmony_ci if (abilityInfoStr == nullptr || !cJSON_IsString(abilityInfoStr) || (abilityInfoStr->valuestring == nullptr)) { 438686862fbSopenharmony_ci cJSON_Delete(rootValue); 439686862fbSopenharmony_ci HILOGE("AbilityInfo term is null or not string."); 440686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 441686862fbSopenharmony_ci } 442686862fbSopenharmony_ci Parcel abilityParcel; 443686862fbSopenharmony_ci ret = Base64StrToParcel(abilityInfoStr->valuestring, abilityParcel); 444686862fbSopenharmony_ci if (ret != ERR_OK) { 445686862fbSopenharmony_ci cJSON_Delete(rootValue); 446686862fbSopenharmony_ci HILOGE("AbilityInfo parcel Base64Str unmarshal fail, ret %{public}d.", ret); 447686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 448686862fbSopenharmony_ci } 449686862fbSopenharmony_ci auto abilityInfoPtr = AppExecFwk::CompatibleAbilityInfo::Unmarshalling(abilityParcel); 450686862fbSopenharmony_ci if (abilityInfoPtr == nullptr) { 451686862fbSopenharmony_ci cJSON_Delete(rootValue); 452686862fbSopenharmony_ci HILOGE("AppExecFwk CompatibleAbilityInfo unmarshalling fail, check return null."); 453686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 454686862fbSopenharmony_ci } 455686862fbSopenharmony_ci abilityInfo_ = *abilityInfoPtr; 456686862fbSopenharmony_ci 457686862fbSopenharmony_ci cJSON_Delete(rootValue); 458686862fbSopenharmony_ci return ERR_OK; 459686862fbSopenharmony_ci} 460686862fbSopenharmony_ci 461686862fbSopenharmony_ciint32_t DSchedContinueDataCmd::UnmarshalCallerInfo(std::string &jsonStr) 462686862fbSopenharmony_ci{ 463686862fbSopenharmony_ci cJSON *rootValue = cJSON_Parse(jsonStr.c_str()); 464686862fbSopenharmony_ci if (rootValue == nullptr) { 465686862fbSopenharmony_ci HILOGE("Caller info json string parse to cjson fail."); 466686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 467686862fbSopenharmony_ci } 468686862fbSopenharmony_ci 469686862fbSopenharmony_ci const char *strKeys[] = { 470686862fbSopenharmony_ci "SourceDeviceId", "CallerAppId" 471686862fbSopenharmony_ci }; 472686862fbSopenharmony_ci std::string *strValues[] = { 473686862fbSopenharmony_ci &callerInfo_.sourceDeviceId, &callerInfo_.callerAppId 474686862fbSopenharmony_ci }; 475686862fbSopenharmony_ci int32_t strLength = sizeof(strKeys) / sizeof(strKeys[0]); 476686862fbSopenharmony_ci for (int32_t i = 0; i < strLength; i++) { 477686862fbSopenharmony_ci cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, strKeys[i]); 478686862fbSopenharmony_ci if (item == nullptr || !cJSON_IsString(item) || (item->valuestring == nullptr)) { 479686862fbSopenharmony_ci cJSON_Delete(rootValue); 480686862fbSopenharmony_ci HILOGE("Caller info json %{public}s term is null or not string.", strKeys[i]); 481686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 482686862fbSopenharmony_ci } 483686862fbSopenharmony_ci *strValues[i] = item->valuestring; 484686862fbSopenharmony_ci } 485686862fbSopenharmony_ci 486686862fbSopenharmony_ci const char *numKeys[] = { 487686862fbSopenharmony_ci "Uid", "Pid", "CallerType", "Duid" 488686862fbSopenharmony_ci }; 489686862fbSopenharmony_ci int32_t *numValues[] = { 490686862fbSopenharmony_ci &callerInfo_.uid, &callerInfo_.pid, &callerInfo_.callerType, &callerInfo_.duid 491686862fbSopenharmony_ci }; 492686862fbSopenharmony_ci int32_t numLength = sizeof(numKeys) / sizeof(numKeys[0]); 493686862fbSopenharmony_ci for (int32_t i = 0; i < numLength; i++) { 494686862fbSopenharmony_ci cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, numKeys[i]); 495686862fbSopenharmony_ci if (item == nullptr || !cJSON_IsNumber(item)) { 496686862fbSopenharmony_ci cJSON_Delete(rootValue); 497686862fbSopenharmony_ci HILOGE("Caller info json %{public}s term is null or not number.", numKeys[i]); 498686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 499686862fbSopenharmony_ci } 500686862fbSopenharmony_ci *numValues[i] = item->valueint; 501686862fbSopenharmony_ci } 502686862fbSopenharmony_ci 503686862fbSopenharmony_ci if (UnmarshalCallerInfoExtra(jsonStr) != ERR_OK) { 504686862fbSopenharmony_ci cJSON_Delete(rootValue); 505686862fbSopenharmony_ci HILOGE("Unmarshal CallerInfoExtra term from caller info json string fail."); 506686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 507686862fbSopenharmony_ci } 508686862fbSopenharmony_ci 509686862fbSopenharmony_ci cJSON_Delete(rootValue); 510686862fbSopenharmony_ci return ERR_OK; 511686862fbSopenharmony_ci} 512686862fbSopenharmony_ci 513686862fbSopenharmony_ciint32_t DSchedContinueDataCmd::UnmarshalCallerInfoExtra(std::string &jsonStr) 514686862fbSopenharmony_ci{ 515686862fbSopenharmony_ci cJSON *rootValue = cJSON_Parse(jsonStr.c_str()); 516686862fbSopenharmony_ci if (rootValue == nullptr) { 517686862fbSopenharmony_ci HILOGE("Caller info extra json string parse to cjson fail."); 518686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 519686862fbSopenharmony_ci } 520686862fbSopenharmony_ci 521686862fbSopenharmony_ci cJSON *bundleName = nullptr; 522686862fbSopenharmony_ci std::vector<std::string> bundleNameList; 523686862fbSopenharmony_ci cJSON *bundleNames = cJSON_GetObjectItemCaseSensitive(rootValue, "BundleNames"); 524686862fbSopenharmony_ci cJSON_ArrayForEach(bundleName, bundleNames) { 525686862fbSopenharmony_ci if (bundleName == nullptr || !cJSON_IsString(bundleName) || (bundleName->valuestring == nullptr)) { 526686862fbSopenharmony_ci cJSON_Delete(rootValue); 527686862fbSopenharmony_ci HILOGE("BundleNames term in CallerInfoExtra json is null or not string."); 528686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 529686862fbSopenharmony_ci } 530686862fbSopenharmony_ci bundleNameList.push_back(bundleName->valuestring); 531686862fbSopenharmony_ci } 532686862fbSopenharmony_ci callerInfo_.bundleNames = bundleNameList; 533686862fbSopenharmony_ci 534686862fbSopenharmony_ci cJSON *extraInfo = cJSON_GetObjectItemCaseSensitive(rootValue, "ExtraInfo"); 535686862fbSopenharmony_ci if (extraInfo == nullptr || !cJSON_IsString(extraInfo) || (extraInfo->valuestring == nullptr)) { 536686862fbSopenharmony_ci cJSON_Delete(rootValue); 537686862fbSopenharmony_ci HILOGE("ExtraInfo term in CallerInfoExtra json is null or not string."); 538686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 539686862fbSopenharmony_ci } 540686862fbSopenharmony_ci cJSON *extraInfoValue = cJSON_Parse(extraInfo->valuestring); 541686862fbSopenharmony_ci if (extraInfoValue == nullptr) { 542686862fbSopenharmony_ci cJSON_Delete(rootValue); 543686862fbSopenharmony_ci HILOGE("ExtraInfo term json string parse to cjson fail in CallerInfoExtra json."); 544686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 545686862fbSopenharmony_ci } 546686862fbSopenharmony_ci 547686862fbSopenharmony_ci cJSON *accessToken = cJSON_GetObjectItemCaseSensitive(extraInfoValue, EXTRO_INFO_JSON_KEY_ACCESS_TOKEN); 548686862fbSopenharmony_ci if (accessToken != nullptr && cJSON_IsNumber(accessToken)) { 549686862fbSopenharmony_ci callerInfo_.accessToken = static_cast<unsigned int>(accessToken->valueint); 550686862fbSopenharmony_ci } 551686862fbSopenharmony_ci 552686862fbSopenharmony_ci cJSON *dmsVersion = cJSON_GetObjectItemCaseSensitive(extraInfoValue, DMS_VERSION_ID); 553686862fbSopenharmony_ci if (dmsVersion != nullptr && !cJSON_IsString(dmsVersion) && (dmsVersion->valuestring != nullptr)) { 554686862fbSopenharmony_ci callerInfo_.extraInfoJson[DMS_VERSION_ID] = dmsVersion->valuestring; 555686862fbSopenharmony_ci } 556686862fbSopenharmony_ci cJSON_Delete(extraInfoValue); 557686862fbSopenharmony_ci cJSON_Delete(rootValue); 558686862fbSopenharmony_ci return ERR_OK; 559686862fbSopenharmony_ci} 560686862fbSopenharmony_ci 561686862fbSopenharmony_ciint32_t DSchedContinueDataCmd::UnmarshalAccountInfo(std::string &jsonStr) 562686862fbSopenharmony_ci{ 563686862fbSopenharmony_ci cJSON *rootValue = cJSON_Parse(jsonStr.c_str()); 564686862fbSopenharmony_ci if (rootValue == nullptr) { 565686862fbSopenharmony_ci HILOGE("Account info json string parse to cjson fail."); 566686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 567686862fbSopenharmony_ci } 568686862fbSopenharmony_ci 569686862fbSopenharmony_ci cJSON *accountType = cJSON_GetObjectItemCaseSensitive(rootValue, "AccountType"); 570686862fbSopenharmony_ci if (accountType == nullptr || !cJSON_IsNumber(accountType)) { 571686862fbSopenharmony_ci cJSON_Delete(rootValue); 572686862fbSopenharmony_ci HILOGE("AccountType term in account info json is null or not number."); 573686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 574686862fbSopenharmony_ci } 575686862fbSopenharmony_ci accountInfo_.accountType = accountType->valueint; 576686862fbSopenharmony_ci 577686862fbSopenharmony_ci cJSON *groupId = nullptr; 578686862fbSopenharmony_ci std::vector<std::string> groupIdList; 579686862fbSopenharmony_ci cJSON *groupIdListStr = cJSON_GetObjectItemCaseSensitive(rootValue, "groupIdList"); 580686862fbSopenharmony_ci cJSON_ArrayForEach(groupId, groupIdListStr) { 581686862fbSopenharmony_ci if (groupId == nullptr || !cJSON_IsString(groupId) || (groupId->valuestring == nullptr)) { 582686862fbSopenharmony_ci cJSON_Delete(rootValue); 583686862fbSopenharmony_ci HILOGE("groupId term in account info json is null or not string."); 584686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 585686862fbSopenharmony_ci } 586686862fbSopenharmony_ci groupIdList.push_back(groupId->valuestring); 587686862fbSopenharmony_ci } 588686862fbSopenharmony_ci accountInfo_.groupIdList = groupIdList; 589686862fbSopenharmony_ci 590686862fbSopenharmony_ci cJSON *accountId = cJSON_GetObjectItemCaseSensitive(rootValue, Constants::EXTRO_INFO_JSON_KEY_ACCOUNT_ID.c_str()); 591686862fbSopenharmony_ci if (accountId == nullptr || !cJSON_IsString(accountId)) { 592686862fbSopenharmony_ci HILOGE("accountId term in account info json is null or not string."); 593686862fbSopenharmony_ci } else { 594686862fbSopenharmony_ci accountInfo_.activeAccountId = accountId->valuestring; 595686862fbSopenharmony_ci } 596686862fbSopenharmony_ci cJSON *userId = cJSON_GetObjectItemCaseSensitive(rootValue, Constants::EXTRO_INFO_JSON_KEY_USERID_ID.c_str()); 597686862fbSopenharmony_ci if (userId == nullptr || !cJSON_IsNumber(userId)) { 598686862fbSopenharmony_ci HILOGE("userId term in account info json is null or not number."); 599686862fbSopenharmony_ci } else { 600686862fbSopenharmony_ci accountInfo_.userId = userId->valueint; 601686862fbSopenharmony_ci } 602686862fbSopenharmony_ci 603686862fbSopenharmony_ci cJSON_Delete(rootValue); 604686862fbSopenharmony_ci return ERR_OK; 605686862fbSopenharmony_ci} 606686862fbSopenharmony_ci 607686862fbSopenharmony_ciint32_t DSchedContinueReplyCmd::Marshal(std::string &jsonStr) 608686862fbSopenharmony_ci{ 609686862fbSopenharmony_ci cJSON *rootValue = cJSON_CreateObject(); 610686862fbSopenharmony_ci if (rootValue == nullptr) { 611686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 612686862fbSopenharmony_ci } 613686862fbSopenharmony_ci 614686862fbSopenharmony_ci std::string baseJsonStr; 615686862fbSopenharmony_ci if (DSchedContinueCmdBase::Marshal(baseJsonStr) != ERR_OK) { 616686862fbSopenharmony_ci cJSON_Delete(rootValue); 617686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 618686862fbSopenharmony_ci } 619686862fbSopenharmony_ci cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str()); 620686862fbSopenharmony_ci 621686862fbSopenharmony_ci cJSON_AddNumberToObject(rootValue, "ReplyCmd", replyCmd_); 622686862fbSopenharmony_ci cJSON_AddNumberToObject(rootValue, "AppVersion", appVersion_); 623686862fbSopenharmony_ci cJSON_AddNumberToObject(rootValue, "Result", result_); 624686862fbSopenharmony_ci cJSON_AddStringToObject(rootValue, "Reason", reason_.c_str()); 625686862fbSopenharmony_ci 626686862fbSopenharmony_ci char *data = cJSON_Print(rootValue); 627686862fbSopenharmony_ci if (data == nullptr) { 628686862fbSopenharmony_ci cJSON_Delete(rootValue); 629686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 630686862fbSopenharmony_ci } 631686862fbSopenharmony_ci jsonStr = std::string(data); 632686862fbSopenharmony_ci cJSON_Delete(rootValue); 633686862fbSopenharmony_ci cJSON_free(data); 634686862fbSopenharmony_ci return ERR_OK; 635686862fbSopenharmony_ci} 636686862fbSopenharmony_ci 637686862fbSopenharmony_ciint32_t DSchedContinueReplyCmd::Unmarshal(const std::string &jsonStr) 638686862fbSopenharmony_ci{ 639686862fbSopenharmony_ci cJSON *rootValue = cJSON_Parse(jsonStr.c_str()); 640686862fbSopenharmony_ci if (rootValue == nullptr) { 641686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 642686862fbSopenharmony_ci } 643686862fbSopenharmony_ci 644686862fbSopenharmony_ci cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd"); 645686862fbSopenharmony_ci if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) { 646686862fbSopenharmony_ci cJSON_Delete(rootValue); 647686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 648686862fbSopenharmony_ci } 649686862fbSopenharmony_ci std::string baseCmdStr = baseCmd->valuestring; 650686862fbSopenharmony_ci if (DSchedContinueCmdBase::Unmarshal(baseCmdStr) != ERR_OK) { 651686862fbSopenharmony_ci cJSON_Delete(rootValue); 652686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 653686862fbSopenharmony_ci } 654686862fbSopenharmony_ci 655686862fbSopenharmony_ci const char *numKeys[] = { 656686862fbSopenharmony_ci "ReplyCmd", "AppVersion", "Result" 657686862fbSopenharmony_ci }; 658686862fbSopenharmony_ci int32_t *numValues[] = { 659686862fbSopenharmony_ci &replyCmd_, &appVersion_, &result_ 660686862fbSopenharmony_ci }; 661686862fbSopenharmony_ci int32_t numLength = sizeof(numKeys) / sizeof(numKeys[0]); 662686862fbSopenharmony_ci for (int32_t i = 0; i < numLength; i++) { 663686862fbSopenharmony_ci cJSON *item = cJSON_GetObjectItemCaseSensitive(rootValue, numKeys[i]); 664686862fbSopenharmony_ci if (item == nullptr || !cJSON_IsNumber(item)) { 665686862fbSopenharmony_ci cJSON_Delete(rootValue); 666686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 667686862fbSopenharmony_ci } 668686862fbSopenharmony_ci *numValues[i] = item->valueint; 669686862fbSopenharmony_ci } 670686862fbSopenharmony_ci 671686862fbSopenharmony_ci cJSON *reason = cJSON_GetObjectItemCaseSensitive(rootValue, "Reason"); 672686862fbSopenharmony_ci if (reason == nullptr || !cJSON_IsString(reason) || (reason->valuestring == nullptr)) { 673686862fbSopenharmony_ci cJSON_Delete(rootValue); 674686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 675686862fbSopenharmony_ci } 676686862fbSopenharmony_ci reason_ = reason->valuestring; 677686862fbSopenharmony_ci 678686862fbSopenharmony_ci cJSON_Delete(rootValue); 679686862fbSopenharmony_ci return ERR_OK; 680686862fbSopenharmony_ci} 681686862fbSopenharmony_ci 682686862fbSopenharmony_ciint32_t DSchedContinueEndCmd::Marshal(std::string &jsonStr) 683686862fbSopenharmony_ci{ 684686862fbSopenharmony_ci cJSON *rootValue = cJSON_CreateObject(); 685686862fbSopenharmony_ci if (rootValue == nullptr) { 686686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 687686862fbSopenharmony_ci } 688686862fbSopenharmony_ci 689686862fbSopenharmony_ci std::string baseJsonStr; 690686862fbSopenharmony_ci if (DSchedContinueCmdBase::Marshal(baseJsonStr) != ERR_OK) { 691686862fbSopenharmony_ci cJSON_Delete(rootValue); 692686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 693686862fbSopenharmony_ci } 694686862fbSopenharmony_ci cJSON_AddStringToObject(rootValue, "BaseCmd", baseJsonStr.c_str()); 695686862fbSopenharmony_ci 696686862fbSopenharmony_ci cJSON_AddNumberToObject(rootValue, "Result", result_); 697686862fbSopenharmony_ci 698686862fbSopenharmony_ci char *data = cJSON_Print(rootValue); 699686862fbSopenharmony_ci if (data == nullptr) { 700686862fbSopenharmony_ci cJSON_Delete(rootValue); 701686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 702686862fbSopenharmony_ci } 703686862fbSopenharmony_ci jsonStr = std::string(data); 704686862fbSopenharmony_ci cJSON_Delete(rootValue); 705686862fbSopenharmony_ci cJSON_free(data); 706686862fbSopenharmony_ci return ERR_OK; 707686862fbSopenharmony_ci} 708686862fbSopenharmony_ci 709686862fbSopenharmony_ciint32_t DSchedContinueEndCmd::Unmarshal(const std::string &jsonStr) 710686862fbSopenharmony_ci{ 711686862fbSopenharmony_ci cJSON *rootValue = cJSON_Parse(jsonStr.c_str()); 712686862fbSopenharmony_ci if (rootValue == nullptr) { 713686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 714686862fbSopenharmony_ci } 715686862fbSopenharmony_ci 716686862fbSopenharmony_ci cJSON *baseCmd = cJSON_GetObjectItemCaseSensitive(rootValue, "BaseCmd"); 717686862fbSopenharmony_ci if (baseCmd == nullptr || !cJSON_IsString(baseCmd) || (baseCmd->valuestring == nullptr)) { 718686862fbSopenharmony_ci cJSON_Delete(rootValue); 719686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 720686862fbSopenharmony_ci } 721686862fbSopenharmony_ci std::string baseCmdStr = baseCmd->valuestring; 722686862fbSopenharmony_ci if (DSchedContinueCmdBase::Unmarshal(baseCmdStr) != ERR_OK) { 723686862fbSopenharmony_ci cJSON_Delete(rootValue); 724686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 725686862fbSopenharmony_ci } 726686862fbSopenharmony_ci 727686862fbSopenharmony_ci cJSON *result = cJSON_GetObjectItemCaseSensitive(rootValue, "Result"); 728686862fbSopenharmony_ci if (result == nullptr || !cJSON_IsNumber(result)) { 729686862fbSopenharmony_ci cJSON_Delete(rootValue); 730686862fbSopenharmony_ci return INVALID_PARAMETERS_ERR; 731686862fbSopenharmony_ci } 732686862fbSopenharmony_ci result_ = result->valueint; 733686862fbSopenharmony_ci 734686862fbSopenharmony_ci cJSON_Delete(rootValue); 735686862fbSopenharmony_ci return ERR_OK; 736686862fbSopenharmony_ci} 737686862fbSopenharmony_ci} // namespace DistributedSchedule 738686862fbSopenharmony_ci} // namespace OHOS 739