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