GNU Radio's BLUETOOTH Package
bluetooth_packet.h
Go to the documentation of this file.
1/* -*- c++ -*- */
2/*
3 * Copyright 2008, 2009 Dominic Spill, Michael Ossmann
4 * Copyright 2007 Dominic Spill
5 * Copyright 2005, 2006 Free Software Foundation, Inc.
6 *
7 * This file is part of gr-bluetooth
8 *
9 * gr-bluetooth 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, or (at your option)
12 * any later version.
13 *
14 * gr-bluetooth 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 gr-bluetooth; see the file COPYING. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street,
22 * Boston, MA 02110-1301, USA.
23 */
24//FIXME this file should not be here - copied from gr-bluetooth/src/lib
25#ifndef INCLUDED_BLUETOOTH_PACKET_H
26#define INCLUDED_BLUETOOTH_PACKET_H
27
28#include <stdint.h>
29#include <string>
30#include <cstdio>
31#include <boost/enable_shared_from_this.hpp>
32
33using namespace std;
34
36typedef std::shared_ptr<bluetooth_packet> bluetooth_packet_sptr;
37
38/*!
39 * \brief Return a shared_ptr to a new instance of bluetooth_packet.
40 */
41bluetooth_packet_sptr bluetooth_make_packet(char *stream, int length);
42
43/* construct with known CLKN and channel */
44bluetooth_packet_sptr bluetooth_make_packet(char *stream, int length,
45 uint32_t clkn, int channel);
46
47
48class bluetooth_packet
49{
50private:
51 /* allow bluetooth_make_packet to access the private constructor. */
52 friend bluetooth_packet_sptr bluetooth_make_packet(char *stream, int length);
53 friend bluetooth_packet_sptr bluetooth_make_packet(char *stream, int length,
54 uint32_t clkn, int channel);
55
56 /* constructor */
57 bluetooth_packet(char *stream, int length);
58
59 /* maximum number of symbols */
60 static const int MAX_SYMBOLS = 3125;
61
62 /* minimum header bit errors to indicate that this is an ID packet */
63 static const int ID_THRESHOLD = 5;
64
65 /* index into whitening data array */
66 static const uint8_t INDICES[64];
67
68 /* whitening data */
69 static const uint8_t WHITENING_DATA[127];
70
71 /* lookup table for preamble hamming distance */
72 static const uint8_t PREAMBLE_DISTANCE[32];
73
74 /* lookup table for trailer hamming distance */
75 static const uint8_t TRAILER_DISTANCE[2048];
76
77 /* string representations of packet type */
78 static const string TYPE_NAMES[16];
79
80 /* the raw symbol stream, one bit per char */
81 //FIXME maybe this should be a vector so we can grow it only to the size
82 //needed and later shrink it if we find we have more symbols than necessary
83 char d_symbols[MAX_SYMBOLS];
84
85 /* lower address part found in access code */
86 uint32_t d_LAP;
87
88 /* upper address part */
89 uint8_t d_UAP;
90
91 /* non-significant address part */
92 uint16_t d_NAP;
93
94 /* number of symbols */
95 int d_length;
96
97 /* packet type */
98 int d_packet_type;
99
100 /* packet header, one bit per char */
101 char d_packet_header[18];
102
103 /* payload header, one bit per char */
104 /* the packet may have a payload header of 0, 1, or 2 bytes, reserving 2 */
105 char d_payload_header[16];
106
107 /* number of payload header bytes */
108 /* set to 0, 1, 2, or -1 for unknown */
109 int d_payload_header_length;
110
111 /* LLID field of payload header (2 bits) */
112 uint8_t d_payload_llid;
113
114 /* flow field of payload header (1 bit) */
115 uint8_t d_payload_flow;
116
117 /* payload length: the total length of the asynchronous data in bytes.
118 * This does not include the length of synchronous data, such as the voice
119 * field of a DV packet.
120 * If there is a payload header, this payload length is payload body length
121 * (the length indicated in the payload header's length field) plus
122 * d_payload_header_length plus 2 bytes CRC (if present).
123 */
124 int d_payload_length;
125
126 /* The actual payload data in host format
127 * Ready for passing to wireshark
128 * 2744 is the maximum length, but most packets are shorter.
129 * Dynamic allocation would probably be better in the long run but is
130 * problematic in the short run.
131 */
132 char d_payload[2744];
133
134 /* is the packet whitened? */
135 bool d_whitened;
136
137 /* do we know the UAP/NAP? */
138 bool d_have_UAP;
139 bool d_have_NAP;
140
141 /* do we know the master clock? */
142 bool d_have_clk6;
143 bool d_have_clk27;
144
145 bool d_have_payload;
146
147 /* CLK1-27 of master */
148 uint32_t d_clock;
149
150 /* type-specific CRC checks and decoding */
151 int fhs(int clock);
152 int DM(int clock);
153 int DH(int clock);
154 int EV3(int clock);
155 int EV4(int clock);
156 int EV5(int clock);
157 int HV(int clock);
158
159 /* decode payload header, return value indicates success */
160 bool decode_payload_header(char *stream, int clock, int header_bytes, int size, bool fec);
161
162 /* Remove the whitening from an air order array */
163 void unwhiten(char* input, char* output, int clock, int length, int skip);
164
165 /* verify the payload CRC */
166 bool payload_crc();
167
168public:
170
171 /* native (local) clock */
172 uint32_t d_clkn;
173
174 /* search a symbol stream to find a packet, return index */
175 static int sniff_ac(char *stream, int stream_length);
176
177 /* Error correction coding for Access Code */
178 static uint8_t *lfsr(uint8_t *data, int length, int k, uint8_t *g);
179
180 /* Reverse the bits in a byte */
181 static uint8_t reverse(char byte);
182
183 /* Generate Access Code from an LAP */
184 static uint8_t *acgen(int LAP);
185
186 /* Convert from normal bytes to one-LSB-per-byte format */
187 static void convert_to_grformat(uint8_t input, uint8_t *output);
188
189 /* Decode 1/3 rate FEC, three like symbols in a row */
190 static bool unfec13(char *input, char *output, int length);
191
192 /* Decode 2/3 rate FEC, a (15,10) shortened Hamming code */
193 static char *unfec23(char *input, int length);
194
195 /* When passed 10 bits of data this returns a pointer to a 5 bit hamming code */
196 static char *fec23gen(char *data);
197
198 /* Create an Access Code from LAP and check it against stream */
199 static bool check_ac(char *stream, int LAP);
200
201 /* Convert some number of bits of an air order array to a host order integer */
202 static uint8_t air_to_host8(char *air_order, int bits);
203 static uint16_t air_to_host16(char *air_order, int bits);
204 static uint32_t air_to_host32(char *air_order, int bits);
205 // hmmm, maybe these should have pointer output so they can be overloaded
206
207 /* Convert some number of bits in a host order integer to an air order array */
208 static void host_to_air(uint8_t host_order, char *air_order, int bits);
209
210 /* Create the 16bit CRC for packet payloads - input air order stream */
211 static uint16_t crcgen(char *payload, int length, int UAP);
212
213 /* extract UAP by reversing the HEC computation */
214 static int UAP_from_hec(uint16_t data, uint8_t hec);
215
216 /* check if the packet's CRC is correct for a given clock (CLK1-6) */
217 int crc_check(int clock);
218
219 /* decode the packet header */
221
222 /* decode the packet header */
224
225 /* decode the whole packet */
226 void decode();
227
228 /* print packet information */
229 void print();
230
231 /* format payload for tun interface */
232 char *tun_format();
233
234 /* return the packet's LAP */
235 uint32_t get_LAP();
236
237 /* return the packet's UAP */
238 uint8_t get_UAP();
239
240 /* set the packet's UAP */
241 void set_UAP(uint8_t UAP);
242
243 /* set the packet's NAP */
244 void set_NAP(uint16_t NAP);
245
246 /* is the packet whitened? */
248
249 /* set the packet's whitened flag */
250 void set_whitened(bool whitened);
251
252 /* have we decoded the payload yet? */
254
255 /* return the packet's clock (CLK1-27) */
256 uint32_t get_clock();
257
258 /* set the packet's clock */
259 void set_clock(uint32_t clk6, bool have27);
260
261 /* Retrieve the length of the payload data */
263
264 int get_type();
265
266 /* try a clock value (CLK1-6) to unwhiten packet header,
267 * sets resultant d_packet_type and d_UAP, returns UAP.
268 */
269 uint8_t try_clock(int clock);
270
271 /* check to see if the packet has a header */
273
274 /* extract LAP from FHS payload */
275 uint32_t lap_from_fhs();
276
277 /* extract UAP from FHS payload */
278 uint8_t uap_from_fhs();
279
280 /* extract NAP from FHS payload */
281 uint16_t nap_from_fhs();
282
283 /* extract clock from FHS payload */
284 uint32_t clock_from_fhs();
285
286 /* destructor */
288};
289
290#endif /* INCLUDED_BLUETOOTH_PACKET_H */
bluetooth_packet_sptr bluetooth_make_packet(char *stream, int length)
Return a shared_ptr to a new instance of bluetooth_packet.
Definition bluetooth_packet.h:49
void set_UAP(uint8_t UAP)
static uint8_t reverse(char byte)
uint32_t get_clock()
char * tun_format()
void set_NAP(uint16_t NAP)
void set_whitened(bool whitened)
void set_clock(uint32_t clk6, bool have27)
uint32_t lap_from_fhs()
int d_channel
Definition bluetooth_packet.h:169
friend bluetooth_packet_sptr bluetooth_make_packet(char *stream, int length)
Return a shared_ptr to a new instance of bluetooth_packet.
static char * unfec23(char *input, int length)
static int sniff_ac(char *stream, int stream_length)
static uint8_t * acgen(int LAP)
uint8_t uap_from_fhs()
static int UAP_from_hec(uint16_t data, uint8_t hec)
uint8_t try_clock(int clock)
uint16_t nap_from_fhs()
static void host_to_air(uint8_t host_order, char *air_order, int bits)
static void convert_to_grformat(uint8_t input, uint8_t *output)
static uint8_t * lfsr(uint8_t *data, int length, int k, uint8_t *g)
static uint16_t air_to_host16(char *air_order, int bits)
int get_payload_length()
friend bluetooth_packet_sptr bluetooth_make_packet(char *stream, int length, uint32_t clkn, int channel)
static uint8_t air_to_host8(char *air_order, int bits)
uint8_t get_UAP()
static bool unfec13(char *input, char *output, int length)
uint32_t clock_from_fhs()
int crc_check(int clock)
static bool check_ac(char *stream, int LAP)
static uint16_t crcgen(char *payload, int length, int UAP)
uint32_t d_clkn
Definition bluetooth_packet.h:172
static char * fec23gen(char *data)
uint32_t get_LAP()
static uint32_t air_to_host32(char *air_order, int bits)