• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_metadata_setting_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)26 int32_t DCameraMetadataSettingCmd::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 settings;
34     for (auto iter = value_.begin(); iter != value_.end(); iter++) {
35         Json::Value setting;
36         setting["SettingType"] = (*iter)->type_;
37         setting["SettingValue"] = (*iter)->value_;
38         settings.append(setting);
39     }
40 
41     rootValue["Value"] = settings;
42     jsonStr = rootValue.toStyledString();
43     return DCAMERA_OK;
44 }
45 
Unmarshal(const std::string & jsonStr)46 int32_t DCameraMetadataSettingCmd::Unmarshal(const std::string& jsonStr)
47 {
48     JSONCPP_STRING errs;
49     Json::CharReaderBuilder readerBuilder;
50     Json::Value rootValue;
51 
52     std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
53     if (!jsonReader->parse(jsonStr.c_str(), jsonStr.c_str() + jsonStr.length(), &rootValue, &errs) ||
54         !rootValue.isObject()) {
55         return DCAMERA_BAD_VALUE;
56     }
57 
58     if (!rootValue.isMember("Type") || !rootValue["Type"].isString()) {
59         return DCAMERA_BAD_VALUE;
60     }
61     type_ = rootValue["Type"].asString();
62 
63     if (!rootValue.isMember("dhId") || !rootValue["dhId"].isString()) {
64         return DCAMERA_BAD_VALUE;
65     }
66     dhId_ = rootValue["dhId"].asString();
67 
68     if (!rootValue.isMember("Command") || !rootValue["Command"].isString()) {
69         return DCAMERA_BAD_VALUE;
70     }
71     command_ = rootValue["Command"].asString();
72 
73     if (!rootValue.isMember("Value") || !rootValue["Value"].isArray()) {
74         return DCAMERA_BAD_VALUE;
75     }
76 
77     for (Json::ArrayIndex i = 0; i < rootValue["Value"].size(); i++) {
78         Json::Value valueJsonEle = rootValue["Value"][i];
79         if (!valueJsonEle.isMember("SettingType") || !valueJsonEle["SettingType"].isInt()) {
80             return DCAMERA_BAD_VALUE;
81         }
82         if (!valueJsonEle.isMember("SettingValue") || !valueJsonEle["SettingValue"].isString()) {
83             return DCAMERA_BAD_VALUE;
84         }
85         std::shared_ptr<DCameraSettings> setting = std::make_shared<DCameraSettings>();
86         setting->type_ = (DCSettingsType)valueJsonEle["SettingType"].asInt();
87         setting->value_ = valueJsonEle["SettingValue"].asString();
88         value_.push_back(setting);
89     }
90     return DCAMERA_OK;
91 }
92 } // namespace DistributedHardware
93 } // namespace OHOS
94