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