• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2001-2002  Wayne Lee <waynelee@qualcomm.com>
6  *  Copyright (C) 2003-2011  Marcel Holtmann <marcel@holtmann.org>
7  *
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24 
25 #ifndef __RFCOMM_H
26 #define __RFCOMM_H
27 
28 #include <endian.h>
29 
30 #define RFCOMM_PSM 3
31 
32 #define TRUE  1
33 #define FALSE 0
34 
35 #define RFCOMM_MAX_CONN 10
36 #define BT_NBR_DATAPORTS RFCOMM_MAX_CONN
37 
38 #define GET_BIT(pos,bitfield) ((bitfield[(pos)/32]) & (1 << ((pos) % 32)))
39 #define SET_BIT(pos,bitfield) ((bitfield[(pos)/32]) |= (1 << ((pos) % 32)))
40 #define CLR_BIT(pos,bitfield) ((bitfield[(pos)/32]) &= ((1 << ((pos) % 32)) ^ (~0)))
41 
42 /* Sets the P/F-bit in the control field */
43 #define SET_PF(ctr) ((ctr) | (1 << 4))
44 /* Clears the P/F-bit in the control field */
45 #define CLR_PF(ctr) ((ctr) & 0xef)
46 /* Returns the P/F-bit */
47 #define GET_PF(ctr) (((ctr) >> 4) & 0x1)
48 
49 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
50 
51 /* Endian-swapping macros for structs */
52 #define swap_long_frame(x) ((x)->h.length.val = le16_to_cpu((x)->h.length.val))
53 #define swap_mcc_long_frame(x) (swap_long_frame(x))
54 
55 /* Used for UIH packets */
56 #define SHORT_CRC_CHECK 2
57 /* Used for all packet exepts for the UIH packets */
58 #define LONG_CRC_CHECK 3
59 /* Short header for short UIH packets */
60 #define SHORT_HDR 2
61 /* Long header for long UIH packets */
62 #define LONG_HDR 3
63 
64 /* FIXME: Should this one be defined here? */
65 #define SHORT_PAYLOAD_SIZE 127
66 /* Used for setting the EA field in different packets, really neccessary? */
67 #define EA 1
68 /* Yes the FCS size is only one byte */
69 #define FCS_SIZE 1
70 
71 #define RFCOMM_MAX_HDR_SIZE 5
72 
73 #define MAX_CREDITS   30
74 #define START_CREDITS 7
75 #define MIN_CREDITS   6
76 
77 #define DEF_RFCOMM_MTU 127
78 
79 /* The values in the control field when sending ordinary rfcomm packets */
80 #define SABM 0x2f	/* set asynchronous balanced mode */
81 #define UA   0x63	/* unnumbered acknolodgement */
82 #define DM   0x0f	/* disconnected mode */
83 #define DISC 0x43	/* disconnect */
84 #define UIH  0xef	/* unnumbered information with header check (only) */
85 #define UI   0x03	/* unnumbered information (with all data check) */
86 
87 #define SABM_SIZE 4
88 #define UA_SIZE   4
89 
90 /* The values in the type field in a multiplexer command packet */
91 #define PN    (0x80 >> 2)	/* parameter negotiation */
92 #define PSC   (0x40 >> 2)	/* power saving control */
93 #define CLD   (0xc0 >> 2)	/* close down */
94 #define TEST  (0x20 >> 2)	/* test */
95 #define FCON  (0xa0 >> 2)	/* flow control on */
96 #define FCOFF (0x60 >> 2)	/* flow control off */
97 #define MSC   (0xe0 >> 2)	/* modem status command */
98 #define NSC   (0x10 >> 2)	/* not supported command response */
99 #define RPN   (0x90 >> 2)	/* remote port negotiation */
100 #define RLS   (0x50 >> 2)	/* remote line status */
101 #define SNC   (0xd0 >> 2)	/* service negotiation command */
102 
103 /* Define of some V.24 signals modem control signals in RFCOMM */
104 #define DV  0x80	/* data valid */
105 #define IC  0x40	/* incoming call */
106 #define RTR 0x08	/* ready to receive */
107 #define RTC 0x04	/* ready to communicate */
108 #define FC  0x02	/* flow control (unable to accept frames) */
109 
110 #define CTRL_CHAN 0	/* The control channel is defined as DLCI 0 in rfcomm */
111 #define MCC_CMD 1	 /* Multiplexer command */
112 #define MCC_RSP 0	 /* Multiplexer response */
113 
114 #if __BYTE_ORDER == __LITTLE_ENDIAN
115 
116 typedef struct parameter_mask {
117 	uint8_t bit_rate:1;
118 	uint8_t data_bits:1;
119 	uint8_t stop_bit:1;
120 	uint8_t parity:1;
121 	uint8_t parity_type:1;
122 	uint8_t xon:1;
123 	uint8_t xoff:1;
124 	uint8_t res1:1;
125 	uint8_t xon_input:1;
126 	uint8_t xon_output:1;
127 	uint8_t rtr_input:1;
128 	uint8_t rtr_output:1;
129 	uint8_t rtc_input:1;
130 	uint8_t rtc_output:1;
131 	uint8_t res2:2;
132 } __attribute__ ((packed)) parameter_mask;
133 
134 typedef struct rpn_values {
135 	uint8_t bit_rate;
136 	uint8_t data_bits:2;
137 	uint8_t stop_bit:1;
138 	uint8_t parity:1;
139 	uint8_t parity_type:2;
140 	uint8_t res1:2;
141 	uint8_t xon_input:1;
142 	uint8_t xon_output:1;
143 	uint8_t rtr_input:1;
144 	uint8_t rtr_output:1;
145 	uint8_t rtc_input:1;
146 	uint8_t rtc_output:1;
147 	uint8_t res2:2;
148 	uint8_t xon;
149 	uint8_t xoff;
150 	uint16_t pm;
151 	//parameter_mask pm;
152 } __attribute__ ((packed)) rpn_values;
153 
154 #elif __BYTE_ORDER == __BIG_ENDIAN
155 
156 typedef struct parameter_mask {
157 	uint8_t res1:1;
158 	uint8_t xoff:1;
159 	uint8_t xon:1;
160 	uint8_t parity_type:1;
161 	uint8_t parity:1;
162 	uint8_t stop_bit:1;
163 	uint8_t data_bits:1;
164 	uint8_t bit_rate:1;
165 	uint8_t res2:2;
166 	uint8_t rtc_output:1;
167 	uint8_t rtc_input:1;
168 	uint8_t rtr_output:1;
169 	uint8_t rtr_input:1;
170 	uint8_t xon_output:1;
171 	uint8_t xon_input:1;
172 
173 } __attribute__ ((packed)) parameter_mask;
174 
175 typedef struct rpn_values {
176 	uint8_t bit_rate;
177 	uint8_t res1:2;
178 	uint8_t parity_type:2;
179 	uint8_t parity:1;
180 	uint8_t stop_bit:1;
181 	uint8_t data_bits:2;
182 	uint8_t res2:2;
183 	uint8_t rtc_output:1;
184 	uint8_t rtc_input:1;
185 	uint8_t rtr_output:1;
186 	uint8_t rtr_input:1;
187 	uint8_t xon_output:1;
188 	uint8_t xon_input:1;
189 	uint8_t xon;
190 	uint8_t xoff;
191 	uint16_t pm;
192 	//parameter_mask pm;
193 } __attribute__ ((packed)) rpn_values;
194 
195 #else
196 #error "Unknown byte order"
197 #endif
198 
199 /* Typedefinitions of stuctures used for creating and parsing packets, for a
200  * further description of the structures please se the bluetooth core
201  * specification part F:1 and the ETSI TS 07.10 specification  */
202 
203 #if __BYTE_ORDER == __LITTLE_ENDIAN
204 
205 typedef struct address_field {
206 	uint8_t ea:1;
207 	uint8_t cr:1;
208 	uint8_t d:1;
209 	uint8_t server_chn:5;
210 } __attribute__ ((packed)) address_field;
211 
212 typedef struct short_length {
213 	uint8_t ea:1;
214 	uint8_t len:7;
215 } __attribute__ ((packed)) short_length;
216 
217 typedef union long_length {
218 	struct bits {
219 		uint8_t ea:1;
220 		unsigned short len:15;
221 	} __attribute__ ((packed)) bits ;
222 	uint16_t val ;
223 } __attribute__ ((packed)) long_length;
224 
225 typedef struct short_frame_head {
226 	address_field addr;
227 	uint8_t control;
228 	short_length length;
229 } __attribute__ ((packed)) short_frame_head;
230 
231 typedef struct short_frame {
232 	short_frame_head h;
233 	uint8_t data[0];
234 } __attribute__ ((packed)) short_frame;
235 
236 typedef struct long_frame_head {
237 	address_field addr;
238 	uint8_t control;
239 	long_length length;
240 	uint8_t data[0];
241 } __attribute__ ((packed)) long_frame_head;
242 
243 typedef struct long_frame {
244 	long_frame_head h;
245 	uint8_t data[0];
246 } __attribute__ ((packed)) long_frame;
247 
248 /* Typedefinitions for structures used for the multiplexer commands */
249 typedef struct mcc_type {
250 	uint8_t ea:1;
251 	uint8_t cr:1;
252 	uint8_t type:6;
253 } __attribute__ ((packed)) mcc_type;
254 
255 typedef struct mcc_short_frame_head {
256 	mcc_type type;
257 	short_length length;
258 	uint8_t value[0];
259 } __attribute__ ((packed)) mcc_short_frame_head;
260 
261 typedef struct mcc_short_frame {
262 	mcc_short_frame_head h;
263 	uint8_t value[0];
264 } __attribute__ ((packed)) mcc_short_frame;
265 
266 typedef struct mcc_long_frame_head {
267 	mcc_type type;
268 	long_length length;
269 	uint8_t value[0];
270 } __attribute__ ((packed)) mcc_long_frame_head;
271 
272 typedef struct mcc_long_frame {
273 	mcc_long_frame_head h;
274 	uint8_t value[0];
275 } __attribute__ ((packed)) mcc_long_frame;
276 
277 /* MSC-command */
278 typedef struct v24_signals {
279 	uint8_t ea:1;
280 	uint8_t fc:1;
281 	uint8_t rtc:1;
282 	uint8_t rtr:1;
283 	uint8_t reserved:2;
284 	uint8_t ic:1;
285 	uint8_t dv:1;
286 } __attribute__ ((packed)) v24_signals;
287 
288 typedef struct break_signals {
289 	uint8_t ea:1;
290 	uint8_t b1:1;
291 	uint8_t b2:1;
292 	uint8_t b3:1;
293 	uint8_t len:4;
294 } __attribute__ ((packed)) break_signals;
295 
296 typedef struct msc_msg {
297 	short_frame_head s_head;
298 	mcc_short_frame_head mcc_s_head;
299 	address_field dlci;
300 	v24_signals v24_sigs;
301 	//break_signals break_sigs;
302 	uint8_t fcs;
303 } __attribute__ ((packed)) msc_msg;
304 
305 typedef struct rpn_msg {
306 	short_frame_head s_head;
307 	mcc_short_frame_head mcc_s_head;
308 	address_field dlci;
309 	rpn_values rpn_val;
310 	uint8_t fcs;
311 } __attribute__ ((packed)) rpn_msg;
312 
313 /* RLS-command */
314 typedef struct rls_msg {
315 	short_frame_head s_head;
316 	mcc_short_frame_head mcc_s_head;
317 	address_field dlci;
318 	uint8_t error:4;
319 	uint8_t res:4;
320 	uint8_t fcs;
321 } __attribute__ ((packed)) rls_msg;
322 
323 /* PN-command */
324 typedef struct pn_msg {
325 	short_frame_head s_head;
326 	mcc_short_frame_head mcc_s_head;
327 /* The res1, res2 and res3 values have to be set to 0 by the sender */
328 	uint8_t dlci:6;
329 	uint8_t res1:2;
330 	uint8_t frame_type:4;
331 	uint8_t credit_flow:4;
332 	uint8_t prior:6;
333 	uint8_t res2:2;
334 	uint8_t ack_timer;
335 	uint16_t frame_size:16;
336 	uint8_t max_nbrof_retrans;
337 	uint8_t credits;
338 	uint8_t fcs;
339 } __attribute__ ((packed)) pn_msg;
340 
341 /* NSC-command */
342 typedef struct nsc_msg {
343 	short_frame_head s_head;
344 	mcc_short_frame_head mcc_s_head;
345 	mcc_type command_type;
346 	uint8_t fcs;
347 } __attribute__ ((packed)) nsc_msg;
348 
349 #elif __BYTE_ORDER == __BIG_ENDIAN
350 
351 typedef struct address_field {
352 	uint8_t server_chn:5;
353 	uint8_t d:1;
354 	uint8_t cr:1;
355 	uint8_t ea:1;
356 } __attribute__ ((packed)) address_field;
357 
358 typedef struct short_length {
359 	uint8_t len:7;
360 	uint8_t ea:1;
361 } __attribute__ ((packed)) short_length;
362 
363 typedef union long_length {
364 	struct bits {
365 		unsigned short len:15;
366 		uint8_t ea:1;
367 	} __attribute__ ((packed)) bits;
368 	uint16_t val;
369 } __attribute__ ((packed)) long_length;
370 
371 typedef struct short_frame_head {
372 	address_field addr;
373 	uint8_t control;
374 	short_length length;
375 } __attribute__ ((packed)) short_frame_head;
376 
377 typedef struct short_frame {
378 	short_frame_head h;
379 	uint8_t data[0];
380 } __attribute__ ((packed)) short_frame;
381 
382 typedef struct long_frame_head {
383 	address_field addr;
384 	uint8_t control;
385 	long_length length;
386 	uint8_t data[0];
387 } __attribute__ ((packed)) long_frame_head;
388 
389 typedef struct long_frame {
390 	long_frame_head h;
391 	uint8_t data[0];
392 } __attribute__ ((packed)) long_frame;
393 
394 typedef struct mcc_type {
395 	uint8_t type:6;
396 	uint8_t cr:1;
397 	uint8_t ea:1;
398 } __attribute__ ((packed)) mcc_type;
399 
400 typedef struct mcc_short_frame_head {
401 	mcc_type type;
402 	short_length length;
403 	uint8_t value[0];
404 } __attribute__ ((packed)) mcc_short_frame_head;
405 
406 typedef struct mcc_short_frame {
407 	mcc_short_frame_head h;
408 	uint8_t value[0];
409 } __attribute__ ((packed)) mcc_short_frame;
410 
411 typedef struct mcc_long_frame_head {
412 	mcc_type type;
413 	long_length length;
414 	uint8_t value[0];
415 } __attribute__ ((packed)) mcc_long_frame_head;
416 
417 typedef struct mcc_long_frame {
418 	mcc_long_frame_head h;
419 	uint8_t value[0];
420 } __attribute__ ((packed)) mcc_long_frame;
421 
422 typedef struct v24_signals {
423 	uint8_t dv:1;
424 	uint8_t ic:1;
425 	uint8_t reserved:2;
426 	uint8_t rtr:1;
427 	uint8_t rtc:1;
428 	uint8_t fc:1;
429 	uint8_t ea:1;
430 } __attribute__ ((packed)) v24_signals;
431 
432 typedef struct break_signals {
433 	uint8_t len:4;
434 	uint8_t b3:1;
435 	uint8_t b2:1;
436 	uint8_t b1:1;
437 	uint8_t ea:1;
438 } __attribute__ ((packed)) break_signals;
439 
440 typedef struct msc_msg {
441 	short_frame_head s_head;
442 	mcc_short_frame_head mcc_s_head;
443 	address_field dlci;
444 	v24_signals v24_sigs;
445 	//break_signals break_sigs;
446 	uint8_t fcs;
447 } __attribute__ ((packed)) msc_msg;
448 
449 typedef struct rpn_msg {
450 	short_frame_head s_head;
451 	mcc_short_frame_head mcc_s_head;
452 	address_field dlci;
453 	rpn_values rpn_val;
454 	uint8_t fcs;
455 } __attribute__ ((packed)) rpn_msg;
456 
457 typedef struct rls_msg {
458 	short_frame_head s_head;
459 	mcc_short_frame_head mcc_s_head;
460 	address_field dlci;
461 	uint8_t res:4;
462 	uint8_t error:4;
463 	uint8_t fcs;
464 } __attribute__ ((packed)) rls_msg;
465 
466 typedef struct pn_msg {
467 	short_frame_head s_head;
468 	mcc_short_frame_head mcc_s_head;
469 	uint8_t res1:2;
470 	uint8_t dlci:6;
471 	uint8_t credit_flow:4;
472 	uint8_t frame_type:4;
473 	uint8_t res2:2;
474 	uint8_t prior:6;
475 	uint8_t ack_timer;
476 	uint16_t frame_size:16;
477 	uint8_t max_nbrof_retrans;
478 	uint8_t credits;
479 	uint8_t fcs;
480 } __attribute__ ((packed)) pn_msg;
481 
482 typedef struct nsc_msg {
483 	short_frame_head s_head;
484 	mcc_short_frame_head mcc_s_head;
485 	mcc_type command_type;
486 	uint8_t fcs;
487 } __attribute__ ((packed)) nsc_msg;
488 
489 #else
490 #error "Unknown byte order"
491 #error Processor endianness unknown!
492 #endif
493 
494 #endif /* __RFCOMM_H */
495