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 * @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 Registers packet handler for LE Connection Parameter Update events 452 */ 453 void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)); 454 455 /** 456 * @brief Get max MTU for Classic connections based on btstack configuration 457 */ 458 uint16_t l2cap_max_mtu(void); 459 460 /** 461 * @brief Get max MTU for LE connections based on btstack configuration 462 */ 463 uint16_t l2cap_max_le_mtu(void); 464 465 /** 466 * @brief Set the max MTU for LE connections, if not set l2cap_max_mtu() will be used. 467 */ 468 void l2cap_set_max_le_mtu(uint16_t max_mtu); 469 470 /** 471 * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary. 472 * @param packet_handler 473 * @param address 474 * @param psm 475 * @param mtu 476 * @param local_cid 477 * @return status 478 */ 479 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); 480 481 /** 482 * @brief Creates L2CAP channel to the PSM of a remote device with baseband address using Enhanced Retransmission Mode. 483 * A new baseband connection will be initiated if necessary. 484 * @param packet_handler 485 * @param address 486 * @param psm 487 * @param ertm_config 488 * @param buffer to store reassembled rx packet, out-of-order packets and unacknowledged outgoing packets with their tretransmission timers 489 * @param size of buffer 490 * @param local_cid 491 * @return status 492 */ 493 uint8_t l2cap_create_ertm_channel(btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm, 494 l2cap_ertm_config_t * ertm_contig, uint8_t * buffer, uint32_t size, uint16_t * out_local_cid); 495 496 /** 497 * @brief Disconnects L2CAP channel with given identifier. 498 */ 499 void l2cap_disconnect(uint16_t local_cid, uint8_t reason); 500 501 /** 502 * @brief Queries the maximal transfer unit (MTU) for L2CAP channel with given identifier. 503 */ 504 uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid); 505 506 /** 507 * @brief Sends L2CAP data packet to the channel with given identifier. 508 */ 509 uint8_t l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len); 510 511 /** 512 * @brief Registers L2CAP service with given PSM and MTU, and assigns a packet handler. 513 * @param packet_handler 514 * @param psm 515 * @param mtu 516 * @param security_level 517 * @return status ERROR_CODE_SUCCESS if successful, otherwise L2CAP_SERVICE_ALREADY_REGISTERED or BTSTACK_MEMORY_ALLOC_FAILED 518 */ 519 uint8_t l2cap_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level); 520 521 /** 522 * @brief Unregisters L2CAP service with given PSM. 523 */ 524 uint8_t l2cap_unregister_service(uint16_t psm); 525 526 /** 527 * @brief Accepts incoming L2CAP connection. 528 */ 529 void l2cap_accept_connection(uint16_t local_cid); 530 531 /** 532 * @brief Accepts incoming L2CAP connection for Enhanced Retransmission Mode 533 * @param local_cid 534 * @param ertm_config 535 * @param buffer to store reassembled rx packet, out-of-order packets and unacknowledged outgoing packets with their tretransmission timers 536 * @param size of buffer 537 * @return status 538 */ 539 uint8_t l2cap_accept_ertm_connection(uint16_t local_cid, l2cap_ertm_config_t * ertm_contig, uint8_t * buffer, uint32_t size); 540 541 /** 542 * @brief Deny incoming L2CAP connection. 543 */ 544 void l2cap_decline_connection(uint16_t local_cid); 545 546 /** 547 * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module 548 * @return true if packet can be sent 549 */ 550 bool l2cap_can_send_packet_now(uint16_t local_cid); 551 552 /** 553 * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible 554 * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function 555 * so packet handler should be ready to handle it 556 * @param local_cid 557 */ 558 void l2cap_request_can_send_now_event(uint16_t local_cid); 559 560 /** 561 * @brief Reserve outgoing buffer 562 * @note Only for L2CAP Basic Mode Channels 563 * @return true on success 564 */ 565 bool l2cap_reserve_packet_buffer(void); 566 567 /** 568 * @brief Get outgoing buffer and prepare data. 569 * @note Only for L2CAP Basic Mode Channels 570 */ 571 uint8_t *l2cap_get_outgoing_buffer(void); 572 573 /** 574 * @brief Send L2CAP packet prepared in outgoing buffer to channel 575 * @note Only for L2CAP Basic Mode Channels 576 */ 577 uint8_t l2cap_send_prepared(uint16_t local_cid, uint16_t len); 578 579 /** 580 * @brief Release outgoing buffer (only needed if l2cap_send_prepared is not called) 581 * @note Only for L2CAP Basic Mode Channels 582 */ 583 void l2cap_release_packet_buffer(void); 584 585 586 // 587 // LE Connection Oriented Channels feature with the LE Credit Based Flow Control Mode == LE Data Channel 588 // 589 590 591 /** 592 * @brief Register L2CAP LE Data Channel service 593 * @note MTU and initial credits are specified in l2cap_le_accept_connection(..) call 594 * @param packet_handler 595 * @param psm 596 * @param security_level 597 */ 598 uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level); 599 600 /** 601 * @brief Unregister L2CAP LE Data Channel service 602 * @param psm 603 */ 604 605 uint8_t l2cap_le_unregister_service(uint16_t psm); 606 607 /* 608 * @brief Accept incoming LE Data Channel connection 609 * @param local_cid L2CAP LE Data Channel Identifier 610 * @param receive_buffer buffer used for reassembly of L2CAP LE Information Frames into service data unit (SDU) with given MTU 611 * @param receive_buffer_size buffer size equals MTU 612 * @param initial_credits Number of initial credits provided to peer or L2CAP_LE_AUTOMATIC_CREDITS to enable automatic credits 613 */ 614 615 uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits); 616 617 /** 618 * @brief Deny incoming LE Data Channel connection due to resource constraints 619 * @param local_cid L2CAP LE Data Channel Identifier 620 */ 621 622 uint8_t l2cap_le_decline_connection(uint16_t local_cid); 623 624 /** 625 * @brief Create LE Data Channel 626 * @param packet_handler Packet handler for this connection 627 * @param con_handle ACL-LE HCI Connction Handle 628 * @param psm Service PSM to connect to 629 * @param receive_buffer buffer used for reassembly of L2CAP LE Information Frames into service data unit (SDU) with given MTU 630 * @param receive_buffer_size buffer size equals MTU 631 * @param initial_credits Number of initial credits provided to peer or L2CAP_LE_AUTOMATIC_CREDITS to enable automatic credits 632 * @param security_level Minimum required security level 633 * @param out_local_cid L2CAP LE Channel Identifier is stored here 634 */ 635 uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle, 636 uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level, 637 uint16_t * out_local_cid); 638 639 /** 640 * @brief Provide credtis for LE Data Channel 641 * @param local_cid L2CAP LE Data Channel Identifier 642 * @param credits Number additional credits for peer 643 */ 644 uint8_t l2cap_le_provide_credits(uint16_t local_cid, uint16_t credits); 645 646 /** 647 * @brief Check if packet can be scheduled for transmission 648 * @param local_cid L2CAP LE Data Channel Identifier 649 * @return true if packet can be sent on local_cid 650 */ 651 bool l2cap_le_can_send_now(uint16_t local_cid); 652 653 /** 654 * @brief Request emission of L2CAP_EVENT_LE_CAN_SEND_NOW as soon as possible 655 * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function 656 * so packet handler should be ready to handle it 657 * @param local_cid L2CAP LE Data Channel Identifier 658 */ 659 uint8_t l2cap_le_request_can_send_now_event(uint16_t local_cid); 660 661 /** 662 * @brief Send data via LE Data Channel 663 * @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event 664 * @param local_cid L2CAP LE Data Channel Identifier 665 * @param data data to send 666 * @param size data size 667 */ 668 uint8_t l2cap_le_send_data(uint16_t local_cid, uint8_t * data, uint16_t size); 669 670 /** 671 * @brief Disconnect from LE Data Channel 672 * @param local_cid L2CAP LE Data Channel Identifier 673 */ 674 uint8_t l2cap_le_disconnect(uint16_t local_cid); 675 676 /** 677 * @brief ERTM Set channel as busy. 678 * @note Can be cleared by l2cap_ertm_set_ready 679 * @param local_cid 680 */ 681 uint8_t l2cap_ertm_set_busy(uint16_t local_cid); 682 683 /** 684 * @brief ERTM Set channel as ready 685 * @note Used after l2cap_ertm_set_busy 686 * @param local_cid 687 */ 688 uint8_t l2cap_ertm_set_ready(uint16_t local_cid); 689 690 /** 691 * @brief De-Init L2CAP 692 */ 693 void l2cap_deinit(void); 694 695 /* API_END */ 696 697 #if defined __cplusplus 698 } 699 #endif 700 701 #endif // L2CAP_H 702