GNU Radio's FOOT Package
piconet_impl.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2013 Christopher D. Kilgour
4  * Copyright 2008, 2009 Dominic Spill, Michael Ossmann
5  * Copyright 2007 Dominic Spill
6  * Copyright 2005, 2006 Free Software Foundation, Inc.
7  *
8  * This file is part of gr-bluetooth
9  *
10  * This is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2, or (at your option)
13  * any later version.
14  *
15  * This software is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this software; see the file COPYING. If not, write to
22  * the Free Software Foundation, Inc., 51 Franklin Street,
23  * Boston, MA 02110-1301, USA.
24  */
25 
26 #ifndef INCLUDED_BLUETOOTH_GR_BLUETOOTH_PICONET_IMPL_H
27 #define INCLUDED_BLUETOOTH_GR_BLUETOOTH_PICONET_IMPL_H
28 
29 #include "gr_bluetooth/piconet.h"
30 #include "gr_bluetooth/packet.h"
31 #include <vector>
32 
33 namespace gr {
34  namespace bluetooth {
35 
37  private:
38  /* number of channels in use */
39  static const int CHANNELS = 79;
40 
41  /* number of aliased channels received */
42  static const int ALIASED_CHANNELS = 25;
43 
44  /* maximum number of hops to remember */
45  static const int MAX_PATTERN_LENGTH = 1000;
46 
47  /* true if using a particular aliased receiver implementation */
48  bool d_aliased;
49 
50  /* using adaptive frequency hopping (AFH) */
51  bool d_afh;
52 
53  /* observed pattern that looks like AFH */
54  bool d_looks_like_afh;
55 
56  /* lower address part (of master's BD_ADDR) */
57  uint32_t d_LAP;
58 
59  /* upper address part (of master's BD_ADDR) */
60  uint8_t d_UAP;
61 
62  /* non-significant address part (of master's BD_ADDR) */
63  uint16_t d_NAP;
64 
65  /* CLK1-27 candidates */
66  uint32_t *d_clock_candidates;
67 
68  /* these values for hop() can be precalculated */
69  int d_b, d_e;
70 
71  /* these values for hop() can be precalculated in part (e.g. a1 is the
72  * precalculated part of a) */
73  int d_a1, d_c1, d_d1;
74 
75  /* frequency register bank */
76  int d_bank[CHANNELS];
77 
78  /* speed up the perm5 function with a lookup table */
79  char d_perm_table[0x20][0x20][0x200];
80 
81  /* this holds the entire hopping sequence */
82  char *d_sequence;
83 
84  /* number of candidates for CLK1-27 */
85  int d_num_candidates;
86 
87  /* have we collected the first packet in a UAP discovery attempt? */
88  bool d_got_first_packet;
89 
90  /* number of packets observed during one attempt at UAP/clock discovery */
91  int d_packets_observed;
92 
93  /* total number of packets observed */
94  int d_total_packets_observed;
95 
96  /* number of observed packets that have been used to winnow the candidates */
97  int d_winnowed;
98 
99  /* CLK1-6 candidates */
100  int d_clock6_candidates[64];
101 
102  /* remember patterns of observed hops */
103  int d_pattern_indices[MAX_PATTERN_LENGTH];
104  uint8_t d_pattern_channels[MAX_PATTERN_LENGTH];
105 
106  bool d_hop_reversal_inited;
107 
108  /* do all the precalculation that can be done before knowing the address */
109  void precalc();
110 
111  /* do precalculation that requires the address */
112  void address_precalc(int address);
113 
114  /* drop-in replacement for perm5() using lookup table */
115  int fast_perm(int z, int p_high, int p_low);
116 
117  /* 5 bit permutation */
118  /* assumes z is constrained to 5 bits, p_high to 5 bits, p_low to 9 bits */
119  int perm5(int z, int p_high, int p_low);
120 
121  /* generate the complete hopping sequence */
122  void gen_hops();
123 
124  /* determine channel for a particular hop */
125  /* replaced with gen_hops() for a complete sequence but could still come in handy */
126  char single_hop(int clock);
127 
128  /* create list of initial candidate clock values (hops with same channel as first observed hop) */
129  int init_candidates(char channel, int known_clock_bits);
130 
131  /* discovery status */
132  bool d_have_UAP;
133  bool d_have_NAP;
134  bool d_have_clk6;
135  bool d_have_clk27;
136 
137  /* offset between CLKN (local) and CLK of piconet */
138  uint32_t d_clk_offset;
139 
140  /* local clock (clkn) at time of first packet */
141  uint32_t d_first_pkt_time;
142 
143  public:
144  basic_rate_piconet_impl(uint32_t LAP);
146 
147  /* initialize the hop reversal process */
148  /* returns number of initial candidates for CLK1-27 */
149  int init_hop_reversal(bool aliased);
150 
151  /* look up channel for a particular hop */
152  char hop(int clock);
153 
154  /* narrow a list of candidate clock values based on a single observed hop */
155  int winnow(int offset, char channel);
156 
157  /* narrow a list of candidate clock values based on all observed hops */
158  int winnow();
159 
160  /* offset between CLKN (local) and CLK of piconet */
161  uint32_t get_offset();
162  void set_offset(uint32_t offset);
163 
164  /* UAP */
165  uint8_t get_UAP();
166  void set_UAP(uint8_t uap);
167 
168  /* NAP */
169  uint16_t get_NAP();
170  void set_NAP(uint16_t nap);
171 
172  /* discovery status */
173  bool have_UAP();
174  bool have_NAP();
175  bool have_clk6();
176  bool have_clk27();
177 
178  /* use packet headers to determine UAP */
180 
181  /* return the observable channel (26-50) for a given channel (0-78) */
182  char aliased_channel(char channel);
183 
184  /* reset UAP/clock discovery */
185  void reset();
186  };
187 
189  private:
190  uint8_t d_chan_list[38];
191 
192  public:
193  low_energy_piconet_impl(uint32_t aa);
195 
196  int init_hop_reversal(bool aliased);
197  char hop(int clock);
198  char aliased_channel(char channel);
199  void reset();
200 
201  };
202 
203  } // namespace bluetooth
204 } // namespace gr
205 
206 #endif /* INCLUDED_BLUETOOTH_GR_BLUETOOTH_PICONET_IMPL_H */
207 
Definition: piconet.h:68
Definition: packet.h:37
Definition: piconet_impl.h:36
boost::shared_ptr< classic_packet > sptr
Definition: packet.h:169
bool UAP_from_header(classic_packet::sptr packet)
Definition: multi_block.h:33
Definition: piconet.h:128
Definition: piconet_impl.h:188