Zephyr API Documentation 4.1.99
A Scalable Open Source RTOS
 4.1.99
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
l2cap.h
Go to the documentation of this file.
1
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
21#include <stddef.h>
22#include <stdint.h>
23
27#include <zephyr/kernel.h>
28#include <zephyr/net_buf.h>
29#include <zephyr/sys/atomic.h>
30#include <zephyr/sys/slist.h>
31#include <zephyr/sys/util.h>
32#include <sys/types.h>
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
39#define BT_L2CAP_HDR_SIZE 4
40
42#define BT_L2CAP_TX_MTU (CONFIG_BT_L2CAP_TX_MTU)
43
45#define BT_L2CAP_RX_MTU (CONFIG_BT_BUF_ACL_RX_SIZE - BT_L2CAP_HDR_SIZE)
46
54#define BT_L2CAP_BUF_SIZE(mtu) BT_BUF_ACL_SIZE(BT_L2CAP_HDR_SIZE + (mtu))
55
57#define BT_L2CAP_SDU_HDR_SIZE 2
58
67#define BT_L2CAP_SDU_TX_MTU (BT_L2CAP_TX_MTU - BT_L2CAP_SDU_HDR_SIZE)
68
79#define BT_L2CAP_SDU_RX_MTU (BT_L2CAP_RX_MTU - BT_L2CAP_SDU_HDR_SIZE)
80
90#define BT_L2CAP_SDU_BUF_SIZE(mtu) BT_L2CAP_BUF_SIZE(BT_L2CAP_SDU_HDR_SIZE + (mtu))
91
101#define BT_L2CAP_ECRED_MIN_MTU 64
102
112#define BT_L2CAP_ECRED_MIN_MPS 64
113
126#define BT_L2CAP_ECRED_CHAN_MAX_PER_REQ 5
127
128struct bt_l2cap_chan;
129
135typedef void (*bt_l2cap_chan_destroy_t)(struct bt_l2cap_chan *chan);
136
162
167
174
177
178 /* Total number of status - must be at the end of the enum */
181
193
205
223
226
237#if defined(CONFIG_BT_L2CAP_DYNAMIC_CHANNEL)
239 struct net_buf *_sdu;
240 uint16_t _sdu_len;
241#if defined(CONFIG_BT_L2CAP_SEG_RECV)
242 uint16_t _sdu_len_done;
243#endif /* CONFIG_BT_L2CAP_SEG_RECV */
244
245 struct k_work rx_work;
246 struct k_fifo rx_queue;
247
250 uint16_t psm;
252 uint8_t ident;
253 bt_security_t required_sec_level;
254
255 /* Response Timeout eXpired (RTX) timer */
256 struct k_work_delayable rtx_work;
257 struct k_work_sync rtx_sync;
258#endif
259
261 sys_snode_t _pdu_ready;
263 atomic_t _pdu_ready_lock;
265 size_t _pdu_remaining;
266};
267
277#define BT_L2CAP_LE_CHAN(_ch) CONTAINER_OF(_ch, struct bt_l2cap_le_chan, chan)
278
280#define BT_L2CAP_BR_LINK_MODE_BASIC 0x00
282#define BT_L2CAP_BR_LINK_MODE_RET 0x01
284#define BT_L2CAP_BR_LINK_MODE_FC 0x02
286#define BT_L2CAP_BR_LINK_MODE_ERET 0x03
288#define BT_L2CAP_BR_LINK_MODE_STREAM 0x04
289
291#define BT_L2CAP_BR_FCS_NO 0x00
293#define BT_L2CAP_BR_FCS_16BIT 0x01
294
301#if defined(CONFIG_BT_L2CAP_RET_FC) || defined(__DOXYGEN__)
362#endif /* CONFIG_BT_L2CAP_RET_FC */
363};
364
388
397 /* For internal use only */
399
406
407 /* Response Timeout eXpired (RTX) timer */
410
412 sys_snode_t _pdu_ready;
414 atomic_t _pdu_ready_lock;
416 sys_slist_t _pdu_tx_queue;
417
418#if defined(CONFIG_BT_L2CAP_RET_FC) || defined(__DOXYGEN__)
420 uint16_t _sdu_total_len;
421
423 size_t _pdu_remaining;
424
426 struct net_buf *_pdu_buf;
427
429 sys_slist_t _pdu_outstanding;
430
432 struct net_buf_simple_state _pdu_state;
433
435 struct k_fifo _free_tx_win;
436
438 struct bt_l2cap_br_window tx_win[CONFIG_BT_L2CAP_MAX_WINDOW_SIZE];
439
441 struct net_buf *_sdu;
443 uint16_t _sdu_len;
444#if defined(CONFIG_BT_L2CAP_SEG_RECV) || defined(__DOXYGEN__)
445 uint16_t _sdu_len_done;
446#endif /* CONFIG_BT_L2CAP_SEG_RECV */
479
486
491#endif /* CONFIG_BT_L2CAP_RET_FC */
492};
493
506 void (*connected)(struct bt_l2cap_chan *chan);
507
516 void (*disconnected)(struct bt_l2cap_chan *chan);
517
533 void (*encrypt_change)(struct bt_l2cap_chan *chan, uint8_t hci_status);
534
546 struct net_buf *(*alloc_seg)(struct bt_l2cap_chan *chan);
547
560 struct net_buf *(*alloc_buf)(struct bt_l2cap_chan *chan);
561
584 int (*recv)(struct bt_l2cap_chan *chan, struct net_buf *buf);
585
595 void (*sent)(struct bt_l2cap_chan *chan);
596
605 void (*status)(struct bt_l2cap_chan *chan, atomic_t *status);
606
607 /* @brief Channel released callback
608 *
609 * If this callback is set it is called when the stack has release all
610 * references to the channel object.
611 */
612 void (*released)(struct bt_l2cap_chan *chan);
613
622 void (*reconfigured)(struct bt_l2cap_chan *chan);
623
624#if defined(CONFIG_BT_L2CAP_SEG_RECV)
656 void (*seg_recv)(struct bt_l2cap_chan *chan, size_t sdu_len,
657 off_t seg_offset, struct net_buf_simple *seg);
658#endif /* CONFIG_BT_L2CAP_SEG_RECV */
659};
660
664#define BT_L2CAP_CHAN_SEND_RESERVE (BT_L2CAP_BUF_SIZE(0))
665
669#define BT_L2CAP_SDU_CHAN_SEND_RESERVE (BT_L2CAP_SDU_BUF_SIZE(0))
670
703
706
724 int (*accept)(struct bt_conn *conn, struct bt_l2cap_server *server,
725 struct bt_l2cap_chan **chan);
726
728};
729
750
771
788 struct bt_l2cap_chan **chans, uint16_t psm);
789
803
847int bt_l2cap_ecred_chan_reconfigure_explicit(struct bt_l2cap_chan **chans, size_t chan_count,
848 uint16_t mtu, uint16_t mps);
849
871int bt_l2cap_chan_connect(struct bt_conn *conn, struct bt_l2cap_chan *chan,
872 uint16_t psm);
873
886
931int bt_l2cap_chan_send(struct bt_l2cap_chan *chan, struct net_buf *buf);
932
954int bt_l2cap_chan_give_credits(struct bt_l2cap_chan *chan, uint16_t additional_credits);
955
969 struct net_buf *buf);
970
971#ifdef __cplusplus
972}
973#endif
974
979#endif /* ZEPHYR_INCLUDE_BLUETOOTH_L2CAP_H_ */
long atomic_t
Definition atomic_types.h:15
Bluetooth data buffer API.
Bluetooth connection handling.
#define ATOMIC_DEFINE(name, num_bits)
Define an array of atomic variables.
Definition atomic.h:111
bt_security_t
Security level.
Definition conn.h:814
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:164
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:149
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.
void(* bt_l2cap_chan_destroy_t)(struct bt_l2cap_chan *chan)
Channel destroy callback.
Definition l2cap.h:135
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:173
@ BT_L2CAP_STATUS_OUT
Channel can send at least one PDU.
Definition l2cap.h:166
@ BT_L2CAP_NUM_STATUS
Definition l2cap.h:179
@ BT_L2CAP_STATUS_ENCRYPT_PENDING
Channel encryption pending status.
Definition l2cap.h:176
@ BT_L2CAP_DISCONNECTED
Channel disconnected.
Definition l2cap.h:151
@ BT_L2CAP_CONFIG
Channel in config state, BR/EDR specific.
Definition l2cap.h:155
@ BT_L2CAP_CONNECTED
Channel ready for upper layer traffic on it.
Definition l2cap.h:157
@ BT_L2CAP_DISCONNECTING
Channel in disconnecting state.
Definition l2cap.h:159
@ BT_L2CAP_CONNECTING
Channel in connecting state.
Definition l2cap.h:153
struct _slist sys_slist_t
Single-linked list structure.
Definition slist.h:49
struct _snode sys_snode_t
Single-linked list node structure.
Definition slist.h:39
Public kernel APIs.
__INTPTR_TYPE__ off_t
Definition types.h:36
Buffer management.
flags
Definition parser.h:97
state
Definition parser_state.h:29
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
BREDR L2CAP Channel structure.
Definition l2cap.h:390
struct bt_l2cap_br_endpoint rx
Channel Receiving Endpoint.
Definition l2cap.h:394
struct k_work_delayable rtx_work
Definition l2cap.h:408
bt_security_t required_sec_level
Definition l2cap.h:405
uint16_t psm
Remote PSM to be connected.
Definition l2cap.h:402
struct k_work_sync rtx_sync
Definition l2cap.h:409
struct bt_l2cap_chan chan
Common L2CAP channel reference object.
Definition l2cap.h:392
uint16_t retry_count
Definition l2cap.h:483
bt_l2cap_chan_state_t state
Definition l2cap.h:400
struct bt_l2cap_br_endpoint tx
Channel Transmission Endpoint.
Definition l2cap.h:396
struct k_work_delayable ret_work
Definition l2cap.h:488
uint16_t next_tx_seq
Definition l2cap.h:458
uint16_t expected_tx_seq
Definition l2cap.h:473
struct bt_l2cap_br_window tx_win[CONFIG_BT_L2CAP_MAX_WINDOW_SIZE]
Definition l2cap.h:438
uint16_t srej_save_req_seq
Definition l2cap.h:485
uint16_t buffer_seq
Definition l2cap.h:478
struct k_work_delayable monitor_work
Definition l2cap.h:490
uint8_t ident
Helps match request context during CoC.
Definition l2cap.h:404
uint16_t tx_seq
Definition l2cap.h:454
uint16_t expected_ack_seq
Definition l2cap.h:462
uint16_t req_seq
Definition l2cap.h:470
BREDR L2CAP Endpoint structure.
Definition l2cap.h:296
uint16_t max_window
Endpoint Maximum Window Size MAX supported window size is configured by \c BT_L2CAP_MAX_WINDOW_SIZE.
Definition l2cap.h:343
uint16_t monitor_timeout
Endpoint Monitor Timeout The field is configured by \c BT_L2CAP_BR_MONITOR_TIMEOUT
Definition l2cap.h:335
uint8_t max_transmit
Endpoint Maximum Transmit The field is used to set the max retransmission count.
Definition l2cap.h:323
uint16_t mtu
Endpoint Maximum Transmission Unit.
Definition l2cap.h:300
uint16_t mps
Endpoint Maximum PDU payload Size.
Definition l2cap.h:337
bool extended_control
Endpoint Extended Control.
Definition l2cap.h:361
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:354
uint16_t cid
Endpoint Channel Identifier (CID)
Definition l2cap.h:298
uint16_t ret_timeout
Endpoint Retransmission Timeout The field is configured by \c BT_L2CAP_BR_RET_TIMEOUT The field shoul...
Definition l2cap.h:330
uint8_t mode
Endpoint Link Mode.
Definition l2cap.h:305
bool optional
Whether Endpoint Link Mode is optional If the optional is true, the mode could be changed according t...
Definition l2cap.h:315
I-Frame transmission window for none BASIC mode L2cap connected channel.
Definition l2cap.h:366
uint8_t * data
data address
Definition l2cap.h:374
bool srej
srej flag
Definition l2cap.h:380
uint16_t sdu_total_len
Definition l2cap.h:386
struct net_buf_simple_state sdu_state
Definition l2cap.h:382
uint16_t len
data len
Definition l2cap.h:372
uint8_t transmit_counter
Transmit Counter.
Definition l2cap.h:376
uint8_t sar
SAR flag.
Definition l2cap.h:378
sys_snode_t node
Definition l2cap.h:367
uint16_t tx_seq
tx seq
Definition l2cap.h:370
struct net_buf * sdu
Definition l2cap.h:384
L2CAP Channel operations structure.
Definition l2cap.h:498
int(* recv)(struct bt_l2cap_chan *chan, struct net_buf *buf)
Channel recv callback.
Definition l2cap.h:584
void(* encrypt_change)(struct bt_l2cap_chan *chan, uint8_t hci_status)
Channel encrypt_change callback.
Definition l2cap.h:533
void(* disconnected)(struct bt_l2cap_chan *chan)
Channel disconnected callback.
Definition l2cap.h:516
void(* connected)(struct bt_l2cap_chan *chan)
Channel connected callback.
Definition l2cap.h:506
void(* status)(struct bt_l2cap_chan *chan, atomic_t *status)
Channel status callback.
Definition l2cap.h:605
void(* released)(struct bt_l2cap_chan *chan)
Definition l2cap.h:612
void(* sent)(struct bt_l2cap_chan *chan)
Channel sent callback.
Definition l2cap.h:595
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:656
void(* reconfigured)(struct bt_l2cap_chan *chan)
Channel reconfigured callback.
Definition l2cap.h:622
L2CAP Channel structure.
Definition l2cap.h:183
struct bt_conn * conn
Channel connection reference.
Definition l2cap.h:185
sys_snode_t node
Definition l2cap.h:188
const struct bt_l2cap_chan_ops * ops
Channel operations reference.
Definition l2cap.h:187
atomic_t status[ATOMIC_BITMAP_SIZE(BT_L2CAP_NUM_STATUS)]
Definition l2cap.h:191
bt_l2cap_chan_destroy_t destroy
Definition l2cap.h:189
LE L2CAP Channel structure.
Definition l2cap.h:207
struct bt_l2cap_le_endpoint tx
Channel Transmission Endpoint.
Definition l2cap.h:234
uint16_t pending_rx_mtu
Pending RX MTU on ECFC reconfigure, used internally by stack.
Definition l2cap.h:225
struct k_fifo tx_queue
Channel Transmission queue (for SDUs)
Definition l2cap.h:236
struct bt_l2cap_le_endpoint rx
Channel Receiving Endpoint.
Definition l2cap.h:222
struct bt_l2cap_chan chan
Common L2CAP channel reference object.
Definition l2cap.h:209
LE L2CAP Endpoint structure.
Definition l2cap.h:195
uint16_t mtu
Endpoint Maximum Transmission Unit.
Definition l2cap.h:199
uint16_t mps
Endpoint Maximum PDU payload Size.
Definition l2cap.h:201
atomic_t credits
Endpoint credits.
Definition l2cap.h:203
uint16_t cid
Endpoint Channel Identifier (CID)
Definition l2cap.h:197
L2CAP Server structure.
Definition l2cap.h:672
uint16_t psm
Server PSM.
Definition l2cap.h:702
sys_snode_t node
Definition l2cap.h:727
bt_security_t sec_level
Required minimum security level.
Definition l2cap.h:705
int(* accept)(struct bt_conn *conn, struct bt_l2cap_server *server, struct bt_l2cap_chan **chan)
Server accept callback.
Definition l2cap.h:724
Definition kernel.h:2524
A structure used to submit work after a delay.
Definition kernel.h:4063
A structure holding internal state for a pending synchronous operation on a work item or queue.
Definition kernel.h:4146
A structure used to submit work.
Definition kernel.h:4035
Parsing state of a buffer.
Definition net_buf.h:950
Simple network buffer representation.
Definition net_buf.h:89
Network buffer representation.
Definition net_buf.h:1006
Misc utilities.