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