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