• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 SOFTBUS_CONN_INTERFACE_H
17 #define SOFTBUS_CONN_INTERFACE_H
18 #include <stdint.h>
19 
20 #include "softbus_common.h"
21 #include "softbus_def.h"
22 #include "softbus_protocol_def.h"
23 
24 #ifdef __cplusplus
25 #if __cplusplus
26 extern "C" {
27 #endif
28 #endif
29 typedef enum {
30     MODULE_TRUST_ENGINE = 1,
31     MODULE_HICHAIN = 2,
32     MODULE_AUTH_SDK = 3,
33     MODULE_AUTH_CONNECTION = 5,
34     MODULE_MESSAGE_SERVICE = 8,
35     MODULE_AUTH_CHANNEL = 8,
36     MODULE_AUTH_MSG = 9,
37     MODULE_BLUETOOTH_MANAGER = 9,
38     MODULE_CONNECTION = 11,
39     MODULE_DIRECT_CHANNEL = 12,
40     MODULE_PROXY_CHANNEL = 13,
41     MODULE_DEVICE_AUTH = 14,
42     MODULE_P2P_LINK = 15,
43     MODULE_P2P_LISTEN = 16,
44     MODULE_UDP_INFO = 17,
45     MODULE_TIME_SYNC = 18,
46     MODULE_PKG_VERIFY = 20,
47     MODULE_META_AUTH = 21,
48     MODULE_P2P_NEGO = 22,
49     MODULE_BLE_NET = 100,
50     MODULE_BLE_CONN = 101,
51     MODULE_NIP_BR_CHANNEL = 201,
52     MODULE_OLD_NEARBY = 300,
53 } ConnModule;
54 
55 typedef enum {
56     CONNECT_TCP = 1,
57     CONNECT_BR,
58     CONNECT_BLE,
59     CONNECT_P2P,
60     CONNECT_P2P_REUSE,
61     CONNECT_BLE_DIRECT,
62     CONNECT_HML,
63     CONNECT_TRIGGER_HML,
64     CONNECT_TYPE_MAX
65 } ConnectType;
66 
67 #define CONN_INVALID_LISTENER_MODULE_ID    0xffff
68 #define CONN_DYNAMIC_LISTENER_MODULE_COUNT 32
69 #define DEVID_BUFF_LEN                     65
70 
71 #define BT_LINK_TYPE_BR  1
72 #define BT_LINK_TYPE_BLE 2
73 #define HML_INDEX 3
74 #define AUTH_ENHANCED_P2P_NUM 4
75 
76 typedef enum {
77     PROXY = 0,
78     AUTH,
79     AUTH_P2P,
80     AUTH_ENHANCED_P2P_START,
81     AUTH_ENHANCED_P2P_END = AUTH_ENHANCED_P2P_START + AUTH_ENHANCED_P2P_NUM - 1,
82     DIRECT_CHANNEL_SERVER_P2P,
83     DIRECT_CHANNEL_CLIENT,
84     DIRECT_CHANNEL_SERVER_WIFI,
85     DIRECT_CHANNEL_SERVER_HML_START,
86     DIRECT_CHANNEL_SERVER_HML_END = DIRECT_CHANNEL_SERVER_HML_START + HML_INDEX,
87     LANE,
88 
89     LISTENER_MODULE_DYNAMIC_START,
90     LISTENER_MODULE_DYNAMIC_END = LISTENER_MODULE_DYNAMIC_START + CONN_DYNAMIC_LISTENER_MODULE_COUNT,
91     UNUSE_BUTT,
92 } ListenerModule;
93 
94 struct BrInfo {
95     char brMac[BT_MAC_LEN];
96 };
97 struct BleInfo {
98     char bleMac[BT_MAC_LEN];
99     char deviceIdHash[UDID_HASH_LEN];
100     BleProtocolType protocol;
101     uint32_t psm;
102     uint16_t challengeCode;
103 };
104 struct ConnSocketInfo {
105     char addr[IP_LEN];
106     ProtocolType protocol;
107     int32_t port;
108     int32_t fd;
109     int32_t moduleId; /* For details, see {@link ListenerModule}. */
110 };
111 
112 typedef struct {
113     int32_t isAvailable;
114     int32_t isServer;
115     ConnectType type;
116     union {
117         struct BrInfo brInfo;
118         struct BleInfo bleInfo;
119         struct ConnSocketInfo socketInfo;
120     };
121 } ConnectionInfo;
122 
123 typedef struct {
124     void (*OnConnected)(uint32_t connectionId, const ConnectionInfo *info);
125     void (*OnReusedConnected)(uint32_t connectionId, const ConnectionInfo *info);
126     void (*OnDisconnected)(uint32_t connectionId, const ConnectionInfo *info);
127     void (*OnDataReceived)(uint32_t connectionId, ConnModule moduleId, int64_t seq, char *data, int32_t len);
128 } ConnectCallback;
129 
130 typedef enum {
131     CONN_DEFAULT = 0,
132     CONN_LOW,
133     CONN_MIDDLE,
134     CONN_HIGH
135 } SendPriority;
136 
137 typedef enum {
138     CONN_SIDE_ANY = 0,
139     CONN_SIDE_CLIENT,
140     CONN_SIDE_SERVER
141 } ConnSideType;
142 
143 typedef struct {
144     int32_t module; // ConnModule
145     int64_t seq;
146     int32_t flag; // SendPriority
147     int32_t pid;
148     uint32_t len;
149     char *buf;
150 } ConnPostData;
151 
152 typedef struct {
153     void (*OnConnectSuccessed)(uint32_t requestId, uint32_t connectionId, const ConnectionInfo *info);
154     void (*OnConnectFailed)(uint32_t requestId, int32_t reason);
155 } ConnectResult;
156 
157 struct BrOption {
158     char brMac[BT_MAC_LEN];
159     ConnSideType sideType;
160 };
161 
162 struct BleOption {
163     BleProtocolType protocol;
164     char bleMac[BT_MAC_LEN];
165     char deviceIdHash[UDID_HASH_LEN];
166     bool fastestConnectEnable;
167     int32_t psm;
168     uint16_t challengeCode;
169 };
170 
171 struct BleDirectOption {
172     BleProtocolType protoType;
173     char networkId[NETWORK_ID_BUF_LEN];
174 };
175 
176 struct SocketOption {
177     char addr[IP_LEN];
178     int32_t port;
179     int32_t moduleId; /* For details, see {@link ListenerModule}. */
180     ProtocolType protocol;
181     int32_t keepAlive;
182 };
183 
184 typedef struct {
185     ConnectType type;
186     union {
187         struct BrOption brOption;
188         struct BleOption bleOption;
189         struct SocketOption socketOption;
190         struct BleDirectOption bleDirectOption;
191     };
192 } ConnectOption;
193 
194 typedef enum {
195     CONN_BLE_PRIORITY_BALANCED = 0x0,
196     CONN_BLE_PRIORITY_HIGH,
197     CONN_BLE_PRIORITY_LOW_POWER,
198 } ConnectBlePriority;
199 
200 typedef struct {
201     ConnectType type;
202     union {
203         struct {
204             ConnectBlePriority priority;
205         } bleOption;
206     };
207 } UpdateOption;
208 
209 struct ListenerSocketOption {
210     char addr[IP_LEN];
211     int32_t port;
212     ListenerModule moduleId; /* For details, see {@link ListenerModule}. */
213     ProtocolType protocol;
214 };
215 
216 typedef struct {
217     ConnectType type;
218     union {
219         struct ListenerSocketOption socketOption;
220     };
221 } LocalListenerInfo;
222 
223 /**
224  * @ingroup softbus_conn_manager
225  * @brief Get connection header size.
226  * @return <b>SOFTBUS_OK</b> if the header length get is successfully.
227  */
228 uint32_t ConnGetHeadSize(void);
229 
230 /**
231  * @brief The initialization of the connection server is mainly for the initialization of tcp, br, and ble.
232  * This interface is only called once when the soft bus service is created.
233  * @see {@link ConnServerDeinit}
234  * @return <b>SOFTBUS_ERR</b> Repeated initialization or creation of watchlist failed.
235  * @return <b>SOFTBUS_OK</b> Successfully initialized connection server
236  */
237 int32_t ConnServerInit(void);
238 
239 /**
240  * @brief Deinitialize the connection server, the tcp, br, and ble connection servers will be deinitialized.
241  * This interface is only called once when the soft bus service is destroyed.
242  * @see {@link ConnServerInit}
243  */
244 void ConnServerDeinit(void);
245 
246 /**
247  * @ingroup Softbus_conn_manager
248  * @brief Register connection callback.
249  * @see {@link ConnUnSetConnectCallback}
250  * @param[in] moduleId Module ID. For details, see {@link ConnModule}.
251  * @param[in] callback Indicates a pointer to the connection callback. For details, see {@link ConnectCallback}.
252  * @return <b>SOFTBUS_INVALID_PARAM</b> if any parameter is null or invalid.
253  * @return <b>SOFTBUS_OK</b> if set the connection callback is successfully.
254  */
255 int32_t ConnSetConnectCallback(ConnModule moduleId, const ConnectCallback *callback);
256 
257 /**
258  * @ingroup Softbus_conn_manager
259  * @brief Unset the connection callback, clear the callback setting of ConnSetConnectCallback.
260  * @see {@link ConnSetConnectCallback}
261  * @param[in] moduleId Module ID.For details, see {@link ConnModule}.
262  */
263 void ConnUnSetConnectCallback(ConnModule moduleId);
264 
265 /**
266  * @ingroup Softbus_conn_manager
267  * @brief Send data to peer.
268  * @param[in] connectionId Connection ID.
269  * @param[in] data Connection message content. For details, see {@link ConnPostData}.
270  * @return <b>SOFTBUS_INVALID_PARAM</b> if any parameter is null.
271  * @return <b>SOFTBUS_CONN_MANAGER_PKT_LEN_INVALID</b> if the data parameter length is wrong.
272  * @return <b>SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT</b> if the type is null or invalid.
273  * @return <b>SOFTBUS_CONN_MANAGER_OP_NOT_SUPPORT</b> if the bytes result is null.
274  * @return <b>SOFTBUS_OK</b> if sending by byte is successfully.
275  */
276 int32_t ConnPostBytes(uint32_t connectionId, ConnPostData *data);
277 
278 /**
279  * @ingroup Softbus_conn_manager
280  * @brief Type checking of the connection module to check if this type is supported.
281  * @param[in] type Connection type. For details, see {@link ConnectType}.
282  * @return <b>SOFTBUS_OK</b> If checked the connection type is successfully.
283  */
284 int32_t ConnTypeIsSupport(ConnectType type);
285 
286 /**
287  * @ingroup Softbus_conn_manager
288  * @brief Get inner object based on connection id.
289  * @param[in] connectionId Connection ID.
290  * @param[in] info Indicates a pointer to the connection information. For details, see {@link ConnectionInfo}.
291  * @return <b>SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT</b> if the type is null or invalid.
292  * @return <b>SOFTBUS_CONN_MANAGER_OP_NOT_SUPPORT</b> if the result is null.
293  * @return <b>SOFTBUS_OK</b> if the connection information get is successfully.
294  */
295 int32_t ConnGetConnectionInfo(uint32_t connectionId, ConnectionInfo *info);
296 
297 /**
298  * @ingroup Softbus_conn_manager
299  * @brief Request connection id.
300  * @param[in] moduleId ConnModule module ID. For details, see {@link ConnModule}.
301  * @return <b>SOFTBUS_OK</b> if get new request ID is successfully.
302  */
303 uint32_t ConnGetNewRequestId(ConnModule moduleId);
304 
305 /**
306  * @ingroup Softbus_conn_manager
307  * @brief Connect the device interface, call this interface to initiate a connection to the remote end.
308  * @see {@link ConnDisconnectDevice}
309  * @param[in] option Indicates a pointer to the connection option. For details, see {@link ConnectOption}.
310  * @param[in] requestId Request ID.
311  * @param[in] result Indicates a pointer to the connection request. For details, see {@link ConnectResult}.
312  * @return <b>SOFTBUS_INVALID_PARAM</b> if the info is null.
313  * @return <b>SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT</b> if the type is null or invalid.
314  * @return <b>SOFTBUS_ERR</b> if the connection device function of type is null.
315  * @return <b>SOFTBUS_OK</b> if the connection to the device is successfully.
316  */
317 int32_t ConnConnectDevice(const ConnectOption *option, uint32_t requestId, const ConnectResult *result);
318 
319 /**
320  * @ingroup Softbus_conn_manager
321  * @brief Disconnect the device connection interface, disconnect the device logical connection,
322  * and disconnect the physical connection when the logical connection reference is zero.
323  * @see {@link ConnConnectDevice}
324  * @param[in] connectionId Connection ID.
325  * @return <b>SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT</b> if the type is null.
326  * @return <b>SOFTBUS_CONN_MANAGER_OP_NOT_SUPPORT</b> if the disconnection device function of type is null.
327  * @return <b>SOFTBUS_OK</b> if the device disconnected is successfully.
328  */
329 int32_t ConnDisconnectDevice(uint32_t connectionId);
330 
331 /**
332  * @ingroup Softbus_conn_manager
333  * @brief Disconnects all connected device interfaces,
334  * and disconnects the logical and physical connections on the specified device.
335  * @param[in] option Indicates a pointer to the connection option. For details, see {@link ConnectOption}.
336  * @return <b>SOFTBUS_INVALID_PARAM</b> if the option is null.
337  * @return <b>SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT</b> if the type is null.
338  * @return <b>SOFTBUS_CONN_MANAGER_OP_NOT_SUPPORT</b>
339  * if all connected devices all disconnected function of type is null.
340  * @return <b>SOFTBUS_OK</b> if all connected devices all disconnected are successfully.
341  */
342 int32_t ConnDisconnectDeviceAllConn(const ConnectOption *option);
343 
344 /**
345  * @ingroup Softbus_conn_manager
346  * @brief Stop the local monitoring service and stop monitoring the peer connection event.
347  * @see {@link ConnStartLocalListening}
348  * @param[in] info Indicates a pointer to local listener information. For details, see {@link LocalListenerInfo}.
349  * @return <b>SOFTBUS_INVALID_PARAM</b> if the info is null.
350  * @return <b>SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT</b> if the type is null.
351  * @return <b>SOFTBUS_CONN_MANAGER_OP_NOT_SUPPORT</b> if local listener stop function of type is null.
352  * @return <b>SOFTBUS_OK</b> if local listener stop successfully.
353  */
354 int32_t ConnStopLocalListening(const LocalListenerInfo *info);
355 
356 /**
357  * @ingroup Softbus_conn_manager
358  * @brief Start the local monitoring service and listen for the peer connection event.
359  * @see {@link ConnStopLocalListening}
360  * @param[in] info Indicates a pointer to local listener information. For details, see {@link LocalListenerInfo}.
361  * @return <b>SOFTBUS_INVALID_PARAM</b> if the info is null.
362  * @return <b>SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT</b> if the type is null.
363  * @return <b>SOFTBUS_CONN_MANAGER_OP_NOT_SUPPORT</b> if local listener start function of type is null.
364  * @return <b>SOFTBUS_OK</b> if local listeners start successfully.
365  */
366 int32_t ConnStartLocalListening(const LocalListenerInfo *info);
367 
368 /**
369  * @ingroup Softbus_conn_manager
370  * @brief call this interface to initiate a ble direct connection to the remote end.
371  * @param[in] option Indicates a pointer to the connection option. For details, see {@link ConnectOption}.
372  * @param[in] requestId Request ID.
373  * @param[in] result Indicates a pointer to the connection request. For details, see {@link ConnectResult}.
374  * @return <b>SOFTBUS_INVALID_PARAM</b> if the info is null.
375  * @return <b>SOFTBUS_ERR</b> if the connection device function of type is null.
376  * @return <b>SOFTBUS_OK</b> if the connection to the device is successfully.
377  */
378 int32_t ConnBleDirectConnectDevice(const ConnectOption *option, uint32_t requestId, const ConnectResult *result);
379 
380 /**
381  * @ingroup Softbus_conn_manager.
382  * @brief call this interface to check ble direct connect support or not.
383  * @return <b>false</b> if not support.
384  * @return <b>true</b> if support.
385  */
386 bool ConnBleDirectIsEnable(BleProtocolType protocol);
387 
388 bool CheckActiveConnection(const ConnectOption *option);
389 
390 /**
391  * @ingroup Softbus_conn_manager
392  * @brief update connection properties as need
393  * @param[in] connectionId connection id which should be update.
394  * @param[in] option the option will acts on connection
395  * @return <b>SOFTBUS_OK</b> if update connection properties successfully, others if failed.
396  */
397 int32_t ConnUpdateConnection(uint32_t connectionId, UpdateOption *option);
398 
399 /**
400  * @ingroup Softbus_conn_manager
401  * @brief Prevent connect other devices in specified time.
402  * @param[in] option Indicates a pointer to the connection option. For details, see {@link ConnectOption}.
403  * @param[in] time time in millisecond
404  * @return <b>SOFTBUS_OK</b> if prevent connect other devices successfully, others if failed.
405  */
406 int32_t ConnPreventConnection(const ConnectOption *option, uint32_t time);
407 
408 /**
409  * @ingroup Softbus_conn_manager
410  * @brief Obtain link type based on connection ID.
411  * @param[in] connectionId Connection ID.
412  * @param[out] type Indicates a pointer to the link type. For details, see {@link ConnectType}.
413  * @return <b>SOFTBUS_OK</b> if prevent connect other devices successfully, others if failed.
414  */
415 int32_t ConnGetTypeByConnectionId(uint32_t connectionId, ConnectType *type);
416 
417 #ifdef __cplusplus
418 #if __cplusplus
419 }
420 #endif /* __cplusplus */
421 #endif /* __cplusplus */
422 
423 #endif