1 /*
2 * Copyright (c) 2022 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 "array_usb_device_id_serializer.h"
17 #include "cjson_check.h"
18 #include "edm_constants.h"
19 #include "usb_device_id.h"
20
21 namespace OHOS {
22 namespace EDM {
23
SetUnionPolicyData(std::vector<UsbDeviceId> & data,std::vector<UsbDeviceId> & currentData)24 std::vector<UsbDeviceId> ArrayUsbDeviceIdSerializer::SetUnionPolicyData(std::vector<UsbDeviceId> &data,
25 std::vector<UsbDeviceId> ¤tData)
26 {
27 std::vector<UsbDeviceId> mergeData;
28 std::sort(data.begin(), data.end(), Comp());
29 std::sort(currentData.begin(), currentData.end(), Comp());
30 std::set_union(data.begin(), data.end(), currentData.begin(), currentData.end(), back_inserter(mergeData), Comp());
31 return mergeData;
32 }
33
SetDifferencePolicyData(std::vector<UsbDeviceId> & data,std::vector<UsbDeviceId> & currentData)34 std::vector<UsbDeviceId> ArrayUsbDeviceIdSerializer::SetDifferencePolicyData(std::vector<UsbDeviceId> &data,
35 std::vector<UsbDeviceId> ¤tData)
36 {
37 std::vector<UsbDeviceId> mergeData;
38 std::sort(data.begin(), data.end(), Comp());
39 std::sort(currentData.begin(), currentData.end(), Comp());
40 std::set_difference(currentData.begin(), currentData.end(), data.begin(), data.end(), back_inserter(mergeData),
41 Comp());
42 return mergeData;
43 }
44
Deserialize(const std::string & jsonString,std::vector<UsbDeviceId> & dataObj)45 bool ArrayUsbDeviceIdSerializer::Deserialize(const std::string &jsonString, std::vector<UsbDeviceId> &dataObj)
46 {
47 if (jsonString.empty()) {
48 return true;
49 }
50
51 cJSON *root = cJSON_Parse(jsonString.c_str());
52 if (root == nullptr) {
53 EDMLOGE("JSON parse error");
54 return false;
55 }
56
57 if (!cJSON_IsArray(root)) {
58 EDMLOGE("JSON is not an array.");
59 cJSON_Delete(root);
60 return false;
61 }
62
63 const int arraySize = cJSON_GetArraySize(root);
64 if (arraySize > EdmConstants::ALLOWED_USB_DEVICES_MAX_SIZE) {
65 EDMLOGE("ArrayUsbDeviceIdSerializer Deserialize data size=%{public}d is too large", arraySize);
66 cJSON_Delete(root);
67 return false;
68 }
69
70 dataObj.resize(arraySize);
71
72 for (int i = 0; i < arraySize; ++i) {
73 cJSON *item = cJSON_GetArrayItem(root, i);
74 if (item == nullptr) {
75 EDMLOGE("Invalid item.");
76 cJSON_Delete(root);
77 return false;
78 }
79
80 cJSON *vendor_id = cJSON_GetObjectItem(item, "vendorId");
81 cJSON *product_id = cJSON_GetObjectItem(item, "productId");
82
83 if (!vendor_id || !product_id || !cJSON_IsNumber(vendor_id) || !cJSON_IsNumber(product_id)) {
84 EDMLOGE("Invalid USB device data.");
85 cJSON_Delete(root);
86 return false;
87 }
88
89 UsbDeviceId device;
90 device.SetVendorId(vendor_id->valueint);
91 device.SetProductId(product_id->valueint);
92 dataObj[i] = device;
93 }
94
95 cJSON_Delete(root);
96 return true;
97 }
98
Serialize(const std::vector<UsbDeviceId> & dataObj,std::string & jsonString)99 bool ArrayUsbDeviceIdSerializer::Serialize(const std::vector<UsbDeviceId> &dataObj, std::string &jsonString)
100 {
101 if (dataObj.empty()) {
102 jsonString = "";
103 return true;
104 }
105
106 cJSON *root = nullptr;
107 CJSON_CREATE_ARRAY_AND_CHECK(root, false);
108
109 for (const auto &device : dataObj) {
110 cJSON* item = nullptr;
111 CJSON_CREATE_OBJECT_AND_CHECK_AND_CLEAR(item, false, root);
112
113 if (!cJSON_AddNumberToObject(item, "vendorId", device.GetVendorId()) ||
114 !cJSON_AddNumberToObject(item, "productId", device.GetProductId())) {
115 cJSON_Delete(item);
116 cJSON_Delete(root);
117 return false;
118 }
119 CJSON_ADD_ITEM_TO_ARRAY_AND_CHECK_AND_CLEAR(item, root, false);
120 }
121
122 char *jsonStr = cJSON_Print(root);
123 if (jsonStr == nullptr) {
124 cJSON_Delete(root);
125 return false;
126 }
127
128 jsonString = jsonStr;
129 cJSON_free(jsonStr);
130 cJSON_Delete(root);
131 return true;
132 }
133
GetPolicy(MessageParcel & data,std::vector<UsbDeviceId> & result)134 bool ArrayUsbDeviceIdSerializer::GetPolicy(MessageParcel &data, std::vector<UsbDeviceId> &result)
135 {
136 uint32_t size = data.ReadUint32();
137 if (size > EdmConstants::ALLOWED_USB_DEVICES_MAX_SIZE) {
138 EDMLOGE("ArrayUsbDeviceIdSerializer:GetPolicy size=[%{public}u] is too large", size);
139 return false;
140 }
141 for (uint32_t i = 0; i < size; i++) {
142 UsbDeviceId usbDeviceId;
143 if (!UsbDeviceId::Unmarshalling(data, usbDeviceId)) {
144 EDMLOGE("ArrayUsbDeviceIdSerializer::GetPolicy read parcel fail");
145 return false;
146 }
147 result.emplace_back(usbDeviceId);
148 }
149 return true;
150 }
151
WritePolicy(MessageParcel & reply,std::vector<UsbDeviceId> & result)152 bool ArrayUsbDeviceIdSerializer::WritePolicy(MessageParcel &reply, std::vector<UsbDeviceId> &result)
153 {
154 std::for_each(result.begin(), result.end(), [&](const auto usbDeviceId) {
155 usbDeviceId.Marshalling(reply);
156 });
157 return true;
158 }
159
MergePolicy(std::vector<std::vector<UsbDeviceId>> & data,std::vector<UsbDeviceId> & result)160 bool ArrayUsbDeviceIdSerializer::MergePolicy(std::vector<std::vector<UsbDeviceId>> &data,
161 std::vector<UsbDeviceId> &result)
162 {
163 std::set<UsbDeviceId> stData;
164 for (const auto &dataItem : data) {
165 for (const auto &item : dataItem) {
166 stData.insert(item);
167 }
168 }
169 result.assign(stData.begin(), stData.end());
170 return true;
171 }
172 } // namespace EDM
173 } // namespace OHOS