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 "discovery_filter.h"
17
18 #include "dm_device_info.h"
19 #include "dm_log.h"
20 #include "nlohmann/json.hpp"
21
22 namespace OHOS {
23 namespace DistributedHardware {
24 const std::string FILTERS_KEY = "filters";
25 const std::string FILTER_OP_KEY = "filter_op";
26 const std::string FILTERS_TYPE_OR = "OR";
27 const std::string FILTERS_TYPE_AND = "AND";
28 const int32_t DM_OK = 0;
29 const int32_t ERR_DM_INPUT_PARA_INVALID = 96929749;
30 enum DmDiscoveryDeviceFilter {
31 DM_INVALID_DEVICE = 0,
32 DM_VALID_DEVICE = 1,
33 DM_ALL_DEVICE = 2
34 };
35
ParseFilterJson(const std::string & str)36 int32_t DeviceFilterOption::ParseFilterJson(const std::string &str)
37 {
38 nlohmann::json jsonObject = nlohmann::json::parse(str, nullptr, false);
39 if (jsonObject.is_discarded()) {
40 LOGE("FilterOptions parse error.");
41 return ERR_DM_INPUT_PARA_INVALID;
42 }
43 if (!jsonObject.contains(FILTERS_KEY) || !jsonObject[FILTERS_KEY].is_array() || jsonObject[FILTERS_KEY].empty()) {
44 LOGE("Filters invalid.");
45 return ERR_DM_INPUT_PARA_INVALID;
46 }
47 if (jsonObject.contains(FILTER_OP_KEY) && !jsonObject[FILTER_OP_KEY].is_string()) {
48 LOGE("Filters_op invalid.");
49 return ERR_DM_INPUT_PARA_INVALID;
50 }
51 if (!jsonObject.contains(FILTER_OP_KEY)) {
52 filterOp_ = FILTERS_TYPE_OR; // filterOp optional, "OR" default
53 } else {
54 jsonObject[FILTER_OP_KEY].get_to(filterOp_);
55 }
56
57 for (const auto &object : jsonObject[FILTERS_KEY]) {
58 if (!object.contains("type") || !object["type"].is_string()) {
59 LOGE("Filters type invalid");
60 return ERR_DM_INPUT_PARA_INVALID;
61 }
62 if (!object.contains("value") || !object["value"].is_number_integer()) {
63 LOGE("Filters value invalid");
64 return ERR_DM_INPUT_PARA_INVALID;
65 }
66 DeviceFilters deviceFilters;
67 deviceFilters.type = object["type"];
68 deviceFilters.value = object["value"];
69 filters_.push_back(deviceFilters);
70 }
71 return DM_OK;
72 }
73
ParseFilterOptionJson(const std::string & str)74 int32_t DeviceFilterOption::ParseFilterOptionJson(const std::string &str)
75 {
76 nlohmann::json object = nlohmann::json::parse(str, nullptr, false);
77 if (object.is_discarded()) {
78 LOGE("ParseFilterOptionJson parse error.");
79 return ERR_DM_INPUT_PARA_INVALID;
80 }
81 filterOp_ = FILTERS_TYPE_AND;
82 DeviceFilters deviceFilters;
83 if (object.contains("credible") && object["credible"].is_number_integer()) {
84 deviceFilters.type = "credible";
85 deviceFilters.value = object["credible"];
86 filters_.push_back(deviceFilters);
87 }
88 if (object.contains("range") && object["range"].is_number_integer()) {
89 deviceFilters.type = "range";
90 deviceFilters.value = object["range"];
91 filters_.push_back(deviceFilters);
92 }
93 if (object.contains("isTrusted") && object["isTrusted"].is_number_integer()) {
94 deviceFilters.type = "isTrusted";
95 deviceFilters.value = object["isTrusted"];
96 filters_.push_back(deviceFilters);
97 }
98 if (object.contains("authForm") && object["authForm"].is_number_integer()) {
99 deviceFilters.type = "authForm";
100 deviceFilters.value = object["authForm"];
101 filters_.push_back(deviceFilters);
102 }
103 if (object.contains("deviceType") && object["deviceType"].is_number_integer()) {
104 deviceFilters.type = "deviceType";
105 deviceFilters.value = object["deviceType"];
106 filters_.push_back(deviceFilters);
107 }
108 return DM_OK;
109 }
110
TransformToFilter(const std::string & filterOptions)111 int32_t DeviceFilterOption::TransformToFilter(const std::string &filterOptions)
112 {
113 if (filterOptions.empty()) {
114 LOGI("DeviceFilterOption::filterOptions empty");
115 filterOp_ = FILTERS_TYPE_OR;
116 DeviceFilters deviceFilters;
117 deviceFilters.type = "credible";
118 deviceFilters.value = DM_INVALID_DEVICE;
119 filters_.push_back(deviceFilters);
120 return DM_OK;
121 }
122 return ParseFilterJson(filterOptions);
123 }
124
TransformFilterOption(const std::string & filterOptions)125 int32_t DeviceFilterOption::TransformFilterOption(const std::string &filterOptions)
126 {
127 if (filterOptions.empty()) {
128 LOGI("DeviceFilterOption::filterOptions empty");
129 filterOp_ = FILTERS_TYPE_OR;
130 DeviceFilters deviceFilters;
131 deviceFilters.type = "credible";
132 deviceFilters.value = DM_INVALID_DEVICE;
133 filters_.push_back(deviceFilters);
134 return DM_OK;
135 }
136 return ParseFilterOptionJson(filterOptions);
137 }
138
FilterByDeviceState(int32_t value,bool isActive)139 bool DiscoveryFilter::FilterByDeviceState(int32_t value, bool isActive)
140 {
141 if (value == DM_INVALID_DEVICE) {
142 return !isActive;
143 }
144 if (value == DM_VALID_DEVICE) {
145 return isActive;
146 }
147 return (value == DM_ALL_DEVICE);
148 }
149
FilterByRange(int32_t value,int32_t range)150 bool DiscoveryFilter::FilterByRange(int32_t value, int32_t range)
151 {
152 return ((range >= 0) && (range <= value));
153 }
154
FilterByDeviceType(int32_t value,int32_t deviceType)155 bool DiscoveryFilter::FilterByDeviceType(int32_t value, int32_t deviceType)
156 {
157 return (value == deviceType);
158 }
159
FilterByType(const DeviceFilters & filters,const DeviceFilterPara & filterPara)160 bool DiscoveryFilter::FilterByType(const DeviceFilters &filters, const DeviceFilterPara &filterPara)
161 {
162 if (filters.type == "credible") {
163 return FilterByDeviceState(filters.value, filterPara.isOnline);
164 }
165 if (filters.type == "range") {
166 return FilterByRange(filters.value, filterPara.range);
167 }
168 if (filters.type == "isTrusted") {
169 return FilterByDeviceState(filters.value, filterPara.isTrusted);
170 }
171 if (filters.type == "deviceType") {
172 return FilterByDeviceType(filters.value, filterPara.deviceType);
173 }
174 return false;
175 }
176
FilterOr(const std::vector<DeviceFilters> & filters,const DeviceFilterPara & filterPara)177 bool DiscoveryFilter::FilterOr(const std::vector<DeviceFilters> &filters, const DeviceFilterPara &filterPara)
178 {
179 for (auto &iter : filters) {
180 if (FilterByType(iter, filterPara) == true) {
181 return true;
182 }
183 }
184 return false;
185 }
186
FilterAnd(const std::vector<DeviceFilters> & filters,const DeviceFilterPara & filterPara)187 bool DiscoveryFilter::FilterAnd(const std::vector<DeviceFilters> &filters, const DeviceFilterPara &filterPara)
188 {
189 for (auto &iter : filters) {
190 if (FilterByType(iter, filterPara) == false) {
191 return false;
192 }
193 }
194 return true;
195 }
196
IsValidDevice(const std::string & filterOp,const std::vector<DeviceFilters> & filters,const DeviceFilterPara & filterPara)197 bool DiscoveryFilter::IsValidDevice(const std::string &filterOp, const std::vector<DeviceFilters> &filters,
198 const DeviceFilterPara &filterPara)
199 {
200 if (filterOp == FILTERS_TYPE_OR) {
201 return FilterOr(filters, filterPara);
202 }
203 if (filterOp == FILTERS_TYPE_AND) {
204 return FilterAnd(filters, filterPara);
205 }
206 return false;
207 }
208 } // namespace DistributedHardware
209 } // namespace OHOS