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