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