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 /** 17 * @addtogroup Bluetooth 18 * @{ 19 * 20 * @brief This file is a part of BTStack. 21 * 22 * @since 6 23 */ 24 25 /** 26 * @file rfcomm.h 27 * 28 * @brief RFCOMM protocal interface. 29 * 30 * @since 6 31 */ 32 33 #ifndef RFCOMM_H 34 #define RFCOMM_H 35 36 #include <stdlib.h> 37 #include <stdbool.h> 38 #include <stddef.h> 39 #include <stdint.h> 40 41 #include "packet.h" 42 #include "btstack.h" 43 44 #ifdef __cplusplus 45 extern "C" { 46 #endif 47 48 // RFCOMM event declarations 49 // The event is triggered when rfcomm server accept a new connection, 50 // @see RfcommIncomingInfo for (void* event_data)'s struct. 51 #define RFCOMM_CHANNEL_EV_CONNECT_INCOMING 0x00000001 52 // The event is triggered when connection complete successfully. 53 // @see RfcommConnectedInfo for (void* event_data)'s struct. 54 #define RFCOMM_CHANNEL_EV_CONNECT_SUCCESS 0x00000002 55 // The event is triggered when connection failed. 56 // (void* event_data) is null. 57 #define RFCOMM_CHANNEL_EV_CONNECT_FAIL 0x00000004 58 // This event is triggered when a disconnect request is received. 59 // (void* event_data) is null. 60 #define RFCOMM_CHANNEL_EV_DISCONNECTED 0x00000008 61 // This event is triggered when the disconnection process is successful. 62 // (void* event_data) is null. 63 #define RFCOMM_CHANNEL_EV_DISCONNECT_SUCCESS 0x00000010 64 // This event is triggered when the disconnection process fails. 65 // (void* event_data) is null. 66 #define RFCOMM_CHANNEL_EV_DISCONNECT_FAIL 0x00000020 67 // The event is triggered when peer or RFCOMM is available to receive data, 68 // (void* event_data) is null. 69 #define RFCOMM_CHANNEL_EV_FC_ON 0x00000040 70 // The event is triggered when data is received from L2CAP, 71 // (void* event_data) is null. 72 #define RFCOMM_CHANNEL_EV_REV_DATA 0x00000080 73 // The event is triggered when remote's V.24 control signals are changed. 74 // @see RfcommModemStatus for (void* event_data)'s struct. 75 #define RFCOMM_CHANNEL_EV_MODEM_STATUS 0x00000100 76 // The event is triggered when remote port negotiation settings are changed, 77 // @see RfcommRemotePortConfig for (void* event_data)'s struct. 78 #define RFCOMM_CHANNEL_EV_REMOTE_PORT_CONFIG 0x00000200 79 // The event is triggered when remote line status are changed, 80 // @see RfcommRemoteLineStatus for (void* event_data)'s struct. 81 #define RFCOMM_CHANNEL_EV_REMOTE_LINE_STATUS 0x00000400 82 83 // RFCOMM function's return value. 84 #define RFCOMM_SUCCESS BT_SUCCESS // Function successful 85 #define RFCOMM_ERR_NO_RESOURCES BT_OPERATION_FAILED // Not enough resources 86 #define RFCOMM_ERR_NOT_CONNECTED BT_BAD_STATUS // Channel(DLC) is not connected 87 #define RFCOMM_ERR_PARAM BT_BAD_PARAM // paramas is error 88 #define RFCOMM_ALREADY_EXIST BT_ALREADY // Already exist 89 #define RFCOMM_NO_DATA BT_OPERATION_FAILED // No data 90 #define RFCOMM_FAILED BT_OPERATION_FAILED // Function fail 91 #define RFCOMM_QUEUE_FULL BT_OPERATION_FAILED // Send queue is full 92 #define RFCOMM_OVERRUN BT_BAD_PARAM // The length of the sent data exceeds mtu 93 94 /** 95 * @brief The peer's bt-address and connected server number. 96 */ 97 typedef struct { 98 // The client's bt-address 99 BtAddr addr; 100 // The connected server number 101 uint8_t scn; 102 } RfcommIncomingInfo; 103 104 /** 105 * @brief The channel's basic information. 106 */ 107 typedef struct { 108 // The maximum size of data that can be transmitted. 109 uint16_t sendMTU; 110 // The maximum data size that the peer can send. 111 uint16_t recvMTU; 112 } RfcommConnectedInfo; 113 114 /** 115 * @brief Modem Status.(V.24 control signals). 116 */ 117 typedef struct { 118 // Length of break in units of 200ms.Range 0~15. 119 uint8_t break_length; 120 // A break signal.The bit is set to 1 to indicate that a break signal detected in the data stream. 121 uint8_t break_signal; 122 // Flow Control (FC). The bit is set to 1(one) when the device is unable to accept frames. 123 uint8_t fc; 124 // Ready To Communicate (RTC). The bit is set to 1 when the device is ready to communicate. 125 uint8_t rtc; 126 // Ready To Receive (RTR). The bit is set to 1 when the device is ready to receive data. 127 uint8_t rtr; 128 // Incoming call indicator (IC). The bit is set to 1 to indicate an incoming call. 129 uint8_t ic; 130 // Data Valid (DV). The bit is set to 1 to indicate that valid data is being sent. 131 uint8_t dv; 132 } RfcommModemStatus; 133 134 /** 135 * @brief Remote port communication settings. 136 */ 137 typedef struct { 138 // The baudrate.The default value is 1100 0000 (9600 bit/s). 139 uint8_t baudrate; 140 // Data bits.The default value is 11 (8 bits ) 141 uint8_t data_bits; 142 // Stop bits: S=0: 1 stop bit, S=1: 1,5 stop bits. Default value = 0 (1 stop bit) 143 uint8_t stop_bit; 144 // The parity. P=0: no parity, P=1: parity. Default value = 0 (no parity) 145 uint8_t parity; 146 // Parity type.00 odd parity,01 even parity,10 mark parity,11 space parity 147 uint8_t parity_type; 148 // Flow control.(Default value=0, no flow control) 149 // Bit1 XON/XOFF on input,Bit2 XON/XOFF on output,Bit3 RTR on input, 150 // Bit4 RTR on output,Bit5 RTC on input,Bit6 RTC on output. 151 uint8_t fc; 152 // XON character (default DC1) 153 uint8_t xon_char; 154 // XOFF character.(default DC3) 155 uint8_t xoff_char; 156 // The parameter mask is used to indicate which parameters in the RPN command are negotiated. 157 uint8_t parameter_mask1; 158 uint8_t parameter_mask2; 159 } RfcommRemotePortConfig; 160 161 /** 162 * @brief Remote Line Status. 163 */ 164 typedef struct { 165 // Overrun Error - Received character overwrote an unread character 166 uint8_t overrunErr; 167 // Parity Error - Received characters parity was incorrect 168 uint8_t parityErr; 169 // Framing Error - a character did not terminate with a stop bit 170 uint8_t frameErr; 171 } RfcommRemoteLineStatus; 172 173 /** 174 * @brief Port's information. 175 */ 176 typedef struct { 177 // The amount of data received 178 uint32_t receivedBytes; 179 // The amount of data sent 180 uint32_t transmittedBytes; 181 } RfcommPortState; 182 183 /** 184 * @brief The callback function is used to notify the upper layer of the event and related information. 185 * 186 * @param handle The channel(DLC)'s handle number 187 * @param eventId Event Id 188 * @param event_data The event's data 189 * @param context The content passed in from the upper layer. 190 * @since 6 191 */ 192 typedef void (*RFCOMM_EventCallback)(uint16_t handle, uint32_t eventId, const void *eventData, void *context); 193 194 /** 195 * @brief Connection request information. 196 */ 197 typedef struct { 198 BtAddr addr; 199 uint8_t scn; 200 uint16_t mtu; 201 uint32_t eventMask; 202 RFCOMM_EventCallback callback; 203 void *context; 204 } RfcommConnectReqInfo; 205 206 /** 207 * @brief The function is used to assign server numbers to individual servers. 208 * Server number is used to register with the RFCOMM service interface, range is 1~30. 209 * When the return value is 0, it means that there is no available server number. 210 * This Server number shall be registered in the Service Discovery Database; 211 * RFCOMM_SPEC_V12 #5.4 212 * 213 * @return Server number.0(unavailable number),1~30(available number) 214 * @since 6 215 */ 216 uint8_t BTSTACK_API RFCOMM_AssignServerNum(); 217 218 /** 219 * @brief After close the server, free the server number. 220 * 221 * @param scn Server number. 222 * @return Returns <b>RFCOMM_SUCCESS</b> if the operation is successful, otherwise the operation fails. 223 * @since 6 224 */ 225 int BTSTACK_API RFCOMM_FreeServerNum(uint8_t scn); 226 227 /** 228 * @brief The function is used by the client to establish a connection of the channel. 229 * 230 * @param reqInfo Connection request information. 231 * @param handle The handle of the channel created by rfcomm 232 * @return Returns <b>RFCOMM_SUCCESS</b> if the operation is successful, otherwise the operation fails. 233 * @since 6 234 */ 235 int BTSTACK_API RFCOMM_ConnectChannel(const RfcommConnectReqInfo *reqInfo, uint16_t *handle); 236 237 /** 238 * @brief The function is used for the server to register with RFCOMM and 239 * wait for the client to connect. 240 * 241 * @param scn The server's number. 242 * @param mtu The maximum size of data received at a time. 243 * @param eventMask The collection of events followed by upper layers. 244 * @param callback The callback function used by rfcomm to notify uppers of data or events. 245 * @param context The content passed in from the upper layer. 246 * It will be brought back to the upper layer when callback is called. 247 * @return Returns <b>RFCOMM_SUCCESS</b> if the operation is successful, otherwise the operation fails. 248 * @since 6 249 */ 250 int BTSTACK_API RFCOMM_RegisterServer(uint8_t scn, uint16_t mtu, uint32_t eventMask, 251 RFCOMM_EventCallback callback, void *context); 252 253 /** 254 * @brief When the server is shut down, call this function to 255 * release the resources about the server held in RFCOMM. 256 * 257 * @param scn The server's number 258 * @param isRemoveCallback true:remove callback,RFCOMM will not notify event to upper layer.otherwise:false. 259 * @return Returns <b>RFCOMM_SUCCESS</b> if the operation is successful, otherwise the operation fails. 260 * @since 6 261 */ 262 int BTSTACK_API RFCOMM_DeregisterServer(uint8_t scn, bool isRemoveCallback); 263 264 /** 265 * @brief The function is used to tell RFCOMM to accept the connection request when the server 266 * receives the connection notification(eventId is RFCOMM_EVENT_CONNECT_INCOMING). 267 * After receiving the response from the upper layer, RFCOMM notifies the client 268 * of the peer device to accept the connection request. 269 * 270 * @param handle The channel(DLC)'s handle number 271 * @return Returns <b>RFCOMM_SUCCESS</b> if the operation is successful, otherwise the operation fails. 272 * @since 6 273 */ 274 int BTSTACK_API RFCOMM_AcceptConnection(uint16_t handle); 275 276 /** 277 * @brief This function is used to tell RFCOMM to reject the connection request when the server 278 * receives the connection notification(eventId is RFCOMM_EVENT_CONNECT_INCOMING). 279 * After receiving the response from the upper layer, RFCOMM notifies the client 280 * of the peer device to reject the connection request. 281 * 282 * @param handle The channel(DLC)'s handle number 283 * @return Returns <b>RFCOMM_SUCCESS</b> if the operation is successful, otherwise the operation fails. 284 * @since 6 285 */ 286 int BTSTACK_API RFCOMM_RejectConnection(uint16_t handle); 287 288 /** 289 * @brief The function is used to disconnect the specified channel. 290 * RFCOMM_SPEC_V12 #5.2 291 * 292 * @param handle The channel(DLC)'s handle number 293 * @return Returns <b>RFCOMM_SUCCESS</b> if the operation is successful, otherwise the operation fails. 294 * @since 6 295 */ 296 int BTSTACK_API RFCOMM_DisconnectChannel(uint16_t handle); 297 298 /** 299 * @brief The function is used for set the remote port communication settings. 300 * The command may be used before a new DLC is opened and shall be used 301 * whenever the port settings change. 302 * RFCOMM_SPEC_V12 #5.5.1 303 * 304 * @param handle The channel(DLC)'s handle number 305 * @param config Remote port negotiation parameters 306 * @return Returns <b>RFCOMM_SUCCESS</b> if the operation is successful, otherwise the operation fails. 307 * @since 6 308 */ 309 int BTSTACK_API RFCOMM_SetPortConfig(uint16_t handle, const RfcommRemotePortConfig *config); 310 311 /** 312 * @brief The function is used to obtain the remote port negotiation information 313 * of the peer device. 314 * 315 * @param handle The channel(DLC)'s handle number 316 * @return Returns <b>RFCOMM_SUCCESS</b> if the operation is successful, otherwise the operation fails. 317 * @since 6 318 */ 319 int BTSTACK_API RFCOMM_ReqPortConfig(uint16_t handle); 320 321 /** 322 * @brief The function is used for indication of remote port line status. 323 * RFCOMM_SPEC_V12 #5.5.2 324 * 325 * @param handle The channel(DLC)'s handle number 326 * @param lineStatus Remote line status 327 * @return Returns <b>RFCOMM_SUCCESS</b> if the operation is successful, otherwise the operation fails. 328 * @since 6 329 */ 330 int BTSTACK_API RFCOMM_SendRemoteLineStatus(uint16_t handle, const RfcommRemoteLineStatus *lineStatus); 331 332 /** 333 * @brief The function is used to convey the RS-232 control signals and the break signal. 334 * RFCOMM_SPEC_V12 #2.2 335 * 336 * @param handle The channel(DLC)'s handle number 337 * @param modemStatus Control signals and the break signal 338 * @return Returns <b>RFCOMM_SUCCESS</b> if the operation is successful, otherwise the operation fails. 339 * @since 6 340 */ 341 int BTSTACK_API RFCOMM_SetModemStatus(uint16_t handle, const RfcommModemStatus *modemStatus); 342 343 /** 344 * @brief This function is used to obtain port related information. 345 * Currently, the amount of data sent and received by the port can be obtained. 346 * 347 * @param handle The channel(DLC)'s handle number 348 * @param state The port's information 349 * @return Returns <b>RFCOMM_SUCCESS</b> if the operation is successful, otherwise the operation fails. 350 * @since 6 351 */ 352 int BTSTACK_API RFCOMM_GetPortState(uint16_t handle, RfcommPortState *state); 353 354 /** 355 * @brief This function is used to get the payload packet sent by the peer from RFCOMM. 356 * After the caller finishes using this interface, it creates a packet reference or 357 * reads the payload buffer as needed, and must free the packet obtained from RFCOMM. 358 * 359 * @param handle The channel(DLC)'s handle number 360 * @param pkt The packet point for receiving data 361 * @return Returns <b>RFCOMM_SUCCESS</b> if the operation is successful, otherwise the operation fails. 362 * @since 6 363 */ 364 int BTSTACK_API RFCOMM_Read(uint16_t handle, Packet **pkt); 365 366 /** 367 * @brief This function is used to write the data to be transmitted to the opposite end to RFCOMM. 368 * 369 * @param handle The channel(DLC)'s handle number 370 * @param pkt The packet for sending data 371 * @return Returns <b>RFCOMM_SUCCESS</b> if the operation is successful, otherwise the operation fails. 372 * @since 6 373 */ 374 int BTSTACK_API RFCOMM_Write(uint16_t handle, Packet *pkt); 375 376 /** 377 * @brief This function is used to send Test Command to the peer. 378 * 379 * @param handle The channel(DLC)'s handle number 380 * @param pkt The packet for sending data 381 * @return Returns <b>RFCOMM_SUCCESS</b> if the operation is successful, otherwise the operation fails. 382 * @since 6 383 */ 384 int BTSTACK_API RFCOMM_SendTestCmd(uint16_t handle, Packet *pkt); 385 386 #ifdef __cplusplus 387 } 388 #endif 389 390 #endif // RFCOMM_H