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