• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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_DM_SOFTBUS_CONNECTOR_H
17 #define OHOS_DM_SOFTBUS_CONNECTOR_H
18 
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 #include <queue>
23 #include <string>
24 #include <vector>
25 
26 #include "softbus_bus_center.h"
27 #include "dm_device_info.h"
28 #include "dm_publish_info.h"
29 #include "dm_subscribe_info.h"
30 #include "softbus_discovery_callback.h"
31 #include "softbus_publish_callback.h"
32 #include "softbus_session.h"
33 #include "softbus_state_callback.h"
34 
35 namespace OHOS {
36 namespace DistributedHardware {
37 class SoftbusConnector {
38 public:
39     /**
40      * @tc.name: SoftbusConnector::OnSoftbusPublishResult
41      * @tc.desc: OnSoftbusPublishResult of the SoftbusConnector
42      * @tc.type: FUNC
43      */
44     static void OnSoftbusPublishResult(int32_t publishId, PublishResult result);
45 
46     /**
47      * @tc.name: SoftbusConnector::OnSoftbusDeviceFound
48      * @tc.desc: OnSoftbus DeviceFound of the SoftbusConnector
49      * @tc.type: FUNC
50      */
51     static void OnSoftbusDeviceFound(const DeviceInfo *device);
52 
53     /**
54      * @tc.name: SoftbusConnector::OnSoftbusDeviceDiscovery
55      * @tc.desc: OnSoftbus DeviceDiscovery of the SoftbusConnector
56      * @tc.type: FUNC
57      */
58     static void OnSoftbusDeviceDiscovery(const DeviceInfo *device);
59 
60     /**
61      * @tc.name: SoftbusConnector::OnSoftbusDiscoveryResult
62      * @tc.desc: OnSoftbus Discovery Result of the SoftbusConnector
63      * @tc.type: FUNC
64      */
65     static void OnSoftbusDiscoveryResult(int subscribeId, RefreshResult result);
66 
67     /**
68      * @tc.name: SoftbusConnector::OnSoftbusJoinLNNResult
69      * @tc.desc: OnSoftbus JoinLNN Result of the SoftbusConnector
70      * @tc.type: FUNC
71      */
72     static void OnSoftbusJoinLNNResult(ConnectionAddr *addr, const char *networkId, int32_t result);
73 
74     /**
75      * @tc.name: SoftbusConnector::OnParameterChgCallback
76      * @tc.desc: OnParameter Chg Callback of the SoftbusConnector
77      * @tc.type: FUNC
78      */
79     static void OnParameterChgCallback(const char *key, const char *value, void *context);
80 
81     /**
82      * @tc.name: SoftbusConnector::GetConnectionIpAddress
83      * @tc.desc: Get Connection Ip Address of the SoftbusConnector
84      * @tc.type: FUNC
85      */
86     static int32_t GetConnectionIpAddress(const std::string &deviceId, std::string &ipAddress);
87 
88     /**
89      * @tc.name: SoftbusConnector::GetConnectAddr
90      * @tc.desc: Get Connect Addr of the SoftbusConnector
91      * @tc.type: FUNC
92      */
93     static ConnectionAddr *GetConnectAddr(const std::string &deviceId, std::string &connectAddr);
94 
95     /**
96      * @tc.name: SoftbusConnector::GetUdidByNetworkId
97      * @tc.desc: Get Udid By NetworkId of the SoftbusConnector
98      * @tc.type: FUNC
99      */
100     static int32_t GetUdidByNetworkId(const char *networkId, std::string &udid);
101 
102     /**
103      * @tc.name: SoftbusConnector::GetUuidByNetworkId
104      * @tc.desc: Get Uuid By NetworkId of the SoftbusConnector
105      * @tc.type: FUNC
106      */
107     static int32_t GetUuidByNetworkId(const char *networkId, std::string &uuid);
108 
109     /**
110      * @tc.name: SoftbusConnector::GetDeviceUdidByUdidHash
111      * @tc.desc: Get Udid By DeviceId of the SoftbusConnector
112      * @tc.type: FUNC
113      */
114     static std::string GetDeviceUdidByUdidHash(const std::string &udidHash);
115 
116     /**
117      * @tc.name: SoftbusConnector::JoinLnn
118      * @tc.desc: Add the current device to the LNN where a specified device resides
119      * @tc.type: FUNC
120      */
121     static void JoinLnn(const std::string &deviceId);
122 public:
123     SoftbusConnector();
124     ~SoftbusConnector();
125     int32_t RegisterSoftbusStateCallback(const std::string &pkgName,
126                                          const std::shared_ptr<ISoftbusStateCallback> callback);
127     int32_t UnRegisterSoftbusStateCallback(const std::string &pkgName);
128     int32_t RegisterSoftbusDiscoveryCallback(const std::string &pkgName,
129                                              const std::shared_ptr<ISoftbusDiscoveryCallback> callback);
130     int32_t UnRegisterSoftbusDiscoveryCallback(const std::string &pkgName);
131     int32_t RegisterSoftbusPublishCallback(const std::string &pkgName,
132                                            const std::shared_ptr<ISoftbusPublishCallback> callback);
133     int32_t UnRegisterSoftbusPublishCallback(const std::string &pkgName);
134     int32_t PublishDiscovery(const DmPublishInfo &dmPublishInfo);
135     int32_t UnPublishDiscovery(int32_t publishId);
136     int32_t StartDiscovery(const DmSubscribeInfo &dmSubscribeInfo);
137     int32_t StartDiscovery(const uint16_t subscribeId);
138     int32_t StopDiscovery(uint16_t subscribeId);
139     std::shared_ptr<SoftbusSession> GetSoftbusSession();
140     bool HaveDeviceInMap(std::string deviceId);
141     void HandleDeviceOnline(DmDeviceInfo &info);
142     void HandleDeviceOffline(const DmDeviceInfo &info);
143     void HandleDeviceNameChange(const DmDeviceInfo &info);
144     std::string GetDeviceUdidHashByUdid(const std::string &udid);
145     void EraseUdidFromMap(const std::string &udid);
146     std::string GetLocalDeviceName();
147     int32_t GetLocalDeviceTypeId();
148 
149 private:
150     int32_t Init();
151     static void ConvertDeviceInfoToDmDevice(const DeviceInfo &deviceInfo, DmDeviceInfo &dmDeviceInfo);
152     static void ConvertDeviceInfoToDmDevice(const DeviceInfo &deviceInfo, DmDeviceBasicInfo &dmDeviceBasicInfo);
153     static ConnectionAddr *GetConnectAddrByType(DeviceInfo *deviceInfo, ConnectionAddrType type);
154 
155 private:
156     enum PulishStatus {
157         STATUS_UNKNOWN = 0,
158         ALLOW_BE_DISCOVERY = 1,
159         NOT_ALLOW_BE_DISCOVERY = 2,
160     };
161     static PulishStatus publishStatus;
162     static IRefreshCallback softbusDiscoveryCallback_;
163     static IRefreshCallback softbusDiscoveryByIdCallback_;
164     static IPublishCb softbusPublishCallback_;
165     std::shared_ptr<SoftbusSession> softbusSession_;
166     static std::map<std::string, std::shared_ptr<DeviceInfo>> discoveryDeviceInfoMap_;
167     static std::map<std::string, std::shared_ptr<ISoftbusStateCallback>> stateCallbackMap_;
168     static std::map<std::string, std::shared_ptr<ISoftbusDiscoveryCallback>> discoveryCallbackMap_;
169     static std::map<std::string, std::shared_ptr<ISoftbusPublishCallback>> publishCallbackMap_;
170     static std::queue<std::string> discoveryDeviceIdQueue_;
171     static std::unordered_map<std::string, std::string> deviceUdidMap_;
172     static std::mutex discoveryCallbackMutex_;
173     static std::mutex discoveryDeviceInfoMutex_;
174     static std::mutex stateCallbackMutex_;
175     static std::mutex deviceUdidLocks_;
176 };
177 } // namespace DistributedHardware
178 } // namespace OHOS
179 #endif // OHOS_DM_SOFTBUS_CONNECTOR_H
180