xref: /btstack/src/l2cap.h (revision ffbf82013e3f511bf1169fb051dfccdcd45664b0)
143625864Smatthias.ringwald /*
2a0c35809S[email protected]  * Copyright (C) 2014 BlueKitchen GmbH
31713bceaSmatthias.ringwald  *
41713bceaSmatthias.ringwald  * Redistribution and use in source and binary forms, with or without
51713bceaSmatthias.ringwald  * modification, are permitted provided that the following conditions
61713bceaSmatthias.ringwald  * are met:
71713bceaSmatthias.ringwald  *
81713bceaSmatthias.ringwald  * 1. Redistributions of source code must retain the above copyright
91713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer.
101713bceaSmatthias.ringwald  * 2. Redistributions in binary form must reproduce the above copyright
111713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer in the
121713bceaSmatthias.ringwald  *    documentation and/or other materials provided with the distribution.
131713bceaSmatthias.ringwald  * 3. Neither the name of the copyright holders nor the names of
141713bceaSmatthias.ringwald  *    contributors may be used to endorse or promote products derived
151713bceaSmatthias.ringwald  *    from this software without specific prior written permission.
166b64433eSmatthias.ringwald  * 4. Any redistribution, use, or modification is done solely for
176b64433eSmatthias.ringwald  *    personal benefit and not for any commercial purpose or for
186b64433eSmatthias.ringwald  *    monetary gain.
191713bceaSmatthias.ringwald  *
20a0c35809S[email protected]  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
211713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
231713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
241713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
261713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
271713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
301713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311713bceaSmatthias.ringwald  * SUCH DAMAGE.
321713bceaSmatthias.ringwald  *
33a0c35809S[email protected]  * Please inquire about commercial licensing options at
34a0c35809S[email protected]  * [email protected]
356b64433eSmatthias.ringwald  *
361713bceaSmatthias.ringwald  */
371713bceaSmatthias.ringwald 
381713bceaSmatthias.ringwald /*
3943625864Smatthias.ringwald  *  l2cap.h
4043625864Smatthias.ringwald  *
413de95307SMilanka Ringwald  *  Logical Link Control and Adaption Protocol (L2CAP)
4243625864Smatthias.ringwald  *
4343625864Smatthias.ringwald  *  Created by Matthias Ringwald on 5/16/09.
4443625864Smatthias.ringwald  */
4543625864Smatthias.ringwald 
46f471afd8S[email protected] #ifndef __L2CAP_H
47f471afd8S[email protected] #define __L2CAP_H
48fd7ed8d4Smatthias.ringwald 
4943625864Smatthias.ringwald #include "hci.h"
5095cbd947Smatthias.ringwald #include "l2cap_signaling.h"
51195a8028SMatthias Ringwald #include "utils.h"
52da269baaSmatthias.ringwald 
53e3b6fe7eSmatthias.ringwald #if defined __cplusplus
54e3b6fe7eSmatthias.ringwald extern "C" {
55e3b6fe7eSmatthias.ringwald #endif
56e3b6fe7eSmatthias.ringwald 
57da269baaSmatthias.ringwald #define L2CAP_SIG_ID_INVALID 0
58da269baaSmatthias.ringwald 
595af02935Smatthias.ringwald #define L2CAP_HEADER_SIZE 4
605af02935Smatthias.ringwald 
6123f5e709Smatthias.ringwald // size of HCI ACL + L2CAP Header for regular data packets (8)
6223f5e709Smatthias.ringwald #define COMPLETE_L2CAP_HEADER (HCI_ACL_HEADER_SIZE + L2CAP_HEADER_SIZE)
6323f5e709Smatthias.ringwald 
6423f5e709Smatthias.ringwald // minimum signaling MTU
6523f5e709Smatthias.ringwald #define L2CAP_MINIMAL_MTU 48
6623f5e709Smatthias.ringwald #define L2CAP_DEFAULT_MTU 672
6723f5e709Smatthias.ringwald 
68e5e1518dS[email protected] // Minimum/default MTU
69e5e1518dS[email protected] #define L2CAP_LE_DEFAULT_MTU  23
70e5e1518dS[email protected] 
7123f5e709Smatthias.ringwald // check L2CAP MTU
72db9ab65bSmatthias.ringwald #if (L2CAP_MINIMAL_MTU + L2CAP_HEADER_SIZE) > HCI_ACL_PAYLOAD_SIZE
73db9ab65bSmatthias.ringwald #error "HCI_ACL_PAYLOAD_SIZE too small for minimal L2CAP MTU of 48 bytes"
7423f5e709Smatthias.ringwald #endif
7523f5e709Smatthias.ringwald 
765652b5ffS[email protected] // L2CAP Fixed Channel IDs
775652b5ffS[email protected] #define L2CAP_CID_SIGNALING                 0x0001
785652b5ffS[email protected] #define L2CAP_CID_CONNECTIONLESS_CHANNEL    0x0002
795652b5ffS[email protected] #define L2CAP_CID_ATTRIBUTE_PROTOCOL        0x0004
805652b5ffS[email protected] #define L2CAP_CID_SIGNALING_LE              0x0005
815652b5ffS[email protected] #define L2CAP_CID_SECURITY_MANAGER_PROTOCOL 0x0006
825652b5ffS[email protected] 
8363a7246aSmatthias.ringwald // L2CAP Configuration Result Codes
8463a7246aSmatthias.ringwald #define L2CAP_CONF_RESULT_UNKNOWN_OPTIONS   0x0003
8563a7246aSmatthias.ringwald 
8663a7246aSmatthias.ringwald // L2CAP Reject Result Codes
8763a7246aSmatthias.ringwald #define L2CAP_REJ_CMD_UNKNOWN               0x0000
8863a7246aSmatthias.ringwald 
895932bd7cS[email protected] // Response Timeout eXpired
90f540f3afS[email protected] #define L2CAP_RTX_TIMEOUT_MS   10000
915932bd7cS[email protected] 
925932bd7cS[email protected] // Extended Response Timeout eXpired
935932bd7cS[email protected] #define L2CAP_ERTX_TIMEOUT_MS 120000
945932bd7cS[email protected] 
9573cf2b3dSmatthias.ringwald // private structs
9673cf2b3dSmatthias.ringwald typedef enum {
9773cf2b3dSmatthias.ringwald     L2CAP_STATE_CLOSED = 1,           // no baseband
9873cf2b3dSmatthias.ringwald     L2CAP_STATE_WILL_SEND_CREATE_CONNECTION,
9973cf2b3dSmatthias.ringwald     L2CAP_STATE_WAIT_CONNECTION_COMPLETE,
100df3354fcS[email protected]     L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES,
101df3354fcS[email protected]     L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE,
102df3354fcS[email protected]     L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE,
10373cf2b3dSmatthias.ringwald     L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT,
10473cf2b3dSmatthias.ringwald     L2CAP_STATE_WAIT_CONNECT_RSP, // from peer
10573cf2b3dSmatthias.ringwald     L2CAP_STATE_CONFIG,
10673cf2b3dSmatthias.ringwald     L2CAP_STATE_OPEN,
10773cf2b3dSmatthias.ringwald     L2CAP_STATE_WAIT_DISCONNECT,  // from application
10873cf2b3dSmatthias.ringwald     L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST,
109f85a9399S[email protected]     L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_INSUFFICIENT_SECURITY,
11073cf2b3dSmatthias.ringwald     L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE,
11173cf2b3dSmatthias.ringwald     L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT,
11273cf2b3dSmatthias.ringwald     L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST,
11373cf2b3dSmatthias.ringwald     L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE,
11422c29ab4SMatthias Ringwald     L2CAP_STATE_INVALID,
11573cf2b3dSmatthias.ringwald } L2CAP_STATE;
11673cf2b3dSmatthias.ringwald 
11773cf2b3dSmatthias.ringwald typedef enum {
11873cf2b3dSmatthias.ringwald     L2CAP_CHANNEL_STATE_VAR_NONE                  = 0,
11973cf2b3dSmatthias.ringwald     L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ         = 1 << 0,
12073cf2b3dSmatthias.ringwald     L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP         = 1 << 1,
12173cf2b3dSmatthias.ringwald     L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ         = 1 << 2,
12273cf2b3dSmatthias.ringwald     L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP         = 1 << 3,
12373cf2b3dSmatthias.ringwald     L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ         = 1 << 4,
12473cf2b3dSmatthias.ringwald     L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP         = 1 << 5,
12563a7246aSmatthias.ringwald     L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU     = 1 << 6,   // in CONF RSP, add MTU field
12663a7246aSmatthias.ringwald     L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT    = 1 << 7,   // in CONF RSP, set CONTINUE flag
12763a7246aSmatthias.ringwald     L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID = 1 << 8,   // in CONF RSP, send UNKNOWN OPTIONS
12863a7246aSmatthias.ringwald     L2CAP_CHANNEL_STATE_VAR_SEND_CMD_REJ_UNKNOWN  = 1 << 9,   // send CMD_REJ with reason unknown
129ad671560S[email protected]     L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND   = 1 << 10,  // send Connection Respond with pending
13073cf2b3dSmatthias.ringwald } L2CAP_CHANNEL_STATE_VAR;
13173cf2b3dSmatthias.ringwald 
1323de95307SMilanka Ringwald // info regarding an actual connection
13373cf2b3dSmatthias.ringwald typedef struct {
13473cf2b3dSmatthias.ringwald     // linked list - assert: first field
13573cf2b3dSmatthias.ringwald     linked_item_t    item;
13673cf2b3dSmatthias.ringwald 
13773cf2b3dSmatthias.ringwald     L2CAP_STATE state;
13873cf2b3dSmatthias.ringwald     L2CAP_CHANNEL_STATE_VAR state_var;
13973cf2b3dSmatthias.ringwald 
14073cf2b3dSmatthias.ringwald     bd_addr_t address;
14173cf2b3dSmatthias.ringwald     hci_con_handle_t handle;
14273cf2b3dSmatthias.ringwald 
14373cf2b3dSmatthias.ringwald     uint8_t   remote_sig_id;    // used by other side, needed for delayed response
14473cf2b3dSmatthias.ringwald     uint8_t   local_sig_id;     // own signaling identifier
14573cf2b3dSmatthias.ringwald 
14673cf2b3dSmatthias.ringwald     uint16_t  local_cid;
14773cf2b3dSmatthias.ringwald     uint16_t  remote_cid;
14873cf2b3dSmatthias.ringwald 
14973cf2b3dSmatthias.ringwald     uint16_t  local_mtu;
15073cf2b3dSmatthias.ringwald     uint16_t  remote_mtu;
15173cf2b3dSmatthias.ringwald 
152c9dc710bS[email protected]     uint16_t  flush_timeout;    // default 0xffff
153c9dc710bS[email protected] 
15473cf2b3dSmatthias.ringwald     uint16_t  psm;
15573cf2b3dSmatthias.ringwald 
1561eb2563eS[email protected]     gap_security_level_t required_security_level;
1571eb2563eS[email protected] 
15873cf2b3dSmatthias.ringwald     uint8_t   packets_granted;    // number of L2CAP/ACL packets client is allowed to send
15973cf2b3dSmatthias.ringwald 
16073cf2b3dSmatthias.ringwald     uint8_t   reason; // used in decline internal
16173cf2b3dSmatthias.ringwald 
162a4774f9aS[email protected]     timer_source_t rtx; // also used for ertx
163a4774f9aS[email protected] 
16473cf2b3dSmatthias.ringwald     // internal connection
16573cf2b3dSmatthias.ringwald     btstack_packet_handler_t packet_handler;
16673cf2b3dSmatthias.ringwald 
16773cf2b3dSmatthias.ringwald } l2cap_channel_t;
16873cf2b3dSmatthias.ringwald 
16973cf2b3dSmatthias.ringwald // info regarding potential connections
17073cf2b3dSmatthias.ringwald typedef struct {
17173cf2b3dSmatthias.ringwald     // linked list - assert: first field
17273cf2b3dSmatthias.ringwald     linked_item_t    item;
17373cf2b3dSmatthias.ringwald 
17473cf2b3dSmatthias.ringwald     // service id
17573cf2b3dSmatthias.ringwald     uint16_t  psm;
17673cf2b3dSmatthias.ringwald 
17773cf2b3dSmatthias.ringwald     // incoming MTU
17873cf2b3dSmatthias.ringwald     uint16_t mtu;
17973cf2b3dSmatthias.ringwald 
1807192e786SMatthias Ringwald     // incoming MPS
1817192e786SMatthias Ringwald     uint16_t mps;
1827192e786SMatthias Ringwald 
18373cf2b3dSmatthias.ringwald     // internal connection
18473cf2b3dSmatthias.ringwald     btstack_packet_handler_t packet_handler;
18573cf2b3dSmatthias.ringwald 
186ad671560S[email protected]     // required security level
187df3354fcS[email protected]     gap_security_level_t required_security_level;
18873cf2b3dSmatthias.ringwald } l2cap_service_t;
18973cf2b3dSmatthias.ringwald 
19073cf2b3dSmatthias.ringwald 
19173cf2b3dSmatthias.ringwald typedef struct l2cap_signaling_response {
19273cf2b3dSmatthias.ringwald     hci_con_handle_t handle;
19373cf2b3dSmatthias.ringwald     uint8_t  sig_id;
19473cf2b3dSmatthias.ringwald     uint8_t  code;
19563a7246aSmatthias.ringwald     uint16_t data; // infoType for INFORMATION REQUEST, result for CONNECTION request and command unknown
19673cf2b3dSmatthias.ringwald } l2cap_signaling_response_t;
19773cf2b3dSmatthias.ringwald 
19873cf2b3dSmatthias.ringwald 
199e6cc8be2S[email protected] void l2cap_block_new_credits(uint8_t blocked);
2007856fb31S[email protected] 
2016cd4da6bS[email protected] int  l2cap_can_send_fixed_channel_packet_now(uint16_t handle);
2026cd4da6bS[email protected] 
2036cd4da6bS[email protected] // @deprecated use l2cap_can_send_fixed_channel_packet_now instead
2043cab4fcaS[email protected] int  l2cap_can_send_connectionless_packet_now(void);
2053cab4fcaS[email protected] 
2060e37e417S[email protected] int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len);
207e6cc8be2S[email protected] 
2085e6d58c4SMilanka Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void);  // for PTS testing only
209ac301f95S[email protected] 
2105e6d58c4SMilanka Ringwald /* API_START */
211e6cc8be2S[email protected] 
2123de95307SMilanka Ringwald /**
2133de95307SMilanka Ringwald  * @brief Set up L2CAP and register L2CAP with HCI layer.
2143de95307SMilanka Ringwald  */
215e6cc8be2S[email protected] void l2cap_init(void);
216e6cc8be2S[email protected] 
2173de95307SMilanka Ringwald /**
2183de95307SMilanka Ringwald  * @brief Registers a packet handler that handles HCI and general BTstack events.
2193de95307SMilanka Ringwald  */
220*ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size));
221e6cc8be2S[email protected] 
2223de95307SMilanka Ringwald /**
2233de95307SMilanka Ringwald  * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
2249077cb15SMatthias Ringwald  * @param packet_handler
2259077cb15SMatthias Ringwald  * @param address
2269077cb15SMatthias Ringwald  * @param psm
2279077cb15SMatthias Ringwald  * @param mtu
2289077cb15SMatthias Ringwald  * @param local_cid
2299077cb15SMatthias Ringwald  * @param status
2303de95307SMilanka Ringwald  */
2319077cb15SMatthias Ringwald 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);
232e6cc8be2S[email protected] 
2333de95307SMilanka Ringwald /**
2343de95307SMilanka Ringwald  * @brief Disconnects L2CAP channel with given identifier.
2353de95307SMilanka Ringwald  */
236e6cc8be2S[email protected] void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason);
237e6cc8be2S[email protected] 
2383de95307SMilanka Ringwald /**
2393de95307SMilanka Ringwald  * @brief Queries the maximal transfer unit (MTU) for L2CAP channel with given identifier.
2403de95307SMilanka Ringwald  */
241e6cc8be2S[email protected] uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid);
242e6cc8be2S[email protected] 
2433de95307SMilanka Ringwald /**
2443de95307SMilanka Ringwald  * @brief Sends L2CAP data packet to the channel with given identifier.
2453de95307SMilanka Ringwald  */
246e6cc8be2S[email protected] int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len);
247e6cc8be2S[email protected] 
2483de95307SMilanka Ringwald /**
249be2053a6SMatthias Ringwald  * @brief Registers L2CAP service with given PSM and MTU, and assigns a packet handler.
2503de95307SMilanka Ringwald  */
251be2053a6SMatthias Ringwald uint8_t l2cap_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level);
252e6cc8be2S[email protected] 
2533de95307SMilanka Ringwald /**
2543de95307SMilanka Ringwald  * @brief Unregisters L2CAP service with given PSM.  On embedded systems, use NULL for connection parameter.
2553de95307SMilanka Ringwald  */
2569077cb15SMatthias Ringwald void l2cap_unregister_service(uint16_t psm);
257e6cc8be2S[email protected] 
2583de95307SMilanka Ringwald /**
2593de95307SMilanka Ringwald  * @brief Accepts/Deny incoming L2CAP connection.
2603de95307SMilanka Ringwald  */
261e6cc8be2S[email protected] void l2cap_accept_connection_internal(uint16_t local_cid);
262e6cc8be2S[email protected] void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason);
263e6cc8be2S[email protected] 
2643de95307SMilanka Ringwald /**
2653de95307SMilanka Ringwald  * @brief Non-blocking UART write
2663de95307SMilanka Ringwald  */
2675e6d58c4SMilanka Ringwald int  l2cap_can_send_packet_now(uint16_t local_cid);
2685e6d58c4SMilanka Ringwald int  l2cap_reserve_packet_buffer(void);
2695e6d58c4SMilanka Ringwald void l2cap_release_packet_buffer(void);
2705e6d58c4SMilanka Ringwald 
2713de95307SMilanka Ringwald /**
2723de95307SMilanka Ringwald  * @brief Get outgoing buffer and prepare data.
2733de95307SMilanka Ringwald  */
2745e6d58c4SMilanka Ringwald uint8_t *l2cap_get_outgoing_buffer(void);
2755e6d58c4SMilanka Ringwald 
2765e6d58c4SMilanka Ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len);
2775e6d58c4SMilanka Ringwald 
2785e6d58c4SMilanka Ringwald int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len);
2795e6d58c4SMilanka Ringwald 
2803de95307SMilanka Ringwald /**
2813de95307SMilanka Ringwald  * @brief Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol.
2823de95307SMilanka Ringwald  */
2835e6d58c4SMilanka Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id);
2845e6d58c4SMilanka Ringwald 
285ea392f51SMatthias Ringwald uint16_t l2cap_max_mtu(void);
286ea392f51SMatthias Ringwald uint16_t l2cap_max_le_mtu(void);
287ea392f51SMatthias Ringwald 
288ea392f51SMatthias Ringwald int  l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len);
289ea392f51SMatthias Ringwald 
290ea392f51SMatthias Ringwald /* API_END */
291ea392f51SMatthias Ringwald 
292ea392f51SMatthias Ringwald #if 0
293ea392f51SMatthias Ringwald 
294ea392f51SMatthias Ringwald // to be implemented soon
2957192e786SMatthias Ringwald /**
2967192e786SMatthias Ringwald  * @brief Regster L2CAP LE Credit Based Flow Control Mode service
2977192e786SMatthias Ringwald  * @param
2987192e786SMatthias Ringwald  */
299*ffbf8201SMatthias Ringwald void l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm,
3007192e786SMatthias Ringwald     uint16_t mtu, uint16_t mps, uint16_t initial_credits, gap_security_level_t security_level);
301*ffbf8201SMatthias Ringwald void l2cap_le_unregister_service(uint16_t psm);
302ea392f51SMatthias Ringwald #endif
3035e6d58c4SMilanka Ringwald 
304e3b6fe7eSmatthias.ringwald #if defined __cplusplus
305e3b6fe7eSmatthias.ringwald }
306e3b6fe7eSmatthias.ringwald #endif
307f471afd8S[email protected] 
308f471afd8S[email protected] #endif // __L2CAP_H
309