1 /*
2  * Copyright (c) 2023-2024 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 <set>
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 "mine_softbus_listener.h"
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 typedef enum {
40     PROXY_TRANSMISION = 0,
41     PROXY_HEARTBEAT = 1,
42     PROXY_HICAR = 2,
43     PROXY_ACS = 3,
44     PROXY_SHARE = 4,
45     PROXY_CASTPLUS = 5,
46     PROXY_DM = 6,
47     PROXY_WEAR = 7,
48     PROXY_WINPC = 8,
49     PROXY_COLLABORATION_FWK = 9,
50     PROXY_DMSDP = 10,
51     CUSTOM_UNKNOWN,
52 } MetaNodeType;
53 
54 class DiscoveryManager : public ISoftbusDiscoveringCallback, public std::enable_shared_from_this<DiscoveryManager> {
55 public:
56     DiscoveryManager(std::shared_ptr<SoftbusListener> softbusListener,
57         std::shared_ptr<IDeviceManagerServiceListener> listener);
58     ~DiscoveryManager() override;
59 
60     // interfaces from ISoftbusDiscoveringCallback
61     void OnDeviceFound(const std::string &pkgName, const DmDeviceInfo &info, bool isOnline) override;
62     void OnDiscoveringResult(const std::string &pkgName, int32_t subscribeId, int32_t result) override;
63     int32_t StartDiscovering(const std::string &pkgName, const std::map<std::string, std::string> &discoverParam,
64         const std::map<std::string, std::string> &filterOptions);
65     int32_t StopDiscovering(const std::string &pkgName, uint16_t subscribeId);
66     int32_t EnableDiscoveryListener(const std::string &pkgName, const std::map<std::string, std::string> &discoverParam,
67         const std::map<std::string, std::string> &filterOptions);
68     int32_t DisableDiscoveryListener(const std::string &pkgName, const std::map<std::string, std::string> &extraParam);
69 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
70     static IDeviceProfileConnector* GetCommonDependencyObj();
71     static bool IsCommonDependencyReady();
72     static bool CloseCommonDependencyObj();
73 #endif
74 
75 private:
76     void StartDiscoveryTimer(const std::string &pkgName);
77     void HandleDiscoveryTimeout(const std::string &pkgName);
78     int32_t StartDiscovering4MetaType(const std::string &pkgName, DmSubscribeInfo &dmSubInfo,
79         const std::map<std::string, std::string> &param);
80     int32_t StartDiscoveringNoMetaType(const std::string &pkgName, DmSubscribeInfo &dmSubInfo,
81         const std::map<std::string, std::string> &param);
82     int32_t StartDiscovering4MineLibary(const std::string &pkgName, DmSubscribeInfo &dmSubInfo,
83         const std::string &searchJson);
84     int32_t HandleDiscoveryQueue(const std::string &pkgName, uint16_t subscribeId,
85         const std::map<std::string, std::string> &filterOps);
86     int32_t GetDeviceAclParam(const std::string &pkgName, std::string deviceId, bool &isOnline, int32_t &authForm);
87     void ConfigDiscParam(const std::map<std::string, std::string> &discoverParam, DmSubscribeInfo *dmSubInfo);
88     bool CompareCapability(uint32_t capabilityType, const std::string &capabilityStr);
89     void OnDeviceFound(const std::string &pkgName, const uint32_t capabilityType,
90         const DmDeviceInfo &info, const DeviceFilterPara &filterPara);
91     void UpdateInfoFreq(const std::map<std::string, std::string> &discoverParam, DmSubscribeInfo &dmSubInfo);
92 
93 private:
94     std::mutex locks_;
95     std::mutex subIdMapLocks_;
96     std::shared_ptr<DmTimer> timer_;
97     std::map<std::string, uint16_t> pkgName2SubIdMap_;
98     std::shared_ptr<SoftbusListener> softbusListener_;
99     std::shared_ptr<MineSoftbusListener> mineSoftbusListener_;
100     std::shared_ptr<IDeviceManagerServiceListener> listener_;
101     std::map<std::string, DiscoveryContext> discoveryContextMap_;
102 
103     std::set<std::string> pkgNameSet_;
104     std::map<std::string, std::string> capabilityMap_;
105     std::mutex capabilityMapLocks_;
106 
107 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
108     static bool isSoLoaded_;
109     static IDeviceProfileConnector *dpConnector_;
110     static void *dpConnectorHandle_;
111 #endif
112 };
113 } // namespace DistributedHardware
114 } // namespace OHOS
115 #endif // OHOS_DISCOVERY_MANAGER_H
116