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_info_cmd.h"
17 #include "cJSON.h"
18 
19 #include "distributed_camera_constants.h"
20 #include "distributed_camera_errno.h"
21 #include "distributed_hardware_log.h"
22 
23 namespace OHOS {
24 namespace DistributedHardware {
Marshal(std::string & jsonStr)25 int32_t DCameraInfoCmd::Marshal(std::string& jsonStr)
26 {
27     cJSON *rootValue = cJSON_CreateObject();
28     if (rootValue == nullptr) {
29         return DCAMERA_BAD_VALUE;
30     }
31     cJSON_AddStringToObject(rootValue, "Type", type_.c_str());
32     cJSON_AddStringToObject(rootValue, "dhId", dhId_.c_str());
33     cJSON_AddStringToObject(rootValue, "Command", command_.c_str());
34 
35     cJSON *info = cJSON_CreateObject();
36     if (info == nullptr) {
37         cJSON_Delete(rootValue);
38         return DCAMERA_BAD_VALUE;
39     }
40     cJSON_AddNumberToObject(info, "State", value_->state_);
41     cJSON_AddItemToObject(rootValue, "Value", info);
42 
43     char *data = cJSON_Print(rootValue);
44     if (data == nullptr) {
45         cJSON_Delete(rootValue);
46         return DCAMERA_BAD_VALUE;
47     }
48     jsonStr = std::string(data);
49     cJSON_Delete(rootValue);
50     cJSON_free(data);
51     return DCAMERA_OK;
52 }
53 
Unmarshal(const std::string & jsonStr)54 int32_t DCameraInfoCmd::Unmarshal(const std::string& jsonStr)
55 {
56     cJSON *rootValue = cJSON_Parse(jsonStr.c_str());
57     if (rootValue == nullptr) {
58         return DCAMERA_BAD_VALUE;
59     }
60     cJSON *type = cJSON_GetObjectItemCaseSensitive(rootValue, "Type");
61     if (type == nullptr || !cJSON_IsString(type) || (type->valuestring == nullptr)) {
62         cJSON_Delete(rootValue);
63         return DCAMERA_BAD_VALUE;
64     }
65     type_ = type->valuestring;
66     cJSON *dhId = cJSON_GetObjectItemCaseSensitive(rootValue, "dhId");
67     if (dhId == nullptr || !cJSON_IsString(dhId) || (dhId->valuestring == nullptr)) {
68         cJSON_Delete(rootValue);
69         return DCAMERA_BAD_VALUE;
70     }
71     dhId_ = dhId->valuestring;
72     cJSON *command = cJSON_GetObjectItemCaseSensitive(rootValue, "Command");
73     if (command == nullptr || !cJSON_IsString(command) || (command->valuestring == nullptr)) {
74         cJSON_Delete(rootValue);
75         return DCAMERA_BAD_VALUE;
76     }
77     command_ = command->valuestring;
78     cJSON *valueJson = cJSON_GetObjectItemCaseSensitive(rootValue, "Value");
79     if (valueJson == nullptr || !cJSON_IsObject(valueJson)) {
80         cJSON_Delete(rootValue);
81         return DCAMERA_BAD_VALUE;
82     }
83     cJSON *state = cJSON_GetObjectItemCaseSensitive(valueJson, "State");
84     if (state == nullptr || !cJSON_IsNumber(state)) {
85         cJSON_Delete(rootValue);
86         return DCAMERA_BAD_VALUE;
87     }
88     std::shared_ptr<DCameraInfo> info = std::make_shared<DCameraInfo>();
89     info->state_ = state->valueint;
90 
91     value_ = info;
92     cJSON_Delete(rootValue);
93     return DCAMERA_OK;
94 }
95 } // namespace DistributedHardware
96 } // namespace OHOS
97