xref: /btstack/src/l2cap.h (revision 8257e5f9e1a9f15ac2fe1e69adc5d2eeeadf3fff)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 /*
39  *  l2cap.h
40  *
41  *  Logical Link Control and Adaption Protocol (L2CAP)
42  *
43  *  Created by Matthias Ringwald on 5/16/09.
44  */
45 
46 #ifndef __L2CAP_H
47 #define __L2CAP_H
48 
49 #include "hci.h"
50 #include "l2cap_signaling.h"
51 #include "btstack_util.h"
52 #include "bluetooth.h"
53 
54 #if defined __cplusplus
55 extern "C" {
56 #endif
57 
58 // check L2CAP MTU
59 #if (L2CAP_MINIMAL_MTU + L2CAP_HEADER_SIZE) > HCI_ACL_PAYLOAD_SIZE
60 #error "HCI_ACL_PAYLOAD_SIZE too small for minimal L2CAP MTU of 48 bytes"
61 #endif
62 
63 // private structs
64 typedef enum {
65     L2CAP_STATE_CLOSED = 1,           // no baseband
66     L2CAP_STATE_WILL_SEND_CREATE_CONNECTION,
67     L2CAP_STATE_WAIT_CONNECTION_COMPLETE,
68     L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES,
69     L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE,
70     L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE,
71     L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT,
72     L2CAP_STATE_WAIT_CONNECT_RSP, // from peer
73     L2CAP_STATE_CONFIG,
74     L2CAP_STATE_OPEN,
75     L2CAP_STATE_WAIT_DISCONNECT,  // from application
76     L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST,
77     L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_INSUFFICIENT_SECURITY,
78     L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE,
79     L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT,
80     L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST,
81     L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE,
82     L2CAP_STATE_INVALID,
83 } L2CAP_STATE;
84 
85 typedef enum {
86     L2CAP_CHANNEL_STATE_VAR_NONE                  = 0,
87     L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ         = 1 << 0,
88     L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP         = 1 << 1,
89     L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ         = 1 << 2,
90     L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP         = 1 << 3,
91     L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ         = 1 << 4,
92     L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP         = 1 << 5,
93     L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU     = 1 << 6,   // in CONF RSP, add MTU field
94     L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT    = 1 << 7,   // in CONF RSP, set CONTINUE flag
95     L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID = 1 << 8,   // in CONF RSP, send UNKNOWN OPTIONS
96     L2CAP_CHANNEL_STATE_VAR_SEND_CMD_REJ_UNKNOWN  = 1 << 9,   // send CMD_REJ with reason unknown
97     L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND   = 1 << 10,  // send Connection Respond with pending
98 } L2CAP_CHANNEL_STATE_VAR;
99 
100 // info regarding an actual connection
101 typedef struct {
102     // linked list - assert: first field
103     btstack_linked_item_t    item;
104 
105     // packet handler
106     btstack_packet_handler_t packet_handler;
107 
108     // timer
109     btstack_timer_source_t rtx; // also used for ertx
110 
111     L2CAP_STATE state;
112     L2CAP_CHANNEL_STATE_VAR state_var;
113 
114     // info
115     bd_addr_t address;
116     hci_con_handle_t con_handle;
117 
118     uint8_t   remote_sig_id;    // used by other side, needed for delayed response
119     uint8_t   local_sig_id;     // own signaling identifier
120 
121     uint16_t  local_cid;
122     uint16_t  remote_cid;
123 
124     uint16_t  local_mtu;
125     uint16_t  remote_mtu;
126 
127     uint16_t  flush_timeout;    // default 0xffff
128 
129     uint16_t  psm;
130 
131     gap_security_level_t required_security_level;
132 
133     uint8_t   reason; // used in decline internal
134     uint8_t   waiting_for_can_send_now;
135 
136 } l2cap_channel_t;
137 
138 // info regarding potential connections
139 typedef struct {
140     // linked list - assert: first field
141     btstack_linked_item_t    item;
142 
143     // service id
144     uint16_t  psm;
145 
146     // incoming MTU
147     uint16_t mtu;
148 
149     // incoming MPS
150     uint16_t mps;
151 
152     // internal connection
153     btstack_packet_handler_t packet_handler;
154 
155     // required security level
156     gap_security_level_t required_security_level;
157 } l2cap_service_t;
158 
159 
160 typedef struct l2cap_signaling_response {
161     hci_con_handle_t handle;
162     uint8_t  sig_id;
163     uint8_t  code;
164     uint16_t data; // infoType for INFORMATION REQUEST, result for CONNECTION request and command unknown
165 } l2cap_signaling_response_t;
166 
167 
168 void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id);
169 int  l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id);
170 int  l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len);
171 int  l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len);
172 
173 // PTS Testing
174 int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len);
175 void l2cap_require_security_level_2_for_outgoing_sdp(void);
176 
177 /* API_START */
178 
179 /**
180  * @brief Set up L2CAP and register L2CAP with HCI layer.
181  */
182 void l2cap_init(void);
183 
184 /**
185  * @brief Registers packet handler for LE Connection Parameter Update events
186  */
187 void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size));
188 
189 /**
190  * @brief Get max MTU for Classic connections based on btstack configuration
191  */
192 uint16_t l2cap_max_mtu(void);
193 
194 /**
195  * @brief Get max MTU for LE connections based on btstack configuration
196  */
197 uint16_t l2cap_max_le_mtu(void);
198 
199 /**
200  * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
201  * @param packet_handler
202  * @param address
203  * @param psm
204  * @param mtu
205  * @param local_cid
206  * @param status
207  */
208 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);
209 
210 /**
211  * @brief Disconnects L2CAP channel with given identifier.
212  */
213 void l2cap_disconnect(uint16_t local_cid, uint8_t reason);
214 
215 /**
216  * @brief Queries the maximal transfer unit (MTU) for L2CAP channel with given identifier.
217  */
218 uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid);
219 
220 /**
221  * @brief Sends L2CAP data packet to the channel with given identifier.
222  */
223 int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len);
224 
225 /**
226  * @brief Registers L2CAP service with given PSM and MTU, and assigns a packet handler.
227  */
228 uint8_t l2cap_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level);
229 
230 /**
231  * @brief Unregisters L2CAP service with given PSM.  On embedded systems, use NULL for connection parameter.
232  */
233 void l2cap_unregister_service(uint16_t psm);
234 
235 /**
236  * @brief Accepts incoming L2CAP connection.
237  */
238 void l2cap_accept_connection(uint16_t local_cid);
239 
240 /**
241  * @brief Deny incoming L2CAP connection.
242  */
243 void l2cap_decline_connection(uint16_t local_cid, uint8_t reason);
244 
245 /**
246  * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module
247  */
248 int  l2cap_can_send_packet_now(uint16_t local_cid);
249 
250 /**
251  * @brief Check if there's space on the Bluetooth module
252  */
253 int  l2cap_can_send_prepared_packet_now(uint16_t local_cid);
254 
255 /**
256  * @brief Reserve outgoing buffer
257  */
258 int  l2cap_reserve_packet_buffer(void);
259 
260 /**
261  * @brief Get outgoing buffer and prepare data.
262  */
263 uint8_t *l2cap_get_outgoing_buffer(void);
264 
265 /**
266  * @brief Send L2CAP packet prepared in outgoing buffer to channel
267  */
268 int l2cap_send_prepared(uint16_t local_cid, uint16_t len);
269 
270 /**
271  * @brief Release outgoing buffer (only needed if l2cap_send_prepared is not called)
272  */
273 void l2cap_release_packet_buffer(void);
274 
275 /* API_END */
276 
277 #if 0
278 
279 // to be implemented soon
280 /**
281  * @brief Regster L2CAP LE Credit Based Flow Control Mode service
282  * @param
283  */
284 void l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm,
285     uint16_t mtu, uint16_t mps, uint16_t initial_credits, gap_security_level_t security_level);
286 void l2cap_le_unregister_service(uint16_t psm);
287 #endif
288 
289 #if defined __cplusplus
290 }
291 #endif
292 
293 #endif // __L2CAP_H
294