Adds support for new config variable.
[goodfet] / client / GoodFET.py
index a602b1e..9bf627c 100755 (executable)
@@ -5,16 +5,16 @@
 #
 # This code is being rewritten and refactored.  You've been warned!
 
-import sys, time, string, cStringIO, struct, glob, serial, os;
+import sys, time, string, cStringIO, struct, glob, os;
 import sqlite3;
 
 fmt = ("B", "<H", None, "<L")
 
 def getClient(name="GoodFET"):
-    import GoodFET, GoodFETCC, GoodFETAVR, GoodFETSPI, GoodFETMSP430, GoodFETNRF;
+    import GoodFET, GoodFETCC, GoodFETAVR, GoodFETSPI, GoodFETMSP430, GoodFETNRF, GoodFETCCSPI;
     if(name=="GoodFET" or name=="monitor"): return GoodFET.GoodFET();
     elif name=="cc" or name=="cc51": return GoodFETCC.GoodFETCC();
-    elif name=="cc2420" or name=="ccspi": return GoodFETCC.GoodFETCC();
+    elif name=="cc2420" or name=="ccspi": return GoodFETCCSPI.GoodFETCCSPI();
     elif name=="avr": return GoodFETAVR.GoodFETAVR();
     elif name=="spi": return GoodFETSPI.GoodFETSPI();
     elif name=="msp430": return GoodFETMSP430.GoodFETMSP430();
@@ -48,7 +48,52 @@ class SymbolTable:
                         "values(?,?,?,?,?);", (
                 adr,name,memory,size,comment));
         #print "Set %s=%s." % (name,adr);
+class GoodFETbtser:
+    """py-bluez class for emulating py-serial."""
+    def __init__(self,btaddr):
+        import bluetooth;
+        if btaddr==None or btaddr=="none" or btaddr=="bluetooth":
+            print "performing inquiry..."
+            nearby_devices = bluetooth.discover_devices(lookup_names = True)
+            print "found %d devices" % len(nearby_devices)
+            for addr, name in nearby_devices:
+                print "  %s - '%s'" % (addr, name)
+                if name=='FireFly-A6BD':
+                    btaddr=addr;
+            print "Please set $GOODFET to the address of your device.";
+            sys.exit();
+        print "Identified GoodFET at %s" % btaddr;
 
+        # Manually use the portnumber.
+        port=1;
+        
+        print "Connecting to %s on port %i." % (btaddr, port);
+        sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM);
+        self.sock=sock;
+        sock.connect((btaddr,port));
+        sock.settimeout(10);  #IMPORTANT Must be patient.
+        
+        ##This is what we'd do for a normal reset.
+        #str="";
+        #while not str.endswith("goodfet.sf.net/"):
+        #    str=self.read(64);
+        #    print str;
+        
+        # Instead, just return and hope for the best.
+        return;
+        
+    def write(self,msg):
+        """Send traffic."""
+        import time;
+        self.sock.send(msg);
+        #time.sleep(0.1);
+        return;
+    def read(self,length):
+        """Read traffic."""
+        data="";
+        while len(data)<length:
+            data=data+self.sock.recv(length-len(data));
+        return data;
 class GoodFET:
     """GoodFET Client Library"""
 
@@ -73,8 +118,27 @@ class GoodFET:
     def timeout(self):
         print "timeout\n";
     def serInit(self, port=None, timeout=2, attemptlimit=None):
+        """Open a serial port of some kind."""
+        import re;
+        
+        if port==None:
+            port=os.environ.get("GOODFET");
+        if port=="bluetooth" or (port is not None and re.match("..:..:..:..:..:..",port)):
+            self.btInit(port,timeout,attemptlimit);
+        else:
+            self.pyserInit(port,timeout,attemptlimit);
+    def btInit(self, port, timeout, attemptlimit):
+        """Open a bluetooth port.""";
+        #self.verbose=True;  #For debugging BT.
+        self.serialport=GoodFETbtser(port);
+        
+    def pyserInit(self, port, timeout, attemptlimit):
         """Open the serial port"""
         # Make timeout None to wait forever, 0 for non-blocking mode.
+        import serial;
+        
+        if os.name=='nt' and sys.version.find('64 bit')!=-1:
+            print "WARNING: PySerial requires a 32-bit Python build in Windows.";
         
         if port is None and os.environ.get("GOODFET")!=None:
             glob_list = glob.glob(os.environ.get("GOODFET"));
@@ -94,7 +158,7 @@ class GoodFET:
             glob_list = glob.glob("/dev/ttyU0");
             if len(glob_list) > 0:
                 port = glob_list[0];
-        if os.name=='nt':
+        if port is None and os.name=='nt':
             from scanwin32 import winScan;
             scan=winScan();
             for order,comport,desc,hwid in sorted(scan.comports()):
@@ -106,10 +170,13 @@ class GoodFET:
                     #Do nothing.
                     a=1;
         
+        baud=115200;
+        if(os.environ.get("platform")=='arduino' or os.environ.get("board")=='arduino'):
+            baud=19200; #Slower, for now.
         self.serialport = serial.Serial(
             port,
             #9600,
-            115200,
+            baud,
             parity = serial.PARITY_NONE,
             timeout=timeout
             )
@@ -119,43 +186,50 @@ class GoodFET:
         connected=0;
         while connected==0:
             while self.verb!=0x7F or self.data!="http://goodfet.sf.net/":
+            #while self.data!="http://goodfet.sf.net/":
+                #print "'%s'!=\n'%s'" % (self.data,"http://goodfet.sf.net/");
                 if attemptlimit is not None and attempts >= attemptlimit:
                     return
                 elif attempts>2:
                     print "Resyncing.";
                 self.serialport.flushInput()
                 self.serialport.flushOutput()
-                #Explicitly set RTS and DTR to halt board.
-                self.serialport.setRTS(1);
-                self.serialport.setDTR(1);
-                #Drop DTR, which is !RST, low to begin the app.
-                self.serialport.setDTR(0);
                 
                 #TelosB reset, prefer software to I2C SPST Switch.
-                if(os.environ.get("platform")=='telosb'):
+                if(os.environ.get("platform")=='telosb' or  os.environ.get("board")=='telosb'):
                     #print "TelosB Reset";
                     self.telosBReset();
+                else:
+                    #Explicitly set RTS and DTR to halt board.
+                    self.serialport.setRTS(1);
+                    self.serialport.setDTR(1);
+                    #Drop DTR, which is !RST, low to begin the app.
+                    self.serialport.setDTR(0);
+                
                 #self.serialport.write(chr(0x80));
                 #self.serialport.write(chr(0x80));
                 #self.serialport.write(chr(0x80));
                 #self.serialport.write(chr(0x80));
                 
                 
-                self.serialport.flushInput()
-                self.serialport.flushOutput()
+                #self.serialport.flushInput()
+                #self.serialport.flushOutput()
                 #time.sleep(60);
                 attempts=attempts+1;
                 self.readcmd(); #Read the first command.
+                #print "Got %02x,%02x:'%s'" % (self.app,self.verb,self.data);
             #Here we have a connection, but maybe not a good one.
+            #print "We have a connection."
             connected=1;
             olds=self.infostring();
             clocking=self.monitorclocking();
             for foo in range(1,30):
                 if not self.monitorecho():
-                    if self.verbose: print "Comm error on %i try, resyncing out of %s." % (foo,
-                                                  clocking);
-                    connected=0;
-                    break;
+                    if self.verbose:
+                        print "Comm error on %i try, resyncing out of %s." % (foo,
+                                                                              clocking);
+                        connected=0;
+                        break;
         if self.verbose: print "Connected after %02i attempts." % attempts;
         self.mon_connected();
         self.serialport.setTimeout(12);
@@ -271,8 +345,14 @@ class GoodFET:
             try:
                 #print "Reading...";
                 self.app=ord(self.serialport.read(1));
-                #print "APP=%2x" % self.app;
+                #print "APP=%02x" % self.app;
                 self.verb=ord(self.serialport.read(1));
+                
+                #Fixes an obscure bug in the TelosB.
+                if self.app==0x00:
+                    while self.verb==0x00:
+                        self.verb=ord(self.serialport.read(1));
+                
                 #print "VERB=%02x" % self.verb;
                 self.count=(
                     ord(self.serialport.read(1))
@@ -493,7 +573,7 @@ class GoodFET:
         data="The quick brown fox jumped over the lazy dog.";
         self.writecmd(self.MONITORAPP,0x81,len(data),data);
         if self.data!=data:
-            if self.verbose: print "Comm error recognized by monitorecho().";
+            print "Comm error recognized by monitorecho(), got:\n%s" % self.data;
             return 0;
         return 1;
 
@@ -529,14 +609,20 @@ class GoodFET:
         self.MONpoke16(0x56, clock);
     def monitorgetclock(self):
         """Get the clocking value."""
+        if(os.environ.get("platform")=='arduino' or os.environ.get("board")=='arduino'):
+            return 0xDEAD;
+        #Check for MSP430 before peeking this.
         return self.MONpeek16(0x56);
     # The following functions ought to be implemented in
     # every client.
     
     def infostring(self):
-        a=self.MONpeek8(0xff0);
-        b=self.MONpeek8(0xff1);
-        return "%02x%02x" % (a,b);
+        if(os.environ.get("platform")=='arduino' or os.environ.get("board")=='arduino'):
+            return "Arduino";
+        else:
+            a=self.MONpeek8(0xff0);
+            b=self.MONpeek8(0xff1);
+            return "%02x%02x" % (a,b);
     def lock(self):
         print "Locking Unsupported.";
     def erase(self):