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 #ifndef OHOS_DM_DISCOVERY_FILTER_H 17 #define OHOS_DM_DISCOVERY_FILTER_H 18 19 #include <vector> 20 #include <string> 21 22 #include "nlohmann/json.hpp" 23 24 namespace OHOS { 25 namespace DistributedHardware { 26 struct DmDeviceFilters { 27 std::string type; 28 int32_t value; 29 }; 30 31 struct DmDeviceFilterOption { 32 std::string filterOp_; 33 std::vector<DmDeviceFilters> filters_; 34 int32_t ParseFilterJson(const std::string &str); 35 void ParseFilterOptionJson(const std::string &str); 36 int32_t TransformToFilter(const std::string &filterOptions); 37 void TransformFilterOption(const std::string &filterOptions); 38 }; 39 40 struct DmDeviceFilterPara { 41 bool isOnline; 42 int32_t range; 43 bool isTrusted; 44 int32_t authForm; 45 int32_t deviceType; 46 }; 47 48 class DmDiscoveryFilter { 49 public: 50 bool IsValidDevice(const std::string &filterOp, const std::vector<DmDeviceFilters> &filters, 51 const DmDeviceFilterPara &filterPara); 52 private: 53 bool FilterByDeviceState(int32_t value, bool isActive); 54 bool FilterByRange(int32_t value, int32_t range); 55 bool FilterByAuthForm(int32_t value, int32_t authForm); 56 bool FilterByDeviceType(int32_t value, int32_t deviceType); 57 bool FilterByType(const DmDeviceFilters &filters, const DmDeviceFilterPara &filterPara); 58 bool FilterOr(const std::vector<DmDeviceFilters> &filters, const DmDeviceFilterPara &filterPara); 59 bool FilterAnd(const std::vector<DmDeviceFilters> &filters, const DmDeviceFilterPara &filterPara); 60 }; 61 } // namespace DistributedHardware 62 } // namespace OHOS 63 #endif // OHOS_DM_DISCOVERY_FILTER_H 64