xref: /btstack/src/classic/rfcomm.h (revision 2cc827d4920e09c7219a79fceb0577d37efd4025)
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  *  RFCOMM.h
40  */
41 
42 #ifndef RFCOMM_H
43 #define RFCOMM_H
44 
45 #include "btstack_util.h"
46 
47 #include <stdint.h>
48 #include "btstack_run_loop.h"
49 #include "gap.h"
50 #include "l2cap.h"
51 
52 #if defined __cplusplus
53 extern "C" {
54 #endif
55 
56 #define UNLIMITED_INCOMING_CREDITS 0xff
57 
58 #define RFCOMM_TEST_DATA_MAX_LEN 4
59 
60 #define RFCOMM_RLS_STATUS_INVALID 0xff
61 
62 
63 // private structs
64 typedef enum {
65 	RFCOMM_MULTIPLEXER_CLOSED = 1,
66 	RFCOMM_MULTIPLEXER_W4_CONNECT,  // outgoing
67 	RFCOMM_MULTIPLEXER_SEND_SABM_0,     //    "
68 	RFCOMM_MULTIPLEXER_W4_UA_0,     //    "
69 	RFCOMM_MULTIPLEXER_W4_SABM_0,   // incoming
70     RFCOMM_MULTIPLEXER_SEND_UA_0,
71 	RFCOMM_MULTIPLEXER_OPEN,
72     RFCOMM_MULTIPLEXER_SEND_UA_0_AND_DISC,
73     RFCOMM_MULTIPLEXER_SHUTTING_DOWN,
74 } RFCOMM_MULTIPLEXER_STATE;
75 
76 typedef enum {
77     MULT_EV_READY_TO_SEND = 1,
78 } RFCOMM_MULTIPLEXER_EVENT;
79 
80 typedef enum {
81 	RFCOMM_CHANNEL_CLOSED = 1,
82 	RFCOMM_CHANNEL_W4_MULTIPLEXER,
83 	RFCOMM_CHANNEL_SEND_UIH_PN,
84     RFCOMM_CHANNEL_W4_PN_RSP,
85 	RFCOMM_CHANNEL_SEND_SABM_W4_UA,
86 	RFCOMM_CHANNEL_W4_UA,
87     RFCOMM_CHANNEL_INCOMING_SETUP,
88     RFCOMM_CHANNEL_DLC_SETUP,
89 	RFCOMM_CHANNEL_OPEN,
90     RFCOMM_CHANNEL_SEND_UA_AFTER_DISC,
91     RFCOMM_CHANNEL_SEND_DISC,
92     RFCOMM_CHANNEL_W4_UA_AFTER_DISC,
93     RFCOMM_CHANNEL_SEND_DM,
94     RFCOMM_CHANNEL_EMIT_OPEN_FAILED_AND_DISCARD,
95 } RFCOMM_CHANNEL_STATE;
96 
97 typedef enum {
98     RFCOMM_CHANNEL_STATE_VAR_NONE            = 0,
99     RFCOMM_CHANNEL_STATE_VAR_CLIENT_ACCEPTED = 1 << 0,
100     RFCOMM_CHANNEL_STATE_VAR_RCVD_PN         = 1 << 1,
101     RFCOMM_CHANNEL_STATE_VAR_RCVD_RPN        = 1 << 2,
102     RFCOMM_CHANNEL_STATE_VAR_RCVD_SABM       = 1 << 3,
103 
104     RFCOMM_CHANNEL_STATE_VAR_RCVD_MSC_CMD    = 1 << 4,
105     RFCOMM_CHANNEL_STATE_VAR_RCVD_MSC_RSP    = 1 << 5,
106     RFCOMM_CHANNEL_STATE_VAR_SEND_PN_RSP     = 1 << 6,
107     RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_INFO   = 1 << 7,
108 
109     RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_RSP    = 1 << 8,
110     RFCOMM_CHANNEL_STATE_VAR_SEND_UA         = 1 << 9,
111     RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_CMD    = 1 << 10,
112     RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_RSP    = 1 << 11,
113 
114     RFCOMM_CHANNEL_STATE_VAR_SEND_CREDITS    = 1 << 12,
115     RFCOMM_CHANNEL_STATE_VAR_SENT_MSC_CMD    = 1 << 13,
116     RFCOMM_CHANNEL_STATE_VAR_SENT_MSC_RSP    = 1 << 14,
117     RFCOMM_CHANNEL_STATE_VAR_SENT_CREDITS    = 1 << 15,
118 } RFCOMM_CHANNEL_STATE_VAR;
119 
120 typedef struct rfcomm_rpn_data {
121     uint8_t baud_rate;
122     uint8_t flags;
123     uint8_t flow_control;
124     uint8_t xon;
125     uint8_t xoff;
126     uint8_t parameter_mask_0;   // first byte
127     uint8_t parameter_mask_1;   // second byte
128 } rfcomm_rpn_data_t;
129 
130 // info regarding potential connections
131 typedef struct {
132     // linked list - assert: first field
133     btstack_linked_item_t    item;
134 
135     // packet handler
136     btstack_packet_handler_t packet_handler;
137 
138     // server channel
139     uint8_t server_channel;
140 
141     // incoming max frame size
142     uint16_t max_frame_size;
143 
144     // use incoming flow control
145     uint8_t incoming_flow_control;
146 
147     // initial incoming credits
148     uint8_t incoming_initial_credits;
149 
150 
151 } rfcomm_service_t;
152 
153 // info regarding multiplexer
154 // note: spec mandates single multiplexer per device combination
155 typedef struct {
156     // linked list - assert: first field
157     btstack_linked_item_t    item;
158 
159     btstack_timer_source_t   timer;
160     int              timer_active;
161 
162 	RFCOMM_MULTIPLEXER_STATE state;
163 
164     uint16_t  l2cap_cid;
165 
166     uint8_t   fcon; // only send if fcon & 1, send rsp if fcon & 0x80
167 
168 	bd_addr_t remote_addr;
169     hci_con_handle_t con_handle;
170 
171 	uint8_t   outgoing;
172 
173     // hack to deal with authentication failure only observed by remote side
174     uint8_t at_least_one_connection;
175 
176     uint16_t max_frame_size;
177 
178     // send DM for DLCI != 0
179     uint8_t send_dm_for_dlci;
180 
181     // non supported command, 0 if not set
182     uint8_t nsc_command;
183 
184     // ertm id
185     uint16_t ertm_id;
186 
187     // test data - limited to RFCOMM_TEST_DATA_MAX_LEN
188     uint8_t test_data_len;
189     uint8_t test_data[RFCOMM_TEST_DATA_MAX_LEN];
190 
191 } rfcomm_multiplexer_t;
192 
193 // info regarding an actual connection
194 typedef struct {
195 
196     // linked list - assert: first field
197     btstack_linked_item_t    item;
198 
199     // packet handler
200     btstack_packet_handler_t packet_handler;
201 
202     // server channel (see rfcomm_service_t) - NULL => outgoing channel
203     rfcomm_service_t * service;
204 
205 	// muliplexer for this channel
206     rfcomm_multiplexer_t *multiplexer;
207 
208     // RFCOMM Channel ID
209     uint16_t rfcomm_cid;
210 
211     //
212     uint8_t  dlci;
213 
214     // credits for outgoing traffic
215     uint8_t credits_outgoing;
216 
217     // number of packets remote will be granted
218     uint8_t new_credits_incoming;
219 
220     // credits for incoming traffic
221     uint8_t credits_incoming;
222 
223     // use incoming flow control
224     uint8_t incoming_flow_control;
225 
226     // channel state
227     RFCOMM_CHANNEL_STATE state;
228 
229     // state variables used in RFCOMM_CHANNEL_INCOMING
230     RFCOMM_CHANNEL_STATE_VAR state_var;
231 
232     // priority set by incoming side in PN
233     uint8_t pn_priority;
234 
235 	// negotiated frame size
236     uint16_t max_frame_size;
237 
238     // local rpn data
239     rfcomm_rpn_data_t rpn_data;
240 
241     // rls line status. RFCOMM_RLS_STATUS_INVALID if not set
242     uint8_t rls_line_status;
243 
244     // msc modem status.
245     uint8_t msc_modem_status;
246 
247     //
248     uint8_t   waiting_for_can_send_now;
249 
250 } rfcomm_channel_t;
251 
252 // struct used in ERTM callback
253 typedef struct {
254     // remote address
255     bd_addr_t             addr;
256 
257     // ERTM ID - returned in RFCOMM_EVENT_ERTM_BUFFER_RELEASED.
258     uint16_t              ertm_id;
259 
260     // ERTM Configuration - needs to stay valid indefinitely
261     l2cap_ertm_config_t * ertm_config;
262 
263     // ERTM buffer
264     uint8_t *             ertm_buffer;
265     uint32_t              ertm_buffer_size;
266 } rfcomm_ertm_request_t;
267 
268 /* API_START */
269 
270 /**
271  * @brief Set up RFCOMM.
272  */
273 void rfcomm_init(void);
274 
275 /**
276  * @brief Set security level required for incoming connections, need to be called before registering services.
277  * @deprecated use gap_set_security_level instead
278  */
279 void rfcomm_set_required_security_level(gap_security_level_t security_level);
280 
281 /*
282  * @brief Create RFCOMM connection to a given server channel on a remote deivce.
283  * This channel will automatically provide enough credits to the remote side.
284  * @param addr
285  * @param server_channel
286  * @param out_cid
287  * @result status
288  */
289 uint8_t rfcomm_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t addr, uint8_t server_channel, uint16_t * out_cid);
290 
291 /*
292  * @brief Create RFCOMM connection to a given server channel on a remote deivce.
293  * This channel will use explicit credit management. During channel establishment, an initial  amount of credits is provided.
294  * @param addr
295  * @param server_channel
296  * @param initial_credits
297  * @param out_cid
298  * @result status
299  */
300 uint8_t rfcomm_create_channel_with_initial_credits(btstack_packet_handler_t packet_handler, bd_addr_t addr, uint8_t server_channel, uint8_t initial_credits, uint16_t * out_cid);
301 
302 /**
303  * @brief Disconnects RFCOMM channel with given identifier.
304  */
305 void rfcomm_disconnect(uint16_t rfcomm_cid);
306 
307 /**
308  * @brief Registers RFCOMM service for a server channel and a maximum frame size, and assigns a packet handler.
309  * This channel provides credits automatically to the remote side -> no flow control
310  * @param packet handler for all channels of this service
311  * @param channel
312  * @param max_frame_size
313  */
314 uint8_t rfcomm_register_service(btstack_packet_handler_t packet_handler, uint8_t channel, uint16_t max_frame_size);
315 
316 /**
317  * @brief Registers RFCOMM service for a server channel and a maximum frame size, and assigns a packet handler.
318  * This channel will use explicit credit management. During channel establishment, an initial amount of credits is provided.
319  * @param packet handler for all channels of this service
320  * @param channel
321  * @param max_frame_size
322  * @param initial_credits
323  */
324 uint8_t rfcomm_register_service_with_initial_credits(btstack_packet_handler_t packet_handler, uint8_t channel, uint16_t max_frame_size, uint8_t initial_credits);
325 
326 /**
327  * @brief Unregister RFCOMM service.
328  */
329 void rfcomm_unregister_service(uint8_t service_channel);
330 
331 /**
332  * @brief Accepts incoming RFCOMM connection.
333  */
334 void rfcomm_accept_connection(uint16_t rfcomm_cid);
335 
336 /**
337  * @brief Deny incoming RFCOMM connection.
338  */
339 void rfcomm_decline_connection(uint16_t rfcomm_cid);
340 
341 /**
342  * @brief Grant more incoming credits to the remote side for the given RFCOMM channel identifier.
343  */
344 void rfcomm_grant_credits(uint16_t rfcomm_cid, uint8_t credits);
345 
346 /**
347  * @brief Checks if RFCOMM can send packet.
348  * @param rfcomm_cid
349  * @result != 0 if can send now
350  */
351 int rfcomm_can_send_packet_now(uint16_t rfcomm_cid);
352 
353 /**
354  * @brief Request emission of RFCOMM_EVENT_CAN_SEND_NOW as soon as possible
355  * @note RFCOMM_EVENT_CAN_SEND_NOW might be emitted during call to this function
356  *       so packet handler should be ready to handle it
357  * @param rfcomm_cid
358  */
359 void rfcomm_request_can_send_now_event(uint16_t rfcomm_cid);
360 
361 /**
362  * @brief Sends RFCOMM data packet to the RFCOMM channel with given identifier.
363  * @param rfcomm_cid
364  */
365 int  rfcomm_send(uint16_t rfcomm_cid, uint8_t *data, uint16_t len);
366 
367 /**
368  * @brief Sends Local Line Status, see LINE_STATUS_..
369  * @param rfcomm_cid
370  * @param line_status
371  */
372 int rfcomm_send_local_line_status(uint16_t rfcomm_cid, uint8_t line_status);
373 
374 /**
375  * @brief Send local modem status. see MODEM_STAUS_..
376  * @param rfcomm_cid
377  * @param modem_status
378  */
379 int rfcomm_send_modem_status(uint16_t rfcomm_cid, uint8_t modem_status);
380 
381 /**
382  * @brief Configure remote port
383  * @param rfcomm_cid
384  * @param baud_rate
385  * @param data_bits
386  * @param stop_bits
387  * @param parity
388  * @param flow_control
389  */
390 int rfcomm_send_port_configuration(uint16_t rfcomm_cid, rpn_baud_t baud_rate, rpn_data_bits_t data_bits, rpn_stop_bits_t stop_bits, rpn_parity_t parity, rpn_flow_control_t flow_control);
391 
392 /**
393  * @brief Query remote port
394  * @param rfcomm_cid
395  */
396 int rfcomm_query_port_configuration(uint16_t rfcomm_cid);
397 
398 /**
399  * @brief Query max frame size
400  * @param rfcomm_cid
401  */
402 uint16_t  rfcomm_get_max_frame_size(uint16_t rfcomm_cid);
403 
404 /**
405  * @brief Allow to create RFCOMM packet in outgoing buffer.
406  * if (rfcomm_can_send_packet_now(cid)){
407  *     rfcomm_reserve_packet_buffer();
408  *     uint8_t * buffer = rfcomm_get_outgoing_buffer();
409  *     uint16_t buffer_size = rfcomm_get_max_frame_size(cid);
410  *     .. setup data in buffer with len ..
411  *     rfcomm_send_prepared(cid, len)
412  * }
413  */
414 int       rfcomm_reserve_packet_buffer(void);
415 uint8_t * rfcomm_get_outgoing_buffer(void);
416 int       rfcomm_send_prepared(uint16_t rfcomm_cid, uint16_t len);
417 void      rfcomm_release_packet_buffer(void);
418 
419 /**
420  * @brief Enable L2CAP ERTM mode for RFCOMM. request callback is used to provide ERTM buffer. released callback returns buffer
421  *
422  * @note on request_callback, the app must set the ertm_config, buffer, size fields to enable ERTM for the current connection
423  * @note if buffer is not set, BASIC mode will be used instead
424  *
425  * @note released_callback provides ertm_id from earlier request to match request and release
426  *
427  * @param request_callback
428  * @param released_callback
429  */
430 void rfcomm_enable_l2cap_ertm(void request_callback(rfcomm_ertm_request_t * request), void released_callback(uint16_t ertm_id));
431 
432 /**
433  * @brief De-Init RFCOMM
434  */
435 void rfcomm_deinit(void);
436 
437 /* API_END */
438 
439 #if defined __cplusplus
440 }
441 #endif
442 
443 #endif // RFCOMM_H
444