xref: /libbtbb/lib/src/btbb.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_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 #ifdef __cplusplus
45 extern "C"
46 {
47 #endif
48 
49 typedef struct btbb_packet btbb_packet;
50 
51 /* Initialize the library. Compute the syndrome. Return 0 on success,
52  * negative on error.
53  *
54  * The library limits max_ac_errors to 5. Using a larger value will
55  * take up a lot of memory (several GB), without decoding many useful
56  * packets. Even a limit of 5 results in a syndrome table of several
57  * hundred MB and lots of noise. For embedded targets, a value of 2 is
58  * reasonable. */
59 int btbb_init(int max_ac_errors);
60 
61 btbb_packet *btbb_packet_new(void);
62 void btbb_packet_ref(btbb_packet *pkt);
63 void btbb_packet_unref(btbb_packet *pkt);
64 
65 /* Search for a packet with specified LAP (or LAP_ANY). The stream
66  * must be at least of length serch_length + 72. Limit to
67  * 'max_ac_errors' bit errors.
68  *
69  * Returns offset into 'stream' at which packet was found. If no
70  * packet was found, returns a negative number. If LAP_ANY was
71  * specified, fills lap. 'ac_errors' must be set as an input, replaced
72  * by actual number of errors on output. */
73 int btbb_find_ac(char *stream,
74 	       int search_length,
75 	       uint32_t lap,
76 	       int max_ac_errors,
77 	       btbb_packet **pkt);
78 #define LAP_ANY 0xffffffffUL
79 
80 void btbb_packet_set_flag(btbb_packet *pkt, int flag, int val);
81 int btbb_packet_get_flag(btbb_packet *pkt, int flag);
82 
83 uint32_t btbb_packet_get_lap(btbb_packet *pkt);
84 void btbb_packet_set_uap(btbb_packet *pkt, uint8_t uap);
85 uint8_t btbb_packet_get_uap(btbb_packet *pkt);
86 uint16_t btbb_packet_get_nap(btbb_packet *pkt);
87 
88 uint8_t btbb_packet_get_channel(btbb_packet *pkt);
89 uint8_t btbb_packet_get_ac_errors(btbb_packet *pkt);
90 uint32_t btbb_packet_get_clkn(btbb_packet *pkt);
91 
92 void btbb_packet_set_data(btbb_packet *pkt,
93 			  char *syms,      // Symbol data
94 			  int length,      // Number of symbols
95 			  uint8_t channel, // Bluetooth channel 0-79
96 			  uint32_t clkn);  // 312.5us clock (CLK27-0)
97 
98 /* Get a pointer to packet symbols. */
99 const char *btbb_get_symbols(btbb_packet* pkt);
100 
101 int btbb_packet_get_payload_length(btbb_packet* pkt);
102 
103 /* Get a pointer to payload. */
104 const char *btbb_get_payload(btbb_packet* pkt);
105 
106 /* Pack the payload in to bytes */
107 int btbb_get_payload_packed(btbb_packet* pkt, char *dst);
108 
109 uint8_t btbb_packet_get_type(btbb_packet* pkt);
110 uint8_t btbb_packet_get_lt_addr(btbb_packet* pkt);
111 uint8_t btbb_packet_get_header_flags(btbb_packet* pkt);
112 uint8_t btbb_packet_get_hec(btbb_packet *pkt);
113 
114 /* Generate Sync Word from an LAP */
115 uint64_t btbb_gen_syncword(int LAP);
116 
117 /* decode the packet header */
118 int btbb_decode_header(btbb_packet* pkt);
119 
120 /* decode the packet header */
121 int btbb_decode_payload(btbb_packet* pkt);
122 
123 /* print packet information */
124 void btbb_print_packet(btbb_packet* pkt);
125 
126 /* check to see if the packet has a header */
127 int btbb_header_present(btbb_packet* pkt);
128 
129 /* Packet queue (linked list) */
130 typedef struct pkt_queue {
131 	btbb_packet *pkt;
132 
133 	struct pkt_queue *next;
134 
135 } pkt_queue;
136 
137 typedef struct btbb_piconet btbb_piconet;
138 
139 btbb_piconet *btbb_piconet_new(void);
140 void btbb_piconet_ref(btbb_piconet *pn);
141 void btbb_piconet_unref(btbb_piconet *pn);
142 
143 /* initialize the piconet struct */
144 void btbb_init_piconet(btbb_piconet *pn, uint32_t lap);
145 
146 void btbb_piconet_set_uap(btbb_piconet *pn, uint8_t uap);
147 uint8_t btbb_piconet_get_uap(btbb_piconet *pn);
148 uint32_t btbb_piconet_get_lap(btbb_piconet *pn);
149 uint16_t btbb_piconet_get_nap(btbb_piconet *pn);
150 
151 int btbb_piconet_get_clk_offset(btbb_piconet *pn);
152 void btbb_piconet_set_clk_offset(btbb_piconet *pn, int clk_offset);
153 
154 void btbb_piconet_set_flag(btbb_piconet *pn, int flag, int val);
155 int btbb_piconet_get_flag(btbb_piconet *pn, int flag);
156 
157 void btbb_piconet_set_channel_seen(btbb_piconet *pn, uint8_t channel);
158 void btbb_piconet_set_afh_map(btbb_piconet *pn, uint8_t *afh_map);
159 uint8_t *btbb_piconet_get_afh_map(btbb_piconet *pn);
160 
161 /* Extract as much information (LAP/UAP/CLK) as possible from received packet */
162 int btbb_process_packet(btbb_packet *pkt, btbb_piconet *pn);
163 
164 /* use packet headers to determine UAP */
165 int btbb_uap_from_header(btbb_packet *pkt, btbb_piconet *pn);
166 
167 /* Print hexadecimal representation of the derived AFH map */
168 void btbb_print_afh_map(btbb_piconet *pn);
169 
170 /* decode a whole packet from the given piconet */
171 int btbb_decode(btbb_packet* pkt, btbb_piconet *pn);
172 
173 
174 /* initialize the hop reversal process */
175 /* returns number of initial candidates for CLK1-27 */
176 int btbb_init_hop_reversal(int aliased, btbb_piconet *pn);
177 
178 /* narrow a list of candidate clock values based on all observed hops */
179 int btbb_winnow(btbb_piconet *pn);
180 
181 int btbb_init_survey(void);
182 /* Destructively iterate over survey results - optionally remove elements */
183 btbb_piconet *btbb_next_survey_result(void);
184 
185 #ifdef __cplusplus
186 } // __cplusplus defined.
187 #endif
188 
189 #endif /* INCLUDED_BTBB_H */
190