ca5ed359d4eef327286350d158a6b4f455d1a7cf
[goodfet] / client / goodfet.zigduino
1 #!/usr/bin/env python
2 #GoodFET zigduino client
3 #forked from code by neighbor Travis Goodspeed by bx
4
5 import sys, binascii, os, array, time, glob, argparse
6
7 from GoodFETatmel128 import GoodFETatmel128rfa1
8 from intelhex import IntelHex;
9
10
11 def printpacket(packet):
12     s="";
13     i=0;
14     for foo in packet:
15         i=i+1;
16         if i>client.packetlen: break;
17         s="%s %02x" % (s,ord(foo));
18     print "%s" % s;
19
20
21 commands=["sniff", "beaconreq"]
22 parser = argparse.ArgumentParser(description="Simple GoodFET running on Zigduino tools.")
23 subparsers = parser.add_subparsers(help='sub-command help')
24 p_sniff = subparsers.add_parser('sniff', help='Sniff for packets')
25 p_sniff.add_argument("-f", action="store", dest="channel", help="channel")
26 p_sniff.add_argument("-i", action="store", dest="interface", 
27                     default="/dev/ttyUSB0", help="interface (ie. /dev/ttyUSB0")
28 p_sniff.set_defaults(name="sniff")
29 p_br = subparsers.add_parser('beaconreq', help= 'Send out beacon requests')
30 p_br.add_argument("-f", action="store", dest="channel", help="channel")
31 p_br.add_argument("-c", action="store_true", dest="autocrc", help="Sends beacon requests with autocrc enabled.")
32 p_br.add_argument("-i", action="store", dest="interface", 
33                     default="/dev/ttyUSB0", help="interface (ie. /dev/ttyUSB0")
34
35 p_br.set_defaults(name="beaconreq")
36
37 args = parser.parse_args()
38
39 #Initialize FET and set baud rate
40 client=GoodFETatmel128rfa1()
41 #client.verbose = True
42 client.serInit(args.interface)
43
44 #if(sys.argv[1ce]=="info"):
45 #    printconfig();
46
47 if(args.name=="sniff"):
48     client.RF_setup()
49     if args.channel:
50         print "Set channel to %s" % args.channel
51         client.RF_setchannel(int(args.channel))
52     #Now we're ready to get packets.
53     while 1:
54         packet=None;
55         while packet==None:
56             packet=client.RF_rxpacket();
57             time.sleep(.5)
58         printpacket(packet);
59         sys.stdout.flush();
60
61 if (args.name=="beaconreq"):
62     if (args.autocrc):
63         autocrc = 1
64     else:
65         autocrc = 0
66     client.RF_setup()
67     if args.channel:
68         print "Set channel to %s" % args.channel
69         client.RF_setchannel(int(args.channel))
70     client.RF_autocrc(autocrc)
71     if autocrc:
72         packet="\x03\x08\x46\xff\xff\xff\xff\x07\x00\x00"        
73     else:
74         packet="\x03\x08\x46\xff\xff\xff\xff\x07\x13\x33"
75     while 1:
76         client.RF_txpacket(chr(len(packet))+packet)
77         time.sleep(1)