Zephyr Project API 4.2.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
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
119#define BT_L2CAP_MAX_MTU UINT16_MAX
120
126#define BT_L2CAP_MAX_MPS 65533
127
140#define BT_L2CAP_ECRED_CHAN_MAX_PER_REQ 5
141
142struct bt_l2cap_chan;
143
149typedef void (*bt_l2cap_chan_destroy_t)(struct bt_l2cap_chan *chan);
150
176
181
188
191
192 /* Total number of status - must be at the end of the enum */
195
207
219
237
240
251#if defined(CONFIG_BT_L2CAP_DYNAMIC_CHANNEL)
253 struct net_buf *_sdu;
254 uint16_t _sdu_len;
255#if defined(CONFIG_BT_L2CAP_SEG_RECV)
256 uint16_t _sdu_len_done;
257#endif /* CONFIG_BT_L2CAP_SEG_RECV */
258
259 struct k_work rx_work;
260 struct k_fifo rx_queue;
261
264 uint16_t psm;
266 uint8_t ident;
268 uint8_t pending_req;
269 bt_security_t required_sec_level;
270
271 /* Response Timeout eXpired (RTX) timer */
272 struct k_work_delayable rtx_work;
273 struct k_work_sync rtx_sync;
274#endif
275
277 sys_snode_t _pdu_ready;
279 size_t _pdu_remaining;
280};
281
291#define BT_L2CAP_LE_CHAN(_ch) CONTAINER_OF(_ch, struct bt_l2cap_le_chan, chan)
292
294#define BT_L2CAP_BR_LINK_MODE_BASIC 0x00
296#define BT_L2CAP_BR_LINK_MODE_RET 0x01
298#define BT_L2CAP_BR_LINK_MODE_FC 0x02
300#define BT_L2CAP_BR_LINK_MODE_ERET 0x03
302#define BT_L2CAP_BR_LINK_MODE_STREAM 0x04
303
305#define BT_L2CAP_BR_FCS_NO 0x00
307#define BT_L2CAP_BR_FCS_16BIT 0x01
308
315#if defined(CONFIG_BT_L2CAP_RET_FC) || defined(__DOXYGEN__)
376#endif /* CONFIG_BT_L2CAP_RET_FC */
377};
378
402
411 /* For internal use only */
413
420
421 /* Response Timeout eXpired (RTX) timer */
424
426 sys_snode_t _pdu_ready;
428 atomic_t _pdu_ready_lock;
430 sys_slist_t _pdu_tx_queue;
431
432#if defined(CONFIG_BT_L2CAP_RET_FC) || defined(__DOXYGEN__)
434 uint16_t _sdu_total_len;
435
437 size_t _pdu_remaining;
438
440 struct net_buf *_pdu_buf;
441
443 sys_slist_t _pdu_outstanding;
444
446 struct net_buf_simple_state _pdu_state;
447
449 struct k_fifo _free_tx_win;
450
452 struct bt_l2cap_br_window tx_win[CONFIG_BT_L2CAP_MAX_WINDOW_SIZE];
453
455 struct net_buf *_sdu;
457 uint16_t _sdu_len;
458#if defined(CONFIG_BT_L2CAP_SEG_RECV) || defined(__DOXYGEN__)
459 uint16_t _sdu_len_done;
460#endif /* CONFIG_BT_L2CAP_SEG_RECV */
493
500
505#endif /* CONFIG_BT_L2CAP_RET_FC */
506};
507
520 void (*connected)(struct bt_l2cap_chan *chan);
521
530 void (*disconnected)(struct bt_l2cap_chan *chan);
531
547 void (*encrypt_change)(struct bt_l2cap_chan *chan, uint8_t hci_status);
548
560 struct net_buf *(*alloc_seg)(struct bt_l2cap_chan *chan);
561
574 struct net_buf *(*alloc_buf)(struct bt_l2cap_chan *chan);
575
598 int (*recv)(struct bt_l2cap_chan *chan, struct net_buf *buf);
599
609 void (*sent)(struct bt_l2cap_chan *chan);
610
619 void (*status)(struct bt_l2cap_chan *chan, atomic_t *status);
620
621 /* @brief Channel released callback
622 *
623 * If this callback is set it is called when the stack has release all
624 * references to the channel object.
625 */
626 void (*released)(struct bt_l2cap_chan *chan);
627
636 void (*reconfigured)(struct bt_l2cap_chan *chan);
637
638#if defined(CONFIG_BT_L2CAP_SEG_RECV)
670 void (*seg_recv)(struct bt_l2cap_chan *chan, size_t sdu_len,
671 off_t seg_offset, struct net_buf_simple *seg);
672#endif /* CONFIG_BT_L2CAP_SEG_RECV */
673};
674
678#define BT_L2CAP_CHAN_SEND_RESERVE (BT_L2CAP_BUF_SIZE(0))
679
683#define BT_L2CAP_SDU_CHAN_SEND_RESERVE (BT_L2CAP_SDU_BUF_SIZE(0))
684
717
720
738 int (*accept)(struct bt_conn *conn, struct bt_l2cap_server *server,
739 struct bt_l2cap_chan **chan);
740
742};
743
764
785
795
812 struct bt_l2cap_chan **chans, uint16_t psm);
813
827
871int bt_l2cap_ecred_chan_reconfigure_explicit(struct bt_l2cap_chan **chans, size_t chan_count,
872 uint16_t mtu, uint16_t mps);
873
895int bt_l2cap_chan_connect(struct bt_conn *conn, struct bt_l2cap_chan *chan,
896 uint16_t psm);
897
910
955int bt_l2cap_chan_send(struct bt_l2cap_chan *chan, struct net_buf *buf);
956
978int bt_l2cap_chan_give_credits(struct bt_l2cap_chan *chan, uint16_t additional_credits);
979
993 struct net_buf *buf);
994
995#ifdef __cplusplus
996}
997#endif
998
1003#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:913
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:178
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:163
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.
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.
void(* bt_l2cap_chan_destroy_t)(struct bt_l2cap_chan *chan)
Channel destroy callback.
Definition l2cap.h:149
@ BT_L2CAP_STATUS_SHUTDOWN
Channel shutdown status.
Definition l2cap.h:187
@ BT_L2CAP_STATUS_OUT
Channel can send at least one PDU.
Definition l2cap.h:180
@ BT_L2CAP_NUM_STATUS
Definition l2cap.h:193
@ BT_L2CAP_STATUS_ENCRYPT_PENDING
Channel encryption pending status.
Definition l2cap.h:190
@ BT_L2CAP_DISCONNECTED
Channel disconnected.
Definition l2cap.h:165
@ BT_L2CAP_CONFIG
Channel in config state, BR/EDR specific.
Definition l2cap.h:169
@ BT_L2CAP_CONNECTED
Channel ready for upper layer traffic on it.
Definition l2cap.h:171
@ BT_L2CAP_DISCONNECTING
Channel in disconnecting state.
Definition l2cap.h:173
@ BT_L2CAP_CONNECTING
Channel in connecting state.
Definition l2cap.h:167
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:404
struct bt_l2cap_br_endpoint rx
Channel Receiving Endpoint.
Definition l2cap.h:408
struct k_work_delayable rtx_work
Definition l2cap.h:422
bt_security_t required_sec_level
Definition l2cap.h:419
uint16_t psm
Remote PSM to be connected.
Definition l2cap.h:416
struct k_work_sync rtx_sync
Definition l2cap.h:423
struct bt_l2cap_chan chan
Common L2CAP channel reference object.
Definition l2cap.h:406
uint16_t retry_count
Definition l2cap.h:497
bt_l2cap_chan_state_t state
Definition l2cap.h:414
struct bt_l2cap_br_endpoint tx
Channel Transmission Endpoint.
Definition l2cap.h:410
struct k_work_delayable ret_work
Definition l2cap.h:502
uint16_t next_tx_seq
Definition l2cap.h:472
uint16_t expected_tx_seq
Definition l2cap.h:487
struct bt_l2cap_br_window tx_win[CONFIG_BT_L2CAP_MAX_WINDOW_SIZE]
Definition l2cap.h:452
uint16_t srej_save_req_seq
Definition l2cap.h:499
uint16_t buffer_seq
Definition l2cap.h:492
struct k_work_delayable monitor_work
Definition l2cap.h:504
uint8_t ident
Helps match request context during CoC.
Definition l2cap.h:418
uint16_t tx_seq
Definition l2cap.h:468
uint16_t expected_ack_seq
Definition l2cap.h:476
uint16_t req_seq
Definition l2cap.h:484
BREDR L2CAP Endpoint structure.
Definition l2cap.h:310
uint16_t max_window
Endpoint Maximum Window Size MAX supported window size is configured by \verbatim BT_L2CAP_MAX_WINDOW...
Definition l2cap.h:357
uint16_t monitor_timeout
Endpoint Monitor Timeout The field is configured by \verbatim BT_L2CAP_BR_MONITOR_TIMEOUT \endverbati...
Definition l2cap.h:349
uint8_t max_transmit
Endpoint Maximum Transmit The field is used to set the max retransmission count.
Definition l2cap.h:337
uint16_t mtu
Endpoint Maximum Transmission Unit.
Definition l2cap.h:314
uint16_t mps
Endpoint Maximum PDU payload Size.
Definition l2cap.h:351
bool extended_control
Endpoint Extended Control.
Definition l2cap.h:375
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:368
uint16_t cid
Endpoint Channel Identifier (CID)
Definition l2cap.h:312
uint16_t ret_timeout
Endpoint Retransmission Timeout The field is configured by \verbatim BT_L2CAP_BR_RET_TIMEOUT \endverb...
Definition l2cap.h:344
uint8_t mode
Endpoint Link Mode.
Definition l2cap.h:319
bool optional
Whether Endpoint Link Mode is optional If the optional is true, the mode could be changed according t...
Definition l2cap.h:329
I-Frame transmission window for none BASIC mode L2cap connected channel.
Definition l2cap.h:380
uint8_t * data
data address
Definition l2cap.h:388
bool srej
srej flag
Definition l2cap.h:394
uint16_t sdu_total_len
Definition l2cap.h:400
struct net_buf_simple_state sdu_state
Definition l2cap.h:396
uint16_t len
data len
Definition l2cap.h:386
uint8_t transmit_counter
Transmit Counter.
Definition l2cap.h:390
uint8_t sar
SAR flag.
Definition l2cap.h:392
sys_snode_t node
Definition l2cap.h:381
uint16_t tx_seq
tx seq
Definition l2cap.h:384
struct net_buf * sdu
Definition l2cap.h:398
L2CAP Channel operations structure.
Definition l2cap.h:512
int(* recv)(struct bt_l2cap_chan *chan, struct net_buf *buf)
Channel recv callback.
Definition l2cap.h:598
void(* encrypt_change)(struct bt_l2cap_chan *chan, uint8_t hci_status)
Channel encrypt_change callback.
Definition l2cap.h:547
void(* disconnected)(struct bt_l2cap_chan *chan)
Channel disconnected callback.
Definition l2cap.h:530
void(* connected)(struct bt_l2cap_chan *chan)
Channel connected callback.
Definition l2cap.h:520
void(* status)(struct bt_l2cap_chan *chan, atomic_t *status)
Channel status callback.
Definition l2cap.h:619
void(* released)(struct bt_l2cap_chan *chan)
Definition l2cap.h:626
void(* sent)(struct bt_l2cap_chan *chan)
Channel sent callback.
Definition l2cap.h:609
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:670
void(* reconfigured)(struct bt_l2cap_chan *chan)
Channel reconfigured callback.
Definition l2cap.h:636
L2CAP Channel structure.
Definition l2cap.h:197
struct bt_conn * conn
Channel connection reference.
Definition l2cap.h:199
sys_snode_t node
Definition l2cap.h:202
const struct bt_l2cap_chan_ops * ops
Channel operations reference.
Definition l2cap.h:201
atomic_t status[ATOMIC_BITMAP_SIZE(BT_L2CAP_NUM_STATUS)]
Definition l2cap.h:205
bt_l2cap_chan_destroy_t destroy
Definition l2cap.h:203
LE L2CAP Channel structure.
Definition l2cap.h:221
struct bt_l2cap_le_endpoint tx
Channel Transmission Endpoint.
Definition l2cap.h:248
uint16_t pending_rx_mtu
Pending RX MTU on ECFC reconfigure, used internally by stack.
Definition l2cap.h:239
struct k_fifo tx_queue
Channel Transmission queue (for SDUs)
Definition l2cap.h:250
struct bt_l2cap_le_endpoint rx
Channel Receiving Endpoint.
Definition l2cap.h:236
struct bt_l2cap_chan chan
Common L2CAP channel reference object.
Definition l2cap.h:223
LE L2CAP Endpoint structure.
Definition l2cap.h:209
uint16_t mtu
Endpoint Maximum Transmission Unit.
Definition l2cap.h:213
uint16_t mps
Endpoint Maximum PDU payload Size.
Definition l2cap.h:215
atomic_t credits
Endpoint credits.
Definition l2cap.h:217
uint16_t cid
Endpoint Channel Identifier (CID)
Definition l2cap.h:211
L2CAP Server structure.
Definition l2cap.h:686
uint16_t psm
Server PSM.
Definition l2cap.h:716
sys_snode_t node
Definition l2cap.h:741
bt_security_t sec_level
Required minimum security level.
Definition l2cap.h:719
int(* accept)(struct bt_conn *conn, struct bt_l2cap_server *server, struct bt_l2cap_chan **chan)
Server accept callback.
Definition l2cap.h:738
Definition kernel.h:2540
A structure used to submit work after a delay.
Definition kernel.h:4101
A structure holding internal state for a pending synchronous operation on a work item or queue.
Definition kernel.h:4184
A structure used to submit work.
Definition kernel.h:4073
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.