% !TEX root = btstack_gettingstarted.tex
\section{L2CAP API}
\label{appendix:api_l2cap}
$ $
\begin{lstlisting}
/** 
 * @brief Set up L2CAP and register L2CAP with HCI layer.
 */
void l2cap_init(void);

/** 
 * @brief Registers a packet handler that handles HCI and general BTstack events.
 */
void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size));

/** 
 * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
 */
void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm, uint16_t mtu);

/** 
 * @brief Disconnects L2CAP channel with given identifier. 
 */
void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason);

/** 
 * @brief Queries the maximal transfer unit (MTU) for L2CAP channel with given identifier. 
 */
uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid);

/** 
 * @brief Sends L2CAP data packet to the channel with given identifier.
 */
int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len);

/** 
 * @brief Registers L2CAP service with given PSM and MTU, and assigns a packet handler. On embedded systems, use NULL for connection parameter.
 */
void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level);

/** 
 * @brief Unregisters L2CAP service with given PSM.  On embedded systems, use NULL for connection parameter.
 */
void l2cap_unregister_service_internal(void *connection, uint16_t psm);

/** 
 * @brief Accepts/Deny incoming L2CAP connection.
 */
void l2cap_accept_connection_internal(uint16_t local_cid);
void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason);

/** 
 * @brief Request LE connection parameter update
 */
int l2cap_le_request_connection_parameter_update(uint16_t handle, uint16_t interval_min, uint16_t interval_max, uint16_t slave_latency, uint16_t timeout_multiplier);

/** 
 * @brief Non-blocking UART write
 */
int  l2cap_can_send_packet_now(uint16_t local_cid);    
int  l2cap_reserve_packet_buffer(void);
void l2cap_release_packet_buffer(void);

/** 
 * @brief Get outgoing buffer and prepare data.
 */
uint8_t *l2cap_get_outgoing_buffer(void);

int l2cap_send_prepared(uint16_t local_cid, uint16_t len);

int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len);

/** 
 * @brief Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol.
 */
void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id);

uint16_t l2cap_max_mtu(void);
uint16_t l2cap_max_le_mtu(void);

int  l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len);
\end{lstlisting}
\pagebreak
