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 typedef 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 /* have we collected the first packet in a UAP discovery attempt? */ 73 int got_first_packet; 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 } btbb_piconet; 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, btbb_piconet *pnet); 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 #endif /* INCLUDED_BLUETOOTH_PICONET_H */ 130