1 /* 2 * Copyright (c) 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 #ifndef OHOS_DISCOVERY_MANAGER_H 17 #define OHOS_DISCOVERY_MANAGER_H 18 19 #include <queue> 20 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) 21 #include "deviceprofile_connector.h" 22 #endif 23 #include "discovery_filter.h" 24 #include "idevice_manager_service_listener.h" 25 #include "dm_timer.h" 26 #include "softbus_listener.h" 27 #include "nlohmann/json.hpp" 28 29 namespace OHOS { 30 namespace DistributedHardware { 31 typedef struct DiscoveryContext { 32 std::string pkgName; 33 std::string extra; 34 uint16_t subscribeId; 35 std::string filterOp; 36 std::vector<DeviceFilters> filters; 37 } DiscoveryContext; 38 39 class DiscoveryManager : public ISoftbusDiscoveringCallback, public std::enable_shared_from_this<DiscoveryManager> { 40 public: 41 DiscoveryManager(std::shared_ptr<SoftbusListener> softbusListener, 42 std::shared_ptr<IDeviceManagerServiceListener> listener); 43 ~DiscoveryManager() override; 44 45 // interfaces from ISoftbusDiscoveringCallback 46 void OnDeviceFound(const std::string &pkgName, const DmDeviceInfo &info, bool isOnline) override; 47 void OnDiscoveringResult(const std::string &pkgName, int32_t subscribeId, int32_t result) override; 48 int32_t StartDiscovering(const std::string &pkgName, const std::map<std::string, std::string> &discoverParam, 49 const std::map<std::string, std::string> &filterOptions); 50 int32_t StopDiscovering(const std::string &pkgName, uint16_t subscribeId); 51 int32_t EnableDiscoveryListener(const std::string &pkgName, const std::map<std::string, std::string> &discoverParam, 52 const std::map<std::string, std::string> &filterOptions); 53 int32_t DisableDiscoveryListener(const std::string &pkgName, const std::map<std::string, std::string> &extraParam); 54 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) 55 static IDeviceProfileConnector* GetCommonDependencyObj(); 56 static bool IsCommonDependencyReady(); 57 static bool CloseCommonDependencyObj(); 58 #endif 59 60 private: 61 void StartDiscoveryTimer(); 62 void HandleDiscoveryTimeout(std::string name); 63 int32_t StartDiscovering4MetaType(DmSubscribeInfo &dmSubInfo, const std::map<std::string, std::string> ¶m); 64 int32_t StartDiscoveringNoMetaType(DmSubscribeInfo &dmSubInfo, const std::map<std::string, std::string> ¶m); 65 int32_t StartDiscovering4MineMetaNode(const std::string &pkgName, DmSubscribeInfo &dmSubInfo, 66 const std::string &searchJson); 67 int32_t HandleDiscoveryQueue(const std::string &pkgName, uint16_t subscribeId, 68 const std::map<std::string, std::string> &filterOps); 69 int32_t GetDeviceAclParam(const std::string &pkgName, std::string deviceId, bool &isOnline, int32_t &authForm); 70 71 private: 72 std::mutex locks_; 73 std::shared_ptr<DmTimer> timer_; 74 std::map<std::string, uint16_t> pkgName2SubIdMap_; 75 std::shared_ptr<SoftbusListener> softbusListener_; 76 std::shared_ptr<IDeviceManagerServiceListener> listener_; 77 std::queue<std::string> discoveryQueue_; 78 std::map<std::string, DiscoveryContext> discoveryContextMap_; 79 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) 80 static bool isSoLoaded_; 81 static IDeviceProfileConnector *dpConnector_; 82 static void *dpConnectorHandle_; 83 #endif 84 }; 85 } // namespace DistributedHardware 86 } // namespace OHOS 87 #endif // OHOS_DISCOVERY_MANAGER_H 88