1 /*
2  * Copyright (c) 2021-2023 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 #include "cJSON.h"
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 DCameraEventCmd::Marshal(std::string& jsonStr)
25 {
26     if (value_ == nullptr) {
27         return DCAMERA_BAD_VALUE;
28     }
29     cJSON *rootValue = cJSON_CreateObject();
30     if (rootValue == nullptr) {
31         return DCAMERA_BAD_VALUE;
32     }
33     cJSON_AddStringToObject(rootValue, "Type", type_.c_str());
34     cJSON_AddStringToObject(rootValue, "dhId", dhId_.c_str());
35     cJSON_AddStringToObject(rootValue, "Command", command_.c_str());
36 
37     cJSON *event = cJSON_CreateObject();
38     if (event == nullptr) {
39         cJSON_Delete(rootValue);
40         return DCAMERA_BAD_VALUE;
41     }
42     cJSON_AddNumberToObject(event, "EventType", value_->eventType_);
43     cJSON_AddNumberToObject(event, "EventResult", value_->eventResult_);
44     cJSON_AddStringToObject(event, "EventContent", value_->eventContent_.c_str());
45     cJSON_AddItemToObject(rootValue, "Value", event);
46 
47     char *data = cJSON_Print(rootValue);
48     if (data == nullptr) {
49         cJSON_Delete(rootValue);
50         return DCAMERA_BAD_VALUE;
51     }
52     jsonStr = std::string(data);
53     cJSON_Delete(rootValue);
54     cJSON_free(data);
55     return DCAMERA_OK;
56 }
57 
Unmarshal(const std::string& jsonStr)58 int32_t DCameraEventCmd::Unmarshal(const std::string& jsonStr)
59 {
60     cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
61     if (rootValue == nullptr) {
62         return DCAMERA_BAD_VALUE;
63     }
64     cJSON *type = cJSON_GetObjectItemCaseSensitive(rootValue, "Type");
65     cJSON *dhId = cJSON_GetObjectItemCaseSensitive(rootValue, "dhId");
66     if (type == nullptr || !cJSON_IsString(type) || (type->valuestring == nullptr) ||
67         dhId == nullptr || !cJSON_IsString(dhId) || (dhId->valuestring == nullptr)) {
68         cJSON_Delete(rootValue);
69         return DCAMERA_BAD_VALUE;
70     }
71     type_ = type->valuestring;
72     dhId_ = dhId->valuestring;
73     cJSON *command = cJSON_GetObjectItemCaseSensitive(rootValue, "Command");
74     if (command == nullptr || !cJSON_IsString(command) || (command->valuestring == nullptr)) {
75         cJSON_Delete(rootValue);
76         return DCAMERA_BAD_VALUE;
77     }
78     command_ = command->valuestring;
79     cJSON *valueJson = cJSON_GetObjectItemCaseSensitive(rootValue, "Value");
80     if (valueJson == nullptr || !cJSON_IsObject(valueJson)) {
81         cJSON_Delete(rootValue);
82         return DCAMERA_BAD_VALUE;
83     }
84     cJSON *eventType = cJSON_GetObjectItemCaseSensitive(valueJson, "EventType");
85     if (eventType == nullptr || !cJSON_IsNumber(eventType)) {
86         cJSON_Delete(rootValue);
87         return DCAMERA_BAD_VALUE;
88     }
89     std::shared_ptr<DCameraEvent> event = std::make_shared<DCameraEvent>();
90     event->eventType_ = eventType->valueint;
91     cJSON *eventResult = cJSON_GetObjectItemCaseSensitive(valueJson, "EventResult");
92     if (eventResult == nullptr || !cJSON_IsNumber(eventResult)) {
93         cJSON_Delete(rootValue);
94         return DCAMERA_BAD_VALUE;
95     }
96     event->eventResult_ = eventResult->valueint;
97     cJSON *eventContent = cJSON_GetObjectItemCaseSensitive(valueJson, "EventContent");
98     if (eventContent == nullptr || !cJSON_IsString(eventContent) || (eventContent->valuestring == nullptr)) {
99         cJSON_Delete(rootValue);
100         return DCAMERA_BAD_VALUE;
101     }
102     event->eventContent_ = eventContent->valuestring;
103     value_ = event;
104     cJSON_Delete(rootValue);
105     return DCAMERA_OK;
106 }
107 } // namespace DistributedHardware
108 } // namespace OHOS
109