Microchip® Advanced Software Framework

l2cap.h File Reference
#include "hci.h"
#include "l2cap_signaling.h"
#include "btstack_util.h"
#include "bluetooth.h"

Data Structures

struct  l2cap_channel_t
 
struct  l2cap_ertm_config_t
 
struct  l2cap_ertm_rx_packet_state_t
 
struct  l2cap_ertm_tx_packet_state_t
 
struct  l2cap_service_t
 
struct  l2cap_signaling_response
 

Macros

#define L2CAP_LE_AUTOMATIC_CREDITS   0xffff
 

Typedefs

typedef struct
l2cap_signaling_response 
l2cap_signaling_response_t
 

Enumerations

enum  L2CAP_CHANNEL_STATE_VAR {
  L2CAP_CHANNEL_STATE_VAR_NONE = 0,
  L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ = 1 << 0,
  L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP = 1 << 1,
  L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ = 1 << 2,
  L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP = 1 << 3,
  L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ = 1 << 4,
  L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP = 1 << 5,
  L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU = 1 << 6,
  L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT = 1 << 7,
  L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID = 1 << 8,
  L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_REJECTED = 1 << 9,
  L2CAP_CHANNEL_STATE_VAR_BASIC_FALLBACK_TRIED = 1 << 10,
  L2CAP_CHANNEL_STATE_VAR_SEND_CMD_REJ_UNKNOWN = 1 << 11,
  L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND = 1 << 12,
  L2CAP_CHANNEL_STATE_VAR_INCOMING = 1 << 15
}
 
enum  L2CAP_STATE {
  L2CAP_STATE_CLOSED = 1,
  L2CAP_STATE_WILL_SEND_CREATE_CONNECTION,
  L2CAP_STATE_WAIT_CONNECTION_COMPLETE,
  L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES,
  L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE,
  L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE,
  L2CAP_STATE_WAIT_INCOMING_EXTENDED_FEATURES,
  L2CAP_STATE_WAIT_OUTGOING_EXTENDED_FEATURES,
  L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT,
  L2CAP_STATE_WAIT_CONNECT_RSP,
  L2CAP_STATE_CONFIG,
  L2CAP_STATE_OPEN,
  L2CAP_STATE_WAIT_DISCONNECT,
  L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST,
  L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_INSUFFICIENT_SECURITY,
  L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE,
  L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT,
  L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST,
  L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE,
  L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST,
  L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE,
  L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT,
  L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE,
  L2CAP_STATE_INVALID
}
 

Functions

void l2cap_accept_connection (uint16_t local_cid)
 Accepts incoming L2CAP connection. More...
 
uint8_t l2cap_accept_ertm_connection (uint16_t local_cid, l2cap_ertm_config_t *ertm_contig, uint8_t *buffer, uint32_t size)
 Accepts incoming L2CAP connection for Enhanced Retransmission Mode. More...
 
int l2cap_can_send_fixed_channel_packet_now (hci_con_handle_t con_handle, uint16_t channel_id)
 
int l2cap_can_send_packet_now (uint16_t local_cid)
 Check if outgoing buffer is available and that there's space on the Bluetooth module. More...
 
int l2cap_can_send_prepared_packet_now (uint16_t local_cid)
 
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)
 Creates L2CAP channel to the PSM of a remote device with baseband address. More...
 
uint8_t l2cap_create_ertm_channel (btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm, l2cap_ertm_config_t *ertm_contig, uint8_t *buffer, uint32_t size, uint16_t *out_local_cid)
 Creates L2CAP channel to the PSM of a remote device with baseband address using Enhanced Retransmission Mode. More...
 
void l2cap_decline_connection (uint16_t local_cid)
 Deny incoming L2CAP connection. More...
 
void l2cap_disconnect (uint16_t local_cid, uint8_t reason)
 Disconnects L2CAP channel with given identifier. More...
 
uint8_t l2cap_ertm_set_busy (uint16_t local_cid)
 ERTM Set channel as busy. More...
 
uint8_t l2cap_ertm_set_ready (uint16_t local_cid)
 ERTM Set channel as ready. More...
 
uint8_t * l2cap_get_outgoing_buffer (void)
 Get outgoing buffer and prepare data. More...
 
uint16_t l2cap_get_remote_mtu_for_local_cid (uint16_t local_cid)
 Queries the maximal transfer unit (MTU) for L2CAP channel with given identifier. More...
 
void l2cap_init (void)
 Set up L2CAP and register L2CAP with HCI layer. More...
 
uint8_t l2cap_le_accept_connection (uint16_t local_cid, uint8_t *receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits)
 
int l2cap_le_can_send_now (uint16_t cid)
 Check if packet can be scheduled for transmission. More...
 
uint8_t l2cap_le_create_channel (btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle, uint16_t psm, uint8_t *receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level, uint16_t *out_local_cid)
 Create LE Data Channel. More...
 
uint8_t l2cap_le_decline_connection (uint16_t local_cid)
 Deny incoming LE Data Channel connection due to resource constraints. More...
 
uint8_t l2cap_le_disconnect (uint16_t cid)
 Disconnect from LE Data Channel. More...
 
uint8_t l2cap_le_provide_credits (uint16_t cid, uint16_t credits)
 Provide credtis for LE Data Channel. More...
 
uint8_t l2cap_le_register_service (btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level)
 Register L2CAP LE Data Channel service. More...
 
uint8_t l2cap_le_request_can_send_now_event (uint16_t cid)
 Request emission of L2CAP_EVENT_LE_CAN_SEND_NOW as soon as possible. More...
 
uint8_t l2cap_le_send_data (uint16_t cid, uint8_t *data, uint16_t size)
 Send data via LE Data Channel. More...
 
uint8_t l2cap_le_unregister_service (uint16_t psm)
 Unregister L2CAP LE Data Channel service. More...
 
uint16_t l2cap_max_le_mtu (void)
 Get max MTU for LE connections based on btstack configuration. More...
 
uint16_t l2cap_max_mtu (void)
 Get max MTU for Classic connections based on btstack configuration. More...
 
void l2cap_register_fixed_channel (btstack_packet_handler_t packet_handler, uint16_t channel_id)
 
void l2cap_register_packet_handler (void(*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size))
 Registers packet handler for LE Connection Parameter Update events. More...
 
uint8_t l2cap_register_service (btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level)
 Registers L2CAP service with given PSM and MTU, and assigns a packet handler. More...
 
void l2cap_release_packet_buffer (void)
 Release outgoing buffer (only needed if l2cap_send_prepared is not called) More...
 
void l2cap_request_can_send_fix_channel_now_event (hci_con_handle_t con_handle, uint16_t channel_id)
 
void l2cap_request_can_send_now_event (uint16_t local_cid)
 Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible. More...
 
void l2cap_require_security_level_2_for_outgoing_sdp (void)
 
int l2cap_reserve_packet_buffer (void)
 Reserve outgoing buffer. More...
 
int l2cap_send (uint16_t local_cid, uint8_t *data, uint16_t len)
 Sends L2CAP data packet to the channel with given identifier. More...
 
int l2cap_send_connectionless (hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len)
 
int l2cap_send_echo_request (hci_con_handle_t con_handle, uint8_t *data, uint16_t len)
 
int l2cap_send_prepared (uint16_t local_cid, uint16_t len)
 Send L2CAP packet prepared in outgoing buffer to channel. More...
 
int l2cap_send_prepared_connectionless (hci_con_handle_t con_handle, uint16_t cid, uint16_t len)
 
uint8_t l2cap_unregister_service (uint16_t psm)
 Unregisters L2CAP service with given PSM. More...
 

#define L2CAP_LE_AUTOMATIC_CREDITS   0xffff

Enumerator
L2CAP_CHANNEL_STATE_VAR_NONE 
L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ 
L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP 
L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ 
L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP 
L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ 
L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP 
L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU 
L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT 
L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID 
L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_REJECTED 
L2CAP_CHANNEL_STATE_VAR_BASIC_FALLBACK_TRIED 
L2CAP_CHANNEL_STATE_VAR_SEND_CMD_REJ_UNKNOWN 
L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND 
L2CAP_CHANNEL_STATE_VAR_INCOMING 
Enumerator
L2CAP_STATE_CLOSED 
L2CAP_STATE_WILL_SEND_CREATE_CONNECTION 
L2CAP_STATE_WAIT_CONNECTION_COMPLETE 
L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES 
L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE 
L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE 
L2CAP_STATE_WAIT_INCOMING_EXTENDED_FEATURES 
L2CAP_STATE_WAIT_OUTGOING_EXTENDED_FEATURES 
L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT 
L2CAP_STATE_WAIT_CONNECT_RSP 
L2CAP_STATE_CONFIG 
L2CAP_STATE_OPEN 
L2CAP_STATE_WAIT_DISCONNECT 
L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST 
L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_INSUFFICIENT_SECURITY 
L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE 
L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT 
L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST 
L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE 
L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST 
L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE 
L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT 
L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE 
L2CAP_STATE_INVALID 

void l2cap_accept_connection ( uint16_t  local_cid)

Accepts incoming L2CAP connection.

uint8_t l2cap_accept_ertm_connection ( uint16_t  local_cid,
l2cap_ertm_config_t ertm_contig,
uint8_t *  buffer,
uint32_t  size 
)

Accepts incoming L2CAP connection for Enhanced Retransmission Mode.

Parameters
local_cid
ertm_config
bufferto store reassembled rx packet, out-of-order packets and unacknowledged outgoing packets with their tretransmission timers
sizeof buffer
Returns
status
int l2cap_can_send_fixed_channel_packet_now ( hci_con_handle_t  con_handle,
uint16_t  channel_id 
)
int l2cap_can_send_packet_now ( uint16_t  local_cid)

Check if outgoing buffer is available and that there's space on the Bluetooth module.

int l2cap_can_send_prepared_packet_now ( uint16_t  local_cid)
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 
)

Creates L2CAP channel to the PSM of a remote device with baseband address.

A new baseband connection will be initiated if necessary.

Parameters
packet_handler
address
psm
mtu
local_cid
Returns
status
uint8_t l2cap_create_ertm_channel ( btstack_packet_handler_t  packet_handler,
bd_addr_t  address,
uint16_t  psm,
l2cap_ertm_config_t ertm_contig,
uint8_t *  buffer,
uint32_t  size,
uint16_t *  out_local_cid 
)

Creates L2CAP channel to the PSM of a remote device with baseband address using Enhanced Retransmission Mode.

   A new baseband connection will be initiated if necessary.
Parameters
packet_handler
address
psm
ertm_config
bufferto store reassembled rx packet, out-of-order packets and unacknowledged outgoing packets with their tretransmission timers
sizeof buffer
local_cid
Returns
status
void l2cap_decline_connection ( uint16_t  local_cid)

Deny incoming L2CAP connection.

void l2cap_disconnect ( uint16_t  local_cid,
uint8_t  reason 
)

Disconnects L2CAP channel with given identifier.

uint8_t l2cap_ertm_set_busy ( uint16_t  local_cid)

ERTM Set channel as busy.

Note
Can be cleared by l2cap_ertm_set_ready
Parameters
local_cid
uint8_t l2cap_ertm_set_ready ( uint16_t  local_cid)

ERTM Set channel as ready.

Note
Used after l2cap_ertm_set_busy
Parameters
local_cid
uint16_t l2cap_get_remote_mtu_for_local_cid ( uint16_t  local_cid)

Queries the maximal transfer unit (MTU) for L2CAP channel with given identifier.

uint8_t l2cap_le_accept_connection ( uint16_t  local_cid,
uint8_t *  receive_sdu_buffer,
uint16_t  mtu,
uint16_t  initial_credits 
)
int l2cap_le_can_send_now ( uint16_t  cid)

Check if packet can be scheduled for transmission.

Parameters
local_cidL2CAP LE Data Channel Identifier
uint8_t l2cap_le_create_channel ( btstack_packet_handler_t  packet_handler,
hci_con_handle_t  con_handle,
uint16_t  psm,
uint8_t *  receive_sdu_buffer,
uint16_t  mtu,
uint16_t  initial_credits,
gap_security_level_t  security_level,
uint16_t *  out_local_cid 
)

Create LE Data Channel.

Parameters
packet_handlerPacket handler for this connection
con_handleACL-LE HCI Connction Handle
psmService PSM to connect to
receive_bufferbuffer used for reassembly of L2CAP LE Information Frames into service data unit (SDU) with given MTU
receive_buffer_sizebuffer size equals MTU
initial_creditsNumber of initial credits provided to peer or L2CAP_LE_AUTOMATIC_CREDITS to enable automatic credits
security_levelMinimum required security level
out_local_cidL2CAP LE Channel Identifier is stored here
uint8_t l2cap_le_decline_connection ( uint16_t  local_cid)

Deny incoming LE Data Channel connection due to resource constraints.

Parameters
local_cidL2CAP LE Data Channel Identifier
uint8_t l2cap_le_disconnect ( uint16_t  cid)

Disconnect from LE Data Channel.

Parameters
local_cidL2CAP LE Data Channel Identifier
uint8_t l2cap_le_provide_credits ( uint16_t  cid,
uint16_t  credits 
)

Provide credtis for LE Data Channel.

Parameters
local_cidL2CAP LE Data Channel Identifier
creditsNumber additional credits for peer
uint8_t l2cap_le_register_service ( btstack_packet_handler_t  packet_handler,
uint16_t  psm,
gap_security_level_t  security_level 
)

Register L2CAP LE Data Channel service.

Note
MTU and initial credits are specified in l2cap_le_accept_connection(..) call
Parameters
packet_handler
psm
security_level
uint8_t l2cap_le_request_can_send_now_event ( uint16_t  cid)

Request emission of L2CAP_EVENT_LE_CAN_SEND_NOW as soon as possible.

Note
L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function so packet handler should be ready to handle it
Parameters
local_cidL2CAP LE Data Channel Identifier
uint8_t l2cap_le_send_data ( uint16_t  cid,
uint8_t *  data,
uint16_t  size 
)

Send data via LE Data Channel.

Note
Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event
Parameters
local_cidL2CAP LE Data Channel Identifier
datadata to send
sizedata size
uint8_t l2cap_le_unregister_service ( uint16_t  psm)

Unregister L2CAP LE Data Channel service.

Parameters
psm
uint16_t l2cap_max_le_mtu ( void  )

Get max MTU for LE connections based on btstack configuration.

References l2cap_max_mtu().

Referenced by att_event_packet_handler(), att_exchange_mtu_request(), gatt_client_att_packet_handler(), and peripheral_mtu().

uint16_t l2cap_max_mtu ( void  )

Get max MTU for Classic connections based on btstack configuration.

References L2CAP_HEADER_SIZE.

Referenced by l2cap_max_le_mtu().

void l2cap_register_fixed_channel ( btstack_packet_handler_t  packet_handler,
uint16_t  channel_id 
)
void l2cap_register_packet_handler ( void(*)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)  handler)

Registers packet handler for LE Connection Parameter Update events.

References UNUSED.

uint8_t l2cap_register_service ( btstack_packet_handler_t  packet_handler,
uint16_t  psm,
uint16_t  mtu,
gap_security_level_t  security_level 
)

Registers L2CAP service with given PSM and MTU, and assigns a packet handler.

void l2cap_release_packet_buffer ( void  )

Release outgoing buffer (only needed if l2cap_send_prepared is not called)

References hci_release_packet_buffer().

Referenced by att_server_process_validated_request().

void l2cap_request_can_send_now_event ( uint16_t  local_cid)

Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible.

Note
L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function so packet handler should be ready to handle it
Parameters
local_cid
void l2cap_require_security_level_2_for_outgoing_sdp ( void  )
int l2cap_send ( uint16_t  local_cid,
uint8_t *  data,
uint16_t  len 
)

Sends L2CAP data packet to the channel with given identifier.

int l2cap_send_connectionless ( hci_con_handle_t  con_handle,
uint16_t  cid,
uint8_t *  data,
uint16_t  len 
)
int l2cap_send_echo_request ( hci_con_handle_t  con_handle,
uint8_t *  data,
uint16_t  len 
)
int l2cap_send_prepared ( uint16_t  local_cid,
uint16_t  len 
)

Send L2CAP packet prepared in outgoing buffer to channel.

uint8_t l2cap_unregister_service ( uint16_t  psm)

Unregisters L2CAP service with given PSM.