source: asp3_tinet_ecnl_arm/trunk/btstack/src/rfcomm.h@ 352

Last change on this file since 352 was 352, checked in by coas-nagasima, 6 years ago

arm向けASP3版ECNLを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-chdr;charset=UTF-8
File size: 13.7 KB
Line 
1/*
2 * Copyright (C) 2009-2012 by Matthias Ringwald
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 MATTHIAS RINGWALD 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 btstack@ringwald.ch
34 *
35 */
36
37/*
38 * RFCOMM.h
39 */
40
41#ifndef __RFCOMM_H
42#define __RFCOMM_H
43
44#include <btstack/btstack.h>
45#include <btstack/utils.h>
46
47#include <stdint.h>
48
49#if defined __cplusplus
50extern "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// Line Status
60#define LINE_STATUS_NO_ERROR 0x00
61#define LINE_STATUS_OVERRUN_ERROR 0x03
62#define LINE_STATUS_PARITY_ERORR 0x05
63#define LINE_STATUS_FRAMING_ERROR 0x09
64
65// Modem Status Flags
66#define MODEM_STATUS_FC 0x02
67#define MODEM_STATUS_RTC 0x04
68#define MODEM_STATUS_RTR 0x08
69#define MODEM_STATUS_IC 0x40
70#define MODEM_STATUS_DV 0x80
71
72typedef enum rpn_baud {
73 RPN_BAUD_2400 = 0,
74 RPN_BAUD_4800,
75 RPN_BAUD_7200,
76 RPN_BAUD_9600,
77 RPN_BAUD_19200,
78 RPN_BAUD_38400,
79 RPN_BAUD_57600,
80 RPN_BAUD_115200,
81 RPN_BAUD_230400
82} rpn_baud_t;
83
84typedef enum rpn_data_bits {
85 RPN_DATA_BITS_5 = 0,
86 RPN_DATA_BITS_6 = 0,
87 RPN_DATA_BITS_7 = 0,
88 RPN_DATA_BITS_8 = 0
89} rpn_data_bits_t;
90
91typedef enum rpn_stop_bits {
92 RPN_STOP_BITS_1_0 = 0,
93 RPN_STOP_BITS_1_5
94} rpn_stop_bits_t;
95
96typedef enum rpn_parity {
97 RPN_PARITY_NONE = 0,
98 RPN_PARITY_ODD = 1,
99 RPN_PARITY_EVEN = 3,
100 RPN_PARITY_MARK = 5,
101 RPN_PARITY_SPACE = 7,
102} rpn_parity_t;
103
104typedef enum rpn_flow_control {
105 RPN_FLOW_CONTROL_XONXOFF_ON_INPUT = 1 << 0,
106 RPN_FLOW_CONTROL_XONXOFF_ON_OUTPUT = 1 << 1,
107 RPN_FLOW_CONTROL_RTR_ON_INPUT = 1 << 2,
108 RPN_FLOW_CONTROL_RTR_ON_OUTPUT = 1 << 3,
109 RPN_FLOW_CONTROL_RTC_ON_INPUT = 1 << 4,
110 RPN_FLOW_CONTROL_RTC_ON_OUTPUT = 1 << 5,
111} rpn_flow_control_t;
112
113#define RPN_PARAM_MASK_0_BAUD 0x01
114#define RPN_PARAM_MASK_0_DATA_BITS 0x02
115#define RPN_PARAM_MASK_0_STOP_BITS 0x04
116#define RPN_PARAM_MASK_0_PARITY 0x08
117#define RPN_PARAM_MASK_0_PARITY_TYPE 0x10
118#define RPN_PARAM_MASK_0_XON_CHAR 0x20
119#define RPN_PARAM_MASK_0_XOFF_CHAR 0x40
120#define RPN_PARAM_MASK_0_RESERVED 0x80
121
122// @note: values are identical to rpn_flow_control_t
123#define RPN_PARAM_MASK_1_XONOFF_ON_INPUT 0x01
124#define RPN_PARAM_MASK_1_XONOFF_ON_OUTPUT 0x02
125#define RPN_PARAM_MASK_1_RTR_ON_INPUT 0x04
126#define RPN_PARAM_MASK_1_RTR_ON_OUTPUT 0x08
127#define RPN_PARAM_MASK_1_RTC_ON_INPUT 0x10
128#define RPN_PARAM_MASK_1_RTC_ON_OUTPUT 0x20
129#define RPN_PARAM_MASK_1_RESERVED_0 0x40
130#define RPN_PARAM_MASK_1_RESERVED_1 0x80
131
132
133// private structs
134typedef enum {
135 RFCOMM_MULTIPLEXER_CLOSED = 1,
136 RFCOMM_MULTIPLEXER_W4_CONNECT, // outgoing
137 RFCOMM_MULTIPLEXER_SEND_SABM_0, // "
138 RFCOMM_MULTIPLEXER_W4_UA_0, // "
139 RFCOMM_MULTIPLEXER_W4_SABM_0, // incoming
140 RFCOMM_MULTIPLEXER_SEND_UA_0,
141 RFCOMM_MULTIPLEXER_OPEN,
142 RFCOMM_MULTIPLEXER_SEND_UA_0_AND_DISC
143} RFCOMM_MULTIPLEXER_STATE;
144
145typedef enum {
146 MULT_EV_READY_TO_SEND = 1,
147
148} RFCOMM_MULTIPLEXER_EVENT;
149
150typedef enum {
151 RFCOMM_CHANNEL_CLOSED = 1,
152 RFCOMM_CHANNEL_W4_MULTIPLEXER,
153 RFCOMM_CHANNEL_SEND_UIH_PN,
154 RFCOMM_CHANNEL_W4_PN_RSP,
155 RFCOMM_CHANNEL_SEND_SABM_W4_UA,
156 RFCOMM_CHANNEL_W4_UA,
157 RFCOMM_CHANNEL_INCOMING_SETUP,
158 RFCOMM_CHANNEL_DLC_SETUP,
159 RFCOMM_CHANNEL_OPEN,
160 RFCOMM_CHANNEL_SEND_UA_AFTER_DISC,
161 RFCOMM_CHANNEL_SEND_DISC,
162 RFCOMM_CHANNEL_W4_UA_AFTER_UA,
163 RFCOMM_CHANNEL_SEND_DM,
164
165} RFCOMM_CHANNEL_STATE;
166
167typedef enum {
168 RFCOMM_CHANNEL_STATE_VAR_NONE = 0,
169 RFCOMM_CHANNEL_STATE_VAR_CLIENT_ACCEPTED = 1 << 0,
170 RFCOMM_CHANNEL_STATE_VAR_RCVD_PN = 1 << 1,
171 RFCOMM_CHANNEL_STATE_VAR_RCVD_RPN = 1 << 2,
172 RFCOMM_CHANNEL_STATE_VAR_RCVD_SABM = 1 << 3,
173
174 RFCOMM_CHANNEL_STATE_VAR_RCVD_MSC_CMD = 1 << 4,
175 RFCOMM_CHANNEL_STATE_VAR_RCVD_MSC_RSP = 1 << 5,
176 RFCOMM_CHANNEL_STATE_VAR_SEND_PN_RSP = 1 << 6,
177 RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_INFO = 1 << 7,
178
179 RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_RSP = 1 << 8,
180 RFCOMM_CHANNEL_STATE_VAR_SEND_UA = 1 << 9,
181 RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_CMD = 1 << 10,
182 RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_RSP = 1 << 11,
183
184 RFCOMM_CHANNEL_STATE_VAR_SEND_CREDITS = 1 << 12,
185 RFCOMM_CHANNEL_STATE_VAR_SENT_MSC_CMD = 1 << 13,
186 RFCOMM_CHANNEL_STATE_VAR_SENT_MSC_RSP = 1 << 14,
187 RFCOMM_CHANNEL_STATE_VAR_SENT_CREDITS = 1 << 15,
188} RFCOMM_CHANNEL_STATE_VAR;
189
190typedef enum {
191 CH_EVT_RCVD_SABM = 1,
192 CH_EVT_RCVD_UA,
193 CH_EVT_RCVD_PN,
194 CH_EVT_RCVD_PN_RSP,
195 CH_EVT_RCVD_DISC,
196 CH_EVT_RCVD_DM,
197 CH_EVT_RCVD_MSC_CMD,
198 CH_EVT_RCVD_MSC_RSP,
199 CH_EVT_RCVD_NSC_RSP,
200 CH_EVT_RCVD_RLS_CMD,
201 CH_EVT_RCVD_RLS_RSP,
202 CH_EVT_RCVD_RPN_CMD,
203 CH_EVT_RCVD_RPN_REQ,
204 CH_EVT_RCVD_CREDITS,
205 CH_EVT_MULTIPLEXER_READY,
206 CH_EVT_READY_TO_SEND,
207} RFCOMM_CHANNEL_EVENT;
208
209typedef struct rfcomm_channel_event {
210 RFCOMM_CHANNEL_EVENT type;
211} rfcomm_channel_event_t;
212
213typedef struct rfcomm_channel_event_pn {
214 rfcomm_channel_event_t super;
215 uint16_t max_frame_size;
216 uint8_t priority;
217 uint8_t credits_outgoing;
218} rfcomm_channel_event_pn_t;
219
220typedef struct rfcomm_rpn_data {
221 uint8_t baud_rate;
222 uint8_t flags;
223 uint8_t flow_control;
224 uint8_t xon;
225 uint8_t xoff;
226 uint8_t parameter_mask_0; // first byte
227 uint8_t parameter_mask_1; // second byte
228} rfcomm_rpn_data_t;
229
230typedef struct rfcomm_channel_event_rpn {
231 rfcomm_channel_event_t super;
232 rfcomm_rpn_data_t data;
233} rfcomm_channel_event_rpn_t;
234
235typedef struct rfcomm_channel_event_rls {
236 rfcomm_channel_event_t super;
237 uint8_t line_status;
238} rfcomm_channel_event_rls_t;
239
240typedef struct rfcomm_channel_event_msc {
241 rfcomm_channel_event_t super;
242 uint8_t modem_status;
243} rfcomm_channel_event_msc_t;
244
245// info regarding potential connections
246typedef struct {
247 // linked list - assert: first field
248 linked_item_t item;
249
250 // server channel
251 uint8_t server_channel;
252
253 // incoming max frame size
254 uint16_t max_frame_size;
255
256 // use incoming flow control
257 uint8_t incoming_flow_control;
258
259 // initial incoming credits
260 uint8_t incoming_initial_credits;
261
262 // client connection
263 void *connection;
264
265 // internal connection
266 btstack_packet_handler_t packet_handler;
267
268} rfcomm_service_t;
269
270// info regarding multiplexer
271// note: spec mandates single multplexer per device combination
272typedef struct {
273 // linked list - assert: first field
274 linked_item_t item;
275
276 timer_source_t timer;
277 int timer_active;
278
279 RFCOMM_MULTIPLEXER_STATE state;
280
281 uint16_t l2cap_cid;
282 uint8_t l2cap_credits;
283
284 uint8_t fcon; // only send if fcon & 1, send rsp if fcon & 0x80
285
286 bd_addr_t remote_addr;
287 hci_con_handle_t con_handle;
288
289 uint8_t outgoing;
290
291 // hack to deal with authentication failure only observed by remote side
292 uint8_t at_least_one_connection;
293
294 uint16_t max_frame_size;
295
296 // send DM for DLCI != 0
297 uint8_t send_dm_for_dlci;
298
299 // non supportec command, 0 if not set
300 uint8_t nsc_command;
301
302 // test data - limited to RFCOMM_TEST_DATA_MAX_LEN
303 uint8_t test_data_len;
304 uint8_t test_data[RFCOMM_TEST_DATA_MAX_LEN];
305
306} rfcomm_multiplexer_t;
307
308// info regarding an actual coneection
309typedef struct {
310 // linked list - assert: first field
311 linked_item_t item;
312
313 rfcomm_multiplexer_t *multiplexer;
314 uint16_t rfcomm_cid;
315 uint8_t outgoing;
316 uint8_t dlci;
317
318 // number of packets granted to client
319 uint8_t packets_granted;
320
321 // credits for outgoing traffic
322 uint8_t credits_outgoing;
323
324 // number of packets remote will be granted
325 uint8_t new_credits_incoming;
326
327 // credits for incoming traffic
328 uint8_t credits_incoming;
329
330 // use incoming flow control
331 uint8_t incoming_flow_control;
332
333 // channel state
334 RFCOMM_CHANNEL_STATE state;
335
336 // state variables used in RFCOMM_CHANNEL_INCOMING
337 RFCOMM_CHANNEL_STATE_VAR state_var;
338
339 // priority set by incoming side in PN
340 uint8_t pn_priority;
341
342 // negotiated frame size
343 uint16_t max_frame_size;
344
345 // local rpn data
346 rfcomm_rpn_data_t rpn_data;
347
348 // rls line status. RFCOMM_RLS_STATUS_INVALID if not set
349 uint8_t rls_line_status;
350
351 // msc modem status.
352 uint8_t msc_modem_status;
353
354 // server channel (see rfcomm_service_t) - NULL => outgoing channel
355 rfcomm_service_t * service;
356
357 // internal connection
358 btstack_packet_handler_t packet_handler;
359
360 // client connection
361 void * connection;
362
363} rfcomm_channel_t;
364
365void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
366
367/** Embedded API **/
368
369// Set up RFCOMM.
370void rfcomm_init(void);
371
372// Set security level required for incoming connections, need to be called before registering services
373void rfcomm_set_required_security_level(gap_security_level_t security_level);
374
375// Register packet handler.
376void rfcomm_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type,
377 uint16_t channel, uint8_t *packet, uint16_t size));
378
379// Creates RFCOMM connection (channel) to a given server channel on a remote device with baseband address. A new baseband connection will be initiated if necessary.
380// This channel will automatically provide enough credits to the remote side
381void rfcomm_create_channel_internal(void * connection, bd_addr_t *addr, uint8_t channel);
382
383// Creates RFCOMM connection (channel) to a given server channel on a remote device with baseband address. new baseband connection will be initiated if necessary.
384// This channel will use explicit credit management. During channel establishment, an initial amount of credits is provided.
385void rfcomm_create_channel_with_initial_credits_internal(void * connection, bd_addr_t *addr, uint8_t server_channel, uint8_t initial_credits);
386
387// Disconencts RFCOMM channel with given identifier.
388void rfcomm_disconnect_internal(uint16_t rfcomm_cid);
389
390// Registers RFCOMM service for a server channel and a maximum frame size, and assigns a packet handler. On embedded systems, use NULL for connection parameter.
391// This channel provides automatically enough credits to the remote side.
392void rfcomm_register_service_internal(void * connection, uint8_t channel, uint16_t max_frame_size);
393
394// Registers RFCOMM service for a server channel and a maximum frame size, and assigns a packet handler. On embedded systems, use NULL for connection parameter.
395// This channel will use explicit credit management. During channel establishment, an initial amount of credits is provided.
396void rfcomm_register_service_with_initial_credits_internal(void * connection, uint8_t channel, uint16_t max_frame_size, uint8_t initial_credits);
397
398// Unregister RFCOMM service.
399void rfcomm_unregister_service_internal(uint8_t service_channel);
400
401// Accepts/Deny incoming RFCOMM connection.
402void rfcomm_accept_connection_internal(uint16_t rfcomm_cid);
403void rfcomm_decline_connection_internal(uint16_t rfcomm_cid);
404
405// Grant more incoming credits to the remote side for the given RFCOMM channel identifier.
406void rfcomm_grant_credits(uint16_t rfcomm_cid, uint8_t credits);
407
408// Checks if RFCOMM can send packet. Returns yes if packet can be sent.
409int rfcomm_can_send_packet_now(uint16_t rfcomm_cid);
410
411// Sends RFCOMM data packet to the RFCOMM channel with given identifier.
412int rfcomm_send_internal(uint16_t rfcomm_cid, uint8_t *data, uint16_t len);
413
414// Sends Local Lnie Status, see LINE_STATUS_..
415int rfcomm_send_local_line_status(uint16_t rfcomm_cid, uint8_t line_status);
416
417// Sned local modem status. see MODEM_STAUS_..
418int rfcomm_send_modem_status(uint16_t rfcomm_cid, uint8_t modem_status);
419
420// Configure remote port
421int 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);
422
423// Query remote port
424int rfcomm_query_port_configuration(uint16_t rfcomm_cid);
425
426// allow to create rfcomm packet in outgoing buffer
427int rfcomm_reserve_packet_buffer(void);
428void rfcomm_release_packet_buffer(void);
429uint8_t * rfcomm_get_outgoing_buffer(void);
430uint16_t rfcomm_get_max_frame_size(uint16_t rfcomm_cid);
431int rfcomm_send_prepared(uint16_t rfcomm_cid, uint16_t len);
432
433#if defined __cplusplus
434}
435#endif
436
437#endif // __RFCOMM_H
Note: See TracBrowser for help on using the repository browser.