• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "mine_softbus_listener.h"
27 
28 namespace OHOS {
29 namespace DistributedHardware {
30 typedef struct DiscoveryContext {
31     std::string pkgName;
32     std::string extra;
33     uint16_t subscribeId;
34     std::string filterOp;
35     std::vector<DeviceFilters> filters;
36 } DiscoveryContext;
37 
38 typedef struct MultiUserDiscovery {
39     std::string pkgName;
40     int32_t userId;
41 } MultiUserDiscovery;
42 
43 typedef enum {
44     PROXY_TRANSMISION = 0,
45     PROXY_HEARTBEAT = 1,
46     PROXY_HICAR = 2,
47     PROXY_ACS = 3,
48     PROXY_SHARE = 4,
49     PROXY_CASTPLUS = 5,
50     PROXY_DM = 6,
51     PROXY_WEAR = 7,
52     PROXY_WINPC = 8,
53     PROXY_COLLABORATION_FWK = 9,
54     PROXY_DMSDP = 10,
55     CUSTOM_UNKNOWN,
56 } MetaNodeType;
57 
58 class DiscoveryManager : public ISoftbusDiscoveringCallback, public std::enable_shared_from_this<DiscoveryManager> {
59 public:
60     DiscoveryManager(std::shared_ptr<SoftbusListener> softbusListener,
61         std::shared_ptr<IDeviceManagerServiceListener> listener);
62     ~DiscoveryManager() override;
63 
64     // interfaces from ISoftbusDiscoveringCallback
65     void OnDeviceFound(const std::string &pkgName, const DmDeviceInfo &info, bool isOnline) override;
66     void OnDiscoveringResult(const std::string &pkgName, int32_t subscribeId, int32_t result) override;
67     int32_t StartDiscovering(const std::string &pkgName, const std::map<std::string, std::string> &discoverParam,
68         const std::map<std::string, std::string> &filterOptions);
69     int32_t StopDiscovering(const std::string &pkgName, uint16_t subscribeId);
70     int32_t EnableDiscoveryListener(const std::string &pkgName, const std::map<std::string, std::string> &discoverParam,
71         const std::map<std::string, std::string> &filterOptions);
72     int32_t DisableDiscoveryListener(const std::string &pkgName, const std::map<std::string, std::string> &extraParam);
73 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
74     static IDeviceProfileConnector* GetCommonDependencyObj();
75     static bool IsCommonDependencyReady();
76     static bool CloseCommonDependencyObj();
77 #endif
78     void ClearDiscoveryCache(const ProcessInfo &processInfo);
79     std::set<uint16_t> ClearDiscoveryPkgName(const std::string &pkgName);
80 
81 private:
82     void StartDiscoveryTimer(const std::string &pkgName);
83     void HandleDiscoveryTimeout(const std::string &pkgName);
84     int32_t StartDiscovering4MetaType(const std::string &pkgName, DmSubscribeInfo &dmSubInfo,
85         const std::map<std::string, std::string> &param);
86     int32_t StartDiscoveringNoMetaType(const std::string &pkgName, DmSubscribeInfo &dmSubInfo,
87         const std::map<std::string, std::string> &param);
88     int32_t StartDiscovering4MineLibary(const std::string &pkgName, DmSubscribeInfo &dmSubInfo,
89         const std::string &searchJson);
90     int32_t HandleDiscoveryQueue(const std::string &pkgName, uint16_t subscribeId,
91         const std::map<std::string, std::string> &filterOps);
92     int32_t GetDeviceAclParam(const std::string &pkgName, int32_t userId, std::string deviceId, bool &isOnline,
93         int32_t &authForm);
94     void ConfigDiscParam(const std::map<std::string, std::string> &discoverParam, DmSubscribeInfo *dmSubInfo);
95     bool CompareCapability(uint32_t capabilityType, const std::string &capabilityStr);
96     void OnDeviceFound(const std::string &pkgName, const uint32_t capabilityType,
97         const DmDeviceInfo &info, const DeviceFilterPara &filterPara);
98     void UpdateInfoFreq(const std::map<std::string, std::string> &discoverParam, DmSubscribeInfo &dmSubInfo);
99     void UpdateInfoMedium(const std::map<std::string, std::string> &discoverParam, DmSubscribeInfo &dmSubInfo);
100     std::string AddMultiUserIdentify(const std::string &pkgName);
101     std::string RemoveMultiUserIdentify(const std::string &pkgName);
102     void GetPkgNameAndUserId(const std::string &pkgName, std::string &callerPkgName, int32_t &userId);
103 
104 private:
105     std::mutex locks_;
106     std::mutex subIdMapLocks_;
107     std::shared_ptr<DmTimer> timer_;
108     std::map<std::string, uint16_t> pkgName2SubIdMap_;
109     std::shared_ptr<SoftbusListener> softbusListener_;
110     std::shared_ptr<MineSoftbusListener> mineSoftbusListener_;
111     std::shared_ptr<IDeviceManagerServiceListener> listener_;
112     std::map<std::string, DiscoveryContext> discoveryContextMap_;
113 
114     std::set<std::string> pkgNameSet_;
115     std::map<std::string, std::string> capabilityMap_;
116     std::mutex capabilityMapLocks_;
117     std::mutex multiUserDiscLocks_;
118     std::map<std::string, MultiUserDiscovery> multiUserDiscMap_;
119 
120 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
121     static bool isSoLoaded_;
122     static IDeviceProfileConnector *dpConnector_;
123     static void *dpConnectorHandle_;
124 #endif
125 };
126 } // namespace DistributedHardware
127 } // namespace OHOS
128 #endif // OHOS_DISCOVERY_MANAGER_H
129