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