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