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