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