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_capture_info_cmd.h"
17
18 #include "distributed_camera_constants.h"
19 #include "distributed_camera_errno.h"
20 #include "distributed_hardware_log.h"
21
22 namespace OHOS {
23 namespace DistributedHardware {
Marshal(std::string & jsonStr)24 int32_t DCameraCaptureInfoCmd::Marshal(std::string& jsonStr)
25 {
26 Json::Value rootValue;
27 rootValue["Type"] = Json::Value(type_);
28 rootValue["dhId"] = Json::Value(dhId_);
29 rootValue["Command"] = Json::Value(command_);
30 Json::Value captureInfos;
31 for (auto iter = value_.begin(); iter != value_.end(); iter++) {
32 Json::Value captureInfo;
33 std::shared_ptr<DCameraCaptureInfo> capture = *iter;
34 captureInfo["Width"] = Json::Value(capture->width_);
35 captureInfo["Height"] = Json::Value(capture->height_);
36 captureInfo["Format"] = Json::Value(capture->format_);
37 captureInfo["DataSpace"] = Json::Value(capture->dataspace_);
38 captureInfo["IsCapture"] = Json::Value(capture->isCapture_);
39 captureInfo["EncodeType"] = Json::Value(capture->encodeType_);
40 captureInfo["StreamType"] = Json::Value(capture->streamType_);
41
42 Json::Value captureSettings;
43 for (auto settingIter = capture->captureSettings_.begin();
44 settingIter != capture->captureSettings_.end(); settingIter++) {
45 Json::Value captureSetting;
46 captureSetting["SettingType"] = Json::Value((*settingIter)->type_);
47 captureSetting["SettingValue"] = Json::Value((*settingIter)->value_);
48 captureSettings.append(captureSetting);
49 }
50
51 captureInfo["CaptureSettings"] = captureSettings;
52 captureInfos.append(captureInfo);
53 }
54
55 rootValue["Value"] = captureInfos;
56 jsonStr = rootValue.toStyledString();
57 return DCAMERA_OK;
58 }
59
Unmarshal(const std::string & jsonStr)60 int32_t DCameraCaptureInfoCmd::Unmarshal(const std::string& jsonStr)
61 {
62 JSONCPP_STRING errs;
63 Json::CharReaderBuilder readerBuilder;
64 Json::Value rootValue;
65
66 std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
67 if (!jsonReader->parse(jsonStr.c_str(), jsonStr.c_str() + jsonStr.length(), &rootValue, &errs) ||
68 !rootValue.isObject()) {
69 return DCAMERA_BAD_VALUE;
70 }
71
72 if (!rootValue.isMember("Type") || !rootValue["Type"].isString()) {
73 return DCAMERA_BAD_VALUE;
74 }
75 type_ = rootValue["Type"].asString();
76
77 if (!rootValue.isMember("dhId") || !rootValue["dhId"].isString()) {
78 return DCAMERA_BAD_VALUE;
79 }
80 dhId_ = rootValue["dhId"].asString();
81
82 if (!rootValue.isMember("Command") || !rootValue["Command"].isString()) {
83 return DCAMERA_BAD_VALUE;
84 }
85 command_ = rootValue["Command"].asString();
86
87 if (!rootValue.isMember("Value") || !rootValue["Value"].isArray()) {
88 return DCAMERA_BAD_VALUE;
89 }
90
91 int32_t ret = UmarshalValue(rootValue);
92 if (ret != DCAMERA_OK) {
93 return ret;
94 }
95 return DCAMERA_OK;
96 }
97
UmarshalValue(Json::Value & rootValue)98 int32_t DCameraCaptureInfoCmd::UmarshalValue(Json::Value& rootValue)
99 {
100 for (Json::ArrayIndex i = 0; i < rootValue["Value"].size(); i++) {
101 Json::Value valueJson = rootValue["Value"][i];
102 std::shared_ptr<DCameraCaptureInfo> captureInfo = std::make_shared<DCameraCaptureInfo>();
103 if (!valueJson.isMember("Width") || !valueJson["Width"].isInt()) {
104 return DCAMERA_BAD_VALUE;
105 }
106 captureInfo->width_ = valueJson["Width"].asInt();
107
108 if (!valueJson.isMember("Height") || !valueJson["Height"].isInt()) {
109 return DCAMERA_BAD_VALUE;
110 }
111 captureInfo->height_ = valueJson["Height"].asInt();
112
113 if (!valueJson.isMember("Format") || !valueJson["Format"].isInt()) {
114 return DCAMERA_BAD_VALUE;
115 }
116 captureInfo->format_ = valueJson["Format"].asInt();
117
118 if (!valueJson.isMember("DataSpace") || !valueJson["DataSpace"].isInt()) {
119 return DCAMERA_BAD_VALUE;
120 }
121 captureInfo->dataspace_ = valueJson["DataSpace"].asInt();
122
123 if (!valueJson.isMember("IsCapture") || !valueJson["IsCapture"].isBool()) {
124 return DCAMERA_BAD_VALUE;
125 }
126 captureInfo->isCapture_ = valueJson["IsCapture"].asBool();
127
128 if (!valueJson.isMember("EncodeType") || !valueJson["EncodeType"].isInt()) {
129 return DCAMERA_BAD_VALUE;
130 }
131 captureInfo->encodeType_ = (DCEncodeType)valueJson["EncodeType"].asInt();
132
133 if (!valueJson.isMember("StreamType") || !valueJson["StreamType"].isInt()) {
134 return DCAMERA_BAD_VALUE;
135 }
136 captureInfo->streamType_ = (DCStreamType)valueJson["StreamType"].asInt();
137
138 if (!valueJson.isMember("CaptureSettings") || !valueJson["CaptureSettings"].isArray()) {
139 return DCAMERA_BAD_VALUE;
140 }
141
142 int32_t ret = UmarshalSettings(valueJson, captureInfo);
143 if (ret != DCAMERA_OK) {
144 return ret;
145 }
146 value_.push_back(captureInfo);
147 }
148 return DCAMERA_OK;
149 }
150
UmarshalSettings(Json::Value & valueJson,std::shared_ptr<DCameraCaptureInfo> & captureInfo)151 int32_t DCameraCaptureInfoCmd::UmarshalSettings(Json::Value& valueJson,
152 std::shared_ptr<DCameraCaptureInfo>& captureInfo)
153 {
154 for (Json::ArrayIndex j = 0; j < valueJson["CaptureSettings"].size(); j++) {
155 Json::Value settingJson = valueJson["CaptureSettings"][j];
156 if (!settingJson.isMember("SettingType") || !settingJson["SettingType"].isInt()) {
157 return DCAMERA_BAD_VALUE;
158 }
159 if (!settingJson.isMember("SettingValue") || !settingJson["SettingValue"].isString()) {
160 return DCAMERA_BAD_VALUE;
161 }
162 std::shared_ptr<DCameraSettings> setting = std::make_shared<DCameraSettings>();
163 setting->type_ = static_cast<DCSettingsType>(settingJson["SettingType"].asInt());
164 setting->value_ = settingJson["SettingValue"].asString();
165 captureInfo->captureSettings_.push_back(setting);
166 }
167 return DCAMERA_OK;
168 }
169 } // namespace DistributedHardware
170 } // namespace OHOS
171