• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "edm_constants.h"
18 #include "json/json.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> &currentData)
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> &currentData)
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     Json::Value root;
51     Json::CharReaderBuilder builder;
52     const std::unique_ptr<Json::CharReader> charReader(builder.newCharReader());
53     std::string err;
54     if (!charReader->parse(jsonString.c_str(), jsonString.c_str() + jsonString.length(), &root, &err)) {
55         return false;
56     }
57     if (!root.isArray()) {
58         EDMLOGE("ArrayUsbDeviceIdSerializer Deserialize root is not array");
59         return false;
60     }
61     if (root.size() > EdmConstants::ALLOWED_USB_DEVICES_MAX_SIZE) {
62         EDMLOGE("ArrayUsbDeviceIdSerializer Deserialize data size=[%{public}u] is too large", root.size());
63         return false;
64     }
65     dataObj = std::vector<UsbDeviceId>(root.size());
66 
67     for (std::uint32_t i = 0; i < root.size(); ++i) {
68         const Json::Value& item = root[i];
69 
70         if (!item.isMember("vendorId") || !item.isMember("productId") ||
71             !item["vendorId"].isConvertibleTo(Json::intValue) || !item["productId"].isConvertibleTo(Json::intValue)) {
72             EDMLOGE("ArrayUsbDeviceIdSerializer Deserialize invalid data at index %{public}u", i);
73             return false;
74         }
75         UsbDeviceId value;
76         value.SetVendorId(item["vendorId"].asInt());
77         value.SetProductId(item["productId"].asInt());
78         dataObj[i] = value;
79     }
80     return true;
81 }
82 
Serialize(const std::vector<UsbDeviceId> & dataObj,std::string & jsonString)83 bool ArrayUsbDeviceIdSerializer::Serialize(const std::vector<UsbDeviceId> &dataObj, std::string &jsonString)
84 {
85     if (dataObj.empty()) {
86         jsonString = "";
87         return true;
88     }
89     Json::Value arrayData(Json::arrayValue);
90     for (const auto& item : dataObj) {
91         Json::Value root;
92         root["vendorId"] = item.GetVendorId();
93         root["productId"] = item.GetProductId();
94         arrayData.append(root);
95     }
96     Json::StreamWriterBuilder builder;
97     builder["indentation"] = "    ";
98     jsonString = Json::writeString(builder, arrayData);
99     return true;
100 }
101 
GetPolicy(MessageParcel & data,std::vector<UsbDeviceId> & result)102 bool ArrayUsbDeviceIdSerializer::GetPolicy(MessageParcel &data, std::vector<UsbDeviceId> &result)
103 {
104     uint32_t size = data.ReadUint32();
105     if (size > EdmConstants::ALLOWED_USB_DEVICES_MAX_SIZE) {
106         EDMLOGE("ArrayUsbDeviceIdSerializer:GetPolicy size=[%{public}u] is too large", size);
107         return false;
108     }
109     for (uint32_t i = 0; i < size; i++) {
110         UsbDeviceId usbDeviceId;
111         if (!UsbDeviceId::Unmarshalling(data, usbDeviceId)) {
112             EDMLOGE("ArrayUsbDeviceIdSerializer::GetPolicy read parcel fail");
113             return false;
114         }
115         result.emplace_back(usbDeviceId);
116     }
117     return true;
118 }
119 
WritePolicy(MessageParcel & reply,std::vector<UsbDeviceId> & result)120 bool ArrayUsbDeviceIdSerializer::WritePolicy(MessageParcel &reply, std::vector<UsbDeviceId> &result)
121 {
122     std::for_each(result.begin(), result.end(), [&](const auto usbDeviceId) {
123         usbDeviceId.Marshalling(reply);
124     });
125     return true;
126 }
127 
MergePolicy(std::vector<std::vector<UsbDeviceId>> & data,std::vector<UsbDeviceId> & result)128 bool ArrayUsbDeviceIdSerializer::MergePolicy(std::vector<std::vector<UsbDeviceId>> &data,
129     std::vector<UsbDeviceId> &result)
130 {
131     std::set<UsbDeviceId> stData;
132     for (const auto &dataItem : data) {
133         for (const auto &item : dataItem) {
134             stData.insert(item);
135         }
136     }
137     result.assign(stData.begin(), stData.end());
138     return true;
139 }
140 } // namespace EDM
141 } // namespace OHOS