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