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_channel_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 DCameraChannelInfoCmd::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 channelInfo; 34 channelInfo["SourceDevId"] = Json::Value(value_->sourceDevId_); 35 Json::Value details; 36 for (auto iter = value_->detail_.begin(); iter != value_->detail_.end(); iter++) { 37 Json::Value detail; 38 detail["DataSessionFlag"] = Json::Value(iter->dataSessionFlag_); 39 detail["StreamType"] = Json::Value(iter->streamType_); 40 details.append(detail); 41 } 42 channelInfo["Detail"] = details; 43 rootValue["Value"] = channelInfo; 44 45 jsonStr = rootValue.toStyledString(); 46 return DCAMERA_OK; 47 } 48 Unmarshal(const std::string & jsonStr)49int32_t DCameraChannelInfoCmd::Unmarshal(const std::string& jsonStr) 50 { 51 JSONCPP_STRING errs; 52 Json::CharReaderBuilder readerBuilder; 53 Json::Value rootValue; 54 55 std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader()); 56 if (!jsonReader->parse(jsonStr.c_str(), jsonStr.c_str() + jsonStr.length(), &rootValue, &errs) || 57 !rootValue.isObject()) { 58 return DCAMERA_BAD_VALUE; 59 } 60 61 if (!rootValue.isMember("Type") || !rootValue["Type"].isString()) { 62 return DCAMERA_BAD_VALUE; 63 } 64 type_ = rootValue["Type"].asString(); 65 66 if (!rootValue.isMember("dhId") || !rootValue["dhId"].isString()) { 67 return DCAMERA_BAD_VALUE; 68 } 69 dhId_ = rootValue["dhId"].asString(); 70 71 if (!rootValue.isMember("Command") || !rootValue["Command"].isString()) { 72 return DCAMERA_BAD_VALUE; 73 } 74 command_ = rootValue["Command"].asString(); 75 76 if (!rootValue.isMember("Value") || !rootValue["Value"].isObject()) { 77 return DCAMERA_BAD_VALUE; 78 } 79 Json::Value valueJson = rootValue["Value"]; 80 81 if (!valueJson.isMember("SourceDevId") || !valueJson["SourceDevId"].isString()) { 82 return DCAMERA_BAD_VALUE; 83 } 84 std::shared_ptr<DCameraChannelInfo> channelInfo = std::make_shared<DCameraChannelInfo>(); 85 channelInfo->sourceDevId_ = valueJson["SourceDevId"].asString(); 86 87 if (!valueJson.isMember("Detail") || !valueJson["Detail"].isArray()) { 88 return DCAMERA_BAD_VALUE; 89 } 90 91 for (Json::ArrayIndex i = 0; i < valueJson["Detail"].size(); i++) { 92 Json::Value detailJson = valueJson["Detail"][i]; 93 DCameraChannelDetail channelDetail; 94 if (!detailJson.isMember("DataSessionFlag") || !detailJson["DataSessionFlag"].isString()) { 95 return DCAMERA_BAD_VALUE; 96 } 97 if (!detailJson.isMember("StreamType") || !detailJson["StreamType"].isInt()) { 98 return DCAMERA_BAD_VALUE; 99 } 100 channelDetail.dataSessionFlag_ = detailJson["DataSessionFlag"].asString(); 101 channelDetail.streamType_ = static_cast<DCStreamType>(detailJson["StreamType"].asInt()); 102 channelInfo->detail_.push_back(channelDetail); 103 } 104 value_ = channelInfo; 105 return DCAMERA_OK; 106 } 107 } // namespace DistributedHardware 108 } // namespace OHOS 109