xref: /btstack/src/l2cap.h (revision ab2c6ae4b737d5e801d3defe4117331eb244ebb7)
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 #ifdef ENABLE_CLASSIC
60 #if (L2CAP_HEADER_SIZE + L2CAP_MINIMAL_MTU) > HCI_ACL_PAYLOAD_SIZE
61 #error "HCI_ACL_PAYLOAD_SIZE too small for minimal L2CAP MTU of 48 bytes"
62 #endif
63 #endif
64 #ifdef ENABLE_BLE
65 #if (L2CAP_HEADER_SIZE + L2CAP_LE_DEFAULT_MTU) > HCI_ACL_PAYLOAD_SIZE
66 #error "HCI_ACL_PAYLOAD_SIZE too small for minimal L2CAP LE MTU of 23 bytes"
67 #endif
68 #endif
69 
70 #define L2CAP_LE_AUTOMATIC_CREDITS 0xffff
71 
72 // private structs
73 typedef enum {
74     L2CAP_STATE_CLOSED = 1,           // no baseband
75     L2CAP_STATE_WILL_SEND_CREATE_CONNECTION,
76     L2CAP_STATE_WAIT_CONNECTION_COMPLETE,
77     L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES,
78     L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE,
79     L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE,
80     L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT,
81     L2CAP_STATE_WAIT_CONNECT_RSP, // from peer
82     L2CAP_STATE_CONFIG,
83     L2CAP_STATE_OPEN,
84     L2CAP_STATE_WAIT_DISCONNECT,  // from application
85     L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST,
86     L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_INSUFFICIENT_SECURITY,
87     L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE,
88     L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT,
89     L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST,
90     L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE,
91     L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST,
92     L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE,
93     L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT,
94     L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE,
95     L2CAP_STATE_INVALID,
96 } L2CAP_STATE;
97 
98 typedef enum {
99     L2CAP_CHANNEL_STATE_VAR_NONE                  = 0,
100     L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ         = 1 << 0,
101     L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP         = 1 << 1,
102     L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ         = 1 << 2,
103     L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP         = 1 << 3,
104     L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ         = 1 << 4,
105     L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP         = 1 << 5,
106     L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU     = 1 << 6,   // in CONF RSP, add MTU field
107     L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT    = 1 << 7,   // in CONF RSP, set CONTINUE flag
108     L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID = 1 << 8,   // in CONF RSP, send UNKNOWN OPTIONS
109     L2CAP_CHANNEL_STATE_VAR_SEND_CMD_REJ_UNKNOWN  = 1 << 9,   // send CMD_REJ with reason unknown
110     L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND   = 1 << 10,  // send Connection Respond with pending
111     L2CAP_CHANNEL_STATE_VAR_INCOMING              = 1 << 15,  // channel is incoming
112 } L2CAP_CHANNEL_STATE_VAR;
113 
114 // info regarding an actual connection
115 typedef struct {
116     // linked list - assert: first field
117     btstack_linked_item_t    item;
118 
119     // packet handler
120     btstack_packet_handler_t packet_handler;
121 
122     // timer
123     btstack_timer_source_t rtx; // also used for ertx
124 
125     L2CAP_STATE state;
126     L2CAP_CHANNEL_STATE_VAR state_var;
127 
128     // info
129     hci_con_handle_t con_handle;
130 
131     bd_addr_t address;
132     bd_addr_type_t address_type;
133 
134     uint8_t   remote_sig_id;    // used by other side, needed for delayed response
135     uint8_t   local_sig_id;     // own signaling identifier
136 
137     uint16_t  local_cid;
138     uint16_t  remote_cid;
139 
140     uint16_t  local_mtu;
141     uint16_t  remote_mtu;
142 
143     uint16_t  flush_timeout;    // default 0xffff
144 
145     uint16_t  psm;
146 
147     gap_security_level_t required_security_level;
148 
149     uint8_t   reason; // used in decline internal
150     uint8_t   waiting_for_can_send_now;
151 
152     // LE Data Channels
153 
154     // incoming SDU
155     uint8_t * receive_sdu_buffer;
156     uint16_t  receive_sdu_len;
157     uint16_t  receive_sdu_pos;
158 
159     // outgoing SDU
160     uint8_t  * send_sdu_buffer;
161     uint16_t   send_sdu_len;
162     uint16_t   send_sdu_pos;
163 
164     // max PDU size
165     uint16_t  remote_mps;
166 
167     // credits for outgoing traffic
168     uint16_t credits_outgoing;
169 
170     // number of packets remote will be granted
171     uint16_t new_credits_incoming;
172 
173     // credits for incoming traffic
174     uint16_t credits_incoming;
175 
176     // automatic credits incoming
177     uint16_t automatic_credits;
178 
179 } l2cap_channel_t;
180 
181 // info regarding potential connections
182 typedef struct {
183     // linked list - assert: first field
184     btstack_linked_item_t    item;
185 
186     // service id
187     uint16_t  psm;
188 
189     // incoming MTU
190     uint16_t mtu;
191 
192     // internal connection
193     btstack_packet_handler_t packet_handler;
194 
195     // required security level
196     gap_security_level_t required_security_level;
197 
198 } l2cap_service_t;
199 
200 
201 typedef struct l2cap_signaling_response {
202     hci_con_handle_t handle;
203     uint8_t  sig_id;
204     uint8_t  code;
205     uint16_t cid;  // source cid for CONNECTION REQUEST
206     uint16_t data; // infoType for INFORMATION REQUEST, result for CONNECTION REQUEST and COMMAND UNKNOWN
207 } l2cap_signaling_response_t;
208 
209 
210 void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id);
211 int  l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id);
212 void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t channel_id);
213 int  l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len);
214 int  l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len);
215 
216 // PTS Testing
217 int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len);
218 void l2cap_require_security_level_2_for_outgoing_sdp(void);
219 
220 // Used by RFCOMM - similar to l2cap_can-send_packet_now but does not check if outgoing buffer is reserved
221 int  l2cap_can_send_prepared_packet_now(uint16_t local_cid);
222 
223 /* API_START */
224 
225 /**
226  * @brief Set up L2CAP and register L2CAP with HCI layer.
227  */
228 void l2cap_init(void);
229 
230 /**
231  * @brief Registers packet handler for LE Connection Parameter Update events
232  */
233 void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size));
234 
235 /**
236  * @brief Get max MTU for Classic connections based on btstack configuration
237  */
238 uint16_t l2cap_max_mtu(void);
239 
240 /**
241  * @brief Get max MTU for LE connections based on btstack configuration
242  */
243 uint16_t l2cap_max_le_mtu(void);
244 
245 /**
246  * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
247  * @param packet_handler
248  * @param address
249  * @param psm
250  * @param mtu
251  * @param local_cid
252  * @return status
253  */
254 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);
255 
256 /**
257  * @brief Disconnects L2CAP channel with given identifier.
258  */
259 void l2cap_disconnect(uint16_t local_cid, uint8_t reason);
260 
261 /**
262  * @brief Queries the maximal transfer unit (MTU) for L2CAP channel with given identifier.
263  */
264 uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid);
265 
266 /**
267  * @brief Sends L2CAP data packet to the channel with given identifier.
268  */
269 int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len);
270 
271 /**
272  * @brief Registers L2CAP service with given PSM and MTU, and assigns a packet handler.
273  */
274 uint8_t l2cap_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level);
275 
276 /**
277  * @brief Unregisters L2CAP service with given PSM.  On embedded systems, use NULL for connection parameter.
278  */
279 uint8_t l2cap_unregister_service(uint16_t psm);
280 
281 /**
282  * @brief Accepts incoming L2CAP connection.
283  */
284 void l2cap_accept_connection(uint16_t local_cid);
285 
286 /**
287  * @brief Deny incoming L2CAP connection.
288  */
289 void l2cap_decline_connection(uint16_t local_cid);
290 
291 /**
292  * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module
293  */
294 int  l2cap_can_send_packet_now(uint16_t local_cid);
295 
296 /**
297  * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible
298  * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function
299  *       so packet handler should be ready to handle it
300  * @param local_cid
301  */
302 void l2cap_request_can_send_now_event(uint16_t local_cid);
303 
304 /**
305  * @brief Reserve outgoing buffer
306  */
307 int  l2cap_reserve_packet_buffer(void);
308 
309 /**
310  * @brief Get outgoing buffer and prepare data.
311  */
312 uint8_t *l2cap_get_outgoing_buffer(void);
313 
314 /**
315  * @brief Send L2CAP packet prepared in outgoing buffer to channel
316  */
317 int l2cap_send_prepared(uint16_t local_cid, uint16_t len);
318 
319 /**
320  * @brief Release outgoing buffer (only needed if l2cap_send_prepared is not called)
321  */
322 void l2cap_release_packet_buffer(void);
323 
324 
325 //
326 // LE Connection Oriented Channels feature with the LE Credit Based Flow Control Mode == LE Data Channel
327 //
328 
329 
330 /**
331  * @brief Register L2CAP LE Data Channel service
332  * @note MTU and initial credits are specified in l2cap_le_accept_connection(..) call
333  * @param packet_handler
334  * @param psm
335  * @param security_level
336  */
337 uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level);
338 
339 /**
340  * @brief Unregister L2CAP LE Data Channel service
341  * @param psm
342  */
343 
344 uint8_t l2cap_le_unregister_service(uint16_t psm);
345 
346 /*
347  * @brief Accept incoming LE Data Channel connection
348  * @param local_cid             L2CAP LE Data Channel Identifier
349  * @param receive_buffer        buffer used for reassembly of L2CAP LE Information Frames into service data unit (SDU) with given MTU
350  * @param receive_buffer_size   buffer size equals MTU
351  * @param initial_credits       Number of initial credits provided to peer or L2CAP_LE_AUTOMATIC_CREDITS to enable automatic credits
352  */
353 
354 uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits);
355 
356 /**
357  * @brief Deny incoming LE Data Channel connection due to resource constraints
358  * @param local_cid             L2CAP LE Data Channel Identifier
359  */
360 
361 uint8_t l2cap_le_decline_connection(uint16_t local_cid);
362 
363 /**
364  * @brief Create LE Data Channel
365  * @param packet_handler        Packet handler for this connection
366  * @param con_handle            ACL-LE HCI Connction Handle
367  * @param psm                   Service PSM to connect to
368  * @param receive_buffer        buffer used for reassembly of L2CAP LE Information Frames into service data unit (SDU) with given MTU
369  * @param receive_buffer_size   buffer size equals MTU
370  * @param initial_credits       Number of initial credits provided to peer or L2CAP_LE_AUTOMATIC_CREDITS to enable automatic credits
371  * @param security_level        Minimum required security level
372  * @param out_local_cid         L2CAP LE Channel Identifier is stored here
373  */
374 uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle,
375     uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level,
376     uint16_t * out_local_cid);
377 
378 /**
379  * @brief Provide credtis for LE Data Channel
380  * @param local_cid             L2CAP LE Data Channel Identifier
381  * @param credits               Number additional credits for peer
382  */
383 uint8_t l2cap_le_provide_credits(uint16_t cid, uint16_t credits);
384 
385 /**
386  * @brief Check if packet can be scheduled for transmission
387  * @param local_cid             L2CAP LE Data Channel Identifier
388  */
389 int l2cap_le_can_send_now(uint16_t cid);
390 
391 /**
392  * @brief Request emission of L2CAP_EVENT_LE_CAN_SEND_NOW as soon as possible
393  * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function
394  *       so packet handler should be ready to handle it
395  * @param local_cid             L2CAP LE Data Channel Identifier
396  */
397 uint8_t l2cap_le_request_can_send_now_event(uint16_t cid);
398 
399 /**
400  * @brief Send data via LE Data Channel
401  * @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event
402  * @param local_cid             L2CAP LE Data Channel Identifier
403  * @param data                  data to send
404  * @param size                  data size
405  */
406 uint8_t l2cap_le_send_data(uint16_t cid, uint8_t * data, uint16_t size);
407 
408 /**
409  * @brief Disconnect from LE Data Channel
410  * @param local_cid             L2CAP LE Data Channel Identifier
411  */
412 uint8_t l2cap_le_disconnect(uint16_t cid);
413 
414 /* API_END */
415 
416 #if defined __cplusplus
417 }
418 #endif
419 
420 #endif // __L2CAP_H
421