• 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_event_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 DCameraEventCmd::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 event;
34     event["EventType"] = Json::Value(value_->eventType_);
35     event["EventResult"] = Json::Value(value_->eventResult_);
36     event["EventContent"] = Json::Value(value_->eventContent_);
37     rootValue["Value"] = event;
38 
39     jsonStr = rootValue.toStyledString();
40     return DCAMERA_OK;
41 }
42 
Unmarshal(const std::string & jsonStr)43 int32_t DCameraEventCmd::Unmarshal(const std::string& jsonStr)
44 {
45     JSONCPP_STRING errs;
46     Json::CharReaderBuilder readerBuilder;
47     Json::Value rootValue;
48 
49     std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
50     if (!jsonReader->parse(jsonStr.c_str(), jsonStr.c_str() + jsonStr.length(), &rootValue, &errs) ||
51         !rootValue.isObject()) {
52         return DCAMERA_BAD_VALUE;
53     }
54 
55     if (!rootValue.isMember("Type") || !rootValue["Type"].isString()) {
56         return DCAMERA_BAD_VALUE;
57     }
58     type_ = rootValue["Type"].asString();
59 
60     if (!rootValue.isMember("dhId") || !rootValue["dhId"].isString()) {
61         return DCAMERA_BAD_VALUE;
62     }
63     dhId_ = rootValue["dhId"].asString();
64 
65     if (!rootValue.isMember("Command") || !rootValue["Command"].isString()) {
66         return DCAMERA_BAD_VALUE;
67     }
68     command_ = rootValue["Command"].asString();
69 
70     if (!rootValue.isMember("Value") || !rootValue["Value"].isObject()) {
71         return DCAMERA_BAD_VALUE;
72     }
73     Json::Value valueJson = rootValue["Value"];
74 
75     if (!valueJson.isMember("EventType") || !valueJson["EventType"].isInt()) {
76         return DCAMERA_BAD_VALUE;
77     }
78     std::shared_ptr<DCameraEvent> event = std::make_shared<DCameraEvent>();
79     event->eventType_ = valueJson["EventType"].asInt();
80 
81     if (!valueJson.isMember("EventResult") || !valueJson["EventResult"].isInt()) {
82         return DCAMERA_BAD_VALUE;
83     }
84     event->eventResult_ = valueJson["EventResult"].asInt();
85 
86     if (!valueJson.isMember("EventContent") || !valueJson["EventContent"].isString()) {
87         return DCAMERA_BAD_VALUE;
88     }
89     event->eventContent_ = valueJson["EventContent"].asString();
90 
91     value_ = event;
92     return DCAMERA_OK;
93 }
94 } // namespace DistributedHardware
95 } // namespace OHOS
96