Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS

L2CAP. More...

Data Structures

struct  bt_l2cap_chan
 L2CAP Channel structure. More...
 
struct  bt_l2cap_le_endpoint
 LE L2CAP Endpoint structure. More...
 
struct  bt_l2cap_le_chan
 LE L2CAP Channel structure. More...
 
struct  bt_l2cap_br_endpoint
 BREDR L2CAP Endpoint structure. More...
 
struct  bt_l2cap_br_chan
 BREDR L2CAP Channel structure. More...
 
struct  bt_l2cap_chan_ops
 L2CAP Channel operations structure. More...
 
struct  bt_l2cap_server
 L2CAP Server structure. More...
 

Macros

#define BT_L2CAP_HDR_SIZE   4
 
#define BT_L2CAP_TX_MTU   (CONFIG_BT_L2CAP_TX_MTU)
 
#define BT_L2CAP_RX_MTU   (CONFIG_BT_BUF_ACL_RX_SIZE - BT_L2CAP_HDR_SIZE)
 
#define BT_L2CAP_BUF_SIZE(mtu)   BT_BUF_ACL_SIZE(BT_L2CAP_HDR_SIZE + (mtu))
 Helper to calculate needed buffer size for L2CAP PDUs. Useful for creating buffer pools. More...
 
#define BT_L2CAP_SDU_HDR_SIZE   2
 
#define BT_L2CAP_SDU_TX_MTU   (BT_L2CAP_TX_MTU - BT_L2CAP_SDU_HDR_SIZE)
 Maximum Transmission Unit for an unsegmented outgoing L2CAP SDU. More...
 
#define BT_L2CAP_SDU_RX_MTU   (BT_L2CAP_RX_MTU - BT_L2CAP_SDU_HDR_SIZE)
 Maximum Transmission Unit for an unsegmented incoming L2CAP SDU. More...
 
#define BT_L2CAP_SDU_BUF_SIZE(mtu)   BT_L2CAP_BUF_SIZE(BT_L2CAP_SDU_HDR_SIZE + (mtu))
 Helper to calculate needed buffer size for L2CAP SDUs. Useful for creating buffer pools. More...
 
#define BT_L2CAP_LE_CHAN(_ch)   CONTAINER_OF(_ch, struct bt_l2cap_le_chan, chan)
 Helper macro getting container object of type bt_l2cap_le_chan address having the same container chan member address as object in question. More...
 
#define BT_L2CAP_CHAN_SEND_RESERVE   (BT_L2CAP_BUF_SIZE(0))
 Headroom needed for outgoing L2CAP PDUs. More...
 
#define BT_L2CAP_SDU_CHAN_SEND_RESERVE   (BT_L2CAP_SDU_BUF_SIZE(0))
 Headroom needed for outgoing L2CAP SDUs. More...
 

Typedefs

typedef void(* bt_l2cap_chan_destroy_t) (struct bt_l2cap_chan *chan)
 Channel destroy callback. More...
 
typedef enum bt_l2cap_chan_state bt_l2cap_chan_state_t
 Life-span states of L2CAP CoC channel. More...
 
typedef enum bt_l2cap_chan_status bt_l2cap_chan_status_t
 Status of L2CAP channel. More...
 

Enumerations

enum  bt_l2cap_chan_state {
  BT_L2CAP_DISCONNECTED , BT_L2CAP_CONNECT , BT_L2CAP_CONFIG , BT_L2CAP_CONNECTED ,
  BT_L2CAP_DISCONNECT
}
 Life-span states of L2CAP CoC channel. More...
 
enum  bt_l2cap_chan_status { BT_L2CAP_STATUS_OUT , BT_L2CAP_STATUS_SHUTDOWN , BT_L2CAP_STATUS_ENCRYPT_PENDING , BT_L2CAP_NUM_STATUS }
 Status of L2CAP channel. More...
 

Functions

int bt_l2cap_server_register (struct bt_l2cap_server *server)
 Register L2CAP server. More...
 
int bt_l2cap_br_server_register (struct bt_l2cap_server *server)
 Register L2CAP server on BR/EDR oriented connection. More...
 
int bt_l2cap_ecred_chan_connect (struct bt_conn *conn, struct bt_l2cap_chan **chans, uint16_t psm)
 Connect Enhanced Credit Based L2CAP channels. More...
 
int bt_l2cap_chan_connect (struct bt_conn *conn, struct bt_l2cap_chan *chan, uint16_t psm)
 Connect L2CAP channel. More...
 
int bt_l2cap_chan_disconnect (struct bt_l2cap_chan *chan)
 Disconnect L2CAP channel. More...
 
int bt_l2cap_chan_send (struct bt_l2cap_chan *chan, struct net_buf *buf)
 Send data to L2CAP channel. More...
 
int bt_l2cap_chan_recv_complete (struct bt_l2cap_chan *chan, struct net_buf *buf)
 Complete receiving L2CAP channel data. More...
 

Detailed Description

L2CAP.

Macro Definition Documentation

◆ BT_L2CAP_BUF_SIZE

#define BT_L2CAP_BUF_SIZE (   mtu)    BT_BUF_ACL_SIZE(BT_L2CAP_HDR_SIZE + (mtu))

#include <include/bluetooth/l2cap.h>

Helper to calculate needed buffer size for L2CAP PDUs. Useful for creating buffer pools.

Parameters
mtuNeeded L2CAP PDU MTU.
Returns
Needed buffer size to match the requested L2CAP PDU MTU.

◆ BT_L2CAP_CHAN_SEND_RESERVE

#define BT_L2CAP_CHAN_SEND_RESERVE   (BT_L2CAP_BUF_SIZE(0))

#include <include/bluetooth/l2cap.h>

Headroom needed for outgoing L2CAP PDUs.

◆ BT_L2CAP_HDR_SIZE

#define BT_L2CAP_HDR_SIZE   4

#include <include/bluetooth/l2cap.h>

L2CAP PDU header size, used for buffer size calculations

◆ BT_L2CAP_LE_CHAN

#define BT_L2CAP_LE_CHAN (   _ch)    CONTAINER_OF(_ch, struct bt_l2cap_le_chan, chan)

#include <include/bluetooth/l2cap.h>

Helper macro getting container object of type bt_l2cap_le_chan address having the same container chan member address as object in question.

Parameters
_chAddress of object of bt_l2cap_chan type
Returns
Address of in memory bt_l2cap_le_chan object type containing the address of in question object.

◆ BT_L2CAP_RX_MTU

#define BT_L2CAP_RX_MTU   (CONFIG_BT_BUF_ACL_RX_SIZE - BT_L2CAP_HDR_SIZE)

#include <include/bluetooth/l2cap.h>

Maximum Transmission Unit (MTU) for an incoming L2CAP PDU.

◆ BT_L2CAP_SDU_BUF_SIZE

#define BT_L2CAP_SDU_BUF_SIZE (   mtu)    BT_L2CAP_BUF_SIZE(BT_L2CAP_SDU_HDR_SIZE + (mtu))

#include <include/bluetooth/l2cap.h>

Helper to calculate needed buffer size for L2CAP SDUs. Useful for creating buffer pools.

Parameters
mtuRequired BT_L2CAP_*_SDU.
Returns
Needed buffer size to match the requested L2CAP SDU MTU.

◆ BT_L2CAP_SDU_CHAN_SEND_RESERVE

#define BT_L2CAP_SDU_CHAN_SEND_RESERVE   (BT_L2CAP_SDU_BUF_SIZE(0))

#include <include/bluetooth/l2cap.h>

Headroom needed for outgoing L2CAP SDUs.

◆ BT_L2CAP_SDU_HDR_SIZE

#define BT_L2CAP_SDU_HDR_SIZE   2

#include <include/bluetooth/l2cap.h>

L2CAP SDU header size, used for buffer size calculations

◆ BT_L2CAP_SDU_RX_MTU

#define BT_L2CAP_SDU_RX_MTU   (BT_L2CAP_RX_MTU - BT_L2CAP_SDU_HDR_SIZE)

#include <include/bluetooth/l2cap.h>

Maximum Transmission Unit for an unsegmented incoming L2CAP SDU.

The Maximum Transmission Unit for an incoming L2CAP SDU when sent without segmentation, i.e a single L2CAP SDU will fit inside a single L2CAP PDU.

The MTU for incoming L2CAP SDUs with segmentation is defined by the size of the application buffer pool. The application will have to define an alloc_buf callback for the channel in order to support receiving segmented L2CAP SDUs.

◆ BT_L2CAP_SDU_TX_MTU

#define BT_L2CAP_SDU_TX_MTU   (BT_L2CAP_TX_MTU - BT_L2CAP_SDU_HDR_SIZE)

#include <include/bluetooth/l2cap.h>

Maximum Transmission Unit for an unsegmented outgoing L2CAP SDU.

The Maximum Transmission Unit for an outgoing L2CAP SDU when sent without segmentation, i.e a single L2CAP SDU will fit inside a single L2CAP PDU.

The MTU for outgoing L2CAP SDUs with segmentation is defined by the size of the application buffer pool.

◆ BT_L2CAP_TX_MTU

#define BT_L2CAP_TX_MTU   (CONFIG_BT_L2CAP_TX_MTU)

#include <include/bluetooth/l2cap.h>

Maximum Transmission Unit (MTU) for an outgoing L2CAP PDU.

Typedef Documentation

◆ bt_l2cap_chan_destroy_t

bt_l2cap_chan_destroy_t

#include <include/bluetooth/l2cap.h>

Channel destroy callback.

Parameters
chanChannel object.

◆ bt_l2cap_chan_state_t

#include <include/bluetooth/l2cap.h>

Life-span states of L2CAP CoC channel.

Used only by internal APIs dealing with setting channel to proper state depending on operational context.

◆ bt_l2cap_chan_status_t

#include <include/bluetooth/l2cap.h>

Status of L2CAP channel.

Enumeration Type Documentation

◆ bt_l2cap_chan_state

#include <include/bluetooth/l2cap.h>

Life-span states of L2CAP CoC channel.

Used only by internal APIs dealing with setting channel to proper state depending on operational context.

Enumerator
BT_L2CAP_DISCONNECTED 

Channel disconnected

BT_L2CAP_CONNECT 

Channel in connecting state

BT_L2CAP_CONFIG 

Channel in config state, BR/EDR specific

BT_L2CAP_CONNECTED 

Channel ready for upper layer traffic on it

BT_L2CAP_DISCONNECT 

Channel in disconnecting state

◆ bt_l2cap_chan_status

#include <include/bluetooth/l2cap.h>

Status of L2CAP channel.

Enumerator
BT_L2CAP_STATUS_OUT 

Channel output status

BT_L2CAP_STATUS_SHUTDOWN 

Channel shutdown status.

   Once this status is notified it means the channel will no longer be
   able to transmit or receive data.
BT_L2CAP_STATUS_ENCRYPT_PENDING 

Channel encryption pending status.

BT_L2CAP_NUM_STATUS 

Function Documentation

◆ bt_l2cap_br_server_register()

int bt_l2cap_br_server_register ( struct bt_l2cap_server server)

#include <include/bluetooth/l2cap.h>

Register L2CAP server on BR/EDR oriented connection.

Register L2CAP server for a PSM, each new connection is authorized using the accept() callback which in case of success shall allocate the channel structure to be used by the new connection.

Parameters
serverServer structure.
Returns
0 in case of success or negative value in case of error.

◆ bt_l2cap_chan_connect()

int bt_l2cap_chan_connect ( struct bt_conn *  conn,
struct bt_l2cap_chan chan,
uint16_t  psm 
)

#include <include/bluetooth/l2cap.h>

Connect L2CAP channel.

Connect L2CAP channel by PSM, once the connection is completed channel connected() callback will be called. If the connection is rejected disconnected() callback is called instead. Channel object passed (over an address of it) as second parameter shouldn't be instantiated in application as standalone. Instead of, application should create transport dedicated L2CAP objects, i.e. type of bt_l2cap_le_chan for LE and/or type of bt_l2cap_br_chan for BR/EDR. Then pass to this API the location (address) of bt_l2cap_chan type object which is a member of both transport dedicated objects.

Parameters
connConnection object.
chanChannel object.
psmChannel PSM to connect to.
Returns
0 in case of success or negative value in case of error.

◆ bt_l2cap_chan_disconnect()

int bt_l2cap_chan_disconnect ( struct bt_l2cap_chan chan)

#include <include/bluetooth/l2cap.h>

Disconnect L2CAP channel.

Disconnect L2CAP channel, if the connection is pending it will be canceled and as a result the channel disconnected() callback is called. Regarding to input parameter, to get details see reference description to bt_l2cap_chan_connect() API above.

Parameters
chanChannel object.
Returns
0 in case of success or negative value in case of error.

◆ bt_l2cap_chan_recv_complete()

int bt_l2cap_chan_recv_complete ( struct bt_l2cap_chan chan,
struct net_buf buf 
)

#include <include/bluetooth/l2cap.h>

Complete receiving L2CAP channel data.

Complete the reception of incoming data. This shall only be called if the channel recv callback has returned -EINPROGRESS to process some incoming data. The buffer shall contain the original user_data as that is used for storing the credits/segments used by the packet.

Parameters
chanChannel object.
bufBuffer containing the data.
Returns
0 in case of success or negative value in case of error.

◆ bt_l2cap_chan_send()

int bt_l2cap_chan_send ( struct bt_l2cap_chan chan,
struct net_buf buf 
)

#include <include/bluetooth/l2cap.h>

Send data to L2CAP channel.

Send data from buffer to the channel. If credits are not available, buf will be queued and sent as and when credits are received from peer. Regarding to first input parameter, to get details see reference description to bt_l2cap_chan_connect() API above.

When sending L2CAP data over an BR/EDR connection the application is sending L2CAP PDUs. The application is required to have reserved BT_L2CAP_CHAN_SEND_RESERVE bytes in the buffer before sending. The application should use the BT_L2CAP_BUF_SIZE() helper to correctly size the buffers for the for the outgoing buffer pool.

When sending L2CAP data over an LE connection the applicatios is sending L2CAP SDUs. The application can optionally reserve BT_L2CAP_SDU_CHAN_SEND_RESERVE bytes in the buffer before sending. By reserving bytes in the buffer the stack can use this buffer as a segment directly, otherwise it will have to allocate a new segment for the first segment. If the application is reserving the bytes it should use the BT_L2CAP_BUF_SIZE() helper to correctly size the buffers for the for the outgoing buffer pool. When segmenting an L2CAP SDU into L2CAP PDUs the stack will first attempt to allocate buffers from the original buffer pool of the L2CAP SDU before using the stacks own buffer pool.

Note
Buffer ownership is transferred to the stack in case of success, in case of an error the caller retains the ownership of the buffer.
Returns
Bytes sent in case of success or negative value in case of error.

◆ bt_l2cap_ecred_chan_connect()

int bt_l2cap_ecred_chan_connect ( struct bt_conn *  conn,
struct bt_l2cap_chan **  chans,
uint16_t  psm 
)

#include <include/bluetooth/l2cap.h>

Connect Enhanced Credit Based L2CAP channels.

Connect up to 5 L2CAP channels by PSM, once the connection is completed each channel connected() callback will be called. If the connection is rejected disconnected() callback is called instead.

Parameters
connConnection object.
chansArray of channel objects.
psmChannel PSM to connect to.
Returns
0 in case of success or negative value in case of error.

◆ bt_l2cap_server_register()

int bt_l2cap_server_register ( struct bt_l2cap_server server)

#include <include/bluetooth/l2cap.h>

Register L2CAP server.

Register L2CAP server for a PSM, each new connection is authorized using the accept() callback which in case of success shall allocate the channel structure to be used by the new connection.

For fixed, SIG-assigned PSMs (in the range 0x0001-0x007f) the PSM should be assigned to server->psm before calling this API. For dynamic PSMs (in the range 0x0080-0x00ff) server->psm may be pre-set to a given value (this is however not recommended) or be left as 0, in which case upon return a newly allocated value will have been assigned to it. For dynamically allocated values the expectation is that it's exposed through a GATT service, and that's how L2CAP clients discover how to connect to the server.

Parameters
serverServer structure.
Returns
0 in case of success or negative value in case of error.