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_channel_info_cmd.h"
17 #include "cJSON.h"
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 DCameraChannelInfoCmd::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 *channelInfo = cJSON_CreateObject();
35 if (channelInfo == nullptr) {
36 cJSON_Delete(rootValue);
37 return DCAMERA_BAD_VALUE;
38 }
39 cJSON_AddStringToObject(channelInfo, "SourceDevId", value_->sourceDevId_.c_str());
40 cJSON_AddItemToObject(rootValue, "Value", channelInfo);
41
42 cJSON *details = cJSON_CreateArray();
43 if (details == nullptr) {
44 cJSON_Delete(rootValue);
45 return DCAMERA_BAD_VALUE;
46 }
47 cJSON_AddItemToObject(channelInfo, "Detail", details);
48 for (auto iter = value_->detail_.begin(); iter != value_->detail_.end(); iter++) {
49 cJSON *detail = cJSON_CreateObject();
50 if (detail == nullptr) {
51 cJSON_Delete(rootValue);
52 return DCAMERA_BAD_VALUE;
53 }
54 cJSON_AddStringToObject(detail, "DataSessionFlag", iter->dataSessionFlag_.c_str());
55 cJSON_AddNumberToObject(detail, "StreamType", iter->streamType_);
56 cJSON_AddItemToArray(details, detail);
57 }
58
59 char *data = cJSON_Print(rootValue);
60 if (data == nullptr) {
61 cJSON_Delete(rootValue);
62 return DCAMERA_BAD_VALUE;
63 }
64 jsonStr = std::string(data);
65 cJSON_Delete(rootValue);
66 cJSON_free(data);
67 return DCAMERA_OK;
68 }
69
Unmarshal(const std::string & jsonStr)70 int32_t DCameraChannelInfoCmd::Unmarshal(const std::string& jsonStr)
71 {
72 cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
73 if (rootValue == nullptr) {
74 return DCAMERA_BAD_VALUE;
75 }
76 cJSON *type = cJSON_GetObjectItemCaseSensitive(rootValue, "Type");
77 if (type == nullptr || !cJSON_IsString(type) || (type->valuestring == nullptr)) {
78 cJSON_Delete(rootValue);
79 return DCAMERA_BAD_VALUE;
80 }
81 type_ = type->valuestring;
82 cJSON *dhId = cJSON_GetObjectItemCaseSensitive(rootValue, "dhId");
83 if (dhId == nullptr || !cJSON_IsString(dhId) || (dhId->valuestring == nullptr)) {
84 cJSON_Delete(rootValue);
85 return DCAMERA_BAD_VALUE;
86 }
87 dhId_ = dhId->valuestring;
88 cJSON *command = cJSON_GetObjectItemCaseSensitive(rootValue, "Command");
89 if (command == nullptr || !cJSON_IsString(command) || (command->valuestring == nullptr)) {
90 cJSON_Delete(rootValue);
91 return DCAMERA_BAD_VALUE;
92 }
93 command_ = command->valuestring;
94 cJSON *valueJson = cJSON_GetObjectItemCaseSensitive(rootValue, "Value");
95 if (valueJson == nullptr || !cJSON_IsObject(valueJson)) {
96 cJSON_Delete(rootValue);
97 return DCAMERA_BAD_VALUE;
98 }
99 cJSON *sourceDevId = cJSON_GetObjectItemCaseSensitive(valueJson, "SourceDevId");
100 if (sourceDevId == nullptr || !cJSON_IsString(sourceDevId) || (sourceDevId->valuestring == nullptr)) {
101 cJSON_Delete(rootValue);
102 return DCAMERA_BAD_VALUE;
103 }
104 std::shared_ptr<DCameraChannelInfo> channelInfo = std::make_shared<DCameraChannelInfo>();
105 channelInfo->sourceDevId_ = sourceDevId->valuestring;
106 cJSON *details = cJSON_GetObjectItemCaseSensitive(valueJson, "Detail");
107 if (details == nullptr || !cJSON_IsArray(details) || cJSON_GetArraySize(details) == 0) {
108 cJSON_Delete(rootValue);
109 return DCAMERA_BAD_VALUE;
110 }
111 if (UnmarshalDetails(details, channelInfo) != DCAMERA_OK) {
112 cJSON_Delete(rootValue);
113 return DCAMERA_BAD_VALUE;
114 }
115 value_ = channelInfo;
116 cJSON_Delete(rootValue);
117 return DCAMERA_OK;
118 }
119
UnmarshalDetails(cJSON * details,std::shared_ptr<DCameraChannelInfo> channelInfo)120 int32_t DCameraChannelInfoCmd::UnmarshalDetails(cJSON *details, std::shared_ptr<DCameraChannelInfo> channelInfo)
121 {
122 cJSON *detail = nullptr;
123 cJSON_ArrayForEach(detail, details) {
124 cJSON *dataSessionFlag = cJSON_GetObjectItemCaseSensitive(detail, "DataSessionFlag");
125 cJSON *streamType = cJSON_GetObjectItemCaseSensitive(detail, "StreamType");
126 if (dataSessionFlag == nullptr || !cJSON_IsString(dataSessionFlag) ||
127 (dataSessionFlag->valuestring == nullptr)) {
128 return DCAMERA_BAD_VALUE;
129 }
130 if (streamType == nullptr || !cJSON_IsNumber(streamType)) {
131 return DCAMERA_BAD_VALUE;
132 }
133 DCameraChannelDetail channelDetail;
134 channelDetail.dataSessionFlag_ = dataSessionFlag->valuestring;
135 channelDetail.streamType_ = static_cast<DCStreamType>(streamType->valueint);
136 channelInfo->detail_.push_back(channelDetail);
137 }
138 return DCAMERA_OK;
139 }
140 } // namespace DistributedHardware
141 } // namespace OHOS
142