• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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_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 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
31 #include "softbus_session.h"
32 #endif
33 #include "softbus_connector_callback.h"
34 #include "softbus_state_callback.h"
35 
36 namespace OHOS {
37 namespace DistributedHardware {
38 class SoftbusConnector {
39 public:
40     /**
41      * @tc.name: SoftbusConnector::OnSoftbusJoinLNNResult
42      * @tc.desc: OnSoftbus JoinLNN Result of the SoftbusConnector
43      * @tc.type: FUNC
44      */
45     static void OnSoftbusJoinLNNResult(ConnectionAddr *addr, const char *networkId, int32_t result);
46 
47     /**
48      * @tc.name: SoftbusConnector::GetConnectAddr
49      * @tc.desc: Get Connect Addr of the SoftbusConnector
50      * @tc.type: FUNC
51      */
52     static ConnectionAddr *GetConnectAddr(const std::string &deviceId, std::string &connectAddr);
53 
54     /**
55      * @tc.name: SoftbusConnector::GetUdidByNetworkId
56      * @tc.desc: Get Udid By NetworkId of the SoftbusConnector
57      * @tc.type: FUNC
58      */
59     static int32_t GetUdidByNetworkId(const char *networkId, std::string &udid);
60 
61     /**
62      * @tc.name: SoftbusConnector::GetUuidByNetworkId
63      * @tc.desc: Get Uuid By NetworkId of the SoftbusConnector
64      * @tc.type: FUNC
65      */
66     static int32_t GetUuidByNetworkId(const char *networkId, std::string &uuid);
67 
68     /**
69      * @tc.name: SoftbusConnector::GetDeviceUdidByUdidHash
70      * @tc.desc: Get Udid By DeviceId of the SoftbusConnector
71      * @tc.type: FUNC
72      */
73     static std::string GetDeviceUdidByUdidHash(const std::string &udidHash);
74 
75     /**
76      * @tc.name: SoftbusConnector::JoinLnn
77      * @tc.desc: Add the current device to the LNN where a specified device resides
78      * @tc.type: FUNC
79      */
80     static void JoinLnn(const std::string &deviceId, bool isForceJoin = false);
81 
82     /**
83      * @tc.name: SoftbusConnector::JoinLnnByHml
84      * @tc.desc: join lnn by session id
85      * @tc.type: FUNC
86      */
87     static void JoinLnnByHml(int32_t sessionId, int32_t sessionKeyId, int32_t remoteSessionKeyId);
88 
89     /**
90      * @tc.name: SoftbusConnector::RegisterConnectorCallback
91      * @tc.desc: RegisterConnectorCallback of the Softbus Connector
92      * @tc.type: FUNC
93      */
94     int32_t RegisterConnectorCallback(std::shared_ptr<ISoftbusConnectorCallback> callback);
95 
96     /**
97      * @tc.name: SoftbusConnector::UnRegisterConnectorCallback
98      * @tc.desc: UnRegister ConnectorCallback of the Softbus Connector
99      * @tc.type: FUNC
100      */
101     int32_t UnRegisterConnectorCallback();
102 
103 public:
104     SoftbusConnector();
105     ~SoftbusConnector();
106     int32_t RegisterSoftbusStateCallback(const std::shared_ptr<ISoftbusStateCallback> callback);
107     int32_t UnRegisterSoftbusStateCallback();
108 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
109     std::shared_ptr<SoftbusSession> GetSoftbusSession();
110 #endif
111     bool HaveDeviceInMap(std::string deviceId);
112     std::string GetDeviceUdidHashByUdid(const std::string &udid);
113     void EraseUdidFromMap(const std::string &udid);
114     std::string GetLocalDeviceName();
115     std::string GetLocalDeviceNetworkId();
116     int32_t GetLocalDeviceTypeId();
117     int32_t AddMemberToDiscoverMap(const std::string &deviceId, std::shared_ptr<DeviceInfo> deviceInfo);
118     std::string GetNetworkIdByDeviceId(const std::string &deviceId);
119     void HandleDeviceOnline(std::string deviceId, int32_t authForm);
120     void HandleDeviceOffline(std::string deviceId);
121     void SetProcessInfo(ProcessInfo processInfo);
122     bool CheckIsOnline(const std::string &targetDeviceId);
123     void SetProcessInfoVec(std::vector<ProcessInfo> processInfoVec);
124     std::vector<ProcessInfo> GetProcessInfo();
125     void ClearProcessInfo();
126     DmDeviceInfo GetDeviceInfoByDeviceId(const std::string &deviceId);
127     void DeleteOffLineTimer(std::string &udidHash);
128 
129 private:
130     static void ConvertDeviceInfoToDmDevice(const DeviceInfo &deviceInfo, DmDeviceInfo &dmDeviceInfo);
131     static void ConvertDeviceInfoToDmDevice(const DeviceInfo &deviceInfo, DmDeviceBasicInfo &dmDeviceBasicInfo);
132     static ConnectionAddr *GetConnectAddrByType(DeviceInfo *deviceInfo, ConnectionAddrType type);
133     static void ConvertNodeBasicInfoToDmDevice(const NodeBasicInfo &nodeBasicInfo, DmDeviceInfo &dmDeviceInfo);
134 
135 private:
136     static std::string remoteUdidHash_;
137 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
138     std::shared_ptr<SoftbusSession> softbusSession_;
139 #endif
140     static std::map<std::string, std::shared_ptr<DeviceInfo>> discoveryDeviceInfoMap_;
141     std::shared_ptr<ISoftbusStateCallback> deviceStateManagerCallback_;
142     static std::unordered_map<std::string, std::string> deviceUdidMap_;
143     static std::vector<ProcessInfo> processInfoVec_;
144     static std::mutex discoveryDeviceInfoMutex_;
145     static std::mutex deviceUdidLocks_;
146     static std::mutex processInfoVecMutex_;
147     static std::shared_ptr<ISoftbusConnectorCallback> connectorCallback_;
148 };
149 } // namespace DistributedHardware
150 } // namespace OHOS
151 #endif // OHOS_DM_SOFTBUS_CONNECTOR_H
152