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