1 /* 2 * Copyright (C) 2023 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 OHOS_BT_SOCKET_H 17 #define OHOS_BT_SOCKET_H 18 19 #include <stdbool.h> 20 #include <stdint.h> 21 #include "ohos_bt_def.h" 22 23 #ifdef __cplusplus 24 extern "C" { 25 #endif 26 27 #define BT_SOCKET_READ_SOCKET_CLOSED (0) 28 #define BT_SOCKET_READ_FAILED (-1) 29 #define BT_SOCKET_WRITE_FAILED (-1) 30 #define BT_SOCKET_INVALID_ID (-1) 31 #define BT_SOCKET_INVALID_PSM (-1) 32 #define BT_SOCKET_INVALID_SCN (-1) 33 #define BT_SOCKET_INVALID_TYPE (-2) 34 35 typedef enum { 36 OHOS_SOCKET_SPP_RFCOMM = 0x0, 37 OHOS_SOCKET_L2CAP_LE = 0x02, 38 } BluetoothSocketType; 39 40 typedef struct { 41 /** uuid This parameter is required when type is {@link OHOS_SOCKET_SPP_RFCOMM} */ 42 BtUuid uuid; 43 /** type type of socket */ 44 BluetoothSocketType socketType; 45 /** encrypt require the connection to be encrypted */ 46 bool isEncrypt; 47 } BluetoothCreateSocketPara; 48 49 typedef struct { 50 int minInterval; 51 int maxInterval; 52 int peripheralLatency; 53 int supervisionTimeout; 54 int minConnEventLen; 55 int maxConnEventLen; 56 } BluetoothCocUpdateSocketParam; 57 58 /** 59 * @brief callback invoked when the socket connection state changed. 60 * @param bdAddr Indicates the ID of the GATT client. 61 * @param uuid This parameter is required when type is {@link OHOS_SOCKET_SPP_RFCOMM}. 62 * @param status Indicates the connection status {@link ConnStatus}. 63 * @param result Indicates the operation result. 64 */ 65 typedef void (*SocketConnectionStateChangedCallback)(const BdAddr *bdAddr, BtUuid uuid, int status, int result); 66 typedef void (*SocketBleConnectionStateChangedCallback)(const BdAddr *bdAddr, int psm, int status, int result); 67 typedef struct { 68 SocketConnectionStateChangedCallback connStateCb; 69 SocketBleConnectionStateChangedCallback bleConnStateCb; 70 } BtSocketConnectionCallback; 71 72 /** 73 * @brief Creates an server listening socket based on the service record. 74 * 75 * @param socketPara The parameters to create a server socket. 76 * @param name The service's name. 77 * @return Returns a server ID, if create fail return {@link BT_SOCKET_INVALID_ID}. 78 */ 79 int SocketServerCreate(const BluetoothCreateSocketPara *socketPara, const char *name); 80 81 /** 82 * @brief Waits for a remote device to connect to this server socket. 83 * 84 * This method return a client ID indicates a client socket 85 * can be used to read data from and write data to remote device. 86 * This method will block until a connection is established. 87 * 88 * @param serverId The relative ID used to identify the current server socket, obtain the value by calling 89 * {@link SocketServerCreate}. 90 * @return Returns a client ID, if accept fail return {@link BT_SOCKET_INVALID_ID}. 91 */ 92 int SocketServerAccept(int serverId); 93 94 /** 95 * @brief Disables an socket server socket and releases related resources. 96 * 97 * @param serverId The relative ID used to identify the current server socket, obtain the value by calling 98 * {@link SocketServerCreate}. 99 * @return Returns the operation result status {@link BtStatus}. 100 */ 101 int SocketServerClose(int serverId); 102 103 /** 104 * @brief Set fast connection flag 105 * 106 * @param bdAddr The remote device address to connect. 107 * @return Returns the operation result status {@link BtStatus}. 108 */ 109 int SocketSetFastConnection(const BdAddr *bdAddr); 110 111 /** 112 * @brief Connects to a remote device over the socket. 113 * This method will block until a connection is made or the connection fails. 114 * 115 * @param socketPara The param to create a client socket and connect to a remote device. 116 * @param bdAddr The remote device address to connect. 117 * @param psm BluetoothSocketType is {@link OHOS_SOCKET_L2CAP_LE} use dynamic PSM value from remote device. 118 * BluetoothSocketType is {@link OHOS_SOCKET_SPP_RFCOMM} use -1. 119 * @return Returns a client ID, if connect fail return {@link BT_SOCKET_INVALID_ID}. 120 */ 121 int SocketConnect(const BluetoothCreateSocketPara *socketPara, const BdAddr *bdAddr, int psm); 122 123 /** 124 * @brief Connects to a remote device over the socket. 125 * This method will block until a connection is made or the connection fails. 126 * 127 * @param socketPara The param to create a client socket and connect to a remote device. 128 * @param bdAddr The remote device address to connect. 129 * @param psm BluetoothSocketType is {@link OHOS_SOCKET_L2CAP_LE} use dynamic PSM value from remote device. 130 * BluetoothSocketType is {@link OHOS_SOCKET_SPP_RFCOMM} use -1. 131 * @param callback Reference to the socket state observer. 132 * @return Returns a client ID, if connect fail return {@link BT_SOCKET_INVALID_ID}. 133 */ 134 int SocketConnectEx(const BluetoothCreateSocketPara *socketPara, const BdAddr *bdAddr, int psm, 135 BtSocketConnectionCallback *callback); 136 137 /** 138 * @brief Disables a connection and releases related resources. 139 * 140 * @param clientId The relative ID used to identify the current client socket. 141 * @return Returns the operation result status {@link BtStatus}. 142 */ 143 int SocketDisconnect(int clientId); 144 145 /** 146 * @brief Get the connection status of this socket. 147 * 148 * @param clientId The relative ID used to identify the current client socket. 149 * @return Returns true is connected or false is not connected. 150 */ 151 bool IsSocketConnected(int clientId); 152 153 /** 154 * @brief Socket get remote device's address. 155 * 156 * @param clientId The relative ID used to identify the current client socket. 157 * @param remoteAddr Remote device's address, memory allocated by caller. 158 * @return Returns the operation result status {@link BtStatus}. 159 */ 160 int SocketGetRemoteAddr(int clientId, BdAddr *remoteAddr); 161 162 /** 163 * @brief Read data from socket. 164 * This method blocks until input data is available. 165 * 166 * @param clientId The relative ID used to identify the current client socket. 167 * @param buf Indicate the buffer which read in, memory allocated by caller. 168 * @param bufLen Indicate the buffer length. 169 * @return Returns the length greater than 0 as read the actual length. 170 * Returns {@link BT_SOCKET_READ_SOCKET_CLOSED} if the socket is closed. 171 * Returns {@link BT_SOCKET_READ_FAILED} if the operation failed. 172 */ 173 int SocketRead(int clientId, uint8_t *buf, uint32_t bufLen); 174 175 /** 176 * @brief Client write data to socket. 177 * 178 * @param clientId The relative ID used to identify the current client socket. 179 * @param data Indicate the data to be written. 180 * @param len Indicates the length of the data to be written. 181 * @return Returns the actual write length. 182 * Returns {@link BT_SOCKET_WRITE_FAILED} if the operation failed. 183 */ 184 int SocketWrite(int clientId, const uint8_t *data, uint32_t len); 185 186 /** 187 * @brief Get dynamic PSM value for OHOS_SOCKET_L2CAP. 188 * 189 * @param serverId The relative ID used to identify the current server socket, obtain the value by calling 190 * {@link SocketServerCreate}. 191 * @return Returns the PSM value. 192 * Returns {@link BT_SOCKET_INVALID_PSM} if the operation failed. 193 */ 194 int SocketGetPsm(int serverId); 195 196 /** 197 * @brief Get server scm number for OHOS_SOCKET_RFCOMM. 198 * 199 * @param serverId The relative ID used to identify the current server socket, obtain the value by calling 200 * {@link SocketServerCreate}. 201 * @return Returns the scn number. 202 * Returns {@link BT_SOCKET_INVALID_SCN} if the operation failed. 203 */ 204 int SocketGetScn(int serverId); 205 206 /** 207 * @brief Adjust the socket send and recv buffer size, limit range is 4KB to 50KB 208 * 209 * @param clientId The relative ID used to identify the current client socket. 210 * @param bufferSize The buffer size want to set, unit is byte. 211 * @return Returns the operation result status {@link BtStatus}. 212 */ 213 int SetSocketBufferSize(int clientId, uint32_t bufferSize); 214 215 /** 216 * @brief Update the coc connection params. 217 * 218 * @param param CocUpdateSocketParam instance for carry params. 219 * @return Returns the operation result status {@link BtStatus}. 220 */ 221 int SocketUpdateCocConnectionParams(BluetoothCocUpdateSocketParam* param, const BdAddr *bdAddr); 222 223 #ifdef __cplusplus 224 } 225 #endif 226 #endif