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 BLUEKITCHEN 24 * GMBH 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 * @title RFCOMM 40 * 41 */ 42 43 #ifndef RFCOMM_H 44 #define RFCOMM_H 45 46 #include "btstack_util.h" 47 48 #include <stdint.h> 49 #include "btstack_run_loop.h" 50 #include "gap.h" 51 #include "l2cap.h" 52 53 #if defined __cplusplus 54 extern "C" { 55 #endif 56 57 #define UNLIMITED_INCOMING_CREDITS 0xff 58 59 #define RFCOMM_TEST_DATA_MAX_LEN 4 60 61 #define RFCOMM_RLS_STATUS_INVALID 0xff 62 63 64 // private structs 65 typedef enum { 66 RFCOMM_MULTIPLEXER_CLOSED = 1, 67 RFCOMM_MULTIPLEXER_W4_CONNECT, // outgoing 68 RFCOMM_MULTIPLEXER_SEND_SABM_0, // " 69 RFCOMM_MULTIPLEXER_W4_UA_0, // " 70 RFCOMM_MULTIPLEXER_W4_SABM_0, // incoming 71 RFCOMM_MULTIPLEXER_SEND_UA_0, 72 RFCOMM_MULTIPLEXER_OPEN, 73 RFCOMM_MULTIPLEXER_SEND_UA_0_AND_DISC, 74 RFCOMM_MULTIPLEXER_SHUTTING_DOWN, 75 } RFCOMM_MULTIPLEXER_STATE; 76 77 typedef enum { 78 MULT_EV_READY_TO_SEND = 1, 79 } RFCOMM_MULTIPLEXER_EVENT; 80 81 typedef enum { 82 RFCOMM_CHANNEL_CLOSED = 1, 83 RFCOMM_CHANNEL_W4_MULTIPLEXER, 84 RFCOMM_CHANNEL_SEND_UIH_PN, 85 RFCOMM_CHANNEL_W4_PN_RSP, 86 RFCOMM_CHANNEL_SEND_SABM_W4_UA, 87 RFCOMM_CHANNEL_W4_UA, 88 RFCOMM_CHANNEL_INCOMING_SETUP, 89 RFCOMM_CHANNEL_DLC_SETUP, 90 RFCOMM_CHANNEL_OPEN, 91 RFCOMM_CHANNEL_SEND_UA_AFTER_DISC, 92 RFCOMM_CHANNEL_SEND_DISC, 93 RFCOMM_CHANNEL_W4_UA_AFTER_DISC, 94 RFCOMM_CHANNEL_SEND_DM, 95 RFCOMM_CHANNEL_EMIT_OPEN_FAILED_AND_DISCARD, 96 } RFCOMM_CHANNEL_STATE; 97 98 99 #define RFCOMM_CHANNEL_STATE_VAR_NONE 0 100 #define RFCOMM_CHANNEL_STATE_VAR_CLIENT_ACCEPTED 1 << 0 101 #define RFCOMM_CHANNEL_STATE_VAR_RCVD_PN 1 << 1 102 #define RFCOMM_CHANNEL_STATE_VAR_RCVD_SABM 1 << 2 103 #define RFCOMM_CHANNEL_STATE_VAR_RCVD_MSC_CMD 1 << 3 104 #define RFCOMM_CHANNEL_STATE_VAR_RCVD_MSC_RSP 1 << 4 105 #define RFCOMM_CHANNEL_STATE_VAR_SEND_PN_RSP 1 << 5 106 #define RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_QUERY 1 << 6 107 #define RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_CONFIG 1 << 7 108 #define RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_RESPONSE 1 << 8 109 #define RFCOMM_CHANNEL_STATE_VAR_EMIT_RPN_RESPONSE 1 << 9 110 #define RFCOMM_CHANNEL_STATE_VAR_SEND_UA 1 << 10 111 #define RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_CMD 1 << 11 112 #define RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_RSP 1 << 12 113 #define RFCOMM_CHANNEL_STATE_VAR_SEND_CREDITS 1 << 13 114 #define RFCOMM_CHANNEL_STATE_VAR_SENT_MSC_CMD 1 << 14 115 #define RFCOMM_CHANNEL_STATE_VAR_SENT_MSC_RSP 1 << 15 116 #define RFCOMM_CHANNEL_STATE_VAR_SENT_CREDITS 1 << 16 117 118 typedef struct rfcomm_rpn_data { 119 uint8_t baud_rate; 120 uint8_t flags; 121 uint8_t flow_control; 122 uint8_t xon; 123 uint8_t xoff; 124 uint8_t parameter_mask_0; // first byte 125 uint8_t parameter_mask_1; // second byte 126 } rfcomm_rpn_data_t; 127 128 // info regarding potential connections 129 typedef struct { 130 // linked list - assert: first field 131 btstack_linked_item_t item; 132 133 // packet handler 134 btstack_packet_handler_t packet_handler; 135 136 // server channel 137 uint8_t server_channel; 138 139 // incoming max frame size 140 uint16_t max_frame_size; 141 142 // use incoming flow control 143 uint8_t incoming_flow_control; 144 145 // initial incoming credits 146 uint8_t incoming_initial_credits; 147 148 149 } rfcomm_service_t; 150 151 // info regarding multiplexer 152 // note: spec mandates single multiplexer per device combination 153 typedef struct { 154 // linked list - assert: first field 155 btstack_linked_item_t item; 156 157 btstack_timer_source_t timer; 158 int timer_active; 159 160 RFCOMM_MULTIPLEXER_STATE state; 161 162 uint16_t l2cap_cid; 163 164 uint8_t fcon; // only send if fcon & 1, send rsp if fcon & 0x80 165 166 bd_addr_t remote_addr; 167 hci_con_handle_t con_handle; 168 169 uint8_t outgoing; 170 171 // hack to deal with authentication failure only observed by remote side 172 uint8_t at_least_one_connection; 173 174 uint16_t max_frame_size; 175 176 // send DM for DLCI != 0 177 uint8_t send_dm_for_dlci; 178 179 // non supported command, 0 if not set 180 uint8_t nsc_command; 181 182 // ertm id 183 uint16_t ertm_id; 184 185 // test data - limited to RFCOMM_TEST_DATA_MAX_LEN 186 uint8_t test_data_len; 187 uint8_t test_data[RFCOMM_TEST_DATA_MAX_LEN]; 188 189 } rfcomm_multiplexer_t; 190 191 // info regarding an actual connection 192 typedef struct { 193 194 // linked list - assert: first field 195 btstack_linked_item_t item; 196 197 // packet handler 198 btstack_packet_handler_t packet_handler; 199 200 // server channel (see rfcomm_service_t) - NULL => outgoing channel 201 rfcomm_service_t * service; 202 203 // muliplexer for this channel 204 rfcomm_multiplexer_t *multiplexer; 205 206 // RFCOMM Channel ID 207 uint16_t rfcomm_cid; 208 209 // 210 uint8_t dlci; 211 212 // credits for outgoing traffic 213 uint8_t credits_outgoing; 214 215 // number of packets remote will be granted 216 uint8_t new_credits_incoming; 217 218 // credits for incoming traffic 219 uint8_t credits_incoming; 220 221 // use incoming flow control 222 uint8_t incoming_flow_control; 223 224 // channel state 225 RFCOMM_CHANNEL_STATE state; 226 227 // state variables/flags 228 uint32_t state_var; 229 230 // priority set by incoming side in PN 231 uint8_t pn_priority; 232 233 // negotiated frame size 234 uint16_t max_frame_size; 235 236 // local rpn data 237 rfcomm_rpn_data_t local_rpn_data; 238 239 // remote rpn data 240 rfcomm_rpn_data_t remote_rpn_data; 241 242 // local line status. RFCOMM_RLS_STATUS_INVALID if not set 243 // buffers local status for RLS CMD 244 uint8_t local_line_status; 245 246 // remote line status. RFCOMM_RLS_STATUS_INVALID if not set 247 // send RLS RSP with status from the RLS CMD 248 uint8_t remote_line_status; 249 250 // local modem status. 251 uint8_t local_modem_status; 252 253 // remote modem status. 254 uint8_t remote_modem_status; 255 256 // 257 uint8_t waiting_for_can_send_now; 258 259 } rfcomm_channel_t; 260 261 // struct used in ERTM callback 262 typedef struct { 263 // remote address 264 bd_addr_t addr; 265 266 // ERTM ID - returned in RFCOMM_EVENT_ERTM_BUFFER_RELEASED. 267 uint16_t ertm_id; 268 269 // ERTM Configuration - needs to stay valid indefinitely 270 l2cap_ertm_config_t * ertm_config; 271 272 // ERTM buffer 273 uint8_t * ertm_buffer; 274 uint32_t ertm_buffer_size; 275 } rfcomm_ertm_request_t; 276 277 /* API_START */ 278 279 /** 280 * @brief Set up RFCOMM. 281 */ 282 void rfcomm_init(void); 283 284 /** 285 * @brief Set security level required for incoming connections, need to be called before registering services. 286 * @deprecated use gap_set_security_level instead 287 */ 288 void rfcomm_set_required_security_level(gap_security_level_t security_level); 289 290 /* 291 * @brief Create RFCOMM connection to a given server channel on a remote deivce. 292 * This channel will automatically provide enough credits to the remote side. 293 * @param addr 294 * @param server_channel 295 * @param out_cid 296 * @result status 297 */ 298 uint8_t rfcomm_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t addr, uint8_t server_channel, uint16_t * out_cid); 299 300 /* 301 * @brief Create RFCOMM connection to a given server channel on a remote deivce. 302 * This channel will use explicit credit management. During channel establishment, an initial amount of credits is provided. 303 * @param addr 304 * @param server_channel 305 * @param initial_credits 306 * @param out_cid 307 * @result status 308 */ 309 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); 310 311 /** 312 * @brief Disconnects RFCOMM channel with given identifier. 313 * @return status 314 */ 315 uint8_t rfcomm_disconnect(uint16_t rfcomm_cid); 316 317 /** 318 * @brief Registers RFCOMM service for a server channel and a maximum frame size, and assigns a packet handler. 319 * This channel provides credits automatically to the remote side -> no flow control 320 * @param packet handler for all channels of this service 321 * @param channel 322 * @param max_frame_size 323 * @return status ERROR_CODE_SUCCESS if successful, otherwise L2CAP_SERVICE_ALREADY_REGISTERED or BTSTACK_MEMORY_ALLOC_FAILED 324 */ 325 uint8_t rfcomm_register_service(btstack_packet_handler_t packet_handler, uint8_t channel, uint16_t max_frame_size); 326 327 /** 328 * @brief Registers RFCOMM service for a server channel and a maximum frame size, and assigns a packet handler. 329 * This channel will use explicit credit management. During channel establishment, an initial amount of credits is provided. 330 * @param packet handler for all channels of this service 331 * @param channel 332 * @param max_frame_size 333 * @param initial_credits 334 * @return status ERROR_CODE_SUCCESS if successful, otherwise L2CAP_SERVICE_ALREADY_REGISTERED or BTSTACK_MEMORY_ALLOC_FAILED 335 */ 336 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); 337 338 /** 339 * @brief Unregister RFCOMM service. 340 */ 341 void rfcomm_unregister_service(uint8_t service_channel); 342 343 /** 344 * @brief Accepts incoming RFCOMM connection. 345 * @return status 346 */ 347 uint8_t rfcomm_accept_connection(uint16_t rfcomm_cid); 348 349 /** 350 * @brief Deny incoming RFCOMM connection. 351 * @return status 352 */ 353 uint8_t rfcomm_decline_connection(uint16_t rfcomm_cid); 354 355 /** 356 * @brief Grant more incoming credits to the remote side for the given RFCOMM channel identifier. 357 * @return status 358 */ 359 uint8_t rfcomm_grant_credits(uint16_t rfcomm_cid, uint8_t credits); 360 361 /** 362 * @brief Checks if RFCOMM can send packet. 363 * @param rfcomm_cid 364 * @result true if can send now 365 */ 366 bool rfcomm_can_send_packet_now(uint16_t rfcomm_cid); 367 368 /** 369 * @brief Request emission of RFCOMM_EVENT_CAN_SEND_NOW as soon as possible 370 * @note RFCOMM_EVENT_CAN_SEND_NOW might be emitted during call to this function 371 * so packet handler should be ready to handle it 372 * @param rfcomm_cid 373 * @prarm status 374 */ 375 uint8_t rfcomm_request_can_send_now_event(uint16_t rfcomm_cid); 376 377 /** 378 * @brief Sends RFCOMM data packet to the RFCOMM channel with given identifier. 379 * @param rfcomm_cid 380 * @return status 381 */ 382 uint8_t rfcomm_send(uint16_t rfcomm_cid, uint8_t *data, uint16_t len); 383 384 /** 385 * @brief Sends Local Line Status, see LINE_STATUS_.. 386 * @param rfcomm_cid 387 * @param line_status 388 * @return status 389 */ 390 uint8_t rfcomm_send_local_line_status(uint16_t rfcomm_cid, uint8_t line_status); 391 392 /** 393 * @brief Send local modem status. see MODEM_STAUS_.. 394 * @param rfcomm_cid 395 * @param modem_status 396 * @return status 397 */ 398 uint8_t rfcomm_send_modem_status(uint16_t rfcomm_cid, uint8_t modem_status); 399 400 /** 401 * @brief Configure remote port 402 * @param rfcomm_cid 403 * @param baud_rate 404 * @param data_bits 405 * @param stop_bits 406 * @param parity 407 * @param flow_control 408 * @return status 409 */ 410 uint8_t 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, uint8_t flow_control); 411 412 /** 413 * @brief Query remote port 414 * @param rfcomm_cid 415 * @return status 416 */ 417 uint8_t rfcomm_query_port_configuration(uint16_t rfcomm_cid); 418 419 /** 420 * @brief Query max frame size 421 * @param rfcomm_cid 422 * @return max frame size 423 */ 424 uint16_t rfcomm_get_max_frame_size(uint16_t rfcomm_cid); 425 426 /** 427 * @brief Reserve packet buffer to allow to create RFCOMM packet in place 428 * @return true on success 429 * 430 * if (rfcomm_can_send_packet_now(cid)){ 431 * rfcomm_reserve_packet_buffer(); 432 * uint8_t * buffer = rfcomm_get_outgoing_buffer(); 433 * uint16_t buffer_size = rfcomm_get_max_frame_size(cid); 434 * .. setup data in buffer with len .. 435 * rfcomm_send_prepared(cid, len) 436 * } 437 */ 438 bool rfcomm_reserve_packet_buffer(void); 439 440 /** 441 * @brief Get outgoing buffer 442 * @return pointer to outgoing rfcomm buffer 443 */ 444 uint8_t * rfcomm_get_outgoing_buffer(void); 445 446 /** 447 * @brief Send packet prepared in outgoing buffer 448 * @note This releases the outgoing rfcomm buffer 449 * @param rfcomm_cid 450 * @param len 451 * @return status 452 */ 453 uint8_t rfcomm_send_prepared(uint16_t rfcomm_cid, uint16_t len); 454 455 /** 456 * @brief Release outgoing buffer in case rfcomm_send_prepared was not called 457 */ 458 void rfcomm_release_packet_buffer(void); 459 460 /** 461 * @brief Enable L2CAP ERTM mode for RFCOMM. request callback is used to provide ERTM buffer. released callback returns buffer 462 * 463 * @note on request_callback, the app must set the ertm_config, buffer, size fields to enable ERTM for the current connection 464 * @note if buffer is not set, BASIC mode will be used instead 465 * 466 * @note released_callback provides ertm_id from earlier request to match request and release 467 * 468 * @param request_callback 469 * @param released_callback 470 */ 471 void rfcomm_enable_l2cap_ertm(void request_callback(rfcomm_ertm_request_t * request), void released_callback(uint16_t ertm_id)); 472 473 474 // Event getters for RFCOMM_EVENT_PORT_CONFIGURATION 475 476 /** 477 * @brief Get field rfcomm_cid from event RFCOMM_EVENT_PORT_CONFIGURATION 478 * @param event packet 479 * @return rfcomm_cid 480 */ 481 static inline uint16_t rfcomm_event_port_configuration_get_rfcomm_cid(const uint8_t * event){ 482 return little_endian_read_16(event, 2); 483 } 484 485 /** 486 * @brief Get field local from event RFCOMM_EVENT_PORT_CONFIGURATION 487 * @param event packet 488 * @return remote - false for local port, true for remote port 489 */ 490 static inline bool rfcomm_event_port_configuration_get_remote(const uint8_t * event){ 491 return event[4] != 0; 492 } 493 494 /** 495 * @brief Get field baud_rate from event RFCOMM_EVENT_PORT_CONFIGURATION 496 * @param event packet 497 * @return baud_rate 498 */ 499 500 static inline rpn_baud_t rfcomm_event_port_configuration_get_baud_rate(const uint8_t * event){ 501 return (rpn_baud_t) event[5]; 502 } 503 504 /** 505 * @brief Get field data_bits from event RFCOMM_EVENT_PORT_CONFIGURATION 506 * @param event packet 507 * @return data_bits 508 */ 509 510 static inline rpn_data_bits_t rfcomm_event_port_configuration_get_data_bits(const uint8_t * event){ 511 return (rpn_data_bits_t) (event[6] & 3); 512 } 513 /** 514 * @brief Get field stop_bits from event RFCOMM_EVENT_PORT_CONFIGURATION 515 * @param event packet 516 * @return stop_bits 517 */ 518 static inline rpn_stop_bits_t rfcomm_event_port_configuration_get_stop_bits(const uint8_t * event){ 519 return (rpn_stop_bits_t) ((event[6] >> 2) & 1); 520 } 521 522 /** 523 * @brief Get field parity from event RFCOMM_EVENT_PORT_CONFIGURATION 524 * @param event packet 525 * @return parity 526 */ 527 static inline rpn_parity_t rfcomm_event_port_configuration_get_parity(const uint8_t * event){ 528 return (rpn_parity_t) ((event[6] >> 3) & 7); 529 } 530 531 /** 532 * @brief Get field flow_control from event RFCOMM_EVENT_PORT_CONFIGURATION 533 * @param event packet 534 * @return flow_control 535 */ 536 537 static inline uint8_t rfcomm_event_port_configuration_get_flow_control(const uint8_t * event){ 538 return event[7] & 0x3f; 539 } 540 541 /** 542 * @brief Get field xon from event RFCOMM_EVENT_PORT_CONFIGURATION 543 * @param event packet 544 * @return xon 545 */ 546 static inline uint8_t rfcomm_event_port_configuration_get_xon(const uint8_t * event){ 547 return event[8]; 548 } 549 550 /** 551 * @brief Get field xoff from event RFCOMM_EVENT_PORT_CONFIGURATION 552 * @param event packet 553 * @return xoff 554 */ 555 static inline uint8_t rfcomm_event_port_configuration_get_xoff(const uint8_t * event){ 556 return event[9]; 557 } 558 559 /** 560 * @brief De-Init RFCOMM 561 */ 562 void rfcomm_deinit(void); 563 564 /* API_END */ 565 566 #if defined __cplusplus 567 } 568 #endif 569 570 #endif // RFCOMM_H 571