xref: /libbtbb/lib/src/bluetooth_packet.h (revision e25b118a40ed6b5c2ea76bae29e388cfbc2f6e92)
1 /* -*- c -*- */
2 /*
3  * Copyright 2007 - 2013 Dominic Spill, Michael Ossmann, Will Code
4  *
5  * This file is part of libbtbb
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with libbtbb; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 #ifndef INCLUDED_BLUETOOTH_PACKET_H
23 #define INCLUDED_BLUETOOTH_PACKET_H
24 #include "btbb.h"
25 
26 /* maximum number of symbols */
27 #define MAX_SYMBOLS 3125
28 
29 /* maximum number of payload bits */
30 #define MAX_PAYLOAD_LENGTH 2744
31 
32 /* minimum header bit errors to indicate that this is an ID packet */
33 #define ID_THRESHOLD 5
34 
35 typedef struct btbb_packet {
36 
37 	uint32_t refcount;
38 
39 	uint32_t flags;
40 
41 	uint8_t channel; /* Bluetooth channel (0-79) */
42 	uint8_t UAP;     /* upper address part */
43 	uint16_t NAP;    /* non-significant address part */
44 	uint32_t LAP;    /* lower address part found in access code */
45 
46 	uint8_t packet_type;
47 	uint8_t packet_lt_addr; /* LLID field of payload header (2 bits) */
48 	uint8_t packet_flags; /* Flags - FLOW/ARQN/SQEN */
49 	uint8_t packet_hec; /* Flags - FLOW/ARQN/SQEN */
50 
51 	/* packet header, one bit per char */
52 	char packet_header[18];
53 
54 	/* number of payload header bytes: 0, 1, 2, or -1 for
55 	 * unknown. payload is one bit per char. */
56 	int payload_header_length;
57 	char payload_header[16];
58 
59 	/* LLID field of payload header (2 bits) */
60 	uint8_t payload_llid;
61 
62 	/* flow field of payload header (1 bit) */
63 	uint8_t payload_flow;
64 
65 	/* payload length: the total length of the asynchronous data
66 	* in bytes.  This does not include the length of synchronous
67 	* data, such as the voice field of a DV packet.  If there is a
68 	* payload header, this payload length is payload body length
69 	* (the length indicated in the payload header's length field)
70 	* plus payload_header_length plus 2 bytes CRC (if present).
71 	*/
72 	int payload_length;
73 
74 	/* The actual payload data in host format
75 	* Ready for passing to wireshark
76 	* 2744 is the maximum length, but most packets are shorter.
77 	* Dynamic allocation would probably be better in the long run but is
78 	* problematic in the short run.
79 	*/
80 	char payload[MAX_PAYLOAD_LENGTH];
81 
82 	uint16_t crc;
83 	uint32_t clock; /* CLK1-27 of master */
84 	uint32_t clkn;  /* native (local) clock, CLK0-27 */
85 	uint8_t ac_errors; /* Number of bit errors in the AC */
86 
87 	/* the raw symbol stream (less the preamble), one bit per char */
88 	//FIXME maybe this should be a vector so we can grow it only
89 	//to the size needed and later shrink it if we find we have
90 	//more symbols than necessary
91 	uint16_t length; /* number of symbols */
92 	char symbols[MAX_SYMBOLS];
93 
94 } btbb_packet;
95 
96 /* type-specific CRC checks and decoding */
97 int fhs(int clock, btbb_packet* p);
98 int DM(int clock, btbb_packet* p);
99 int DH(int clock, btbb_packet* p);
100 int EV3(int clock, btbb_packet* p);
101 int EV4(int clock, btbb_packet* p);
102 int EV5(int clock, btbb_packet* p);
103 int HV(int clock, btbb_packet* p);
104 
105 /* check if the packet's CRC is correct for a given clock (CLK1-6) */
106 int crc_check(int clock, btbb_packet* p);
107 
108 /* format payload for tun interface */
109 char *tun_format(btbb_packet* p);
110 
111 /* try a clock value (CLK1-6) to unwhiten packet header,
112  * sets resultant d_packet_type and d_UAP, returns UAP.
113  */
114 uint8_t try_clock(int clock, btbb_packet* p);
115 
116 /* extract LAP from FHS payload */
117 uint32_t lap_from_fhs(btbb_packet* p);
118 
119 /* extract UAP from FHS payload */
120 uint8_t uap_from_fhs(btbb_packet* p);
121 
122 /* extract NAP from FHS payload */
123 uint16_t nap_from_fhs(btbb_packet* p);
124 
125 /* extract clock from FHS payload */
126 uint32_t clock_from_fhs(btbb_packet* p);
127 
128 #endif /* INCLUDED_BLUETOOTH_PACKET_H */
129