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