1 /* 2 * Copyright (c) 2021 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 "dcamera_open_info_cmd.h" 17 18 #include "json/json.h" 19 20 #include "distributed_camera_constants.h" 21 #include "distributed_camera_errno.h" 22 #include "distributed_hardware_log.h" 23 24 namespace OHOS { 25 namespace DistributedHardware { Marshal(std::string & jsonStr)26int32_t DCameraOpenInfoCmd::Marshal(std::string& jsonStr) 27 { 28 Json::Value rootValue; 29 rootValue["Type"] = Json::Value(type_); 30 rootValue["dhId"] = Json::Value(dhId_); 31 rootValue["Command"] = Json::Value(command_); 32 33 Json::Value openInfo; 34 openInfo["SourceDevId"] = Json::Value(value_->sourceDevId_); 35 rootValue["Value"] = openInfo; 36 37 jsonStr = rootValue.toStyledString(); 38 return DCAMERA_OK; 39 } 40 Unmarshal(const std::string & jsonStr)41int32_t DCameraOpenInfoCmd::Unmarshal(const std::string& jsonStr) 42 { 43 JSONCPP_STRING errs; 44 Json::CharReaderBuilder readerBuilder; 45 Json::Value rootValue; 46 47 std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader()); 48 if (!jsonReader->parse(jsonStr.c_str(), jsonStr.c_str() + jsonStr.length(), &rootValue, &errs) || 49 !rootValue.isObject()) { 50 return DCAMERA_BAD_VALUE; 51 } 52 53 if (!rootValue.isMember("Type") || !rootValue["Type"].isString()) { 54 return DCAMERA_BAD_VALUE; 55 } 56 type_ = rootValue["Type"].asString(); 57 58 if (!rootValue.isMember("dhId") || !rootValue["dhId"].isString()) { 59 return DCAMERA_BAD_VALUE; 60 } 61 dhId_ = rootValue["dhId"].asString(); 62 63 if (!rootValue.isMember("Command") || !rootValue["Command"].isString()) { 64 return DCAMERA_BAD_VALUE; 65 } 66 command_ = rootValue["Command"].asString(); 67 68 if (!rootValue.isMember("Value") || !rootValue["Value"].isObject()) { 69 return DCAMERA_BAD_VALUE; 70 } 71 Json::Value valueJson = rootValue["Value"]; 72 73 if (!valueJson.isMember("SourceDevId") || !valueJson["SourceDevId"].isString()) { 74 return DCAMERA_BAD_VALUE; 75 } 76 std::shared_ptr<DCameraOpenInfo> openInfo = std::make_shared<DCameraOpenInfo>(); 77 openInfo->sourceDevId_ = valueJson["SourceDevId"].asString(); 78 value_ = openInfo; 79 return DCAMERA_OK; 80 } 81 } // namespace DistributedHardware 82 } // namespace OHOS 83