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_metadata_setting_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 DCameraMetadataSettingCmd::Marshal(std::string& jsonStr)
26 {
27 cJSON *rootValue = cJSON_CreateObject();
28 if (rootValue == nullptr) {
29 return DCAMERA_BAD_VALUE;
30 }
31 cJSON_AddStringToObject(rootValue, "Type", type_.c_str());
32 cJSON_AddStringToObject(rootValue, "dhId", dhId_.c_str());
33 cJSON_AddStringToObject(rootValue, "Command", command_.c_str());
34
35 cJSON *settings = cJSON_CreateArray();
36 if (settings == nullptr) {
37 cJSON_Delete(rootValue);
38 return DCAMERA_BAD_VALUE;
39 }
40 cJSON_AddItemToObject(rootValue, "Value", settings);
41 for (auto iter = value_.begin(); iter != value_.end(); iter++) {
42 if ((*iter) == nullptr) {
43 cJSON_Delete(rootValue);
44 return DCAMERA_BAD_VALUE;
45 }
46 cJSON *setting = cJSON_CreateObject();
47 if (setting == nullptr) {
48 cJSON_Delete(rootValue);
49 return DCAMERA_BAD_VALUE;
50 }
51 cJSON_AddNumberToObject(setting, "SettingType", (*iter)->type_);
52 cJSON_AddStringToObject(setting, "SettingValue", (*iter)->value_.c_str());
53 cJSON_AddItemToArray(settings, setting);
54 }
55
56 char *data = cJSON_Print(rootValue);
57 if (data == nullptr) {
58 cJSON_Delete(rootValue);
59 return DCAMERA_BAD_VALUE;
60 }
61 jsonStr = std::string(data);
62 cJSON_Delete(rootValue);
63 cJSON_free(data);
64 return DCAMERA_OK;
65 }
66
Unmarshal(const std::string& jsonStr)67 int32_t DCameraMetadataSettingCmd::Unmarshal(const std::string& jsonStr)
68 {
69 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
70 if (rootValue == nullptr) {
71 return DCAMERA_BAD_VALUE;
72 }
73 cJSON *type = cJSON_GetObjectItemCaseSensitive(rootValue, "Type");
74 if (type == nullptr || !cJSON_IsString(type) || (type->valuestring == nullptr)) {
75 cJSON_Delete(rootValue);
76 return DCAMERA_BAD_VALUE;
77 }
78 type_ = type->valuestring;
79 cJSON *dhId = cJSON_GetObjectItemCaseSensitive(rootValue, "dhId");
80 if (dhId == nullptr || !cJSON_IsString(dhId) || (dhId->valuestring == nullptr)) {
81 cJSON_Delete(rootValue);
82 return DCAMERA_BAD_VALUE;
83 }
84 dhId_ = dhId->valuestring;
85 cJSON *command = cJSON_GetObjectItemCaseSensitive(rootValue, "Command");
86 if (command == nullptr || !cJSON_IsString(command) || (command->valuestring == nullptr)) {
87 cJSON_Delete(rootValue);
88 return DCAMERA_BAD_VALUE;
89 }
90 command_ = command->valuestring;
91 cJSON *settings = cJSON_GetObjectItemCaseSensitive(rootValue, "Value");
92 if (settings == nullptr || !cJSON_IsArray(settings) || cJSON_GetArraySize(settings) == 0) {
93 cJSON_Delete(rootValue);
94 return DCAMERA_BAD_VALUE;
95 }
96 cJSON *subSetting = nullptr;
97 cJSON_ArrayForEach(subSetting, settings) {
98 cJSON *settingType = cJSON_GetObjectItemCaseSensitive(subSetting, "SettingType");
99 cJSON *settingValue = cJSON_GetObjectItemCaseSensitive(subSetting, "SettingValue");
100 if (settingType == nullptr || !cJSON_IsNumber(settingType)) {
101 cJSON_Delete(rootValue);
102 return DCAMERA_BAD_VALUE;
103 }
104 if (settingValue == nullptr || !cJSON_IsString(settingValue) || (settingValue->valuestring == nullptr)) {
105 cJSON_Delete(rootValue);
106 return DCAMERA_BAD_VALUE;
107 }
108 std::shared_ptr<DCameraSettings> setting = std::make_shared<DCameraSettings>();
109 setting->type_ = (DCSettingsType)settingType->valueint;
110 setting->value_ = settingValue->valuestring;
111 value_.push_back(setting);
112 }
113 cJSON_Delete(rootValue);
114 return DCAMERA_OK;
115 }
116 } // namespace DistributedHardware
117 } // namespace OHOS
118