• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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     CHECK_NULL_RETURN((rootValue == nullptr), DCAMERA_BAD_VALUE);
28 
29     cJSON_AddStringToObject(rootValue, "Type", type_.c_str());
30     cJSON_AddStringToObject(rootValue, "dhId", dhId_.c_str());
31     cJSON_AddStringToObject(rootValue, "Command", command_.c_str());
32 
33     cJSON *captureInfos = cJSON_CreateArray();
34     CHECK_NULL_FREE_RETURN(captureInfos, DCAMERA_BAD_VALUE, rootValue);
35     cJSON_AddItemToObject(rootValue, "Value", captureInfos);
36     for (auto iter = value_.begin(); iter != value_.end(); iter++) {
37         std::shared_ptr<DCameraCaptureInfo> capture = *iter;
38         CHECK_NULL_FREE_RETURN(capture, DCAMERA_BAD_VALUE, rootValue);
39         cJSON *captureInfo = cJSON_CreateObject();
40         CHECK_NULL_FREE_RETURN(captureInfo, DCAMERA_BAD_VALUE, rootValue);
41         cJSON_AddItemToArray(captureInfos, captureInfo);
42         cJSON_AddNumberToObject(captureInfo, "Width", capture->width_);
43         cJSON_AddNumberToObject(captureInfo, "Height", capture->height_);
44         cJSON_AddNumberToObject(captureInfo, "Format", capture->format_);
45         cJSON_AddNumberToObject(captureInfo, "DataSpace", capture->dataspace_);
46         cJSON_AddBoolToObject(captureInfo, "IsCapture", capture->isCapture_);
47         cJSON_AddNumberToObject(captureInfo, "EncodeType", capture->encodeType_);
48         cJSON_AddNumberToObject(captureInfo, "StreamType", capture->streamType_);
49         cJSON *captureSettings = cJSON_CreateArray();
50         CHECK_NULL_FREE_RETURN(captureSettings, DCAMERA_BAD_VALUE, rootValue);
51         cJSON_AddItemToObject(captureInfo, "CaptureSettings", captureSettings);
52         for (auto settingIter = capture->captureSettings_.begin();
53             settingIter != capture->captureSettings_.end(); settingIter++) {
54             cJSON *captureSetting = cJSON_CreateObject();
55             CHECK_NULL_FREE_RETURN(captureSetting, DCAMERA_BAD_VALUE, rootValue);
56             cJSON_AddNumberToObject(captureSetting, "SettingType", (*settingIter)->type_);
57             cJSON_AddStringToObject(captureSetting, "SettingValue", (*settingIter)->value_.c_str());
58             cJSON_AddItemToArray(captureSettings, captureSetting);
59         }
60     }
61     cJSON_AddNumberToObject(rootValue, "mode", sceneMode_);
62     cJSON_AddNumberToObject(rootValue, "userId", userId_);
63     cJSON_AddNumberToObject(rootValue, "tokenId", tokenId_);
64     cJSON_AddStringToObject(rootValue, "accountId", accountId_.c_str());
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     CHECK_NULL_RETURN((rootValue == nullptr), DCAMERA_BAD_VALUE);
81 
82     cJSON *type = cJSON_GetObjectItemCaseSensitive(rootValue, "Type");
83     CHECK_AND_FREE_RETURN_RET_LOG((type == nullptr || !cJSON_IsString(type) || (type->valuestring == nullptr)),
84         DCAMERA_BAD_VALUE, rootValue, "type parse fail.");
85     type_ = type->valuestring;
86 
87     cJSON *dhId = cJSON_GetObjectItemCaseSensitive(rootValue, "dhId");
88     CHECK_AND_FREE_RETURN_RET_LOG((dhId == nullptr || !cJSON_IsString(dhId) || (dhId->valuestring == nullptr)),
89         DCAMERA_BAD_VALUE, rootValue, "dhId parse fail.");
90     dhId_ = dhId->valuestring;
91 
92     cJSON *command = cJSON_GetObjectItemCaseSensitive(rootValue, "Command");
93     if (command == nullptr || !cJSON_IsString(command) || (command->valuestring == nullptr)) {
94         cJSON_Delete(rootValue);
95         return DCAMERA_BAD_VALUE;
96     }
97     command_ = command->valuestring;
98 
99     int32_t ret = UmarshalValue(rootValue);
100 
101     cJSON *mode = cJSON_GetObjectItemCaseSensitive(rootValue, "mode");
102     if (mode == nullptr || !cJSON_IsNumber(mode)) {
103         sceneMode_ = 0;
104     } else {
105         sceneMode_ = mode->valueint;
106     }
107     cJSON *userId = cJSON_GetObjectItemCaseSensitive(rootValue, "userId");
108     if (userId == nullptr || !cJSON_IsNumber(userId)) {
109         userId_ = -1;
110     } else {
111         userId_ = userId->valueint;
112     }
113     cJSON *tokenId = cJSON_GetObjectItemCaseSensitive(rootValue, "tokenId");
114     if (tokenId == nullptr || !cJSON_IsNumber(tokenId)) {
115         tokenId_ = 0;
116     } else {
117         tokenId_ = static_cast<uint64_t>(tokenId->valueint);
118     }
119     cJSON *accountId = cJSON_GetObjectItemCaseSensitive(rootValue, "accountId");
120     if (accountId == nullptr || !cJSON_IsString(accountId) || (accountId->valuestring == nullptr)) {
121         accountId_ = "";
122     } else {
123         accountId_ = accountId->valuestring;
124     }
125     cJSON_Delete(rootValue);
126     return ret;
127 }
128 
UmarshalValue(cJSON * rootValue)129 int32_t DCameraCaptureInfoCmd::UmarshalValue(cJSON *rootValue)
130 {
131     cJSON *valueJson = cJSON_GetObjectItemCaseSensitive(rootValue, "Value");
132     if (valueJson == nullptr || !cJSON_IsArray(valueJson)) {
133         return DCAMERA_BAD_VALUE;
134     }
135     cJSON *capInfo = nullptr;
136     cJSON_ArrayForEach(capInfo, valueJson) {
137         std::shared_ptr<DCameraCaptureInfo> captureInfo = std::make_shared<DCameraCaptureInfo>();
138         cJSON *width = cJSON_GetObjectItemCaseSensitive(capInfo, "Width");
139         CHECK_NULL_RETURN((width == nullptr || !cJSON_IsNumber(width)), DCAMERA_BAD_VALUE);
140         captureInfo->width_ = width->valueint;
141 
142         cJSON *height = cJSON_GetObjectItemCaseSensitive(capInfo, "Height");
143         CHECK_NULL_RETURN((height == nullptr || !cJSON_IsNumber(height)), DCAMERA_BAD_VALUE);
144         captureInfo->height_ = height->valueint;
145 
146         cJSON *format = cJSON_GetObjectItemCaseSensitive(capInfo, "Format");
147         CHECK_NULL_RETURN((format == nullptr || !cJSON_IsNumber(format)), DCAMERA_BAD_VALUE);
148         captureInfo->format_ = format->valueint;
149 
150         cJSON *dataSpace = cJSON_GetObjectItemCaseSensitive(capInfo, "DataSpace");
151         CHECK_NULL_RETURN((dataSpace == nullptr || !cJSON_IsNumber(dataSpace)), DCAMERA_BAD_VALUE);
152         captureInfo->dataspace_ = dataSpace->valueint;
153 
154         cJSON *isCapture = cJSON_GetObjectItemCaseSensitive(capInfo, "IsCapture");
155         CHECK_NULL_RETURN((isCapture == nullptr || !cJSON_IsBool(isCapture)), DCAMERA_BAD_VALUE);
156         captureInfo->isCapture_ = cJSON_IsTrue(isCapture);
157 
158         cJSON *encodeType = cJSON_GetObjectItemCaseSensitive(capInfo, "EncodeType");
159         CHECK_NULL_RETURN((encodeType == nullptr || !cJSON_IsNumber(encodeType)), DCAMERA_BAD_VALUE);
160         captureInfo->encodeType_ = static_cast<DCEncodeType>(encodeType->valueint);
161 
162         cJSON *streamType = cJSON_GetObjectItemCaseSensitive(capInfo, "StreamType");
163         CHECK_NULL_RETURN((streamType == nullptr || !cJSON_IsNumber(streamType)), DCAMERA_BAD_VALUE);
164         captureInfo->streamType_ = static_cast<DCStreamType>(streamType->valueint);
165 
166         cJSON *captureSettings = cJSON_GetObjectItemCaseSensitive(capInfo, "CaptureSettings");
167         CHECK_NULL_RETURN((captureSettings == nullptr || !cJSON_IsArray(captureSettings)), DCAMERA_BAD_VALUE);
168         int32_t ret = UmarshalSettings(captureSettings, captureInfo);
169         if (ret != DCAMERA_OK) {
170             return ret;
171         }
172         value_.push_back(captureInfo);
173     }
174     return DCAMERA_OK;
175 }
176 
UmarshalSettings(cJSON * valueJson,std::shared_ptr<DCameraCaptureInfo> & captureInfo)177 int32_t DCameraCaptureInfoCmd::UmarshalSettings(cJSON *valueJson,
178     std::shared_ptr<DCameraCaptureInfo>& captureInfo)
179 {
180     if (captureInfo == nullptr) {
181         return DCAMERA_BAD_VALUE;
182     }
183     cJSON *captureSetting = nullptr;
184     cJSON_ArrayForEach(captureSetting, valueJson) {
185         cJSON *settingType = cJSON_GetObjectItemCaseSensitive(captureSetting, "SettingType");
186         if (settingType == nullptr || !cJSON_IsNumber(settingType)) {
187             return DCAMERA_BAD_VALUE;
188         }
189 
190         cJSON *settingValue = cJSON_GetObjectItemCaseSensitive(captureSetting, "SettingValue");
191         if (settingValue == nullptr || !cJSON_IsString(settingValue) ||
192             (settingValue->valuestring == nullptr)) {
193             return DCAMERA_BAD_VALUE;
194         }
195         std::shared_ptr<DCameraSettings> setting = std::make_shared<DCameraSettings>();
196         setting->type_ = static_cast<DCSettingsType>(settingType->valueint);
197         setting->value_ = settingValue->valuestring;
198         captureInfo->captureSettings_.push_back(setting);
199     }
200     return DCAMERA_OK;
201 }
202 } // namespace DistributedHardware
203 } // namespace OHOS
204