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