xref: /libbtbb/lib/src/btbb.h (revision 25d64f63a355f4c01d10cf4f69da1c2246b040d9)
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_BTBB_H
23 #define INCLUDED_BTBB_H
24 
25 #include <stdint.h>
26 
27 #define BTBB_WHITENED    0
28 #define BTBB_NAP_VALID   1
29 #define BTBB_UAP_VALID   2
30 #define BTBB_LAP_VALID   3
31 #define BTBB_CLK6_VALID  4
32 #define BTBB_CLK27_VALID 5
33 #define BTBB_CRC_CORRECT 6
34 #define BTBB_HAS_PAYLOAD 7
35 #define BTBB_IS_EDR      8
36 
37 #define BTBB_HOP_REVERSAL_INIT 9
38 #define BTBB_GOT_FIRST_PACKET  10
39 #define BTBB_IS_AFH            11
40 #define BTBB_LOOKS_LIKE_AFH    12
41 #define BTBB_IS_ALIASED        13
42 #define BTBB_FOLLOWING         14
43 
44 /* Payload modulation */
45 #define BTBB_MOD_GFSK              0x00
46 #define BTBB_MOD_PI_OVER_2_DQPSK   0x01
47 #define BTBB_MOD_8DPSK             0x02
48 
49 /* Transport types */
50 #define BTBB_TRANSPORT_ANY     0x00
51 #define BTBB_TRANSPORT_SCO     0x01
52 #define BTBB_TRANSPORT_ESCO    0x02
53 #define BTBB_TRANSPORT_ACL     0x03
54 #define BTBB_TRANSPORT_CSB     0x04
55 
56 #ifdef __cplusplus
57 extern "C"
58 {
59 #endif
60 
61 /* BT BR/EDR support */
62 
63 typedef struct btbb_packet btbb_packet;
64 
65 /* Initialize the library. Compute the syndrome. Return 0 on success,
66  * negative on error.
67  *
68  * The library limits max_ac_errors to 5. Using a larger value will
69  * take up a lot of memory (several GB), without decoding many useful
70  * packets. Even a limit of 5 results in a syndrome table of several
71  * hundred MB and lots of noise. For embedded targets, a value of 2 is
72  * reasonable. */
73 int btbb_init(int max_ac_errors);
74 
75 const char* btbb_get_release(void);
76 const char* btbb_get_version(void);
77 
78 btbb_packet *btbb_packet_new(void);
79 void btbb_packet_ref(btbb_packet *pkt);
80 void btbb_packet_unref(btbb_packet *pkt);
81 
82 /* Search for a packet with specified LAP (or LAP_ANY). The stream
83  * must be at least of length serch_length + 72. Limit to
84  * 'max_ac_errors' bit errors.
85  *
86  * Returns offset into 'stream' at which packet was found. If no
87  * packet was found, returns a negative number. If LAP_ANY was
88  * specified, fills lap. 'ac_errors' must be set as an input, replaced
89  * by actual number of errors on output. */
90 int btbb_find_ac(char *stream,
91 	       int search_length,
92 	       uint32_t lap,
93 	       int max_ac_errors,
94 	       btbb_packet **pkt);
95 #define LAP_ANY 0xffffffffUL
96 #define UAP_ANY 0xff
97 
98 void btbb_packet_set_flag(btbb_packet *pkt, int flag, int val);
99 int btbb_packet_get_flag(const btbb_packet *pkt, int flag);
100 
101 uint32_t btbb_packet_get_lap(const btbb_packet *pkt);
102 void btbb_packet_set_uap(btbb_packet *pkt, uint8_t uap);
103 uint8_t btbb_packet_get_uap(const btbb_packet *pkt);
104 uint16_t btbb_packet_get_nap(const btbb_packet *pkt);
105 
106 void btbb_packet_set_modulation(btbb_packet *pkt, uint8_t modulation);
107 void btbb_packet_set_transport(btbb_packet *pkt, uint8_t transport);
108 uint8_t btbb_packet_get_modulation(const btbb_packet *pkt);
109 uint8_t btbb_packet_get_transport(const btbb_packet *pkt);
110 
111 uint8_t btbb_packet_get_channel(const btbb_packet *pkt);
112 uint8_t btbb_packet_get_ac_errors(const btbb_packet *pkt);
113 uint32_t btbb_packet_get_clkn(const btbb_packet *pkt);
114 uint32_t btbb_packet_get_header_packed(const btbb_packet* pkt);
115 
116 void btbb_packet_set_data(btbb_packet *pkt,
117 			  char *syms,      // Symbol data
118 			  int length,      // Number of symbols
119 			  uint8_t channel, // Bluetooth channel 0-79
120 			  uint32_t clkn);  // 312.5us clock (CLK27-0)
121 
122 /* Get a pointer to packet symbols. */
123 const char *btbb_get_symbols(const btbb_packet* pkt);
124 
125 int btbb_packet_get_payload_length(const btbb_packet* pkt);
126 
127 /* Get a pointer to payload. */
128 const char *btbb_get_payload(const btbb_packet* pkt);
129 
130 /* Pack the payload in to bytes */
131 int btbb_get_payload_packed(const btbb_packet* pkt, char *dst);
132 
133 uint8_t btbb_packet_get_type(const btbb_packet* pkt);
134 uint8_t btbb_packet_get_lt_addr(const btbb_packet* pkt);
135 uint8_t btbb_packet_get_header_flags(const btbb_packet* pkt);
136 uint8_t btbb_packet_get_hec(const btbb_packet *pkt);
137 
138 /* Generate Sync Word from an LAP */
139 uint64_t btbb_gen_syncword(const int LAP);
140 
141 /* decode the packet header */
142 int btbb_decode_header(btbb_packet* pkt);
143 
144 /* decode the packet header */
145 int btbb_decode_payload(btbb_packet* pkt);
146 
147 /* print packet information */
148 void btbb_print_packet(const btbb_packet* pkt);
149 
150 /* check to see if the packet has a header */
151 int btbb_header_present(const btbb_packet* pkt);
152 
153 /* Packet queue (linked list) */
154 typedef struct pkt_queue {
155 	btbb_packet *pkt;
156 
157 	struct pkt_queue *next;
158 
159 } pkt_queue;
160 
161 typedef struct btbb_piconet btbb_piconet;
162 
163 btbb_piconet *btbb_piconet_new(void);
164 void btbb_piconet_ref(btbb_piconet *pn);
165 void btbb_piconet_unref(btbb_piconet *pn);
166 
167 /* initialize the piconet struct */
168 void btbb_init_piconet(btbb_piconet *pn, uint32_t lap);
169 
170 void btbb_piconet_set_uap(btbb_piconet *pn, uint8_t uap);
171 uint8_t btbb_piconet_get_uap(const btbb_piconet *pn);
172 uint32_t btbb_piconet_get_lap(const btbb_piconet *pn);
173 uint16_t btbb_piconet_get_nap(const btbb_piconet *pn);
174 uint64_t btbb_piconet_get_bdaddr(const btbb_piconet *pn);
175 
176 int btbb_piconet_get_clk_offset(const btbb_piconet *pn);
177 void btbb_piconet_set_clk_offset(btbb_piconet *pn, int clk_offset);
178 
179 void btbb_piconet_set_flag(btbb_piconet *pn, int flag, int val);
180 int btbb_piconet_get_flag(const btbb_piconet *pn, int flag);
181 
182 uint8_t btbb_piconet_set_channel_seen(btbb_piconet *pn, uint8_t channel);
183 uint8_t btbb_piconet_clear_channel_seen(btbb_piconet *pn, uint8_t channel);
184 uint8_t btbb_piconet_get_channel_seen(btbb_piconet *pn, uint8_t channel);
185 void btbb_piconet_set_afh_map(btbb_piconet *pn, uint8_t *afh_map);
186 uint8_t *btbb_piconet_get_afh_map(btbb_piconet *pn);
187 
188 /* Extract as much information (LAP/UAP/CLK) as possible from received packet */
189 int btbb_process_packet(btbb_packet *pkt, btbb_piconet *pn);
190 
191 /* use packet headers to determine UAP */
192 int btbb_uap_from_header(btbb_packet *pkt, btbb_piconet *pn);
193 
194 /* Print hexadecimal representation of the derived AFH map */
195 void btbb_print_afh_map(btbb_piconet *pn);
196 
197 /* decode a whole packet from the given piconet */
198 int btbb_decode(btbb_packet* pkt);
199 
200 
201 /* initialize the hop reversal process */
202 /* returns number of initial candidates for CLK1-27 */
203 int btbb_init_hop_reversal(int aliased, btbb_piconet *pn);
204 
205 /* narrow a list of candidate clock values based on all observed hops */
206 int btbb_winnow(btbb_piconet *pn);
207 
208 int btbb_init_survey(void);
209 /* Destructively iterate over survey results - optionally remove elements */
210 btbb_piconet *btbb_next_survey_result(void);
211 
212 typedef struct btbb_pcapng_handle btbb_pcapng_handle;
213 /* create a PCAPNG file for BREDR captures */
214 int btbb_pcapng_create_file(const char *filename, const char *interface_desc, btbb_pcapng_handle ** ph);
215 /* save a BREDR packet to PCAPNG capture file */
216 int btbb_pcapng_append_packet(btbb_pcapng_handle * h, const uint64_t ns,
217                               const int8_t sigdbm, const int8_t noisedbm,
218                               const uint32_t reflap, const uint8_t refuap,
219                               const btbb_packet *pkt);
220 /* record a BDADDR to PCAPNG capture file */
221 int btbb_pcapng_record_bdaddr(btbb_pcapng_handle * h, const uint64_t bdaddr,
222                               const uint8_t uapmask, const uint8_t napvalid);
223 /* record BT CLOCK to PCAPNG capture file */
224 int btbb_pcapng_record_btclock(btbb_pcapng_handle * h, const uint64_t bdaddr,
225                                const uint64_t ns, const uint32_t clk, const uint32_t clkmask);
226 int btbb_pcapng_close(btbb_pcapng_handle * h);
227 
228 
229 /* BLE support */
230 typedef struct lell_packet lell_packet;
231 /* decode and allocate LE packet */
232 void lell_allocate_and_decode(const uint8_t *stream, uint16_t phys_channel, uint32_t clk100ns, lell_packet **pkt);
233 lell_packet *lell_packet_new(void);
234 void lell_packet_ref(lell_packet *pkt);
235 void lell_packet_unref(lell_packet *pkt);
236 uint32_t lell_get_access_address(const lell_packet *pkt);
237 unsigned lell_get_access_address_offenses(const lell_packet *pkt);
238 unsigned lell_packet_is_data(const lell_packet *pkt);
239 unsigned lell_get_channel_index(const lell_packet *pkt);
240 unsigned lell_get_channel_k(const lell_packet *pkt);
241 const char * lell_get_adv_type_str(const lell_packet *pkt);
242 void lell_print(const lell_packet *pkt);
243 
244 typedef struct lell_pcapng_handle lell_pcapng_handle;
245 /* create a PCAPNG file for LE captures */
246 int lell_pcapng_create_file(const char *filename, const char *interface_desc, lell_pcapng_handle ** ph);
247 /* save an LE packet to PCAPNG capture file */
248 int lell_pcapng_append_packet(lell_pcapng_handle * h, const uint64_t ns,
249                               const int8_t sigdbm, const int8_t noisedbm,
250                               const uint32_t refAA, const lell_packet *pkt);
251 /* record LE CONNECT_REQ parameters to PCAPNG capture file */
252 int lell_pcapng_record_connect_req(lell_pcapng_handle * h, const uint64_t ns, const uint8_t * pdu);
253 int lell_pcapng_close(lell_pcapng_handle *h);
254 
255 
256 /* PCAP Support */
257 typedef struct btbb_pcap_handle btbb_pcap_handle;
258 /* create a PCAP file for BREDR captures with LINKTYPE_BLUETOOTH_BREDR_BB */
259 int btbb_pcap_create_file(const char *filename, btbb_pcap_handle ** ph);
260 /* write a BREDR packet to PCAP file */
261 int btbb_pcap_append_packet(btbb_pcap_handle * h, const uint64_t ns,
262                             const int8_t sigdbm, const int8_t noisedbm,
263                             const uint32_t reflap, const uint8_t refuap,
264                             const btbb_packet *pkt);
265 int btbb_pcap_close(btbb_pcap_handle * h);
266 
267 typedef struct lell_pcap_handle lell_pcap_handle;
268 /* create a PCAP file for LE captures using LINKTYPE_BLUETOOTH_LE_LL_WITH_PHDR */
269 int lell_pcap_create_file(const char *filename, lell_pcap_handle ** ph);
270 /* create a PCAP file for LE captures using LINKTYPE_PPI */
271 int lell_pcap_ppi_create_file(const char *filename, int btle_ppi_version, lell_pcap_handle ** ph);
272 /* save an LE packet to PCAP capture file */
273 int lell_pcap_append_packet(lell_pcap_handle * h, const uint64_t ns,
274                             const int8_t sigdbm, const int8_t noisedbm,
275                             const uint32_t refAA, const lell_packet *pkt);
276 int lell_pcap_append_ppi_packet(lell_pcap_handle * h, const uint64_t ns,
277                                 const uint8_t clkn_high,
278                                 const int8_t rssi_min, const int8_t rssi_max,
279                                 const int8_t rssi_avg, const uint8_t rssi_count,
280                                 const lell_packet *pkt);
281 int lell_pcap_close(lell_pcap_handle *h);
282 
283 #ifdef __cplusplus
284 } // __cplusplus defined.
285 #endif
286 
287 #endif /* INCLUDED_BTBB_H */
288