2 # GoodFET Chipcon RF Radio Client
4 # (C) 2009, 2012 Travis Goodspeed <travis at radiantmachines.com>
6 # This code is being rewritten and refactored. You've been warned!
8 import sys, time, string, cStringIO, struct, glob, os;
10 from GoodFET import GoodFET;
12 class GoodFETCCSPI(GoodFET):
14 CCversions={0x233d: "CC2420",
17 """Move the FET into the CCSPI application."""
18 self.writecmd(self.CCSPIAPP,0x10,0,self.data); #CCSPI/SETUP
20 #Set up the radio for ZigBee
21 self.strobe(0x01); #SXOSCON
22 self.strobe(0x02); #SCAL
23 self.poke(0x11, 0x0AC2 & (~0x0800)); #MDMCTRL0, promiscuous
24 self.poke(0x12, 0x0500); #MDMCTRL1
25 self.poke(0x1C, 0x007F); #IOCFG0
26 self.poke(0x19, 0x01C4); #SECCTRL0, disabling crypto
27 #self.poke(0x19, 0x0204); #SECCTRL0, as seen elsewhere.
31 return self.peek(0x1E); #MANFIDL
33 manfidl=self.peek(0x1E);
34 #manfidh=self.peek(0x1f);
36 return "%s" % (self.CCversions[manfidl]);
38 return "Unknown0x%04x" % manfidl;
39 def trans8(self,byte):
40 """Read and write 8 bits by CCSPI."""
41 data=self.CCSPItrans([byte]);
45 """Exchange data by CCSPI."""
47 self.writecmd(self.CCSPIAPP,0x00,len(data),data);
49 def strobe(self,reg=0x00):
50 """Strobes a strobe register, returning the status."""
53 return ord(self.data[0]);
54 def CC_RFST_IDLE(self):
55 """Switch the radio to idle mode, clearing overflows and errors."""
56 self.strobe(0x06); #SRXOFF
58 """Switch the radio to TX mode."""
59 self.strobe(0x04); #0x05 for CCA
61 """Switch the radio to RX mode."""
62 self.strobe(0x03); #RX ON
63 def CC_RFST_CAL(self):
64 """Calibrate strobe the radio."""
65 self.strobe(0x02); #RX Calibrate
66 def CC_RFST(self,state=0x00):
69 def peek(self,reg,bytes=2):
70 """Read a CCSPI Register. For long regs, result is flipped."""
72 #Reg is ORed with 0x40 by the GoodFET.
75 #Automatically calibrate the len.
78 self.writecmd(self.CCSPIAPP,0x02,len(data),data);
81 (ord(self.data[1])<<8)
84 def poke(self,reg,val,bytes=2):
85 """Write a CCSPI Register."""
86 data=[reg,(val>>8)&0xFF,val&0xFF];
87 self.writecmd(self.CCSPIAPP,0x03,len(data),data);
88 if self.peek(reg,bytes)!=val and reg!=0x18:
89 print "Warning, failed to set r%02x=0x%04x, got %02x." %(
92 self.peek(reg,bytes));
97 """Read the status byte."""
98 statusbits={0x80: "?",
99 0x40: "XOSC16M_STABLE",
100 0x20: "TX_UNDERFLOW",
106 status=self.strobe(0x00);
111 str="%s %s" % (statusbits[i],str);
115 #Radio stuff begins here.
116 def RF_setenc(self,code="802.15.4"):
117 """Set the encoding type."""
120 """Get the encoding type."""
122 def RF_getrate(self):
124 def RF_setrate(self,rate=0):
126 def RF_getsync(self):
127 return self.peek(0x14);
128 def RF_setsync(self,sync=0xa70F):
129 """Set the SYNC preamble.
130 Use 0xA70F for 0xA7."""
131 self.poke(0x14,sync);
134 def RF_setkey(self,key):
135 """Sets the first key for encryption to the given argument."""
136 print "ERROR: Forgot to set the key.";
139 def RF_setfreq(self,frequency):
140 """Set the frequency in Hz."""
141 mhz=frequency/1000000;
143 fsctrl=self.peek(0x18)&(~0x3FF);
144 fsctrl=fsctrl+int(mhz-2048)
145 self.poke(0x18,fsctrl);
146 #self.CC_RFST_IDLE();
147 self.strobe(0x02);#SCAL
149 self.strobe(0x03);#SRXON
150 def RF_getfreq(self):
151 """Get the frequency in Hz."""
152 fsctrl=self.peek(0x18);
153 mhz=2048+(fsctrl&0x3ff)
155 def RF_setchan(self,channel):
156 """Set the ZigBee/802.15.4 channel number."""
157 if channel < 11 or channel > 26:
158 print "Only 802.15.4 channels 11 to 26 are currently supported.";
160 self.RF_setfreq( ( (channel-11)*5 + 2405 ) * 1000000 );
161 def RF_getsmac(self):
162 """Return the source MAC address."""
164 def RF_setsmac(self,mac):
165 """Set the source MAC address."""
167 def RF_gettmac(self):
168 """Return the target MAC address."""
170 def RF_settmac(self,mac):
171 """Set the target MAC address."""
173 def RF_getrssi(self):
174 """Returns the received signal strength, with a weird offset."""
175 rssival=self.peek(0x13)&0xFF; #raw RSSI register
178 def peekram(self,adr,count):
179 """Peeks data from CC2420 RAM."""
181 adr&0xFF,adr>>8, # Address first.
182 count&0xFF,count>>8 # Then length.
184 self.writecmd(self.CCSPIAPP,0x84,len(data),data);
186 def pokeram(self,adr,data):
187 """Pokes data into CC2420 RAM."""
188 data=[adr&0xFF, adr>>8]+data;
189 self.writecmd(self.CCSPIAPP,0x85,len(data),data);
192 lastpacket=range(0,0xff);
193 def RF_rxpacket(self):
194 """Get a packet from the radio. Returns None if none is
199 self.writecmd(self.CCSPIAPP,0x80,len(data),data);
202 self.lastpacket=buffer;
207 def RF_rxpacketdec(self):
208 """Get and decrypt a packet from the radio. Returns None if
213 self.writecmd(self.CCSPIAPP,0x90,len(data),data);
216 self.lastpacket=buffer;
222 def RF_txpacket(self,packet):
223 """Send a packet through the radio."""
224 self.writecmd(self.CCSPIAPP,0x81,len(packet),packet);
229 def RF_reflexjam(self,duration=0):
230 """Place the device into reflexive jamming mode."""
231 data = [duration&0xff,
233 self.writecmd(self.CCSPIAPP,0xA0,len(data),data);
236 def RF_reflexjam_autoack(self):
237 """Place the device into reflexive jamming mode
238 and that also sends a forged ACK if needed."""
240 self.writecmd(self.CCSPIAPP,0xA1,len(data),data);
241 print "Got:", data, "and", self.data
244 def RF_modulated_spectrum(self):
245 """Hold a carrier wave on the present frequency."""
246 # print "Don't know how to hold a carrier.";
250 # set MDMCTRL1.TX_MODE to 3 0x12 3:2
253 mdmctrl1=self.peek(0x12);
254 #print "mdmctrl1 was %04x" % mdmctrl1;
255 mdmctrl1=mdmctrl1|0x00c0; #MDMCTRL1.TX_MODE = 3
256 self.poke(0x12, mdmctrl1); #MDMCTRL1
258 mdmctrl1=self.peek(0x12);
259 #print "mdmctrl1 is %04x" % mdmctrl1;
261 # http://e2e.ti.com/support/low_power_rf/f/155/t/15914.aspx?PageIndex=2
263 self.strobe(0x02); #STXCAL
264 #print "STXCAL status: %s" % self.status()
267 self.strobe(0x09); #SFLUSHTX
268 #print "SFLUSHTX status: %s" % self.status()
270 self.strobe(0x04); #STXON
271 #print "STXON status: %s" % self.status()
273 def RF_carrier(self):
274 """Hold a carrier wave on the present frequency."""
275 # print "Don't know how to hold a carrier.";
279 # set MDMCTRL1.TX_MODE to 2 or 3 0x12 3:2
280 # set DACTST to 0x1800 0x2E
283 mdmctrl1=self.peek(0x12);
284 #print "mdmctrl1 was %04x" % mdmctrl1;
285 mdmctrl1=mdmctrl1|0x0080;
286 mdmctrl1=mdmctrl1&0x0080; #MDMCTRL1.TX_MODE = 2
287 self.poke(0x12, mdmctrl1); #MDMCTRL1
289 mdmctrl1=self.peek(0x12);
290 #print "mdmctrl1 is %04x" % mdmctrl1;
292 self.poke(0x2E, 0x1800); #DACTST
293 dactst=self.peek(0x2E);
294 #print "dactst is %04x" % dactst;
296 # see above for why this is here
297 self.strobe(0x02); #STXCAL
298 #print "STXCAL status: %s" % self.status()
299 self.strobe(0x09); #SFLUSHTX
300 #print "SFLUSHTX status: %s" % self.status()
302 self.strobe(0x04); #STXON
303 #print "STXON status: %s" % self.status()
305 def RF_promiscuity(self,promiscuous=1):
306 mdmctrl0=self.peek(0x11);
308 mdmctrl0=mdmctrl0&(~0x800);
310 mdmctrl0=mdmctrl0|0x800;
311 self.poke(0x11,mdmctrl0);
313 def RF_autocrc(self,autocrc=1):
314 mdmctrl0=self.peek(0x11);
316 mdmctrl0=mdmctrl0&(~0x0020);
318 mdmctrl0=mdmctrl0|0x0020;
319 self.poke(0x11,mdmctrl0);
321 def RF_autoack(self,autoack=1):
322 mdmctrl0=self.peek(0x11);
324 mdmctrl0=mdmctrl0&(~0x0010);
326 mdmctrl0=mdmctrl0|0x0010;
327 self.poke(0x11,mdmctrl0);
330 def RF_setpacketlen(self,len=16):
331 """Set the number of bytes in the expected payload."""
332 #self.poke(0x11,len);
334 def RF_getpacketlen(self):
335 """Set the number of bytes in the expected payload."""
336 #len=self.peek(0x11);
340 def RF_getmaclen(self):
341 """Get the number of bytes in the MAC address."""
342 choices=[0, 3, 4, 5];
343 choice=self.peek(0x03)&3;
344 self.maclen=choices[choice];
346 def RF_setmaclen(self,len):
347 """Set the number of bytes in the MAC address."""
348 choices=["illegal", "illegal", "illegal",
351 self.poke(0x03,choice);
353 def printpacket(self,packet,prefix="#"):
357 s="%s %02x" % (s,ord(foo));
358 print "%s%s" % (prefix,s);
360 def printdissect(self,packet):
362 from scapy.all import Dot15d4
364 print "To use packet disection, Scapy must be installed and have the Dot15d4 extension present."
365 print "try: hg clone http://hg.secdev.org/scapy-com";
366 print " sudo ./setup.py install";
367 self.printpacket(packet);
369 scapyd = Dot15d4(packet[1:]);