xref: /btstack/src/l2cap.h (revision 046b44372d25c7bd6fe78e5cf6e5176a1979f437)
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 // PSM numbers from https://www.bluetooth.com/specifications/assigned-numbers/logical-link-control
423 //
424 #define PSM_SDP           BLUETOOTH_PROTOCOL_SDP
425 #define PSM_RFCOMM        BLUETOOTH_PROTOCOL_RFCOMM
426 #define PSM_BNEP          BLUETOOTH_PROTOCOL_BNEP
427 // @TODO: scrape PSMs Bluetooth SIG site and put in bluetooth_psm.h or bluetooth_l2cap.h
428 #define PSM_HID_CONTROL   0x11
429 #define PSM_HID_INTERRUPT 0x13
430 #define PSM_ATT           0x1f
431 #define PSM_IPSP          0x23
432 
433 /**
434  * @brief Set up L2CAP and register L2CAP with HCI layer.
435  */
436 void l2cap_init(void);
437 
438 /**
439  * @brief Registers packet handler for LE Connection Parameter Update events
440  */
441 void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size));
442 
443 /**
444  * @brief Get max MTU for Classic connections based on btstack configuration
445  */
446 uint16_t l2cap_max_mtu(void);
447 
448 /**
449  * @brief Get max MTU for LE connections based on btstack configuration
450  */
451 uint16_t l2cap_max_le_mtu(void);
452 
453 /**
454 * @brief Set the max MTU for LE connections, if not set l2cap_max_mtu() will be used.
455 */
456 void l2cap_set_max_le_mtu(uint16_t max_mtu);
457 
458 /**
459  * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
460  * @param packet_handler
461  * @param address
462  * @param psm
463  * @param mtu
464  * @param local_cid
465  * @return status
466  */
467 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);
468 
469 /**
470  * @brief Creates L2CAP channel to the PSM of a remote device with baseband address using Enhanced Retransmission Mode.
471  *        A new baseband connection will be initiated if necessary.
472  * @param packet_handler
473  * @param address
474  * @param psm
475  * @param ertm_config
476  * @param buffer to store reassembled rx packet, out-of-order packets and unacknowledged outgoing packets with their tretransmission timers
477  * @param size of buffer
478  * @param local_cid
479  * @return status
480  */
481 uint8_t l2cap_create_ertm_channel(btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm,
482     l2cap_ertm_config_t * ertm_contig, uint8_t * buffer, uint32_t size, uint16_t * out_local_cid);
483 
484 /**
485  * @brief Disconnects L2CAP channel with given identifier.
486  */
487 void l2cap_disconnect(uint16_t local_cid, uint8_t reason);
488 
489 /**
490  * @brief Queries the maximal transfer unit (MTU) for L2CAP channel with given identifier.
491  */
492 uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid);
493 
494 /**
495  * @brief Sends L2CAP data packet to the channel with given identifier.
496  */
497 int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len);
498 
499 /**
500  * @brief Registers L2CAP service with given PSM and MTU, and assigns a packet handler.
501  */
502 uint8_t l2cap_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level);
503 
504 /**
505  * @brief Unregisters L2CAP service with given PSM.
506  */
507 uint8_t l2cap_unregister_service(uint16_t psm);
508 
509 /**
510  * @brief Accepts incoming L2CAP connection.
511  */
512 void l2cap_accept_connection(uint16_t local_cid);
513 
514 /**
515  * @brief Accepts incoming L2CAP connection for Enhanced Retransmission Mode
516  * @param local_cid
517  * @param ertm_config
518  * @param buffer to store reassembled rx packet, out-of-order packets and unacknowledged outgoing packets with their tretransmission timers
519  * @param size of buffer
520  * @return status
521  */
522 uint8_t l2cap_accept_ertm_connection(uint16_t local_cid, l2cap_ertm_config_t * ertm_contig, uint8_t * buffer, uint32_t size);
523 
524 /**
525  * @brief Deny incoming L2CAP connection.
526  */
527 void l2cap_decline_connection(uint16_t local_cid);
528 
529 /**
530  * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module
531  */
532 int  l2cap_can_send_packet_now(uint16_t local_cid);
533 
534 /**
535  * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible
536  * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function
537  *       so packet handler should be ready to handle it
538  * @param local_cid
539  */
540 void l2cap_request_can_send_now_event(uint16_t local_cid);
541 
542 /**
543  * @brief Reserve outgoing buffer
544  * @note Only for L2CAP Basic Mode Channels
545  */
546 int  l2cap_reserve_packet_buffer(void);
547 
548 /**
549  * @brief Get outgoing buffer and prepare data.
550  * @note Only for L2CAP Basic Mode Channels
551  */
552 uint8_t *l2cap_get_outgoing_buffer(void);
553 
554 /**
555  * @brief Send L2CAP packet prepared in outgoing buffer to channel
556  * @note Only for L2CAP Basic Mode Channels
557  */
558 int l2cap_send_prepared(uint16_t local_cid, uint16_t len);
559 
560 /**
561  * @brief Release outgoing buffer (only needed if l2cap_send_prepared is not called)
562  * @note Only for L2CAP Basic Mode Channels
563  */
564 void l2cap_release_packet_buffer(void);
565 
566 
567 //
568 // LE Connection Oriented Channels feature with the LE Credit Based Flow Control Mode == LE Data Channel
569 //
570 
571 
572 /**
573  * @brief Register L2CAP LE Data Channel service
574  * @note MTU and initial credits are specified in l2cap_le_accept_connection(..) call
575  * @param packet_handler
576  * @param psm
577  * @param security_level
578  */
579 uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level);
580 
581 /**
582  * @brief Unregister L2CAP LE Data Channel service
583  * @param psm
584  */
585 
586 uint8_t l2cap_le_unregister_service(uint16_t psm);
587 
588 /*
589  * @brief Accept incoming LE Data Channel connection
590  * @param local_cid             L2CAP LE Data Channel Identifier
591  * @param receive_buffer        buffer used for reassembly of L2CAP LE Information Frames into service data unit (SDU) with given MTU
592  * @param receive_buffer_size   buffer size equals MTU
593  * @param initial_credits       Number of initial credits provided to peer or L2CAP_LE_AUTOMATIC_CREDITS to enable automatic credits
594  */
595 
596 uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits);
597 
598 /**
599  * @brief Deny incoming LE Data Channel connection due to resource constraints
600  * @param local_cid             L2CAP LE Data Channel Identifier
601  */
602 
603 uint8_t l2cap_le_decline_connection(uint16_t local_cid);
604 
605 /**
606  * @brief Create LE Data Channel
607  * @param packet_handler        Packet handler for this connection
608  * @param con_handle            ACL-LE HCI Connction Handle
609  * @param psm                   Service PSM to connect to
610  * @param receive_buffer        buffer used for reassembly of L2CAP LE Information Frames into service data unit (SDU) with given MTU
611  * @param receive_buffer_size   buffer size equals MTU
612  * @param initial_credits       Number of initial credits provided to peer or L2CAP_LE_AUTOMATIC_CREDITS to enable automatic credits
613  * @param security_level        Minimum required security level
614  * @param out_local_cid         L2CAP LE Channel Identifier is stored here
615  */
616 uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle,
617     uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level,
618     uint16_t * out_local_cid);
619 
620 /**
621  * @brief Provide credtis for LE Data Channel
622  * @param local_cid             L2CAP LE Data Channel Identifier
623  * @param credits               Number additional credits for peer
624  */
625 uint8_t l2cap_le_provide_credits(uint16_t cid, uint16_t credits);
626 
627 /**
628  * @brief Check if packet can be scheduled for transmission
629  * @param local_cid             L2CAP LE Data Channel Identifier
630  */
631 int l2cap_le_can_send_now(uint16_t cid);
632 
633 /**
634  * @brief Request emission of L2CAP_EVENT_LE_CAN_SEND_NOW as soon as possible
635  * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function
636  *       so packet handler should be ready to handle it
637  * @param local_cid             L2CAP LE Data Channel Identifier
638  */
639 uint8_t l2cap_le_request_can_send_now_event(uint16_t cid);
640 
641 /**
642  * @brief Send data via LE Data Channel
643  * @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event
644  * @param local_cid             L2CAP LE Data Channel Identifier
645  * @param data                  data to send
646  * @param size                  data size
647  */
648 uint8_t l2cap_le_send_data(uint16_t cid, uint8_t * data, uint16_t size);
649 
650 /**
651  * @brief Disconnect from LE Data Channel
652  * @param local_cid             L2CAP LE Data Channel Identifier
653  */
654 uint8_t l2cap_le_disconnect(uint16_t cid);
655 
656 /* API_END */
657 
658 /**
659  * @brief ERTM Set channel as busy.
660  * @note Can be cleared by l2cap_ertm_set_ready
661  * @param local_cid
662  */
663 uint8_t l2cap_ertm_set_busy(uint16_t local_cid);
664 
665 /**
666  * @brief ERTM Set channel as ready
667  * @note Used after l2cap_ertm_set_busy
668  * @param local_cid
669  */
670 uint8_t l2cap_ertm_set_ready(uint16_t local_cid);
671 
672 #if defined __cplusplus
673 }
674 #endif
675 
676 #endif // L2CAP_H
677