1 /*
2  * Copyright (c) 2021-2024 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_capture_info_cmd.h"
17 
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 DCameraCaptureInfoCmd::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 *captureInfos = cJSON_CreateArray();
35     CHECK_NULL_FREE_RETURN(captureInfos, DCAMERA_BAD_VALUE, rootValue);
36     cJSON_AddItemToObject(rootValue, "Value", captureInfos);
37     for (auto iter = value_.begin(); iter != value_.end(); iter++) {
38         cJSON *captureInfo = cJSON_CreateObject();
39         if (captureInfo == nullptr) {
40             cJSON_Delete(rootValue);
41             return DCAMERA_BAD_VALUE;
42         }
43         cJSON_AddItemToArray(captureInfos, captureInfo);
44         std::shared_ptr<DCameraCaptureInfo> capture = *iter;
45         cJSON_AddNumberToObject(captureInfo, "Width", capture->width_);
46         cJSON_AddNumberToObject(captureInfo, "Height", capture->height_);
47         cJSON_AddNumberToObject(captureInfo, "Format", capture->format_);
48         cJSON_AddNumberToObject(captureInfo, "DataSpace", capture->dataspace_);
49         cJSON_AddBoolToObject(captureInfo, "IsCapture", capture->isCapture_);
50         cJSON_AddNumberToObject(captureInfo, "EncodeType", capture->encodeType_);
51         cJSON_AddNumberToObject(captureInfo, "StreamType", capture->streamType_);
52         cJSON *captureSettings = cJSON_CreateArray();
53         CHECK_NULL_FREE_RETURN(captureSettings, DCAMERA_BAD_VALUE, rootValue);
54         for (auto settingIter = capture->captureSettings_.begin();
55             settingIter != capture->captureSettings_.end(); settingIter++) {
56             cJSON *captureSetting = cJSON_CreateObject();
57             CHECK_NULL_FREE_RETURN(captureSetting, DCAMERA_BAD_VALUE, rootValue);
58             cJSON_AddNumberToObject(captureSetting, "SettingType", (*settingIter)->type_);
59             cJSON_AddStringToObject(captureSetting, "SettingValue", (*settingIter)->value_.c_str());
60             cJSON_AddItemToArray(captureSettings, captureSetting);
61         }
62         cJSON_AddItemToObject(captureInfo, "CaptureSettings", captureSettings);
63     }
64     cJSON_AddNumberToObject(rootValue, "mode", sceneMode_);
65 
66     char *data = cJSON_Print(rootValue);
67     if (data == nullptr) {
68         cJSON_Delete(rootValue);
69         return DCAMERA_BAD_VALUE;
70     }
71     jsonStr = std::string(data);
72     cJSON_Delete(rootValue);
73     cJSON_free(data);
74     return DCAMERA_OK;
75 }
76 
Unmarshal(const std::string & jsonStr)77 int32_t DCameraCaptureInfoCmd::Unmarshal(const std::string& jsonStr)
78 {
79     cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
80     if (rootValue == nullptr) {
81         return DCAMERA_BAD_VALUE;
82     }
83     cJSON *type = cJSON_GetObjectItemCaseSensitive(rootValue, "Type");
84     if (type == nullptr || !cJSON_IsString(type) || (type->valuestring == nullptr)) {
85         cJSON_Delete(rootValue);
86         return DCAMERA_BAD_VALUE;
87     }
88     type_ = type->valuestring;
89 
90     cJSON *dhId = cJSON_GetObjectItemCaseSensitive(rootValue, "dhId");
91     if (dhId == nullptr || !cJSON_IsString(dhId) || (dhId->valuestring == nullptr)) {
92         cJSON_Delete(rootValue);
93         return DCAMERA_BAD_VALUE;
94     }
95     dhId_ = dhId->valuestring;
96 
97     cJSON *command = cJSON_GetObjectItemCaseSensitive(rootValue, "Command");
98     if (command == nullptr || !cJSON_IsString(command) || (command->valuestring == nullptr)) {
99         cJSON_Delete(rootValue);
100         return DCAMERA_BAD_VALUE;
101     }
102     command_ = command->valuestring;
103 
104     int32_t ret = UmarshalValue(rootValue);
105 
106     cJSON *mode = cJSON_GetObjectItemCaseSensitive(rootValue, "mode");
107     if (mode == nullptr || !cJSON_IsNumber(mode)) {
108         sceneMode_ = 0;
109     } else {
110         sceneMode_ = mode->valueint;
111     }
112     cJSON_Delete(rootValue);
113     return ret;
114 }
115 
UmarshalValue(cJSON * rootValue)116 int32_t DCameraCaptureInfoCmd::UmarshalValue(cJSON *rootValue)
117 {
118     cJSON *valueJson = cJSON_GetObjectItemCaseSensitive(rootValue, "Value");
119     if (valueJson == nullptr || !cJSON_IsArray(valueJson)) {
120         return DCAMERA_BAD_VALUE;
121     }
122     cJSON *capInfo = nullptr;
123     cJSON_ArrayForEach(capInfo, valueJson) {
124         std::shared_ptr<DCameraCaptureInfo> captureInfo = std::make_shared<DCameraCaptureInfo>();
125         cJSON *width = cJSON_GetObjectItemCaseSensitive(capInfo, "Width");
126         CHECK_NULL_RETURN((width == nullptr || !cJSON_IsNumber(width)), DCAMERA_BAD_VALUE);
127         captureInfo->width_ = width->valueint;
128 
129         cJSON *height = cJSON_GetObjectItemCaseSensitive(capInfo, "Height");
130         CHECK_NULL_RETURN((height == nullptr || !cJSON_IsNumber(height)), DCAMERA_BAD_VALUE);
131         captureInfo->height_ = height->valueint;
132 
133         cJSON *format = cJSON_GetObjectItemCaseSensitive(capInfo, "Format");
134         CHECK_NULL_RETURN((format == nullptr || !cJSON_IsNumber(format)), DCAMERA_BAD_VALUE);
135         captureInfo->format_ = format->valueint;
136 
137         cJSON *dataSpace = cJSON_GetObjectItemCaseSensitive(capInfo, "DataSpace");
138         CHECK_NULL_RETURN((dataSpace == nullptr || !cJSON_IsNumber(dataSpace)), DCAMERA_BAD_VALUE);
139         captureInfo->dataspace_ = dataSpace->valueint;
140 
141         cJSON *isCapture = cJSON_GetObjectItemCaseSensitive(capInfo, "IsCapture");
142         CHECK_NULL_RETURN((isCapture == nullptr || !cJSON_IsBool(isCapture)), DCAMERA_BAD_VALUE);
143         captureInfo->isCapture_ = cJSON_IsTrue(isCapture);
144 
145         cJSON *encodeType = cJSON_GetObjectItemCaseSensitive(capInfo, "EncodeType");
146         CHECK_NULL_RETURN((encodeType == nullptr || !cJSON_IsNumber(encodeType)), DCAMERA_BAD_VALUE);
147         captureInfo->encodeType_ = static_cast<DCEncodeType>(encodeType->valueint);
148 
149         cJSON *streamType = cJSON_GetObjectItemCaseSensitive(capInfo, "StreamType");
150         CHECK_NULL_RETURN((streamType == nullptr || !cJSON_IsNumber(streamType)), DCAMERA_BAD_VALUE);
151         captureInfo->streamType_ = static_cast<DCStreamType>(streamType->valueint);
152 
153         cJSON *captureSettings = cJSON_GetObjectItemCaseSensitive(capInfo, "CaptureSettings");
154         CHECK_NULL_RETURN((captureSettings == nullptr || !cJSON_IsArray(captureSettings)), DCAMERA_BAD_VALUE);
155         int32_t ret = UmarshalSettings(captureSettings, captureInfo);
156         if (ret != DCAMERA_OK) {
157             return ret;
158         }
159         value_.push_back(captureInfo);
160     }
161     return DCAMERA_OK;
162 }
163 
UmarshalSettings(cJSON * valueJson,std::shared_ptr<DCameraCaptureInfo> & captureInfo)164 int32_t DCameraCaptureInfoCmd::UmarshalSettings(cJSON *valueJson,
165     std::shared_ptr<DCameraCaptureInfo>& captureInfo)
166 {
167     cJSON *captureSetting = nullptr;
168     cJSON_ArrayForEach(captureSetting, valueJson) {
169         cJSON *settingType = cJSON_GetObjectItemCaseSensitive(captureSetting, "SettingType");
170         if (settingType == nullptr || !cJSON_IsNumber(settingType)) {
171             return DCAMERA_BAD_VALUE;
172         }
173 
174         cJSON *settingValue = cJSON_GetObjectItemCaseSensitive(captureSetting, "SettingValue");
175         if (settingValue == nullptr || !cJSON_IsString(settingValue) ||
176             (settingValue->valuestring == nullptr)) {
177             return DCAMERA_BAD_VALUE;
178         }
179         std::shared_ptr<DCameraSettings> setting = std::make_shared<DCameraSettings>();
180         setting->type_ = static_cast<DCSettingsType>(settingType->valueint);
181         setting->value_ = settingValue->valuestring;
182         captureInfo->captureSettings_.push_back(setting);
183     }
184     return DCAMERA_OK;
185 }
186 } // namespace DistributedHardware
187 } // namespace OHOS
188