Bluetooth stuff.
[goodfet] / client / GoodFET.py
index ab6bb09..d5e132f 100755 (executable)
@@ -52,18 +52,23 @@ class GoodFETbtser:
     """py-bluez class for emulating py-serial."""
     def __init__(self,btaddr):
         import bluetooth;
-        while btaddr==None or btaddr=="none" or btaddr=="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)
+                #TODO switch to wildcards.
                 if name=='FireFly-A6BD':
                     btaddr=addr;
+                if name=='RN42-A94A':
+                    btaddr=addr;
+                
+            print "Please set $GOODFET to the address of your device.";
+            sys.exit();
         print "Identified GoodFET at %s" % btaddr;
 
-        # BlueFET doesn't run the Service Discovery Protocol.
-        # Instead we manually use the portnumber.
+        # Manually use the portnumber.
         port=1;
         
         print "Connecting to %s on port %i." % (btaddr, port);
@@ -72,18 +77,27 @@ class GoodFETbtser:
         sock.connect((btaddr,port));
         sock.settimeout(10);  #IMPORTANT Must be patient.
         
-        #Once connected, we need to get synced.
-        #This might require a firmware patch.
-        str=None;
-        while str!="http://goodfet.sf.net/":
-            str=self.read(64);
-            print str;
+        ##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."""
-        return self.sock.send(msg);
-    def read(self,len):
+        import time;
+        self.sock.send(msg);
+        #time.sleep(0.1);
+        return;
+    def read(self,length):
         """Read traffic."""
-        return self.sock.recv(len);
+        data="";
+        while len(data)<length:
+            data=data+self.sock.recv(length-len(data));
+        return data;
 class GoodFET:
     """GoodFET Client Library"""
 
@@ -109,14 +123,17 @@ class GoodFET:
         print "timeout\n";
     def serInit(self, port=None, timeout=2, attemptlimit=None):
         """Open a serial port of some kind."""
+        import re;
         
-        if port=="bluetooth" or os.environ.get("GOODFET")=="bluetooth":
+        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;
+        #self.verbose=True;  #For debugging BT.
         self.serialport=GoodFETbtser(port);
         
     def pyserInit(self, port, timeout, attemptlimit):
@@ -158,7 +175,7 @@ class GoodFET:
                     a=1;
         
         baud=115200;
-        if(os.environ.get("platform")=='arduino'):
+        if(os.environ.get("platform")=='arduino' or os.environ.get("board")=='arduino'):
             baud=19200; #Slower, for now.
         self.serialport = serial.Serial(
             port,
@@ -183,7 +200,7 @@ class GoodFET:
                 self.serialport.flushOutput()
                 
                 #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:
@@ -210,7 +227,6 @@ class GoodFET:
             connected=1;
             olds=self.infostring();
             clocking=self.monitorclocking();
-            #if(os.environ.get("platform")!='arduino'):
             for foo in range(1,30):
                 if not self.monitorecho():
                     if self.verbose:
@@ -347,8 +363,8 @@ class GoodFET:
                     +(ord(self.serialport.read(1))<<8)
                     );
 
-                #if self.verbose:
-                #print "Rx: ( 0x%02x, 0x%02x, 0x%04x )" % ( self.app, self.verb, self.count )
+                if self.verbose:
+                    print "Rx: ( 0x%02x, 0x%02x, 0x%04x )" % ( self.app, self.verb, self.count )
             
                 #Debugging string; print, but wait.
                 if self.app==0xFF:
@@ -597,7 +613,7 @@ class GoodFET:
         self.MONpoke16(0x56, clock);
     def monitorgetclock(self):
         """Get the clocking value."""
-        if(os.environ.get("platform")=='arduino'):
+        if(os.environ.get("platform")=='arduino' or os.environ.get("board")=='arduino'):
             return 0xDEAD;
         #Check for MSP430 before peeking this.
         return self.MONpeek16(0x56);
@@ -605,7 +621,7 @@ class GoodFET:
     # every client.
     
     def infostring(self):
-        if(os.environ.get("platform")=='arduino'):
+        if(os.environ.get("platform")=='arduino' or os.environ.get("board")=='arduino'):
             return "Arduino";
         else:
             a=self.MONpeek8(0xff0);