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