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 L2CAP 40 * 41 * Logical Link Control and Adaption Protocol 42 * 43 */ 44 45 #ifndef L2CAP_H 46 #define L2CAP_H 47 48 #include "hci.h" 49 #include "l2cap_signaling.h" 50 #include "btstack_util.h" 51 #include "bluetooth.h" 52 53 #if defined __cplusplus 54 extern "C" { 55 #endif 56 57 // check L2CAP MTU 58 #ifdef ENABLE_CLASSIC 59 #if (L2CAP_HEADER_SIZE + L2CAP_MINIMAL_MTU) > HCI_ACL_PAYLOAD_SIZE 60 #error "HCI_ACL_PAYLOAD_SIZE too small for minimal L2CAP MTU of 48 bytes" 61 #endif 62 #endif 63 #ifdef ENABLE_BLE 64 #if (L2CAP_HEADER_SIZE + L2CAP_LE_DEFAULT_MTU) > HCI_ACL_PAYLOAD_SIZE 65 #error "HCI_ACL_PAYLOAD_SIZE too small for minimal L2CAP LE MTU of 23 bytes" 66 #endif 67 #endif 68 69 #define L2CAP_LE_AUTOMATIC_CREDITS 0xffff 70 71 // private structs 72 typedef enum { 73 L2CAP_STATE_CLOSED = 1, // no baseband 74 L2CAP_STATE_WILL_SEND_CREATE_CONNECTION, 75 L2CAP_STATE_WAIT_CONNECTION_COMPLETE, 76 L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES, 77 L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE, 78 L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE, 79 L2CAP_STATE_WAIT_INCOMING_EXTENDED_FEATURES, // only for Enhanced Retransmission Mode 80 L2CAP_STATE_WAIT_OUTGOING_EXTENDED_FEATURES, // only for Enhanced Retransmission Mode 81 L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT, 82 L2CAP_STATE_WAIT_CONNECT_RSP, // from peer 83 L2CAP_STATE_CONFIG, 84 L2CAP_STATE_OPEN, 85 L2CAP_STATE_WAIT_DISCONNECT, // from application 86 L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST, 87 L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_INSUFFICIENT_SECURITY, 88 L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE, 89 L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT, 90 L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST, 91 L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE, 92 L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST, 93 L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE, 94 L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT, 95 L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE, 96 L2CAP_STATE_EMIT_OPEN_FAILED_AND_DISCARD, 97 L2CAP_STATE_INVALID, 98 } L2CAP_STATE; 99 100 #define L2CAP_CHANNEL_STATE_VAR_NONE 0 101 #define L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ 1 << 0 102 #define L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP 1 << 1 103 #define L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ 1 << 2 104 #define L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP 1 << 3 105 #define L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ 1 << 4 106 #define L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP 1 << 5 107 #define L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU 1 << 6 // in CONF RSP, add MTU option 108 #define L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_ERTM 1 << 7 // in CONF RSP, add Retransmission and Flowcontrol option 109 #define L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT 1 << 8 // in CONF RSP, set CONTINUE flag 110 #define L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID 1 << 9 // in CONF RSP, send UNKNOWN OPTIONS 111 #define L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_REJECTED 1 << 10 // in CONF RSP, send Unacceptable Parameters (ERTM) 112 #define L2CAP_CHANNEL_STATE_VAR_BASIC_FALLBACK_TRIED 1 << 11 // set when ERTM was requested but we want only Basic mode (ERM) 113 #define L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND 1 << 12 // send Connection Respond with pending 114 #define L2CAP_CHANNEL_STATE_VAR_SENT_CONN_RESP_PEND 1 << 13 // send CMD_REJ with reason unknown 115 #define L2CAP_CHANNEL_STATE_VAR_INCOMING 1 << 15 // channel is incoming 116 117 118 typedef enum { 119 L2CAP_CHANNEL_TYPE_CLASSIC, // Classic Basic or ERTM 120 L2CAP_CHANNEL_TYPE_CONNECTIONLESS, // Classic Connectionless 121 L2CAP_CHANNEL_TYPE_LE_DATA_CHANNEL, // LE 122 L2CAP_CHANNEL_TYPE_FIXED, // LE ATT + SM, Classic SM 123 } l2cap_channel_type_t; 124 125 126 /* 127 * @brief L2CAP Segmentation And Reassembly packet type in I-Frames 128 */ 129 typedef enum { 130 L2CAP_SEGMENTATION_AND_REASSEMBLY_UNSEGMENTED_L2CAP_SDU = 0, 131 L2CAP_SEGMENTATION_AND_REASSEMBLY_START_OF_L2CAP_SDU, 132 L2CAP_SEGMENTATION_AND_REASSEMBLY_END_OF_L2CAP_SDU, 133 L2CAP_SEGMENTATION_AND_REASSEMBLY_CONTINUATION_OF_L2CAP_SDU 134 } l2cap_segmentation_and_reassembly_t; 135 136 typedef struct { 137 l2cap_segmentation_and_reassembly_t sar; 138 uint16_t len; 139 uint8_t valid; 140 } l2cap_ertm_rx_packet_state_t; 141 142 typedef struct { 143 l2cap_segmentation_and_reassembly_t sar; 144 uint16_t len; 145 uint8_t tx_seq; 146 uint8_t retry_count; 147 uint8_t retransmission_requested; 148 } l2cap_ertm_tx_packet_state_t; 149 150 typedef struct { 151 // If not mandatory, the use of ERTM can be decided by the remote 152 uint8_t ertm_mandatory; 153 154 // Number of retransmissions that L2CAP is allowed to try before accepting that a packet and the channel is lost. 155 uint8_t max_transmit; 156 157 // time before retransmission of i-frame / Recommended : 2000 ms (ACL Flush timeout not used) 158 uint16_t retransmission_timeout_ms; 159 160 // time after withc s-frames are sent / Recommended: 12000 ms (ACL Flush timeout not used) 161 uint16_t monitor_timeout_ms; 162 163 // MTU for incoming SDUs 164 uint16_t local_mtu; 165 166 // Number of buffers for outgoing data 167 uint8_t num_tx_buffers; 168 169 // Number of packets that can be received out of order (-> our tx_window size) 170 uint8_t num_rx_buffers; 171 172 // Frame Check Sequence (FCS) Option 173 uint8_t fcs_option; 174 175 } l2cap_ertm_config_t; 176 177 // info regarding an actual channel 178 // note: l2cap_fixed_channel and l2cap_channel_t share commmon fields 179 180 typedef struct l2cap_fixed_channel { 181 // linked list - assert: first field 182 btstack_linked_item_t item; 183 184 // channel type 185 l2cap_channel_type_t channel_type; 186 187 // local cid, primary key for channel lookup 188 uint16_t local_cid; 189 190 // packet handler 191 btstack_packet_handler_t packet_handler; 192 193 // send request 194 uint8_t waiting_for_can_send_now; 195 196 // -- end of shared prefix 197 198 } l2cap_fixed_channel_t; 199 200 typedef struct { 201 // linked list - assert: first field 202 btstack_linked_item_t item; 203 204 // channel type 205 l2cap_channel_type_t channel_type; 206 207 // local cid, primary key for channel lookup 208 uint16_t local_cid; 209 210 // packet handler 211 btstack_packet_handler_t packet_handler; 212 213 // send request 214 uint8_t waiting_for_can_send_now; 215 216 // -- end of shared prefix 217 218 // timer 219 btstack_timer_source_t rtx; // also used for ertx 220 221 L2CAP_STATE state; 222 uint16_t state_var; 223 224 // info 225 hci_con_handle_t con_handle; 226 227 bd_addr_t address; 228 bd_addr_type_t address_type; 229 230 uint8_t remote_sig_id; // used by other side, needed for delayed response 231 uint8_t local_sig_id; // own signaling identifier 232 233 uint16_t remote_cid; 234 235 uint16_t local_mtu; 236 uint16_t remote_mtu; 237 238 uint16_t flush_timeout; // default 0xffff 239 240 uint16_t psm; 241 242 gap_security_level_t required_security_level; 243 244 uint8_t reason; // used in decline internal 245 246 uint8_t unknown_option; // used for ConfigResponse 247 248 // LE Data Channels 249 250 // incoming SDU 251 uint8_t * receive_sdu_buffer; 252 uint16_t receive_sdu_len; 253 uint16_t receive_sdu_pos; 254 255 // outgoing SDU 256 uint8_t * send_sdu_buffer; 257 uint16_t send_sdu_len; 258 uint16_t send_sdu_pos; 259 260 // max PDU size 261 uint16_t remote_mps; 262 263 // credits for outgoing traffic 264 uint16_t credits_outgoing; 265 266 // number of packets remote will be granted 267 uint16_t new_credits_incoming; 268 269 // credits for incoming traffic 270 uint16_t credits_incoming; 271 272 // automatic credits incoming 273 uint16_t automatic_credits; 274 275 #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 276 277 // l2cap channel mode: basic or enhanced retransmission mode 278 l2cap_channel_mode_t mode; 279 280 // local mps = size of rx/tx buffers 281 uint16_t local_mps; 282 283 // retransmission timer 284 btstack_timer_source_t retransmission_timer; 285 286 // monitor timer 287 btstack_timer_source_t monitor_timer; 288 289 // local/remote config options 290 uint16_t local_retransmission_timeout_ms; 291 uint16_t local_monitor_timeout_ms; 292 293 uint16_t remote_retransmission_timeout_ms; 294 uint16_t remote_monitor_timeout_ms; 295 296 uint8_t remote_tx_window_size; 297 298 uint8_t local_max_transmit; 299 uint8_t remote_max_transmit; 300 301 // if ertm is not mandatory, allow fallback to L2CAP Basic Mode - flag 302 uint8_t ertm_mandatory; 303 304 // Frame Chech Sequence (crc16) is present in both directions 305 uint8_t fcs_option; 306 307 // sender: max num of stored outgoing frames 308 uint8_t num_tx_buffers; 309 310 // sender: num stored outgoing frames 311 uint8_t num_stored_tx_frames; 312 313 // sender: number of unacknowledeged I-Frames - frames have been sent, but not acknowledged yet 314 uint8_t unacked_frames; 315 316 // sender: buffer index of oldest packet 317 uint8_t tx_read_index; 318 319 // sender: buffer index to store next tx packet 320 uint8_t tx_write_index; 321 322 // sender: buffer index of packet to send next 323 uint8_t tx_send_index; 324 325 // sender: next seq nr used for sending 326 uint8_t next_tx_seq; 327 328 // sender: selective retransmission requested 329 uint8_t srej_active; 330 331 332 // receiver: max num out-of-order packets // tx_window 333 uint8_t num_rx_buffers; 334 335 // receiver: buffer index of to store packet with delta = 1 336 uint8_t rx_store_index; 337 338 // receiver: value of tx_seq in next expected i-frame 339 uint8_t expected_tx_seq; 340 341 // receiver: request transmission with tx_seq = req_seq and ack up to and including req_seq 342 uint8_t req_seq; 343 344 // receiver: local busy condition 345 uint8_t local_busy; 346 347 // receiver: send RR frame with optional final flag set - flag 348 uint8_t send_supervisor_frame_receiver_ready; 349 350 // receiver: send RR frame with poll bit set 351 uint8_t send_supervisor_frame_receiver_ready_poll; 352 353 // receiver: send RNR frame - flag 354 uint8_t send_supervisor_frame_receiver_not_ready; 355 356 // receiver: send REJ frame - flag 357 uint8_t send_supervisor_frame_reject; 358 359 // receiver: send SREJ frame - flag 360 uint8_t send_supervisor_frame_selective_reject; 361 362 // set final bit after poll packet with poll bit was received 363 uint8_t set_final_bit_after_packet_with_poll_bit_set; 364 365 // receiver: meta data for out-of-order frames 366 l2cap_ertm_rx_packet_state_t * rx_packets_state; 367 368 // sender: retransmission state 369 l2cap_ertm_tx_packet_state_t * tx_packets_state; 370 371 // receiver: reassemly pos 372 uint16_t reassembly_pos; 373 374 // receiver: reassemly sdu length 375 uint16_t reassembly_sdu_length; 376 377 // receiver: eassembly buffer 378 uint8_t * reassembly_buffer; 379 380 // receiver: num_rx_buffers of size local_mps 381 uint8_t * rx_packets_data; 382 383 // sender: num_tx_buffers of size local_mps 384 uint8_t * tx_packets_data; 385 386 #endif 387 } l2cap_channel_t; 388 389 // info regarding potential connections 390 typedef struct { 391 // linked list - assert: first field 392 btstack_linked_item_t item; 393 394 // service id 395 uint16_t psm; 396 397 // incoming MTU 398 uint16_t mtu; 399 400 // internal connection 401 btstack_packet_handler_t packet_handler; 402 403 // required security level 404 gap_security_level_t required_security_level; 405 406 } l2cap_service_t; 407 408 409 typedef struct l2cap_signaling_response { 410 hci_con_handle_t handle; 411 uint8_t sig_id; 412 uint8_t code; 413 uint16_t cid; // source cid for CONNECTION REQUEST 414 uint16_t data; // infoType for INFORMATION REQUEST, result for CONNECTION REQUEST and COMMAND UNKNOWN 415 } l2cap_signaling_response_t; 416 417 418 void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id); 419 bool l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id); 420 void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t channel_id); 421 uint8_t l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len); 422 uint8_t l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len); 423 424 // PTS Testing 425 int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len); 426 void l2cap_require_security_level_2_for_outgoing_sdp(void); 427 428 // Used by RFCOMM - similar to l2cap_can-send_packet_now but does not check if outgoing buffer is reserved 429 bool l2cap_can_send_prepared_packet_now(uint16_t local_cid); 430 431 /* API_START */ 432 433 // 434 // PSM numbers from https://www.bluetooth.com/specifications/assigned-numbers/logical-link-control 435 // 436 #define PSM_SDP BLUETOOTH_PROTOCOL_SDP 437 #define PSM_RFCOMM BLUETOOTH_PROTOCOL_RFCOMM 438 #define PSM_BNEP BLUETOOTH_PROTOCOL_BNEP 439 // @TODO: scrape PSMs Bluetooth SIG site and put in bluetooth_psm.h or bluetooth_l2cap.h 440 #define PSM_HID_CONTROL 0x11 441 #define PSM_HID_INTERRUPT 0x13 442 #define PSM_ATT 0x1f 443 #define PSM_IPSP 0x23 444 445 /** 446 * @brief Set up L2CAP and register L2CAP with HCI layer. 447 */ 448 void l2cap_init(void); 449 450 /** 451 * @brief Add event packet handler for LE Connection Parameter Update events 452 */ 453 void l2cap_add_event_handler(btstack_packet_callback_registration_t * callback_handler); 454 455 /** 456 * @brief Remove event packet handler. 457 */ 458 void l2cap_remove_event_handler(btstack_packet_callback_registration_t * callback_handler); 459 460 /** 461 * @brief Get max MTU for Classic connections based on btstack configuration 462 */ 463 uint16_t l2cap_max_mtu(void); 464 465 /** 466 * @brief Get max MTU for LE connections based on btstack configuration 467 */ 468 uint16_t l2cap_max_le_mtu(void); 469 470 /** 471 * @brief Set the max MTU for LE connections, if not set l2cap_max_mtu() will be used. 472 */ 473 void l2cap_set_max_le_mtu(uint16_t max_mtu); 474 475 /** 476 * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary. 477 * @param packet_handler 478 * @param address 479 * @param psm 480 * @param mtu 481 * @param local_cid 482 * @return status 483 */ 484 uint8_t l2cap_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm, uint16_t mtu, uint16_t * out_local_cid); 485 486 /** 487 * @brief Creates L2CAP channel to the PSM of a remote device with baseband address using Enhanced Retransmission Mode. 488 * A new baseband connection will be initiated if necessary. 489 * @param packet_handler 490 * @param address 491 * @param psm 492 * @param ertm_config 493 * @param buffer to store reassembled rx packet, out-of-order packets and unacknowledged outgoing packets with their tretransmission timers 494 * @param size of buffer 495 * @param local_cid 496 * @return status 497 */ 498 uint8_t l2cap_create_ertm_channel(btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm, 499 l2cap_ertm_config_t * ertm_contig, uint8_t * buffer, uint32_t size, uint16_t * out_local_cid); 500 501 /** 502 * @brief Disconnects L2CAP channel with given identifier. 503 */ 504 void l2cap_disconnect(uint16_t local_cid, uint8_t reason); 505 506 /** 507 * @brief Queries the maximal transfer unit (MTU) for L2CAP channel with given identifier. 508 */ 509 uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid); 510 511 /** 512 * @brief Sends L2CAP data packet to the channel with given identifier. 513 */ 514 uint8_t l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len); 515 516 /** 517 * @brief Registers L2CAP service with given PSM and MTU, and assigns a packet handler. 518 * @param packet_handler 519 * @param psm 520 * @param mtu 521 * @param security_level 522 * @return status ERROR_CODE_SUCCESS if successful, otherwise L2CAP_SERVICE_ALREADY_REGISTERED or BTSTACK_MEMORY_ALLOC_FAILED 523 */ 524 uint8_t l2cap_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level); 525 526 /** 527 * @brief Unregisters L2CAP service with given PSM. 528 */ 529 uint8_t l2cap_unregister_service(uint16_t psm); 530 531 /** 532 * @brief Accepts incoming L2CAP connection. 533 */ 534 void l2cap_accept_connection(uint16_t local_cid); 535 536 /** 537 * @brief Accepts incoming L2CAP connection for Enhanced Retransmission Mode 538 * @param local_cid 539 * @param ertm_config 540 * @param buffer to store reassembled rx packet, out-of-order packets and unacknowledged outgoing packets with their tretransmission timers 541 * @param size of buffer 542 * @return status 543 */ 544 uint8_t l2cap_accept_ertm_connection(uint16_t local_cid, l2cap_ertm_config_t * ertm_contig, uint8_t * buffer, uint32_t size); 545 546 /** 547 * @brief Deny incoming L2CAP connection. 548 */ 549 void l2cap_decline_connection(uint16_t local_cid); 550 551 /** 552 * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module 553 * @return true if packet can be sent 554 */ 555 bool l2cap_can_send_packet_now(uint16_t local_cid); 556 557 /** 558 * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible 559 * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function 560 * so packet handler should be ready to handle it 561 * @param local_cid 562 */ 563 void l2cap_request_can_send_now_event(uint16_t local_cid); 564 565 /** 566 * @brief Reserve outgoing buffer 567 * @note Only for L2CAP Basic Mode Channels 568 * @return true on success 569 */ 570 bool l2cap_reserve_packet_buffer(void); 571 572 /** 573 * @brief Get outgoing buffer and prepare data. 574 * @note Only for L2CAP Basic Mode Channels 575 */ 576 uint8_t *l2cap_get_outgoing_buffer(void); 577 578 /** 579 * @brief Send L2CAP packet prepared in outgoing buffer to channel 580 * @note Only for L2CAP Basic Mode Channels 581 */ 582 uint8_t l2cap_send_prepared(uint16_t local_cid, uint16_t len); 583 584 /** 585 * @brief Release outgoing buffer (only needed if l2cap_send_prepared is not called) 586 * @note Only for L2CAP Basic Mode Channels 587 */ 588 void l2cap_release_packet_buffer(void); 589 590 591 // 592 // LE Connection Oriented Channels feature with the LE Credit Based Flow Control Mode == LE Data Channel 593 // 594 595 596 /** 597 * @brief Register L2CAP LE Data Channel service 598 * @note MTU and initial credits are specified in l2cap_le_accept_connection(..) call 599 * @param packet_handler 600 * @param psm 601 * @param security_level 602 */ 603 uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level); 604 605 /** 606 * @brief Unregister L2CAP LE Data Channel service 607 * @param psm 608 */ 609 610 uint8_t l2cap_le_unregister_service(uint16_t psm); 611 612 /* 613 * @brief Accept incoming LE Data Channel connection 614 * @param local_cid L2CAP LE Data Channel Identifier 615 * @param receive_buffer buffer used for reassembly of L2CAP LE Information Frames into service data unit (SDU) with given MTU 616 * @param receive_buffer_size buffer size equals MTU 617 * @param initial_credits Number of initial credits provided to peer or L2CAP_LE_AUTOMATIC_CREDITS to enable automatic credits 618 */ 619 620 uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits); 621 622 /** 623 * @brief Deny incoming LE Data Channel connection due to resource constraints 624 * @param local_cid L2CAP LE Data Channel Identifier 625 */ 626 627 uint8_t l2cap_le_decline_connection(uint16_t local_cid); 628 629 /** 630 * @brief Create LE Data Channel 631 * @param packet_handler Packet handler for this connection 632 * @param con_handle ACL-LE HCI Connction Handle 633 * @param psm Service PSM to connect to 634 * @param receive_buffer buffer used for reassembly of L2CAP LE Information Frames into service data unit (SDU) with given MTU 635 * @param receive_buffer_size buffer size equals MTU 636 * @param initial_credits Number of initial credits provided to peer or L2CAP_LE_AUTOMATIC_CREDITS to enable automatic credits 637 * @param security_level Minimum required security level 638 * @param out_local_cid L2CAP LE Channel Identifier is stored here 639 */ 640 uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle, 641 uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level, 642 uint16_t * out_local_cid); 643 644 /** 645 * @brief Provide credtis for LE Data Channel 646 * @param local_cid L2CAP LE Data Channel Identifier 647 * @param credits Number additional credits for peer 648 */ 649 uint8_t l2cap_le_provide_credits(uint16_t local_cid, uint16_t credits); 650 651 /** 652 * @brief Check if packet can be scheduled for transmission 653 * @param local_cid L2CAP LE Data Channel Identifier 654 * @return true if packet can be sent on local_cid 655 */ 656 bool l2cap_le_can_send_now(uint16_t local_cid); 657 658 /** 659 * @brief Request emission of L2CAP_EVENT_LE_CAN_SEND_NOW as soon as possible 660 * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function 661 * so packet handler should be ready to handle it 662 * @param local_cid L2CAP LE Data Channel Identifier 663 */ 664 uint8_t l2cap_le_request_can_send_now_event(uint16_t local_cid); 665 666 /** 667 * @brief Send data via LE Data Channel 668 * @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event 669 * @param local_cid L2CAP LE Data Channel Identifier 670 * @param data data to send 671 * @param size data size 672 */ 673 uint8_t l2cap_le_send_data(uint16_t local_cid, uint8_t * data, uint16_t size); 674 675 /** 676 * @brief Disconnect from LE Data Channel 677 * @param local_cid L2CAP LE Data Channel Identifier 678 */ 679 uint8_t l2cap_le_disconnect(uint16_t local_cid); 680 681 /** 682 * @brief ERTM Set channel as busy. 683 * @note Can be cleared by l2cap_ertm_set_ready 684 * @param local_cid 685 */ 686 uint8_t l2cap_ertm_set_busy(uint16_t local_cid); 687 688 /** 689 * @brief ERTM Set channel as ready 690 * @note Used after l2cap_ertm_set_busy 691 * @param local_cid 692 */ 693 uint8_t l2cap_ertm_set_ready(uint16_t local_cid); 694 695 /** 696 * @brief De-Init L2CAP 697 */ 698 void l2cap_deinit(void); 699 700 /* API_END */ 701 702 #if defined __cplusplus 703 } 704 #endif 705 706 #endif // L2CAP_H 707