xref: /btstack/src/classic/rfcomm.h (revision 6c927b22a98b3f4b0e3ed7d77829cb530efde76d)
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 
49 #if defined __cplusplus
50 extern "C" {
51 #endif
52 
53 #define UNLIMITED_INCOMING_CREDITS 0xff
54 
55 #define RFCOMM_TEST_DATA_MAX_LEN 4
56 
57 #define RFCOMM_RLS_STATUS_INVALID 0xff
58 
59 
60 // private structs
61 typedef enum {
62 	RFCOMM_MULTIPLEXER_CLOSED = 1,
63 	RFCOMM_MULTIPLEXER_W4_CONNECT,  // outgoing
64 	RFCOMM_MULTIPLEXER_SEND_SABM_0,     //    "
65 	RFCOMM_MULTIPLEXER_W4_UA_0,     //    "
66 	RFCOMM_MULTIPLEXER_W4_SABM_0,   // incoming
67     RFCOMM_MULTIPLEXER_SEND_UA_0,
68 	RFCOMM_MULTIPLEXER_OPEN,
69     RFCOMM_MULTIPLEXER_SEND_UA_0_AND_DISC
70 } RFCOMM_MULTIPLEXER_STATE;
71 
72 typedef enum {
73     MULT_EV_READY_TO_SEND = 1,
74 
75 } RFCOMM_MULTIPLEXER_EVENT;
76 
77 typedef enum {
78 	RFCOMM_CHANNEL_CLOSED = 1,
79 	RFCOMM_CHANNEL_W4_MULTIPLEXER,
80 	RFCOMM_CHANNEL_SEND_UIH_PN,
81     RFCOMM_CHANNEL_W4_PN_RSP,
82 	RFCOMM_CHANNEL_SEND_SABM_W4_UA,
83 	RFCOMM_CHANNEL_W4_UA,
84     RFCOMM_CHANNEL_INCOMING_SETUP,
85     RFCOMM_CHANNEL_DLC_SETUP,
86 	RFCOMM_CHANNEL_OPEN,
87     RFCOMM_CHANNEL_SEND_UA_AFTER_DISC,
88     RFCOMM_CHANNEL_SEND_DISC,
89     RFCOMM_CHANNEL_W4_UA_AFTER_UA,
90     RFCOMM_CHANNEL_SEND_DM,
91 
92 } RFCOMM_CHANNEL_STATE;
93 
94 typedef enum {
95     RFCOMM_CHANNEL_STATE_VAR_NONE            = 0,
96     RFCOMM_CHANNEL_STATE_VAR_CLIENT_ACCEPTED = 1 << 0,
97     RFCOMM_CHANNEL_STATE_VAR_RCVD_PN         = 1 << 1,
98     RFCOMM_CHANNEL_STATE_VAR_RCVD_RPN        = 1 << 2,
99     RFCOMM_CHANNEL_STATE_VAR_RCVD_SABM       = 1 << 3,
100 
101     RFCOMM_CHANNEL_STATE_VAR_RCVD_MSC_CMD    = 1 << 4,
102     RFCOMM_CHANNEL_STATE_VAR_RCVD_MSC_RSP    = 1 << 5,
103     RFCOMM_CHANNEL_STATE_VAR_SEND_PN_RSP     = 1 << 6,
104     RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_INFO   = 1 << 7,
105 
106     RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_RSP    = 1 << 8,
107     RFCOMM_CHANNEL_STATE_VAR_SEND_UA         = 1 << 9,
108     RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_CMD    = 1 << 10,
109     RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_RSP    = 1 << 11,
110 
111     RFCOMM_CHANNEL_STATE_VAR_SEND_CREDITS    = 1 << 12,
112     RFCOMM_CHANNEL_STATE_VAR_SENT_MSC_CMD    = 1 << 13,
113     RFCOMM_CHANNEL_STATE_VAR_SENT_MSC_RSP    = 1 << 14,
114     RFCOMM_CHANNEL_STATE_VAR_SENT_CREDITS    = 1 << 15,
115 } RFCOMM_CHANNEL_STATE_VAR;
116 
117 typedef enum {
118     CH_EVT_RCVD_SABM = 1,
119     CH_EVT_RCVD_UA,
120     CH_EVT_RCVD_PN,
121     CH_EVT_RCVD_PN_RSP,
122     CH_EVT_RCVD_DISC,
123     CH_EVT_RCVD_DM,
124     CH_EVT_RCVD_MSC_CMD,
125     CH_EVT_RCVD_MSC_RSP,
126     CH_EVT_RCVD_NSC_RSP,
127     CH_EVT_RCVD_RLS_CMD,
128     CH_EVT_RCVD_RLS_RSP,
129     CH_EVT_RCVD_RPN_CMD,
130     CH_EVT_RCVD_RPN_REQ,
131     CH_EVT_RCVD_CREDITS,
132     CH_EVT_MULTIPLEXER_READY,
133     CH_EVT_READY_TO_SEND,
134 } RFCOMM_CHANNEL_EVENT;
135 
136 typedef struct rfcomm_channel_event {
137     RFCOMM_CHANNEL_EVENT type;
138     uint16_t dummy; // force rfcomm_channel_event to be 2-byte aligned -> avoid -Wcast-align warning
139 } rfcomm_channel_event_t;
140 
141 typedef struct rfcomm_channel_event_pn {
142     rfcomm_channel_event_t super;
143     uint16_t max_frame_size;
144     uint8_t  priority;
145     uint8_t  credits_outgoing;
146 } rfcomm_channel_event_pn_t;
147 
148 typedef struct rfcomm_rpn_data {
149     uint8_t baud_rate;
150     uint8_t flags;
151     uint8_t flow_control;
152     uint8_t xon;
153     uint8_t xoff;
154     uint8_t parameter_mask_0;   // first byte
155     uint8_t parameter_mask_1;   // second byte
156 } rfcomm_rpn_data_t;
157 
158 typedef struct rfcomm_channel_event_rpn {
159     rfcomm_channel_event_t super;
160     rfcomm_rpn_data_t data;
161 } rfcomm_channel_event_rpn_t;
162 
163 typedef struct rfcomm_channel_event_rls {
164     rfcomm_channel_event_t super;
165     uint8_t line_status;
166 } rfcomm_channel_event_rls_t;
167 
168 typedef struct rfcomm_channel_event_msc {
169     rfcomm_channel_event_t super;
170     uint8_t modem_status;
171 } rfcomm_channel_event_msc_t;
172 
173 // info regarding potential connections
174 typedef struct {
175     // linked list - assert: first field
176     btstack_linked_item_t    item;
177 
178     // packet handler
179     btstack_packet_handler_t packet_handler;
180 
181     // server channel
182     uint8_t server_channel;
183 
184     // incoming max frame size
185     uint16_t max_frame_size;
186 
187     // use incoming flow control
188     uint8_t incoming_flow_control;
189 
190     // initial incoming credits
191     uint8_t incoming_initial_credits;
192 
193 
194 } rfcomm_service_t;
195 
196 // info regarding multiplexer
197 // note: spec mandates single multiplexer per device combination
198 typedef struct {
199     // linked list - assert: first field
200     btstack_linked_item_t    item;
201 
202     btstack_timer_source_t   timer;
203     int              timer_active;
204 
205 	RFCOMM_MULTIPLEXER_STATE state;
206 
207     uint16_t  l2cap_cid;
208 
209     uint8_t   fcon; // only send if fcon & 1, send rsp if fcon & 0x80
210 
211 	bd_addr_t remote_addr;
212     hci_con_handle_t con_handle;
213 
214 	uint8_t   outgoing;
215 
216     // hack to deal with authentication failure only observed by remote side
217     uint8_t at_least_one_connection;
218 
219     uint16_t max_frame_size;
220 
221     // send DM for DLCI != 0
222     uint8_t send_dm_for_dlci;
223 
224     // non supported command, 0 if not set
225     uint8_t nsc_command;
226 
227     // test data - limited to RFCOMM_TEST_DATA_MAX_LEN
228     uint8_t test_data_len;
229     uint8_t test_data[RFCOMM_TEST_DATA_MAX_LEN];
230 
231 } rfcomm_multiplexer_t;
232 
233 // info regarding an actual connection
234 typedef struct {
235 
236     // linked list - assert: first field
237     btstack_linked_item_t    item;
238 
239     // packet handler
240     btstack_packet_handler_t packet_handler;
241 
242     // server channel (see rfcomm_service_t) - NULL => outgoing channel
243     rfcomm_service_t * service;
244 
245 	// muliplexer for this channel
246     rfcomm_multiplexer_t *multiplexer;
247 
248     // RFCOMM Channel ID
249     uint16_t rfcomm_cid;
250 
251     //
252     uint8_t  dlci;
253 
254     // credits for outgoing traffic
255     uint8_t credits_outgoing;
256 
257     // number of packets remote will be granted
258     uint8_t new_credits_incoming;
259 
260     // credits for incoming traffic
261     uint8_t credits_incoming;
262 
263     // use incoming flow control
264     uint8_t incoming_flow_control;
265 
266     // channel state
267     RFCOMM_CHANNEL_STATE state;
268 
269     // state variables used in RFCOMM_CHANNEL_INCOMING
270     RFCOMM_CHANNEL_STATE_VAR state_var;
271 
272     // priority set by incoming side in PN
273     uint8_t pn_priority;
274 
275 	// negotiated frame size
276     uint16_t max_frame_size;
277 
278     // local rpn data
279     rfcomm_rpn_data_t rpn_data;
280 
281     // rls line status. RFCOMM_RLS_STATUS_INVALID if not set
282     uint8_t rls_line_status;
283 
284     // msc modem status.
285     uint8_t msc_modem_status;
286 
287     //
288     uint8_t   waiting_for_can_send_now;
289 
290 } rfcomm_channel_t;
291 
292 /* API_START */
293 
294 /**
295  * @brief Set up RFCOMM.
296  */
297 void rfcomm_init(void);
298 
299 /**
300  * @brief Set security level required for incoming connections, need to be called before registering services.
301  */
302 void rfcomm_set_required_security_level(gap_security_level_t security_level);
303 
304 /*
305  * @brief Create RFCOMM connection to a given server channel on a remote deivce.
306  * This channel will automatically provide enough credits to the remote side.
307  * @param addr
308  * @param server_channel
309  * @param out_cid
310  * @result status
311  */
312 uint8_t rfcomm_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t addr, uint8_t server_channel, uint16_t * out_cid);
313 
314 /*
315  * @brief Create RFCOMM connection to a given server channel on a remote deivce.
316  * This channel will use explicit credit management. During channel establishment, an initial  amount of credits is provided.
317  * @param addr
318  * @param server_channel
319  * @param initial_credits
320  * @param out_cid
321  * @result status
322  */
323 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);
324 
325 /**
326  * @brief Disconnects RFCOMM channel with given identifier.
327  */
328 void rfcomm_disconnect(uint16_t rfcomm_cid);
329 
330 /**
331  * @brief Registers RFCOMM service for a server channel and a maximum frame size, and assigns a packet handler.
332  * This channel provides credits automatically to the remote side -> no flow control
333  * @param packet handler for all channels of this service
334  * @param channel
335  * @param max_frame_size
336  */
337 uint8_t rfcomm_register_service(btstack_packet_handler_t packet_handler, uint8_t channel, uint16_t max_frame_size);
338 
339 /**
340  * @brief Registers RFCOMM service for a server channel and a maximum frame size, and assigns a packet handler.
341  * This channel will use explicit credit management. During channel establishment, an initial amount of credits is provided.
342  * @param packet handler for all channels of this service
343  * @param channel
344  * @param max_frame_size
345  * @param initial_credits
346  */
347 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);
348 
349 /**
350  * @brief Unregister RFCOMM service.
351  */
352 void rfcomm_unregister_service(uint8_t service_channel);
353 
354 /**
355  * @brief Accepts incoming RFCOMM connection.
356  */
357 void rfcomm_accept_connection(uint16_t rfcomm_cid);
358 
359 /**
360  * @brief Deny incoming RFCOMM connection.
361  */
362 void rfcomm_decline_connection(uint16_t rfcomm_cid);
363 
364 /**
365  * @brief Grant more incoming credits to the remote side for the given RFCOMM channel identifier.
366  */
367 void rfcomm_grant_credits(uint16_t rfcomm_cid, uint8_t credits);
368 
369 /**
370  * @brief Checks if RFCOMM can send packet. Returns yes if packet can be sent.
371  */
372 int rfcomm_can_send_packet_now(uint16_t rfcomm_cid);
373 
374 /**
375  * @brief Sends RFCOMM data packet to the RFCOMM channel with given identifier.
376  */
377 int  rfcomm_send(uint16_t rfcomm_cid, uint8_t *data, uint16_t len);
378 
379 /**
380  * @brief Sends Local Line Status, see LINE_STATUS_..
381  */
382 int rfcomm_send_local_line_status(uint16_t rfcomm_cid, uint8_t line_status);
383 
384 /**
385  * @brief Send local modem status. see MODEM_STAUS_..
386  */
387 int rfcomm_send_modem_status(uint16_t rfcomm_cid, uint8_t modem_status);
388 
389 /**
390  * @brief Configure remote port
391  */
392 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);
393 
394 /**
395  * @brief Query remote port
396  */
397 int rfcomm_query_port_configuration(uint16_t rfcomm_cid);
398 
399 /**
400  * @brief Query max frame size
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  * CRC8 functions using ETSI TS 101 369 V6.3.0.
421  * Only used by RFCOMM
422  */
423 uint8_t crc8_check(uint8_t *data, uint16_t len, uint8_t check_sum);
424 uint8_t crc8_calc(uint8_t *data, uint16_t len);
425 
426 /* API_END */
427 
428 #if defined __cplusplus
429 }
430 #endif
431 
432 #endif // __RFCOMM_H
433