1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 /* 39 * RFCOMM.h 40 */ 41 42 #ifndef __RFCOMM_H 43 #define __RFCOMM_H 44 45 #include "btstack_util.h" 46 47 #include <stdint.h> 48 #include "btstack_run_loop.h" 49 #include "gap.h" 50 51 #if defined __cplusplus 52 extern "C" { 53 #endif 54 55 #define UNLIMITED_INCOMING_CREDITS 0xff 56 57 #define RFCOMM_TEST_DATA_MAX_LEN 4 58 59 #define RFCOMM_RLS_STATUS_INVALID 0xff 60 61 62 // private structs 63 typedef enum { 64 RFCOMM_MULTIPLEXER_CLOSED = 1, 65 RFCOMM_MULTIPLEXER_W4_CONNECT, // outgoing 66 RFCOMM_MULTIPLEXER_SEND_SABM_0, // " 67 RFCOMM_MULTIPLEXER_W4_UA_0, // " 68 RFCOMM_MULTIPLEXER_W4_SABM_0, // incoming 69 RFCOMM_MULTIPLEXER_SEND_UA_0, 70 RFCOMM_MULTIPLEXER_OPEN, 71 RFCOMM_MULTIPLEXER_SEND_UA_0_AND_DISC, 72 RFCOMM_MULTIPLEXER_SHUTTING_DOWN, 73 } RFCOMM_MULTIPLEXER_STATE; 74 75 typedef enum { 76 MULT_EV_READY_TO_SEND = 1, 77 } RFCOMM_MULTIPLEXER_EVENT; 78 79 typedef enum { 80 RFCOMM_CHANNEL_CLOSED = 1, 81 RFCOMM_CHANNEL_W4_MULTIPLEXER, 82 RFCOMM_CHANNEL_SEND_UIH_PN, 83 RFCOMM_CHANNEL_W4_PN_RSP, 84 RFCOMM_CHANNEL_SEND_SABM_W4_UA, 85 RFCOMM_CHANNEL_W4_UA, 86 RFCOMM_CHANNEL_INCOMING_SETUP, 87 RFCOMM_CHANNEL_DLC_SETUP, 88 RFCOMM_CHANNEL_OPEN, 89 RFCOMM_CHANNEL_SEND_UA_AFTER_DISC, 90 RFCOMM_CHANNEL_SEND_DISC, 91 RFCOMM_CHANNEL_W4_UA_AFTER_DISC, 92 RFCOMM_CHANNEL_SEND_DM, 93 RFCOMM_CHANNEL_EMIT_OPEN_FAILED_AND_DISCARD, 94 } RFCOMM_CHANNEL_STATE; 95 96 typedef enum { 97 RFCOMM_CHANNEL_STATE_VAR_NONE = 0, 98 RFCOMM_CHANNEL_STATE_VAR_CLIENT_ACCEPTED = 1 << 0, 99 RFCOMM_CHANNEL_STATE_VAR_RCVD_PN = 1 << 1, 100 RFCOMM_CHANNEL_STATE_VAR_RCVD_RPN = 1 << 2, 101 RFCOMM_CHANNEL_STATE_VAR_RCVD_SABM = 1 << 3, 102 103 RFCOMM_CHANNEL_STATE_VAR_RCVD_MSC_CMD = 1 << 4, 104 RFCOMM_CHANNEL_STATE_VAR_RCVD_MSC_RSP = 1 << 5, 105 RFCOMM_CHANNEL_STATE_VAR_SEND_PN_RSP = 1 << 6, 106 RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_INFO = 1 << 7, 107 108 RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_RSP = 1 << 8, 109 RFCOMM_CHANNEL_STATE_VAR_SEND_UA = 1 << 9, 110 RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_CMD = 1 << 10, 111 RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_RSP = 1 << 11, 112 113 RFCOMM_CHANNEL_STATE_VAR_SEND_CREDITS = 1 << 12, 114 RFCOMM_CHANNEL_STATE_VAR_SENT_MSC_CMD = 1 << 13, 115 RFCOMM_CHANNEL_STATE_VAR_SENT_MSC_RSP = 1 << 14, 116 RFCOMM_CHANNEL_STATE_VAR_SENT_CREDITS = 1 << 15, 117 } RFCOMM_CHANNEL_STATE_VAR; 118 119 typedef struct rfcomm_rpn_data { 120 uint8_t baud_rate; 121 uint8_t flags; 122 uint8_t flow_control; 123 uint8_t xon; 124 uint8_t xoff; 125 uint8_t parameter_mask_0; // first byte 126 uint8_t parameter_mask_1; // second byte 127 } rfcomm_rpn_data_t; 128 129 // info regarding potential connections 130 typedef struct { 131 // linked list - assert: first field 132 btstack_linked_item_t item; 133 134 // packet handler 135 btstack_packet_handler_t packet_handler; 136 137 // server channel 138 uint8_t server_channel; 139 140 // incoming max frame size 141 uint16_t max_frame_size; 142 143 // use incoming flow control 144 uint8_t incoming_flow_control; 145 146 // initial incoming credits 147 uint8_t incoming_initial_credits; 148 149 150 } rfcomm_service_t; 151 152 // info regarding multiplexer 153 // note: spec mandates single multiplexer per device combination 154 typedef struct { 155 // linked list - assert: first field 156 btstack_linked_item_t item; 157 158 btstack_timer_source_t timer; 159 int timer_active; 160 161 RFCOMM_MULTIPLEXER_STATE state; 162 163 uint16_t l2cap_cid; 164 165 uint8_t fcon; // only send if fcon & 1, send rsp if fcon & 0x80 166 167 bd_addr_t remote_addr; 168 hci_con_handle_t con_handle; 169 170 uint8_t outgoing; 171 172 // hack to deal with authentication failure only observed by remote side 173 uint8_t at_least_one_connection; 174 175 uint16_t max_frame_size; 176 177 // send DM for DLCI != 0 178 uint8_t send_dm_for_dlci; 179 180 // non supported command, 0 if not set 181 uint8_t nsc_command; 182 183 // ertm id 184 uint16_t ertm_id; 185 186 // test data - limited to RFCOMM_TEST_DATA_MAX_LEN 187 uint8_t test_data_len; 188 uint8_t test_data[RFCOMM_TEST_DATA_MAX_LEN]; 189 190 } rfcomm_multiplexer_t; 191 192 // info regarding an actual connection 193 typedef struct { 194 195 // linked list - assert: first field 196 btstack_linked_item_t item; 197 198 // packet handler 199 btstack_packet_handler_t packet_handler; 200 201 // server channel (see rfcomm_service_t) - NULL => outgoing channel 202 rfcomm_service_t * service; 203 204 // muliplexer for this channel 205 rfcomm_multiplexer_t *multiplexer; 206 207 // RFCOMM Channel ID 208 uint16_t rfcomm_cid; 209 210 // 211 uint8_t dlci; 212 213 // credits for outgoing traffic 214 uint8_t credits_outgoing; 215 216 // number of packets remote will be granted 217 uint8_t new_credits_incoming; 218 219 // credits for incoming traffic 220 uint8_t credits_incoming; 221 222 // use incoming flow control 223 uint8_t incoming_flow_control; 224 225 // channel state 226 RFCOMM_CHANNEL_STATE state; 227 228 // state variables used in RFCOMM_CHANNEL_INCOMING 229 RFCOMM_CHANNEL_STATE_VAR state_var; 230 231 // priority set by incoming side in PN 232 uint8_t pn_priority; 233 234 // negotiated frame size 235 uint16_t max_frame_size; 236 237 // local rpn data 238 rfcomm_rpn_data_t rpn_data; 239 240 // rls line status. RFCOMM_RLS_STATUS_INVALID if not set 241 uint8_t rls_line_status; 242 243 // msc modem status. 244 uint8_t msc_modem_status; 245 246 // 247 uint8_t waiting_for_can_send_now; 248 249 } rfcomm_channel_t; 250 251 // struct used in ERTM callback 252 typedef struct { 253 // remote address 254 bd_addr_t addr; 255 256 // ERTM ID - returned in RFCOMM_EVENT_ERTM_BUFFER_RELEASED. 257 uint16_t ertm_id; 258 259 // ERTM Configuration - needs to stay valid indefinitely 260 l2cap_ertm_config_t * ertm_config; 261 262 // ERTM buffer 263 uint8_t * ertm_buffer; 264 uint32_t ertm_buffer_size; 265 } rfcomm_ertm_request_t; 266 267 /* API_START */ 268 269 /** 270 * @brief Set up RFCOMM. 271 */ 272 void rfcomm_init(void); 273 274 /** 275 * @brief Set security level required for incoming connections, need to be called before registering services. 276 */ 277 void rfcomm_set_required_security_level(gap_security_level_t security_level); 278 279 /* 280 * @brief Create RFCOMM connection to a given server channel on a remote deivce. 281 * This channel will automatically provide enough credits to the remote side. 282 * @param addr 283 * @param server_channel 284 * @param out_cid 285 * @result status 286 */ 287 uint8_t rfcomm_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t addr, uint8_t server_channel, uint16_t * out_cid); 288 289 /* 290 * @brief Create RFCOMM connection to a given server channel on a remote deivce. 291 * This channel will use explicit credit management. During channel establishment, an initial amount of credits is provided. 292 * @param addr 293 * @param server_channel 294 * @param initial_credits 295 * @param out_cid 296 * @result status 297 */ 298 uint8_t rfcomm_create_channel_with_initial_credits(btstack_packet_handler_t packet_handler, bd_addr_t addr, uint8_t server_channel, uint8_t initial_credits, uint16_t * out_cid); 299 300 /** 301 * @brief Disconnects RFCOMM channel with given identifier. 302 */ 303 void rfcomm_disconnect(uint16_t rfcomm_cid); 304 305 /** 306 * @brief Registers RFCOMM service for a server channel and a maximum frame size, and assigns a packet handler. 307 * This channel provides credits automatically to the remote side -> no flow control 308 * @param packet handler for all channels of this service 309 * @param channel 310 * @param max_frame_size 311 */ 312 uint8_t rfcomm_register_service(btstack_packet_handler_t packet_handler, uint8_t channel, uint16_t max_frame_size); 313 314 /** 315 * @brief Registers RFCOMM service for a server channel and a maximum frame size, and assigns a packet handler. 316 * This channel will use explicit credit management. During channel establishment, an initial amount of credits is provided. 317 * @param packet handler for all channels of this service 318 * @param channel 319 * @param max_frame_size 320 * @param initial_credits 321 */ 322 uint8_t rfcomm_register_service_with_initial_credits(btstack_packet_handler_t packet_handler, uint8_t channel, uint16_t max_frame_size, uint8_t initial_credits); 323 324 /** 325 * @brief Unregister RFCOMM service. 326 */ 327 void rfcomm_unregister_service(uint8_t service_channel); 328 329 /** 330 * @brief Accepts incoming RFCOMM connection. 331 */ 332 void rfcomm_accept_connection(uint16_t rfcomm_cid); 333 334 /** 335 * @brief Deny incoming RFCOMM connection. 336 */ 337 void rfcomm_decline_connection(uint16_t rfcomm_cid); 338 339 /** 340 * @brief Grant more incoming credits to the remote side for the given RFCOMM channel identifier. 341 */ 342 void rfcomm_grant_credits(uint16_t rfcomm_cid, uint8_t credits); 343 344 /** 345 * @brief Checks if RFCOMM can send packet. 346 * @param rfcomm_cid 347 * @result != 0 if can send now 348 */ 349 int rfcomm_can_send_packet_now(uint16_t rfcomm_cid); 350 351 /** 352 * @brief Request emission of RFCOMM_EVENT_CAN_SEND_NOW as soon as possible 353 * @note RFCOMM_EVENT_CAN_SEND_NOW might be emitted during call to this function 354 * so packet handler should be ready to handle it 355 * @param rfcomm_cid 356 */ 357 void rfcomm_request_can_send_now_event(uint16_t rfcomm_cid); 358 359 /** 360 * @brief Sends RFCOMM data packet to the RFCOMM channel with given identifier. 361 * @param rfcomm_cid 362 */ 363 int rfcomm_send(uint16_t rfcomm_cid, uint8_t *data, uint16_t len); 364 365 /** 366 * @brief Sends Local Line Status, see LINE_STATUS_.. 367 * @param rfcomm_cid 368 * @param line_status 369 */ 370 int rfcomm_send_local_line_status(uint16_t rfcomm_cid, uint8_t line_status); 371 372 /** 373 * @brief Send local modem status. see MODEM_STAUS_.. 374 * @param rfcomm_cid 375 * @param modem_status 376 */ 377 int rfcomm_send_modem_status(uint16_t rfcomm_cid, uint8_t modem_status); 378 379 /** 380 * @brief Configure remote port 381 * @param rfcomm_cid 382 * @param baud_rate 383 * @param data_bits 384 * @param stop_bits 385 * @param parity 386 * @param flow_control 387 */ 388 int rfcomm_send_port_configuration(uint16_t rfcomm_cid, rpn_baud_t baud_rate, rpn_data_bits_t data_bits, rpn_stop_bits_t stop_bits, rpn_parity_t parity, rpn_flow_control_t flow_control); 389 390 /** 391 * @brief Query remote port 392 * @param rfcomm_cid 393 */ 394 int rfcomm_query_port_configuration(uint16_t rfcomm_cid); 395 396 /** 397 * @brief Query max frame size 398 * @param rfcomm_cid 399 */ 400 uint16_t rfcomm_get_max_frame_size(uint16_t rfcomm_cid); 401 402 /** 403 * @brief Allow to create RFCOMM packet in outgoing buffer. 404 * if (rfcomm_can_send_packet_now(cid)){ 405 * rfcomm_reserve_packet_buffer(); 406 * uint8_t * buffer = rfcomm_get_outgoing_buffer(); 407 * uint16_t buffer_size = rfcomm_get_max_frame_size(cid); 408 * // .. setup data in buffer with len 409 * rfcomm_send_prepared(cid, len) 410 * } 411 */ 412 int rfcomm_reserve_packet_buffer(void); 413 uint8_t * rfcomm_get_outgoing_buffer(void); 414 int rfcomm_send_prepared(uint16_t rfcomm_cid, uint16_t len); 415 void rfcomm_release_packet_buffer(void); 416 417 /** 418 * @brief Enable L2CAP ERTM mode for RFCOMM. request callback is used to provide ERTM buffer. released callback returns buffer 419 * 420 * @note on request_callback, the app must set the ertm_config, buffer, size fields to enable ERTM for the current connection 421 * @note if buffer is not set, BASIC mode will be used instead 422 * 423 * @note released_callback provides ertm_id from earlier request to match request and release 424 * 425 * @param request_callback 426 * @param released_callback 427 */ 428 void rfcomm_enable_l2cap_ertm(void request_callback(rfcomm_ertm_request_t * request), void released_callback(uint16_t ertm_id)); 429 430 /* API_END */ 431 432 #if defined __cplusplus 433 } 434 #endif 435 436 #endif // __RFCOMM_H 437