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_open_info_cmd.h"
17 #include "cJSON.h"
18
19 #include "distributed_camera_constants.h"
20 #include "distributed_camera_errno.h"
21 #include "distributed_hardware_log.h"
22
23 namespace OHOS {
24 namespace DistributedHardware {
Marshal(std::string& jsonStr)25 int32_t DCameraOpenInfoCmd::Marshal(std::string& jsonStr)
26 {
27 if (value_ == nullptr) {
28 return DCAMERA_BAD_VALUE;
29 }
30 cJSON *rootValue = cJSON_CreateObject();
31 if (rootValue == nullptr) {
32 return DCAMERA_BAD_VALUE;
33 }
34 cJSON_AddStringToObject(rootValue, "Type", type_.c_str());
35 cJSON_AddStringToObject(rootValue, "dhId", dhId_.c_str());
36 cJSON_AddStringToObject(rootValue, "Command", command_.c_str());
37
38 cJSON *openInfo = cJSON_CreateObject();
39 if (openInfo == nullptr) {
40 cJSON_Delete(rootValue);
41 return DCAMERA_BAD_VALUE;
42 }
43 cJSON_AddStringToObject(openInfo, "SourceDevId", value_->sourceDevId_.c_str());
44 cJSON_AddItemToObject(rootValue, "Value", openInfo);
45
46 char *data = cJSON_Print(rootValue);
47 if (data == nullptr) {
48 cJSON_Delete(rootValue);
49 return DCAMERA_BAD_VALUE;
50 }
51 jsonStr = std::string(data);
52 cJSON_Delete(rootValue);
53 cJSON_free(data);
54 return DCAMERA_OK;
55 }
56
Unmarshal(const std::string& jsonStr)57 int32_t DCameraOpenInfoCmd::Unmarshal(const std::string& jsonStr)
58 {
59 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
60 if (rootValue == nullptr) {
61 return DCAMERA_BAD_VALUE;
62 }
63 cJSON *type = cJSON_GetObjectItemCaseSensitive(rootValue, "Type");
64 if (type == nullptr || !cJSON_IsString(type) || (type->valuestring == nullptr)) {
65 cJSON_Delete(rootValue);
66 return DCAMERA_BAD_VALUE;
67 }
68 type_ = type->valuestring;
69 cJSON *dhId = cJSON_GetObjectItemCaseSensitive(rootValue, "dhId");
70 if (dhId == nullptr || !cJSON_IsString(dhId) || (dhId->valuestring == nullptr)) {
71 cJSON_Delete(rootValue);
72 return DCAMERA_BAD_VALUE;
73 }
74 dhId_ = dhId->valuestring;
75 cJSON *command = cJSON_GetObjectItemCaseSensitive(rootValue, "Command");
76 if (command == nullptr || !cJSON_IsString(command) || (command->valuestring == nullptr)) {
77 cJSON_Delete(rootValue);
78 return DCAMERA_BAD_VALUE;
79 }
80 command_ = command->valuestring;
81 cJSON *valueJson = cJSON_GetObjectItemCaseSensitive(rootValue, "Value");
82 if (valueJson == nullptr || !cJSON_IsObject(valueJson)) {
83 cJSON_Delete(rootValue);
84 return DCAMERA_BAD_VALUE;
85 }
86 cJSON *sourceDevId = cJSON_GetObjectItemCaseSensitive(valueJson, "SourceDevId");
87 if (sourceDevId == nullptr || !cJSON_IsString(sourceDevId) || (sourceDevId->valuestring == nullptr)) {
88 cJSON_Delete(rootValue);
89 return DCAMERA_BAD_VALUE;
90 }
91 std::shared_ptr<DCameraOpenInfo> openInfo = std::make_shared<DCameraOpenInfo>();
92 openInfo->sourceDevId_ = sourceDevId->valuestring;
93 value_ = openInfo;
94 cJSON_Delete(rootValue);
95 return DCAMERA_OK;
96 }
97 } // namespace DistributedHardware
98 } // namespace OHOS
99