Fixed a lot of autotuning scripts.
[goodfet] / client / goodfet.nrf
1 #!/usr/bin/env python
2
3 #GoodFET SPI Flash Client
4 #by Travis Goodspeed
5
6 import sys;
7 import binascii;
8 import array;
9 import time;
10
11 from GoodFETNRF import GoodFETNRF;
12 from intelhex import IntelHex;
13
14
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",
18           "TX_ADDR",
19           "RX_PW_P0","RX_PW_P1","RX_PW_P2","RX_PW_P3","RX_PW_P4","RX_PW_P5",
20           "FIFO_STATUS","?",
21           "?","?","?","DYNPD","FEATURE","AGC_CONFIG","?","?",
22           "?","?","?","?","?","?","?","?"];
23
24 def printpacket(packet):
25     s="";
26     i=0;
27     for foo in packet:
28         i=i+1;
29         if i>client.packetlen: break;
30         s="%s %02x" % (s,ord(foo));
31     print "%s" % s;
32
33 def printmspacket(packet,offset=1):
34     keyword=client.RF_getsmac();
35     #print "keyword=%010x" % key;
36     key=[];
37     for foo in range(0,5):
38         key.append(keyword&0xFF);
39         keyword=(keyword>>8);
40         #print "Keybyte %02x" % key[foo];
41     i=0;
42     s="";
43     for foo in packet:
44         if i>=4:
45             s="%s %02x" % (s,ord(foo)^key[(i+offset)%5]);
46         else:
47             s="%s %02x" % (s,ord(foo));
48         i=i+1;
49     print "%s" % (s);
50 def printconfig():
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();
58
59
60 if(len(sys.argv)==1):
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];
67     print "";
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];
73     print "";
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];
76     print "";
77     print "%s sniffskybrake\n\tSniffs skybrake. [broken?]" % sys.argv[0];
78     print "%s sniffmskb\n\tSniffs MS KB. [broken?]" % sys.argv[0];
79     
80     
81     print "%s hosttp\n\tHosts Turning Point Clicker traffic." % sys.argv[0];
82
83     print "%s carrier [freq]\n\tHolds a carrier on [freq] Hz." % sys.argv[0];
84     sys.exit();
85
86 #Initialize FET and set baud rate
87 client=GoodFETNRF();
88 client.serInit()
89
90 client.NRFsetup();
91
92 if(sys.argv[1]=="info"):
93     printconfig();
94
95 if(sys.argv[1]=="test"):
96     print "Old registers:"
97     printconfig();
98     
99     # Set PWR_UP=1 and PRIM_RX=0 in CONFIG.
100     client.poke(0x00,2);
101     #Delay of 1.5ms by round-trip.
102     
103     print "\n\n";
104     
105     #Try all data rates
106     for foo in [250*10**3,
107                 1*10**6,
108                 2*10**6]:
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());
113     
114     print "\n\n";
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:"
120     printconfig();
121
122 if(sys.argv[1]=="carrier"):
123     if len(sys.argv)>2:
124         client.RF_setfreq(eval(sys.argv[2]));
125     client.RF_carrier();
126     printconfig();
127     print "\nHolding a carrier wave.";
128     while(1):
129         time.sleep(1);
130
131 if(sys.argv[1]=="tune"):
132     if len(sys.argv)>2:
133         client.tune(sys.argv[2]);
134     else:
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]);
142
143 if(sys.argv[1]=="peek"):
144     start=0x0000;
145     if(len(sys.argv)>2):
146         start=int(sys.argv[2],16);
147     stop=start;
148     if(len(sys.argv)>3):
149         stop=int(sys.argv[3],16);
150     print "Peeking from %02x to %02x." % (start,stop);
151     while start<=stop:
152         print "%02x: %010x" % (start,client.peek(start));
153         start=start+1;
154 if(sys.argv[1]=="poke"):
155     start=0x0000;
156     val=0x00;
157     if(len(sys.argv)>2):
158         start=int(sys.argv[2],16);
159     if(len(sys.argv)>3):
160         val=int(sys.argv[3],16);
161     print "Poking %02x to become %010x." % (start,val);
162     
163     client.poke(start,val);
164     print "Poked to %04x" % client.peek(start);
165
166 if(sys.argv[1]=="sniffob"):
167     #Reversal of transmitter code from nRF_CMD.c of OpenBeacon
168     #TODO remove all poke() calls.
169     
170     client.poke(0x00,0x00); #Stop nRF
171     client.poke(0x01,0x00); #Disable Shockburst
172     client.poke(0x02,0x01); #Set RX Pipe 0
173     
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
177     
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);
184     
185     #Set packet length of 16.
186     client.RF_setpacketlen(16);
187     
188     #Power radio, prime for RX, one-byte checksum.
189     client.poke(0x00,0x70|0x03|0x08); #0x08 for one byte, 0x04 for two.
190     
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.
194     while 1:
195         packet=None;
196         while packet==None:
197             #time.sleep(0.1);
198             packet=client.RF_rxpacket();
199         printpacket(packet);
200         sys.stdout.flush();
201
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):
206         old=client.peek(r);
207         #Which bits can be set?
208         client.poke(r,0xFF);
209         ones=client.peek(r);
210         #Which bits can be clear?
211         client.poke(r,0x00);
212         zeroes=client.peek(r);
213         regbits[r]=(ones & (~zeroes));
214     for r in range(0,0x30):
215         if regbits[r]!=0:
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.
220     
221     client.poke(0x00,0x00); #Stop nRF
222     client.poke(0x01,0x00); #Disable Shockburst
223     client.poke(0x02,0x01); #Set RX Pipe 0
224     
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
228     
229     #OpenBeacon defines these in little endian as follows.
230     client.RF_setmaclen(2); # SETUP_AW for shortest
231     
232     #It's better to have a known fragment, when one is available.
233     #client.RF_setsmac(0x00AA);
234     #client.RF_setsmac(0x0055);
235     
236     #Should end in 55 or AA depending upon the packet.
237     tail=0x55
238     if(len(sys.argv)>2):
239         tail=int(sys.argv[2],16);
240     else:
241         print "Please specify a tail of 0xAA or 0x55.";
242         sys.exit(1);
243     client.RF_setsmac(tail);
244     
245     #Longest length.
246     client.RF_setpacketlen(32);
247     
248     #Power radio, prime for RX, no checksum
249     client.poke(0x00,0x70|0x03); #0x08 for checksum, 0x04 for two.
250     
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.
254     while 1:
255         packet=None;
256         while packet==None:
257             #time.sleep(0.1);
258             packet=client.RF_rxpacket();
259         printpacket(packet);
260         sys.stdout.flush();
261
262 class AutoTuner():
263     """This guesses addresses by searching through packets."""
264     addresses={};
265     client=None;
266     
267     #Limits on search space, because you usually know what you're looking for.
268     rate=False;
269     chan=False;
270     sync=False;
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."""
275         self.client=goodfet;
276         self.rate=rate;
277         self.chan=chan;
278         self.sync=sync;
279         
280         client.poke(0x00,0x00); #Stop nRF
281         client.poke(0x01,0x00); #Disable Shockburst
282         client.poke(0x02,0x01); #Set RX Pipe 0
283         
284         #Disable shockburst.
285         client.poke(0x1C,0x00);
286         client.poke(0x1D,0x00);
287     
288         client.RF_setmaclen(2); # SETUP_AW for shortest
289         
290         #historic
291         #client.RF_setsmac(0x00AA);
292         #client.RF_setsmac(0x0055);
293         
294         client.poke(0x00,0x70|0x03); #prime radio.
295         
296         return;
297     def packetaddr(self,packet,justmac=False):
298         """Returns a loaded packet address, including channel and rate."""
299         
300         sync=self.client.RF_getsmac()&0xFF;
301         
302         mac="";
303         #MAC,RF_CH,RATE
304         for i in range(0,5):
305             mac="%s%02x" % (mac,ord(packet[i]));
306         if justmac:
307             return mac;
308         ch=self.client.peek(0x05);
309         rate=self.client.peek(0x06);
310         return "%02x,%s,%02x,%02x" % (
311             sync,mac,ch,rate);
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.
318             return False;
319         if mac=='5555555555' or mac=='aaaaaaaaaa':
320             return False;
321         return True;
322         
323     def handle(self,packet):
324         """Handles a packet."""
325         #printpacket(packet);
326         
327         if not self.validmac(packet):
328             #print "Dropped packet from %s" % self.packetaddr(packet,justmac=True);
329             #printpacket(packet);
330             return;
331         
332         addr=self.packetaddr(packet);
333         
334         #Increment the address count.
335         count=0;
336         try:
337             count=self.addresses[addr];
338         except:
339             pass;
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" % (
344                 addr,count,rate);
345         return addr;
346     tunecount=0;
347     def selftune(self,threshold=2,forever=False,
348                  delay=5.0):
349         """Tunes to the first strong signal.
350         It's important that this not get triggered by false positives."""
351         
352         while 1:
353             self.retune();
354             start=time.mktime(time.localtime());
355             while (time.mktime(time.localtime())-start) < delay:
356                 packet=None;
357                 while packet==None:
358                     packet=client.RF_rxpacket();
359                 addr=guesser.handle(packet);
360                 try:
361                     count=self.addresses[addr];
362                 except:
363                     count=0;
364                 if count>threshold:
365                     #Tune it in here?
366                     client.tune(addr);
367                     return addr;
368             sys.stdout.flush();
369         
370     def retune(self):
371         """Tunes to another channel or preamble looking for the next packet."""
372         count=self.tunecount;
373         self.tunecount=count+1;
374         
375         #Swap the SYNC value most often.
376         if self.sync:
377             sync=0xAA;
378             if count&1:
379                 sync=0x55;
380             self.client.RF_setsmac(sync);
381             count=(count>>1);
382         
383         if self.rate:
384             #Then the data rate.
385             rate=0;
386             
387             #This swaps between 1Mbps and 2Mbps.
388             #TODO add support for 256kbps, if anyone uses it.
389             if count&1:
390                 rate=rate|0x08;
391             #print "Setting rate to 0x%02x" % rate;
392             if(rate==0x20):
393                 rate=0x08;
394             self.client.poke(0x06,rate);
395             count=(count>>1);
396         
397         if self.chan:
398             self.client.poke(0x05,
399                              (count+self.startch)&0x7f);
400             print "Tuned to %i MHz" % (
401                 self.client.RF_getfreq()
402                 /(10**6));
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();
407         return;
408         
409         
410 if(sys.argv[1]=="autotune"):
411     #Reversal of transmitter code from nRF_CMD.c of OpenBeacon
412     #TODO remove all poke() calls.
413     guesser=AutoTuner();
414     guesser.init(client,rate=True,sync=True,chan=True);
415     
416     
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
420     
421     #This is determined by the MAC, which we don't yet know.
422     
423     #Longest length.
424     client.RF_setpacketlen(32);
425     
426     #Power radio, prime for RX, no checksum
427     client.poke(0x00,0x70|0x03); #0x08 for checksum, 0x04 for two.
428     
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,
434                      forever=True);
435
436 if(sys.argv[1]=="sniffmskb"):
437     #MSWK 3000 v2.0
438     #TODO remove all poke() calls.
439     
440     client.poke(0x00,0x00); #Stop nRF
441     client.poke(0x01,0x00); #Disable Shockburst
442     client.poke(0x02,0x01); #Set RX Pipe 0
443     
444     client.poke(0x06,0x09); #2MBps, -18dBm in RF_SETUP
445     client.poke(0x07,0x78); #Reset status register
446     
447     #This is the address of a specific keyboard.
448     #Other keyboards will be different.
449     
450     client.RF_setmaclen(5);
451     
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);
455     
456     #client.RF_setfreq((2400+0x15) * 10**6);
457     #client.RF_setsmac(0xc10446facd);
458     
459     #Mac packet length, illegally 0-length address field.
460     client.RF_setpacketlen(16);
461     
462     #aa,c00a3598cd,13,09
463     if len(sys.argv)>2:
464         client.tune(sys.argv[2]);
465     else:
466         
467         print "Searching for a keyboard.";
468         
469         guesser=AutoTuner();
470         guesser.init(client, rate=False, sync=True, chan=True);
471         guesser.selftune(threshold=4,forever=False,
472                          delay=10.0);
473     
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);
478     
479     #Finally, dynamic payload lengths need to be enabled.
480     #client.poke(0x01,0x01);
481     client.poke(0x1C,0x01);
482     client.poke(0x1D,0x06);
483     
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.
488     while 1:
489         packet=None;
490         while packet==None:
491             #time.sleep(1);
492             packet=client.RF_rxpacket();
493             #print ".";
494         printmspacket(packet);
495         sys.stdout.flush();
496
497
498
499 if(sys.argv[1]=="sniffskybrake"):
500     #Reversal of transmitter code from nRF_CMD.c of OpenBeacon
501     #TODO remove all poke() calls.
502     
503     client.poke(0x00,0x00); #Stop nRF
504     client.poke(0x01,0x00); #Disable Shockburst
505     client.poke(0x02,0x01); #Set RX Pipe 0
506     
507     client.RF_setfreq(2439 * 10**6);
508     client.poke(0x06,0x00); #1MBps
509     client.poke(0x07,0x78); #Reset status register
510     
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
514     
515     client.RF_setsmac(0x070700d2c4); #reversed
516     
517     #client.RF_setsmac(0xd2c4);
518     #client.RF_setsmac(0);
519     
520     #Mac packet length, illegally 0-length address field.
521     client.RF_setpacketlen(32);
522     
523     #Power radio, prime for RX, one-byte checksum.
524     client.poke(0x00,0x70|0x03); #0x08 for one byte, 0x04 for two.
525     
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.
530     while 1:
531         packet=None;
532         while packet==None:
533             #time.sleep(0.1);
534             packet=client.RF_rxpacket();
535         printpacket(packet);
536         sys.stdout.flush();
537
538 if(sys.argv[1]=="sniffsf"):
539     #Reversal of transmitter code from nRF_CMD.c of OpenBeacon
540     #TODO remove all poke() calls.
541     
542     client.poke(0x00,0x00); #Stop nRF
543     client.poke(0x01,0x00); #Disable Shockburst
544     client.poke(0x02,0x01); #Set RX Pipe 0
545     
546     client.RF_setfreq(2402 * 10**6);
547     client.poke(0x06,0x07); #1Mbps
548     client.poke(0x07,0x78); #Reset status register
549     
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);
554     
555     #Set packet length of 16.
556     client.RF_setpacketlen(4);
557     
558     #Power radio, prime for RX, one-byte checksum.
559     client.poke(0x00,0x70|0x03|0x08); #0x08 for one byte, 0x04 for two.
560     
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.
564     while 1:
565         packet=None;
566         while packet==None:
567             #time.sleep(0.1);
568             packet=client.RF_rxpacket();
569         printpacket(packet);
570         sys.stdout.flush();
571
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
576     
577     client.RF_setfreq((2400+0x29) * 10**6);
578     client.poke(0x06,0x00); #1Mbps
579     client.poke(0x07,0x78); #Reset status register
580     
581     client.RF_setmaclen(3); # SETUP_AW for 3-byte addresses.
582     client.RF_setsmac(0x123456);
583     client.RF_setpacketlen(4);
584     
585     #Power radio, prime for RX, two-byte checksum.
586     client.poke(0x00,0x70|0x03|0x04|0x08);
587     
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.
591     while 1:
592         packet=None;
593         while packet==None:
594             #time.sleep(0.1);
595             packet=client.RF_rxpacket();
596         printpacket(packet);
597         sys.stdout.flush();
598
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
603     
604     chan=0x29;
605
606     client.RF_setfreq((2400+chan) * 10**6);
607     client.poke(0x06,0x00); #1Mbps
608     client.poke(0x07,0x78); #Reset status register
609     
610     client.RF_setmaclen(3); # SETUP_AW for 3-byte addresses.
611     client.RF_setsmac(0x123456);
612     client.RF_setpacketlen(4);
613     
614     #Power radio, prime for RX, two-byte checksum.
615     client.poke(0x00,0x70|0x03|0x04|0x08);
616     
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.
620     while 1:
621         packet=None;
622         while packet==None:
623             packet=client.RF_rxpacket();
624         mac=((ord(packet[0])<<16)+
625              (ord(packet[1])<<8)+
626              ord(packet[2]));
627         key=packet[3];
628         print "%c from %06x" % (key,mac);
629         sys.stdout.flush();
630
631 if(sys.argv[1]=="sniff"):
632     #client.poke(0x00,0x00); #Stop nRF
633     client.poke(0x07,0x78); #Reset status register
634     
635     #Power radio, prime for RX, no checksum.
636     client.poke(0x00,0x70|0x03);
637     #Mac packet length.
638     client.RF_setpacketlen(32);
639     #Mac length, reduced
640     #client.RF_setmaclen(3); # SETUP_AW for shortest
641     
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.
645     
646     while 1:
647         packet=None;
648         while packet==None:
649             #time.sleep(0.1);
650             packet=client.RF_rxpacket();
651         printpacket(packet);
652         sys.stdout.flush();
653 if(sys.argv[1]=="explore"):
654     #client.poke(0x00,0x00); #Stop nRF
655     client.poke(0x07,0x78); #Reset status register
656     
657     #Power radio, prime for RX, no checksum.
658     client.poke(0x00,0x70|0x03);
659     
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
664     
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);
670             time.sleep(1);
671             packet=client.RF_rxpacket();
672             if packet!=None:
673                 print "Listening as %010x on %i MHz" % (client.RF_getsmac(),
674                                                         client.RF_getfreq()/10**6);
675                 printpacket(packet);
676                 sys.stdout.flush();