xref: /libbtbb/lib/src/bluetooth_piconet.h (revision 5cf4c9cb441aa735be0ac77081632db31c920700)
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_PICONET_H
23 #define INCLUDED_BLUETOOTH_PICONET_H
24 #include "btbb.h"
25 
26 /* maximum number of hops to remember */
27 #define MAX_PATTERN_LENGTH 1000
28 
29 /* number of channels in use */
30 #define BT_NUM_CHANNELS 79
31 
32 struct btbb_piconet {
33 
34 	uint32_t refcount;
35 
36 	uint32_t flags;
37 
38 	/* true if using a particular aliased receiver implementation */
39 	int aliased;
40 
41 	/* AFH channel map - either read or derived from observed packets */
42 	uint8_t afh_map[10];
43 
44 	/* lower address part (of master's BD_ADDR) */
45 	uint32_t LAP;
46 
47 	/* upper address part (of master's BD_ADDR) */
48 	uint8_t UAP;
49 
50 	/* non-significant address part (of master's BD_ADDR) */
51 	uint16_t NAP;
52 
53 	/* CLK1-27 candidates */
54 	uint32_t *clock_candidates;
55 
56 	/* these values for hop() can be precalculated */
57 	int b, e;
58 
59 	/* these values for hop() can be precalculated in part (e.g. a1 is the
60 	 * precalculated part of a) */
61 	int a1, c1, d1;
62 
63 	/* frequency register bank */
64 	int bank[BT_NUM_CHANNELS];
65 
66 	/* this holds the entire hopping sequence */
67 	char *sequence;
68 
69 	/* number of candidates for CLK1-27 */
70 	int num_candidates;
71 
72 	/* number of packets observed during one attempt at UAP/clock discovery */
73 	int packets_observed;
74 
75 	/* total number of packets observed */
76 	int total_packets_observed;
77 
78 	/* number of observed packets that have been used to winnow the candidates */
79 	int winnowed;
80 
81 	/* CLK1-6 candidates */
82 	int clock6_candidates[64];
83 
84 	/* remember patterns of observed hops */
85 	int pattern_indices[MAX_PATTERN_LENGTH];
86 	uint8_t pattern_channels[MAX_PATTERN_LENGTH];
87 
88 	/* offset between CLKN (local) and CLK of piconet */
89 	int clk_offset;
90 
91 	/* local clock (clkn) at time of first packet */
92 	uint32_t first_pkt_time;
93 
94 	/* queue of packets to be decoded */
95 	pkt_queue *queue;
96 };
97 
98 /* number of hops in the hopping sequence (i.e. number of possible values of CLK1-27) */
99 #define SEQUENCE_LENGTH 134217728
100 
101 /* number of aliased channels received */
102 #define ALIASED_CHANNELS 25
103 
104 /* do all the precalculation that can be done before knowing the address */
105 void precalc(btbb_piconet *pnet);
106 
107 /* do precalculation that requires the address */
108 void address_precalc(int address, btbb_piconet *pnet);
109 
110 /* drop-in replacement for perm5() using lookup table */
111 int fast_perm(int z, int p_high, int p_low);
112 
113 /* 5 bit permutation */
114 /* assumes z is constrained to 5 bits, p_high to 5 bits, p_low to 9 bits */
115 int perm5(int z, int p_high, int p_low);
116 
117 /* determine channel for a particular hop */
118 /* replaced with gen_hops() for a complete sequence but could still come in handy */
119 char single_hop(int clock, btbb_piconet *pnet);
120 
121 /* look up channel for a particular hop */
122 char hop(int clock, btbb_piconet *pnet);
123 
124 void try_hop(btbb_packet *pkt, btbb_piconet *pn);
125 
126 #endif /* INCLUDED_BLUETOOTH_PICONET_H */
127