3 #GoodFET SPI Flash Client
11 from GoodFETNRF import GoodFETNRF;
12 from intelhex import IntelHex;
15 regnames=["CONFIG","EN_AA","EN_RXADDR","SETUP_AW","SETUP_RET",
16 "RF_CH","RF_SETUP","STATUS","OBSERVE_TX","RPD",
17 "RX_ADDR_P0","RX_ADDR_P1","RX_ADDR_P2","RX_ADDR_P3","RX_ADDR_P4","RX_ADDR_P5",
19 "RX_PW_P0","RX_PW_P1","RX_PW_P2","RX_PW_P3","RX_PW_P4","RX_PW_P5",
21 "?","?","?","DYNPD","FEATURE","AGC_CONFIG","?","?",
22 "?","?","?","?","?","?","?","?"];
24 def printpacket(packet):
29 if i>client.packetlen: break;
30 s="%s %02x" % (s,ord(foo));
33 def printmspacket(packet,offset=1):
34 keyword=client.RF_getsmac();
35 #print "keyword=%010x" % key;
37 for foo in range(0,5):
38 key.append(keyword&0xFF);
40 #print "Keybyte %02x" % key[foo];
45 s="%s %02x" % (s,ord(foo)^key[(i+offset)%5]);
47 s="%s %02x" % (s,ord(foo));
51 print "Encoding %s" % client.RF_getenc();
52 print "Freq %10i MHz" % (client.RF_getfreq()/10**6);
53 print "Rate %10i kbps" % (client.RF_getrate()/1000);
54 print "PacketLen %02i bytes" % client.RF_getpacketlen();
55 #print "MacLen %2i bytes" % client.RF_getmaclen();
56 print "SMAC 0x%010x" % client.RF_getsmac();
57 print "TMAC 0x%010x" % client.RF_gettmac();
61 print "Usage: %s verb [objects]\n" % sys.argv[0];
62 print "%s info" % sys.argv[0];
63 print "%s test" % sys.argv[0];
64 print "%s regs" % sys.argv[0];
65 print "%s regbits" % sys.argv[0];
66 print "%s pyregs" % sys.argv[0];
68 print "%s tune aa|55,mac,r5,r6\n\tTunes to a configuration." % sys.argv[0];
69 print "%s sniff\n\tSniffs packets by current config." % sys.argv[0];
70 print "%s sniffob\n\tSniffs OpenBeacon traffic." % sys.argv[0];
71 print "%s snifftp\n\tSniffs Turning Point Clicker traffic." % sys.argv[0];
72 print "%s sniffsf\n\tSniffs SparkFun Dongle traffic." % sys.argv[0];
74 print "%s sniffprom [0xaa|0x55]\n\tSniffs promiscuously for a preamble of 0xAA or 0x55" % sys.argv[0];
75 print "%s autotune\n\tSearches for a valid destination address." % sys.argv[0];
77 print "%s sniffskybrake\n\tSniffs skybrake. [broken?]" % sys.argv[0];
78 print "%s sniffmskb\n\tSniffs MS KB. [broken?]" % sys.argv[0];
81 print "%s hosttp\n\tHosts Turning Point Clicker traffic." % sys.argv[0];
83 print "%s carrier [freq]\n\tHolds a carrier on [freq] Hz." % sys.argv[0];
86 #Initialize FET and set baud rate
92 if(sys.argv[1]=="info"):
95 if(sys.argv[1]=="test"):
96 print "Old registers:"
99 # Set PWR_UP=1 and PRIM_RX=0 in CONFIG.
101 #Delay of 1.5ms by round-trip.
106 for foo in [250*10**3,
109 client.RF_setrate(foo);
110 if(client.RF_getrate()!=foo):
111 print "ERROR Rate %i not supported. Got %i instead." % (foo,
112 client.RF_getrate());
115 client.poke(0x0A,0xDEADBEEF,5);
116 #print "SMAC set to %010x" % client.RF_getsmac();
117 if client.RF_getsmac()!=0xdeadbeef:
118 print "ERROR: Failed to set MAC address.";
119 print "Final registers:"
122 if(sys.argv[1]=="carrier"):
124 client.RF_setfreq(eval(sys.argv[2]));
127 print "\nHolding a carrier wave.";
131 if(sys.argv[1]=="tune"):
133 client.tune(sys.argv[2]);
135 print "Specify a tuning, such as 'aa,c78c65805e,14,09'";
136 if(sys.argv[1]=="regs"):
137 for r in range(0,0x20):
138 print "r[0x%02x]=0x%010x //%16s " % (r,client.peek(r),regnames[r]);
139 if(sys.argv[1]=="pyregs"):
140 for r in range(0,0x20):
141 print "client.set(0x%02x,0x%010x); #%16s " % (r,client.peek(r),regnames[r]);
143 if(sys.argv[1]=="peek"):
146 start=int(sys.argv[2],16);
149 stop=int(sys.argv[3],16);
150 print "Peeking from %02x to %02x." % (start,stop);
152 print "%02x: %010x" % (start,client.peek(start));
154 if(sys.argv[1]=="poke"):
158 start=int(sys.argv[2],16);
160 val=int(sys.argv[3],16);
161 print "Poking %02x to become %010x." % (start,val);
163 client.poke(start,val);
164 print "Poked to %04x" % client.peek(start);
166 if(sys.argv[1]=="sniffob"):
167 #Reversal of transmitter code from nRF_CMD.c of OpenBeacon
168 #TODO remove all poke() calls.
170 client.poke(0x00,0x00); #Stop nRF
171 client.poke(0x01,0x00); #Disable Shockburst
172 client.poke(0x02,0x01); #Set RX Pipe 0
174 client.RF_setfreq(2481 * 10**6);
175 client.poke(0x06,0x09); #2MBps, -18dBm in RF_SETUP
176 client.poke(0x07,0x78); #Reset status register
178 #OpenBeacon defines these in little endian as follows.
179 client.RF_setmaclen(5); # SETUP_AW for 5-byte addresses.
180 #0x01, 0x02, 0x03, 0x02, 0x01
181 client.RF_setsmac(0x0102030201);
182 #'O', 'C', 'A', 'E', 'B'
183 client.RF_settmac(0x424541434F);
185 #Set packet length of 16.
186 client.RF_setpacketlen(16);
188 #Power radio, prime for RX, one-byte checksum.
189 client.poke(0x00,0x70|0x03|0x08); #0x08 for one byte, 0x04 for two.
191 print "Listening as %010x on %i MHz" % (client.RF_getsmac(),
192 client.RF_getfreq()/10**6);
193 #Now we're ready to get packets.
198 packet=client.RF_rxpacket();
202 if(sys.argv[1]=="regbits"):
203 print "Scanning registers to determine which bits are valid."
204 regbits=range(0,0x30);
205 for r in range(0,0x30):
207 #Which bits can be set?
210 #Which bits can be clear?
212 zeroes=client.peek(r);
213 regbits[r]=(ones & (~zeroes));
214 for r in range(0,0x30):
216 print "r[0x%02x] masked %02x // %s" % (r,regbits[r], regnames[r]);
217 if(sys.argv[1]=="sniffprom"):
218 #Reversal of transmitter code from nRF_CMD.c of OpenBeacon
219 #TODO remove all poke() calls.
221 client.poke(0x00,0x00); #Stop nRF
222 client.poke(0x01,0x00); #Disable Shockburst
223 client.poke(0x02,0x01); #Set RX Pipe 0
225 #client.RF_setfreq(2481 * 10**6);
226 #client.poke(0x06,0x09); #2MBps, -18dBm in RF_SETUP
227 client.poke(0x07,0x78); #Reset status register
229 #OpenBeacon defines these in little endian as follows.
230 client.RF_setmaclen(2); # SETUP_AW for shortest
232 #It's better to have a known fragment, when one is available.
233 #client.RF_setsmac(0x00AA);
234 #client.RF_setsmac(0x0055);
236 #Should end in 55 or AA depending upon the packet.
239 tail=int(sys.argv[2],16);
241 print "Please specify a tail of 0xAA or 0x55.";
243 client.RF_setsmac(tail);
246 client.RF_setpacketlen(32);
248 #Power radio, prime for RX, no checksum
249 client.poke(0x00,0x70|0x03); #0x08 for checksum, 0x04 for two.
251 print "Listening as %010x on %i MHz" % (client.RF_getsmac(),
252 client.RF_getfreq()/10**6);
253 #Now we're ready to get packets.
258 packet=client.RF_rxpacket();
263 """This guesses addresses by searching through packets."""
267 #Limits on search space, because you usually know what you're looking for.
271 startch=0; #Useful for forcing an early match.
272 def init(self,goodfet,
273 rate=True,chan=True,sync=True):
274 """Initializes a link to the GoodFET for autotuning."""
280 client.poke(0x00,0x00); #Stop nRF
281 client.poke(0x01,0x00); #Disable Shockburst
282 client.poke(0x02,0x01); #Set RX Pipe 0
285 client.poke(0x1C,0x00);
286 client.poke(0x1D,0x00);
288 client.RF_setmaclen(2); # SETUP_AW for shortest
291 #client.RF_setsmac(0x00AA);
292 #client.RF_setsmac(0x0055);
294 client.poke(0x00,0x70|0x03); #prime radio.
297 def packetaddr(self,packet,justmac=False):
298 """Returns a loaded packet address, including channel and rate."""
300 sync=self.client.RF_getsmac()&0xFF;
305 mac="%s%02x" % (mac,ord(packet[i]));
308 ch=self.client.peek(0x05);
309 rate=self.client.peek(0x06);
310 return "%02x,%s,%02x,%02x" % (
312 def validmac(self,packet):
313 sync=self.client.RF_getsmac()&0xFF;
314 mac=self.packetaddr(packet,justmac=True);
315 if (ord(packet[0])&0x80)^(sync&0x80):
316 #print "%02x%02x invalid entry." % (sync,ord(packet[0]));
317 #This is a special kind of failure. Freq is probably right, but MAC is wrong.
319 if mac=='5555555555' or mac=='aaaaaaaaaa':
323 def handle(self,packet):
324 """Handles a packet."""
325 #printpacket(packet);
327 if not self.validmac(packet):
328 #print "Dropped packet from %s" % self.packetaddr(packet,justmac=True);
329 #printpacket(packet);
332 addr=self.packetaddr(packet);
334 #Increment the address count.
337 count=self.addresses[addr];
340 self.addresses[addr]=count+1;
341 rate=count*1.0/len(self.addresses);
342 if self.addresses[addr]>1 or rate>0.01:
343 print "'%s' looks valid\t%i\t%0.5f" % (
347 def selftune(self,threshold=2,forever=False,
349 """Tunes to the first strong signal.
350 It's important that this not get triggered by false positives."""
354 start=time.mktime(time.localtime());
355 while (time.mktime(time.localtime())-start) < delay:
358 packet=client.RF_rxpacket();
359 addr=guesser.handle(packet);
361 count=self.addresses[addr];
371 """Tunes to another channel or preamble looking for the next packet."""
372 count=self.tunecount;
373 self.tunecount=count+1;
375 #Swap the SYNC value most often.
380 self.client.RF_setsmac(sync);
387 #This swaps between 1Mbps and 2Mbps.
388 #TODO add support for 256kbps, if anyone uses it.
391 #print "Setting rate to 0x%02x" % rate;
394 self.client.poke(0x06,rate);
398 self.client.poke(0x05,
399 (count+self.startch)&0x7f);
400 print "Tuned to %i MHz" % (
401 self.client.RF_getfreq()
403 #Grab two packets to clear buffers.
404 #Should retune only after a few packets to reduce this delay.
405 packet=client.RF_rxpacket();
406 packet=client.RF_rxpacket();
410 if(sys.argv[1]=="autotune"):
411 #Reversal of transmitter code from nRF_CMD.c of OpenBeacon
412 #TODO remove all poke() calls.
414 guesser.init(client,rate=True,sync=True,chan=True);
417 #client.RF_setfreq(2481 * 10**6);
418 client.poke(0x06,0x09); #2MBps, -18dBm in RF_SETUP
419 client.poke(0x07,0x78); #Reset status register
421 #This is determined by the MAC, which we don't yet know.
424 client.RF_setpacketlen(32);
426 #Power radio, prime for RX, no checksum
427 client.poke(0x00,0x70|0x03); #0x08 for checksum, 0x04 for two.
429 print "Autotuning on %i MHz" % (
430 client.RF_getfreq()/10**6);
431 print "sync,mac,r5,r6";
432 #Now we're ready to get packets.
433 guesser.selftune(threshold=2,
436 if(sys.argv[1]=="sniffmskb"):
438 #TODO remove all poke() calls.
440 client.poke(0x00,0x00); #Stop nRF
441 client.poke(0x01,0x00); #Disable Shockburst
442 client.poke(0x02,0x01); #Set RX Pipe 0
444 client.poke(0x06,0x09); #2MBps, -18dBm in RF_SETUP
445 client.poke(0x07,0x78); #Reset status register
447 #This is the address of a specific keyboard.
448 #Other keyboards will be different.
450 client.RF_setmaclen(5);
452 #Known pairs. The channel and the low address bytes must match.
453 #client.RF_setfreq((2400+0x13) * 10**6);
454 #client.RF_setsmac(0xc00a3598cd);
456 #client.RF_setfreq((2400+0x15) * 10**6);
457 #client.RF_setsmac(0xc10446facd);
459 #Mac packet length, illegally 0-length address field.
460 client.RF_setpacketlen(16);
464 client.tune(sys.argv[2]);
467 print "Searching for a keyboard.";
470 guesser.init(client, rate=False, sync=True, chan=True);
471 guesser.selftune(threshold=4,forever=False,
474 client.poke(0x00,0x00); #Stop nRF
475 client.poke(0x01,0x00); #Disable Shockburst
476 client.poke(0x02,0x01); #Set RX Pipe 0
477 #client.RF_setmaclen(3);
479 #Finally, dynamic payload lengths need to be enabled.
480 #client.poke(0x01,0x01);
481 client.poke(0x1C,0x01);
482 client.poke(0x1D,0x06);
484 client.poke(0x00,0x70|0x03); #prime radio.
485 print "Listening as %010x on %i MHz" % (client.RF_getsmac(),
486 client.RF_getfreq()/10**6);
487 #Now we're ready to get packets.
492 packet=client.RF_rxpacket();
494 printmspacket(packet);
499 if(sys.argv[1]=="sniffskybrake"):
500 #Reversal of transmitter code from nRF_CMD.c of OpenBeacon
501 #TODO remove all poke() calls.
503 client.poke(0x00,0x00); #Stop nRF
504 client.poke(0x01,0x00); #Disable Shockburst
505 client.poke(0x02,0x01); #Set RX Pipe 0
507 client.RF_setfreq(2439 * 10**6);
508 client.poke(0x06,0x00); #1MBps
509 client.poke(0x07,0x78); #Reset status register
511 #OpenBeacon defines these in little endian as follows.
512 client.RF_setmaclen(2); # SETUP_AW for 3-byte addresses.
513 #0x01, 0x02, 0x03, 0x02, 0x01
515 client.RF_setsmac(0x070700d2c4); #reversed
517 #client.RF_setsmac(0xd2c4);
518 #client.RF_setsmac(0);
520 #Mac packet length, illegally 0-length address field.
521 client.RF_setpacketlen(32);
523 #Power radio, prime for RX, one-byte checksum.
524 client.poke(0x00,0x70|0x03); #0x08 for one byte, 0x04 for two.
526 print "Listening as %010x on %i MHz" % (client.RF_getsmac(),
527 client.RF_getfreq()/10**6);
528 print "%i byte mac match." % client.RF_getmaclen();
529 #Now we're ready to get packets.
534 packet=client.RF_rxpacket();
538 if(sys.argv[1]=="sniffsf"):
539 #Reversal of transmitter code from nRF_CMD.c of OpenBeacon
540 #TODO remove all poke() calls.
542 client.poke(0x00,0x00); #Stop nRF
543 client.poke(0x01,0x00); #Disable Shockburst
544 client.poke(0x02,0x01); #Set RX Pipe 0
546 client.RF_setfreq(2402 * 10**6);
547 client.poke(0x06,0x07); #1Mbps
548 client.poke(0x07,0x78); #Reset status register
550 #OpenBeacon defines these in little endian as follows.
551 client.RF_setmaclen(5); # SETUP_AW for 5-byte addresses.
552 client.RF_setsmac(0xe7e7e7e7e7);
553 client.RF_settmac(0xe7e7e7e7e7);
555 #Set packet length of 16.
556 client.RF_setpacketlen(4);
558 #Power radio, prime for RX, one-byte checksum.
559 client.poke(0x00,0x70|0x03|0x08); #0x08 for one byte, 0x04 for two.
561 print "Listening as %010x on %i MHz" % (client.RF_getsmac(),
562 client.RF_getfreq()/10**6);
563 #Now we're ready to get packets.
568 packet=client.RF_rxpacket();
572 if(sys.argv[1]=="snifftp"):
573 client.poke(0x00,0x00); #Stop nRF
574 client.poke(0x01,0x00); #Disable Shockburst
575 client.poke(0x02,0x01); #Set RX Pipe 0
577 client.RF_setfreq((2400+0x29) * 10**6);
578 client.poke(0x06,0x00); #1Mbps
579 client.poke(0x07,0x78); #Reset status register
581 client.RF_setmaclen(3); # SETUP_AW for 3-byte addresses.
582 client.RF_setsmac(0x123456);
583 client.RF_setpacketlen(4);
585 #Power radio, prime for RX, two-byte checksum.
586 client.poke(0x00,0x70|0x03|0x04|0x08);
588 print "Listening as %010x on %i MHz" % (client.RF_getsmac(),
589 client.RF_getfreq()/10**6);
590 #Now we're ready to get packets.
595 packet=client.RF_rxpacket();
599 if(sys.argv[1]=="hosttp"):
600 client.poke(0x00,0x00); #Stop nRF
601 client.poke(0x01,0x00); #Disable Shockburst
602 client.poke(0x02,0x01); #Set RX Pipe 0
606 client.RF_setfreq((2400+chan) * 10**6);
607 client.poke(0x06,0x00); #1Mbps
608 client.poke(0x07,0x78); #Reset status register
610 client.RF_setmaclen(3); # SETUP_AW for 3-byte addresses.
611 client.RF_setsmac(0x123456);
612 client.RF_setpacketlen(4);
614 #Power radio, prime for RX, two-byte checksum.
615 client.poke(0x00,0x70|0x03|0x04|0x08);
617 print "Listening as %010x on %i MHz" % (client.RF_getsmac(),
618 client.RF_getfreq()/10**6);
619 #Now we're ready to get packets.
623 packet=client.RF_rxpacket();
624 mac=((ord(packet[0])<<16)+
628 print "%c from %06x" % (key,mac);
631 if(sys.argv[1]=="sniff"):
632 #client.poke(0x00,0x00); #Stop nRF
633 client.poke(0x07,0x78); #Reset status register
635 #Power radio, prime for RX, no checksum.
636 client.poke(0x00,0x70|0x03);
638 client.RF_setpacketlen(32);
640 #client.RF_setmaclen(3); # SETUP_AW for shortest
642 print "Listening as %010x on %i MHz" % (client.RF_getsmac(),
643 client.RF_getfreq()/10**6);
644 #Now we're ready to get packets.
650 packet=client.RF_rxpacket();
653 if(sys.argv[1]=="explore"):
654 #client.poke(0x00,0x00); #Stop nRF
655 client.poke(0x07,0x78); #Reset status register
657 #Power radio, prime for RX, no checksum.
658 client.poke(0x00,0x70|0x03);
660 #Set packet length of 32.
661 #Without checksums, extra data will mix in.
662 client.RF_setpacketlen(32);
663 client.RF_setmaclen(3); # shortest address length
665 #Now we're ready to get packets.
666 for smac in [0x0102030201, 0]:
667 client.RF_setsmac(smac);
668 for chan in range(0,0x80):
669 client.RF_setfreq((2400+chan) * 10**6);
671 packet=client.RF_rxpacket();
673 print "Listening as %010x on %i MHz" % (client.RF_getsmac(),
674 client.RF_getfreq()/10**6);