+class AutoTuner():
+ """This guesses addresses by searching through packets."""
+ addresses={};
+ client=None;
+
+ #Limits on search space, because you usually know what you're looking for.
+ rate=False;
+ chan=False;
+ sync=False;
+ macreject=False;
+ printing=False;
+ startch=0; #Useful for forcing an early match.
+ def init(self,goodfet,
+ rate=True,chan=True,sync=True, macreject=True, printing=False):
+ """Initializes a link to the GoodFET for autotuning."""
+ self.client=goodfet;
+ self.rate=rate;
+ self.chan=chan;
+ self.sync=sync;
+ self.macreject=macreject;
+ self.printing=printing;
+
+ client.poke(0x00,0x00); #Stop nRF
+ client.poke(0x01,0x00); #Disable Shockburst
+ client.poke(0x02,0x01); #Set RX Pipe 0
+
+ #Disable shockburst.
+ client.poke(0x1C,0x00);
+ client.poke(0x1D,0x00);
+
+ client.RF_setmaclen(2); # SETUP_AW for shortest
+
+ #historic
+ #client.RF_setsmac(0x00AA);
+ #client.RF_setsmac(0x0055);
+
+ client.poke(0x00,0x70|0x03); #prime radio.
+
+ return;
+ def packetaddr(self,packet,justmac=False):
+ """Returns a loaded packet address, including channel and rate."""
+
+ sync=self.client.RF_getsmac()&0xFF;
+
+ mac="";
+ #MAC,RF_CH,RATE
+ for i in range(0,5):
+ mac="%s%02x" % (mac,ord(packet[i]));
+ if justmac:
+ return mac;
+ ch=self.client.peek(0x05);
+ rate=self.client.peek(0x06);
+ return "%02x,%s,%02x,%02x" % (
+ sync,mac,ch,rate);
+ def validmac(self,packet):
+ sync=self.client.RF_getsmac()&0xFF;
+ mac=self.packetaddr(packet,justmac=True);
+
+ #BT preamble is A or 5.
+ #Fix this to work on the smallest bit, not the highest.
+ if ((ord(packet[0])&0x80)^(sync&0x80)) and self.macreject:
+ #print "%02x%02x invalid entry." % (sync,ord(packet[0]));
+ #This is a special kind of failure. Freq is probably right, but MAC is wrong.
+ return False;
+ if mac=='5555555555' or mac=='aaaaaaaaaa' or mac=='0000000000' or mac=='ffffffffff':
+ return False;
+ return True;
+
+ def handle(self,packet):
+ """Handles a packet."""
+ if self.printing:
+ printpacket(packet);
+
+ if not self.validmac(packet):
+ #print "Dropped packet from %s" % self.packetaddr(packet,justmac=True);
+ #printpacket(packet);
+ return;
+
+ addr=self.packetaddr(packet);
+
+ #Increment the address count.
+ count=0;
+ try:
+ count=self.addresses[addr];
+ except:
+ pass;
+ self.addresses[addr]=count+1;
+ rate=count*1.0/len(self.addresses);
+ if self.addresses[addr]>1 or rate>0.01:
+ print "'%s' looks valid\t%i\t%0.5f" % (
+ addr,count,rate);
+ return addr;
+ tunecount=0;
+ def selftune(self,threshold=2,forever=False,
+ delay=5.0):
+ """Tunes to the first strong signal.
+ It's important that this not get triggered by false positives."""
+
+ while 1:
+ self.retune();
+ start=time.mktime(time.localtime());
+ sys.stdout.flush();
+ while (time.mktime(time.localtime())-start) < delay:
+ packet=None;
+ while packet==None:
+ packet=client.RF_rxpacket();
+ addr=guesser.handle(packet);
+ try:
+ count=self.addresses[addr];
+ except:
+ count=0;
+ if count>threshold and forever==False:
+ #Tune it in here?
+ client.tune(addr);
+ return addr;
+
+
+ def retune(self):
+ """Tunes to another channel or preamble looking for the next packet."""
+ count=self.tunecount;
+ self.tunecount=count+1;
+
+ #Swap the SYNC value most often.
+ if self.sync:
+ sync=0x00AA;
+ if count&1:
+ sync=0x0055;
+ self.client.RF_setsmac(sync);
+ count=(count>>1);
+
+ if self.rate:
+ #Then the data rate.
+ rate=0;
+
+ #This swaps between 1Mbps and 2Mbps.
+ #TODO add support for 256kbps, if anyone uses it.
+ if count&1:
+ rate=rate|0x08;
+ #print "Setting rate to 0x%02x" % rate;
+ if(rate==0x20):
+ rate=0x08;
+ self.client.poke(0x06,rate);
+ count=(count>>1);
+
+ if self.chan:
+ self.client.poke(0x05,
+ (count+self.startch)&0x7f);
+ print "Tuned to %i MHz" % (
+ self.client.RF_getfreq()
+ /(10**6));
+ #Grab two packets to clear buffers.
+ #Should retune only after a few packets to reduce this delay.
+ packet=client.RF_rxpacket();
+ packet=client.RF_rxpacket();
+ return;
+
+
+if(sys.argv[1]=="autotune"):
+ #Reversal of transmitter code from nRF_CMD.c of OpenBeacon
+ #TODO remove all poke() calls.
+ guesser=AutoTuner();
+ guesser.init(client,rate=True,sync=True,chan=True);
+
+
+ #client.RF_setfreq(2481 * 10**6);
+ client.poke(0x06,0x09); #2MBps, -18dBm in RF_SETUP
+ client.poke(0x07,0x78); #Reset status register
+
+ #This is determined by the MAC, which we don't yet know.
+
+ #Longest length.
+ client.RF_setpacketlen(32);
+
+ #Power radio, prime for RX, no checksum
+ client.poke(0x00,0x70|0x03); #0x08 for checksum, 0x04 for two.
+
+ print "Autotuning on %i MHz" % (
+ client.RF_getfreq()/10**6);
+ print "sync,mac,r5,r6";
+ #Now we're ready to get packets.
+ guesser.selftune(threshold=2,
+ forever=True);
+
+if(sys.argv[1]=="autotunebt"):
+ #Reversal of transmitter code from nRF_CMD.c of OpenBeacon
+ #TODO remove all poke() calls.
+ guesser=AutoTuner();
+ guesser.init(client,rate=True,sync=False,chan=True,
+ macreject=False, printing=True);
+
+
+ #client.RF_setfreq(2481 * 10**6);
+ client.poke(0x06,0x00); #1MBps
+ client.poke(0x07,0x78); #Reset status register
+
+ #Bluetooth preamble is 0xA; BTLE is 0xAA.
+ client.RF_setsmac(0x000A);
+
+ #Longest length.
+ client.RF_setpacketlen(32);
+
+ #Power radio, prime for RX, no checksum
+ client.poke(0x00,0x70|0x03); #0x08 for checksum, 0x04 for two.
+
+ print "Autotuning on %i MHz" % (
+ client.RF_getfreq()/10**6);
+ print "sync,mac,r5,r6";
+ #Now we're ready to get packets.
+ guesser.selftune(threshold=2,
+ forever=True);
+
+if(sys.argv[1]=="sniffmacs"):
+ #Reversal of transmitter code from nRF_CMD.c of OpenBeacon
+ #TODO remove all poke() calls.
+ guesser=AutoTuner();
+ guesser.init(client,rate=False,sync=True,chan=False);
+
+ #Longest length.
+ client.RF_setpacketlen(32);
+
+ #Power radio, prime for RX, no checksum
+ client.poke(0x00,0x70|0x03); #0x08 for checksum, 0x04 for two.
+
+ print "Holding autotune on %i MHz" % (
+ client.RF_getfreq()/10**6);
+ print "sync,mac,r5,r6";
+ #Now we're ready to get packets.
+ guesser.selftune(threshold=2,
+ forever=True);
+