Port number instructions for Bluetooth.
[goodfet] / client / GoodFET.py
index ce83100..8c5d945 100755 (executable)
@@ -5,7 +5,7 @@
 #
 # 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")
@@ -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,24 @@ 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.";
@@ -299,8 +360,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: