xref: /btstack/src/classic/rfcomm.h (revision 7cdc89a533ca236b2c2564b759993b788bae89d3)
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  */
278 void rfcomm_set_required_security_level(gap_security_level_t security_level);
279 
280 /*
281  * @brief Create RFCOMM connection to a given server channel on a remote deivce.
282  * This channel will automatically provide enough credits to the remote side.
283  * @param addr
284  * @param server_channel
285  * @param out_cid
286  * @result status
287  */
288 uint8_t rfcomm_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t addr, uint8_t server_channel, uint16_t * out_cid);
289 
290 /*
291  * @brief Create RFCOMM connection to a given server channel on a remote deivce.
292  * This channel will use explicit credit management. During channel establishment, an initial  amount of credits is provided.
293  * @param addr
294  * @param server_channel
295  * @param initial_credits
296  * @param out_cid
297  * @result status
298  */
299 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);
300 
301 /**
302  * @brief Disconnects RFCOMM channel with given identifier.
303  */
304 void rfcomm_disconnect(uint16_t rfcomm_cid);
305 
306 /**
307  * @brief Registers RFCOMM service for a server channel and a maximum frame size, and assigns a packet handler.
308  * This channel provides credits automatically to the remote side -> no flow control
309  * @param packet handler for all channels of this service
310  * @param channel
311  * @param max_frame_size
312  */
313 uint8_t rfcomm_register_service(btstack_packet_handler_t packet_handler, uint8_t channel, uint16_t max_frame_size);
314 
315 /**
316  * @brief Registers RFCOMM service for a server channel and a maximum frame size, and assigns a packet handler.
317  * This channel will use explicit credit management. During channel establishment, an initial amount of credits is provided.
318  * @param packet handler for all channels of this service
319  * @param channel
320  * @param max_frame_size
321  * @param initial_credits
322  */
323 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);
324 
325 /**
326  * @brief Unregister RFCOMM service.
327  */
328 void rfcomm_unregister_service(uint8_t service_channel);
329 
330 /**
331  * @brief Accepts incoming RFCOMM connection.
332  */
333 void rfcomm_accept_connection(uint16_t rfcomm_cid);
334 
335 /**
336  * @brief Deny incoming RFCOMM connection.
337  */
338 void rfcomm_decline_connection(uint16_t rfcomm_cid);
339 
340 /**
341  * @brief Grant more incoming credits to the remote side for the given RFCOMM channel identifier.
342  */
343 void rfcomm_grant_credits(uint16_t rfcomm_cid, uint8_t credits);
344 
345 /**
346  * @brief Checks if RFCOMM can send packet.
347  * @param rfcomm_cid
348  * @result != 0 if can send now
349  */
350 int rfcomm_can_send_packet_now(uint16_t rfcomm_cid);
351 
352 /**
353  * @brief Request emission of RFCOMM_EVENT_CAN_SEND_NOW as soon as possible
354  * @note RFCOMM_EVENT_CAN_SEND_NOW might be emitted during call to this function
355  *       so packet handler should be ready to handle it
356  * @param rfcomm_cid
357  */
358 void rfcomm_request_can_send_now_event(uint16_t rfcomm_cid);
359 
360 /**
361  * @brief Sends RFCOMM data packet to the RFCOMM channel with given identifier.
362  * @param rfcomm_cid
363  */
364 int  rfcomm_send(uint16_t rfcomm_cid, uint8_t *data, uint16_t len);
365 
366 /**
367  * @brief Sends Local Line Status, see LINE_STATUS_..
368  * @param rfcomm_cid
369  * @param line_status
370  */
371 int rfcomm_send_local_line_status(uint16_t rfcomm_cid, uint8_t line_status);
372 
373 /**
374  * @brief Send local modem status. see MODEM_STAUS_..
375  * @param rfcomm_cid
376  * @param modem_status
377  */
378 int rfcomm_send_modem_status(uint16_t rfcomm_cid, uint8_t modem_status);
379 
380 /**
381  * @brief Configure remote port
382  * @param rfcomm_cid
383  * @param baud_rate
384  * @param data_bits
385  * @param stop_bits
386  * @param parity
387  * @param flow_control
388  */
389 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);
390 
391 /**
392  * @brief Query remote port
393  * @param rfcomm_cid
394  */
395 int rfcomm_query_port_configuration(uint16_t rfcomm_cid);
396 
397 /**
398  * @brief Query max frame size
399  * @param rfcomm_cid
400  */
401 uint16_t  rfcomm_get_max_frame_size(uint16_t rfcomm_cid);
402 
403 /**
404  * @brief Allow to create RFCOMM packet in outgoing buffer.
405  * if (rfcomm_can_send_packet_now(cid)){
406  *     rfcomm_reserve_packet_buffer();
407  *     uint8_t * buffer = rfcomm_get_outgoing_buffer();
408  *     uint16_t buffer_size = rfcomm_get_max_frame_size(cid);
409  *     .. setup data in buffer with len ..
410  *     rfcomm_send_prepared(cid, len)
411  * }
412  */
413 int       rfcomm_reserve_packet_buffer(void);
414 uint8_t * rfcomm_get_outgoing_buffer(void);
415 int       rfcomm_send_prepared(uint16_t rfcomm_cid, uint16_t len);
416 void      rfcomm_release_packet_buffer(void);
417 
418 /**
419  * @brief Enable L2CAP ERTM mode for RFCOMM. request callback is used to provide ERTM buffer. released callback returns buffer
420  *
421  * @note on request_callback, the app must set the ertm_config, buffer, size fields to enable ERTM for the current connection
422  * @note if buffer is not set, BASIC mode will be used instead
423  *
424  * @note released_callback provides ertm_id from earlier request to match request and release
425  *
426  * @param request_callback
427  * @param released_callback
428  */
429 void rfcomm_enable_l2cap_ertm(void request_callback(rfcomm_ertm_request_t * request), void released_callback(uint16_t ertm_id));
430 
431 /* API_END */
432 
433 #if defined __cplusplus
434 }
435 #endif
436 
437 #endif // RFCOMM_H
438