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