Zephyr API Documentation 4.5.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
l2cap.h
Go to the documentation of this file.
1
4
5/*
6 * Copyright (c) 2015-2016 Intel Corporation
7 * Copyright (c) 2023 Nordic Semiconductor
8 *
9 * SPDX-License-Identifier: Apache-2.0
10 */
11#ifndef ZEPHYR_INCLUDE_BLUETOOTH_L2CAP_H_
12#define ZEPHYR_INCLUDE_BLUETOOTH_L2CAP_H_
13
22
23#include <stddef.h>
24#include <stdint.h>
25
29#include <zephyr/kernel.h>
30#include <zephyr/net_buf.h>
31#include <zephyr/sys/atomic.h>
32#include <zephyr/sys/slist.h>
33#include <zephyr/sys/util.h>
34#include <sys/types.h>
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
41#define BT_L2CAP_HDR_SIZE 4
42
44#define BT_L2CAP_TX_MTU (CONFIG_BT_L2CAP_TX_MTU)
45
47#define BT_L2CAP_RX_MTU (CONFIG_BT_BUF_ACL_RX_SIZE - BT_L2CAP_HDR_SIZE)
48
56#define BT_L2CAP_BUF_SIZE(mtu) BT_BUF_ACL_SIZE(BT_L2CAP_HDR_SIZE + (mtu))
57
59#define BT_L2CAP_SDU_HDR_SIZE 2
60
69#define BT_L2CAP_SDU_TX_MTU (BT_L2CAP_TX_MTU - BT_L2CAP_SDU_HDR_SIZE)
70
81#define BT_L2CAP_SDU_RX_MTU (BT_L2CAP_RX_MTU - BT_L2CAP_SDU_HDR_SIZE)
82
92#define BT_L2CAP_SDU_BUF_SIZE(mtu) BT_L2CAP_BUF_SIZE(BT_L2CAP_SDU_HDR_SIZE + (mtu))
93
103#define BT_L2CAP_ECRED_MIN_MTU 64
104
114#define BT_L2CAP_ECRED_MIN_MPS 64
115
121#define BT_L2CAP_MAX_MTU UINT16_MAX
122
128#define BT_L2CAP_MAX_MPS 65533
129
142#define BT_L2CAP_ECRED_CHAN_MAX_PER_REQ 5
143
151
153#define BT_L2CAP_CID_BR_SIG 0x0001
155#define BT_L2CAP_CID_ATT 0x0004
157#define BT_L2CAP_CID_LE_SIG 0x0005
159#define BT_L2CAP_CID_SMP 0x0006
161#define BT_L2CAP_CID_BR_SMP 0x0007
162
164
165struct bt_l2cap_chan;
166
172typedef void (*bt_l2cap_chan_destroy_t)(struct bt_l2cap_chan *chan);
173
199
204
211
214
215 /* Total number of status - must be at the end of the enum */
218
230
242
260
263
297#if defined(CONFIG_BT_L2CAP_DYNAMIC_CHANNEL)
299 struct net_buf *_sdu;
300 uint16_t _sdu_len;
301#if defined(CONFIG_BT_L2CAP_SEG_RECV)
302 uint16_t _sdu_len_done;
303#endif /* CONFIG_BT_L2CAP_SEG_RECV */
304
305 struct k_work rx_work;
306 struct k_fifo rx_queue;
307 /* Calls bt_l2cap_chan_ops.status on behalf of the TX processor */
308 struct k_work status_work;
309
312 uint16_t psm;
314 uint8_t ident;
316 uint8_t pending_req;
317 bt_security_t required_sec_level;
318
320 /* Server that accepted this channel, NULL when the channel was
321 * initiated locally. Keeps a reference to that server for as long as
322 * the channel lives.
323 */
324 struct bt_l2cap_server *_server;
326
327 /* Response Timeout eXpired (RTX) timer */
328 struct k_work_delayable rtx_work;
329 struct k_work_sync rtx_sync;
330#endif
331
333 sys_snode_t _pdu_ready;
335 size_t _pdu_remaining;
336};
337
347#define BT_L2CAP_LE_CHAN(_ch) CONTAINER_OF(_ch, struct bt_l2cap_le_chan, chan)
348
361
396 int (*accept)(struct bt_conn *conn, struct bt_l2cap_chan **chan);
397};
398
412#define BT_L2CAP_FIXED_CHANNEL_DEFINE(_name) \
413 static const STRUCT_SECTION_ITERABLE(bt_l2cap_fixed_chan, \
414 _CONCAT(bt_l2cap_fixed_chan, _name))
415
417#define BT_L2CAP_BR_LINK_MODE_BASIC 0x00
419#define BT_L2CAP_BR_LINK_MODE_RET 0x01
421#define BT_L2CAP_BR_LINK_MODE_FC 0x02
423#define BT_L2CAP_BR_LINK_MODE_ERET 0x03
425#define BT_L2CAP_BR_LINK_MODE_STREAM 0x04
426
428#define BT_L2CAP_BR_FCS_NO 0x00
430#define BT_L2CAP_BR_FCS_16BIT 0x01
431
438#if defined(CONFIG_BT_L2CAP_RET_FC) || defined(__DOXYGEN__)
499#endif /* CONFIG_BT_L2CAP_RET_FC */
500};
501
525
536
543
544 /* Response Timeout eXpired (RTX) timer */
547
549
551 struct k_work_delayable retry_work;
552
554
556 sys_snode_t _pdu_ready;
558 atomic_t _pdu_ready_lock;
560 sys_slist_t _pdu_tx_queue;
561
562#if defined(CONFIG_BT_L2CAP_RET_FC) || defined(__DOXYGEN__)
564 size_t _pdu_remaining;
565
567 struct net_buf *_pdu_buf;
568
570 sys_slist_t _pdu_outstanding;
571
573 struct net_buf_simple_state _pdu_state;
574
576 struct k_fifo _free_tx_win;
577
579 struct bt_l2cap_br_window tx_win[CONFIG_BT_L2CAP_MAX_WINDOW_SIZE];
580
582 struct net_buf *_sdu;
584 uint16_t _sdu_len;
585#if defined(CONFIG_BT_L2CAP_SEG_RECV) || defined(__DOXYGEN__)
586 uint16_t _sdu_len_done;
587#endif /* CONFIG_BT_L2CAP_SEG_RECV */
620
627
632#endif /* CONFIG_BT_L2CAP_RET_FC */
633};
634
655 void (*connected)(struct bt_l2cap_chan *chan);
656
665 void (*disconnected)(struct bt_l2cap_chan *chan);
666
682 void (*encrypt_change)(struct bt_l2cap_chan *chan, uint8_t hci_status);
683
695 struct net_buf *(*alloc_seg)(struct bt_l2cap_chan *chan);
696
714 struct net_buf *(*alloc_buf)(struct bt_l2cap_chan *chan);
715
738 int (*recv)(struct bt_l2cap_chan *chan, struct net_buf *buf);
739
749 void (*sent)(struct bt_l2cap_chan *chan);
750
759 void (*status)(struct bt_l2cap_chan *chan, atomic_t *status);
760
761 /* @brief Channel released callback
762 *
763 * If this callback is set it is called when the stack has release all
764 * references to the channel object.
765 */
766 void (*released)(struct bt_l2cap_chan *chan);
767
776 void (*reconfigured)(struct bt_l2cap_chan *chan);
777
778#if defined(CONFIG_BT_L2CAP_SEG_RECV) || defined(__DOXYGEN__)
810 void (*seg_recv)(struct bt_l2cap_chan *chan, size_t sdu_len,
811 off_t seg_offset, struct net_buf_simple *seg);
812#endif /* CONFIG_BT_L2CAP_SEG_RECV */
813};
814
818#define BT_L2CAP_CHAN_SEND_RESERVE (BT_L2CAP_BUF_SIZE(0))
819
823#define BT_L2CAP_SDU_CHAN_SEND_RESERVE (BT_L2CAP_SDU_BUF_SIZE(0))
824
857
860
889 int (*accept)(struct bt_conn *conn, struct bt_l2cap_server *server,
890 struct bt_l2cap_chan **chan);
891
893#if defined(CONFIG_BT_L2CAP_DYNAMIC_CHANNEL)
894 /* References held on this server: one while a connection request for
895 * it is being handled, and one per channel it accepted.
896 */
897 atomic_t _refs;
898#endif /* CONFIG_BT_L2CAP_DYNAMIC_CHANNEL */
900
902};
903
926
946
967
977
995 struct bt_l2cap_chan **chans, uint16_t psm);
996
1010
1054int bt_l2cap_ecred_chan_reconfigure_explicit(struct bt_l2cap_chan **chans, size_t chan_count,
1055 uint16_t mtu, uint16_t mps);
1056
1085 uint16_t psm);
1086
1110
1163int bt_l2cap_chan_send(struct bt_l2cap_chan *chan, struct net_buf *buf);
1164
1186int bt_l2cap_chan_give_credits(struct bt_l2cap_chan *chan, uint16_t additional_credits);
1187
1201 struct net_buf *buf);
1202
1203#ifdef __cplusplus
1204}
1205#endif
1206
1210
1211#endif /* ZEPHYR_INCLUDE_BLUETOOTH_L2CAP_H_ */
Header file for the Atomic operations API.
Bluetooth data buffer API.
Bluetooth connection handling.
long atomic_t
Atomic integer variable.
Definition atomic_types.h:34
#define ATOMIC_DEFINE(name, num_bits)
Define an array of atomic variables.
Definition atomic.h:122
bt_security_t
Security level.
Definition conn.h:1201
int bt_l2cap_ecred_chan_reconfigure(struct bt_l2cap_chan **chans, uint16_t mtu)
Reconfigure Enhanced Credit Based L2CAP channels.
int bt_l2cap_server_register(struct bt_l2cap_server *server)
Register L2CAP server.
bt_l2cap_chan_status
Status of L2CAP channel.
Definition l2cap.h:201
enum bt_l2cap_chan_status bt_l2cap_chan_status_t
Status of L2CAP channel.
int bt_l2cap_chan_connect(struct bt_conn *conn, struct bt_l2cap_chan *chan, uint16_t psm)
Connect L2CAP channel.
enum bt_l2cap_chan_state bt_l2cap_chan_state_t
Life-span states of L2CAP CoC channel.
int bt_l2cap_br_server_register(struct bt_l2cap_server *server)
Register L2CAP server on BR/EDR oriented connection.
bt_l2cap_chan_state
Life-span states of L2CAP CoC channel.
Definition l2cap.h:186
int bt_l2cap_ecred_chan_reconfigure_explicit(struct bt_l2cap_chan **chans, size_t chan_count, uint16_t mtu, uint16_t mps)
Reconfigure Enhanced Credit Based L2CAP channels.
int bt_l2cap_chan_disconnect(struct bt_l2cap_chan *chan)
Disconnect L2CAP channel.
int bt_l2cap_br_server_unregister(struct bt_l2cap_server *server)
Unregister L2CAP server on BR/EDR oriented connection.
void(* bt_l2cap_chan_destroy_t)(struct bt_l2cap_chan *chan)
Channel destroy callback.
Definition l2cap.h:172
int bt_l2cap_server_unregister(struct bt_l2cap_server *server)
Unregister L2CAP server.
int bt_l2cap_chan_send(struct bt_l2cap_chan *chan, struct net_buf *buf)
Send data to L2CAP channel.
int bt_l2cap_chan_give_credits(struct bt_l2cap_chan *chan, uint16_t additional_credits)
Give credits to the remote.
int bt_l2cap_chan_recv_complete(struct bt_l2cap_chan *chan, struct net_buf *buf)
Complete receiving L2CAP channel data.
int bt_l2cap_ecred_chan_connect(struct bt_conn *conn, struct bt_l2cap_chan **chans, uint16_t psm)
Connect Enhanced Credit Based L2CAP channels.
@ BT_L2CAP_STATUS_SHUTDOWN
Channel shutdown status.
Definition l2cap.h:210
@ BT_L2CAP_STATUS_OUT
Channel can send at least one PDU.
Definition l2cap.h:203
@ BT_L2CAP_NUM_STATUS
Definition l2cap.h:216
@ BT_L2CAP_STATUS_ENCRYPT_PENDING
Channel encryption pending status.
Definition l2cap.h:213
@ BT_L2CAP_DISCONNECTED
Channel disconnected.
Definition l2cap.h:188
@ BT_L2CAP_CONFIG
Channel in config state, BR/EDR specific.
Definition l2cap.h:192
@ BT_L2CAP_CONNECTED
Channel ready for upper layer traffic on it.
Definition l2cap.h:194
@ BT_L2CAP_DISCONNECTING
Channel in disconnecting state.
Definition l2cap.h:196
@ BT_L2CAP_CONNECTING
Channel in connecting state.
Definition l2cap.h:190
struct _slist sys_slist_t
Single-linked list structure.
Definition slist.h:54
struct _snode sys_snode_t
Single-linked list node structure.
Definition slist.h:44
Public kernel APIs.
__INTPTR_TYPE__ off_t
Definition types.h:36
Buffer management.
state
Definition parser_state.h:29
Header file for the single-linked list API.
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Opaque type representing a connection to a remote device.
BREDR L2CAP Channel structure.
Definition l2cap.h:527
struct bt_l2cap_br_endpoint rx
Channel Receiving Endpoint.
Definition l2cap.h:531
struct k_work_delayable rtx_work
Definition l2cap.h:545
bt_security_t required_sec_level
Definition l2cap.h:542
uint16_t psm
Remote PSM to be connected.
Definition l2cap.h:539
struct k_work_sync rtx_sync
Definition l2cap.h:546
struct bt_l2cap_chan chan
Common L2CAP channel reference object.
Definition l2cap.h:529
atomic_t flags[ATOMIC_BITMAP_SIZE(64)]
Channel flags, for internal use only.
Definition l2cap.h:535
uint16_t retry_count
Definition l2cap.h:624
bt_l2cap_chan_state_t state
Definition l2cap.h:537
struct bt_l2cap_br_endpoint tx
Channel Transmission Endpoint.
Definition l2cap.h:533
struct k_work_delayable ret_work
Definition l2cap.h:629
uint16_t next_tx_seq
Definition l2cap.h:599
uint16_t expected_tx_seq
Definition l2cap.h:614
struct bt_l2cap_br_window tx_win[CONFIG_BT_L2CAP_MAX_WINDOW_SIZE]
Definition l2cap.h:579
uint16_t srej_save_req_seq
Definition l2cap.h:626
uint16_t buffer_seq
Definition l2cap.h:619
struct k_work_delayable monitor_work
Definition l2cap.h:631
uint8_t ident
Helps match request context during CoC.
Definition l2cap.h:541
uint16_t tx_seq
Definition l2cap.h:595
uint16_t expected_ack_seq
Definition l2cap.h:603
uint16_t req_seq
Definition l2cap.h:611
BREDR L2CAP Endpoint structure.
Definition l2cap.h:433
uint16_t max_window
Endpoint Maximum Window Size MAX supported window size is configured by <code class='doxyxref' data-...
Definition l2cap.h:480
uint16_t monitor_timeout
Endpoint Monitor Timeout The field is configured by <code class='doxyxref' data-stype='kconfig:optio...
Definition l2cap.h:472
uint8_t max_transmit
Endpoint Maximum Transmit The field is used to set the max retransmission count.
Definition l2cap.h:460
uint16_t mtu
Endpoint Maximum Transmission Unit.
Definition l2cap.h:437
uint16_t mps
Endpoint Maximum PDU payload Size.
Definition l2cap.h:474
bool extended_control
Endpoint Extended Control.
Definition l2cap.h:498
uint8_t fcs
Endpoint FCS Type The value is defined as BT_L2CAP_BR_FCS_* The default setting should be BT_L2CAP_BR...
Definition l2cap.h:491
uint16_t cid
Endpoint Channel Identifier (CID).
Definition l2cap.h:435
uint16_t ret_timeout
Endpoint Retransmission Timeout The field is configured by <code class='doxyxref' data-stype='kconfi...
Definition l2cap.h:467
uint8_t mode
Endpoint Link Mode.
Definition l2cap.h:442
bool optional
Whether Endpoint Link Mode is optional If the optional is true, the mode could be changed according t...
Definition l2cap.h:452
I-Frame transmission window for none BASIC mode L2cap connected channel.
Definition l2cap.h:503
uint8_t * data
data address
Definition l2cap.h:511
bool srej
srej flag
Definition l2cap.h:517
struct net_buf_simple_state sdu_state
Definition l2cap.h:521
uint16_t len
data len
Definition l2cap.h:509
uint8_t transmit_counter
Transmit Counter.
Definition l2cap.h:513
uint8_t sar
SAR flag.
Definition l2cap.h:515
sys_snode_t node
Definition l2cap.h:504
bool retransmit
retransmit flag
Definition l2cap.h:519
uint16_t tx_seq
tx seq
Definition l2cap.h:507
struct net_buf * sdu
Definition l2cap.h:523
L2CAP Channel operations structure.
Definition l2cap.h:647
int(* recv)(struct bt_l2cap_chan *chan, struct net_buf *buf)
Channel recv callback.
Definition l2cap.h:738
void(* encrypt_change)(struct bt_l2cap_chan *chan, uint8_t hci_status)
Channel encrypt_change callback.
Definition l2cap.h:682
void(* disconnected)(struct bt_l2cap_chan *chan)
Channel disconnected callback.
Definition l2cap.h:665
void(* connected)(struct bt_l2cap_chan *chan)
Channel connected callback.
Definition l2cap.h:655
void(* status)(struct bt_l2cap_chan *chan, atomic_t *status)
Channel status callback.
Definition l2cap.h:759
void(* released)(struct bt_l2cap_chan *chan)
Definition l2cap.h:766
void(* sent)(struct bt_l2cap_chan *chan)
Channel sent callback.
Definition l2cap.h:749
void(* seg_recv)(struct bt_l2cap_chan *chan, size_t sdu_len, off_t seg_offset, struct net_buf_simple *seg)
Handle L2CAP segments directly.
Definition l2cap.h:810
void(* reconfigured)(struct bt_l2cap_chan *chan)
Channel reconfigured callback.
Definition l2cap.h:776
L2CAP Channel structure.
Definition l2cap.h:220
struct bt_conn * conn
Channel connection reference.
Definition l2cap.h:222
sys_snode_t node
Definition l2cap.h:225
const struct bt_l2cap_chan_ops * ops
Channel operations reference.
Definition l2cap.h:224
atomic_t status[ATOMIC_BITMAP_SIZE(BT_L2CAP_NUM_STATUS)]
Definition l2cap.h:228
bt_l2cap_chan_destroy_t destroy
Definition l2cap.h:226
Fixed L2CAP Channel structure.
Definition l2cap.h:352
uint16_t cid
Channel Identifier (CID).
Definition l2cap.h:360
int(* accept)(struct bt_conn *conn, struct bt_l2cap_chan **chan)
Channel accept callback.
Definition l2cap.h:396
LE L2CAP Channel structure.
Definition l2cap.h:244
struct bt_l2cap_le_endpoint tx
Channel Transmission Endpoint.
Definition l2cap.h:271
uint16_t pending_rx_mtu
Pending RX MTU on ECFC reconfigure, used internally by stack.
Definition l2cap.h:262
struct k_fifo tx_queue
Channel Transmission queue.
Definition l2cap.h:296
struct bt_l2cap_le_endpoint rx
Channel Receiving Endpoint.
Definition l2cap.h:259
struct bt_l2cap_chan chan
Common L2CAP channel reference object.
Definition l2cap.h:246
LE L2CAP Endpoint structure.
Definition l2cap.h:232
uint16_t mtu
Endpoint Maximum Transmission Unit.
Definition l2cap.h:236
uint16_t mps
Endpoint Maximum PDU payload Size.
Definition l2cap.h:238
atomic_t credits
Endpoint credits.
Definition l2cap.h:240
uint16_t cid
Endpoint Channel Identifier (CID).
Definition l2cap.h:234
L2CAP Server structure.
Definition l2cap.h:826
uint16_t psm
Server PSM.
Definition l2cap.h:856
sys_snode_t node
Definition l2cap.h:901
bt_security_t sec_level
Required minimum security level.
Definition l2cap.h:859
int(* accept)(struct bt_conn *conn, struct bt_l2cap_server *server, struct bt_l2cap_chan **chan)
Server accept callback.
Definition l2cap.h:889
Kernel FIFO structure.
Definition kernel.h:3107
A structure used to submit work after a delay.
Definition kernel.h:4955
A structure holding internal state for a pending synchronous operation on a work item or queue.
Definition kernel.h:5050
A structure used to submit work.
Definition kernel.h:4913
Parsing state of a buffer.
Definition net_buf.h:1006
Simple network buffer representation.
Definition net_buf.h:97
Network buffer representation.
Definition net_buf.h:1062
Misc utilities.