testing commit ability
[goodfet] / client / GoodFETMCPCANCommunication.py
index 67616be..233acf2 100644 (file)
@@ -15,6 +15,8 @@ import sys;
 import binascii;
 import array;
 import csv, time, argparse;
+import datetime
+import os
 
 from GoodFETMCPCAN import GoodFETMCPCAN;
 from intelhex import IntelHex;
@@ -25,7 +27,9 @@ class GoodFETMCPCANCommunication:
        self.client=GoodFETMCPCAN();
        self.client.serInit()
        self.client.MCPsetup();
+       self.DATALOCATION = "../../contrib/ThayerData/"
        
+
     
     def printInfo(self):
         
@@ -63,14 +67,17 @@ class GoodFETMCPCANCommunication:
     def reset(self):
         self.client.MCPsetup();
     
+    
     ##########################
     #   SNIFF
     ##########################
          
-    def sniff(self,freq,duration,filename,description, verbose=True, comment=None, standardid=None):
+    def sniff(self,freq,duration,description, verbose=True, comment=None, filename=None, standardid=None, debug = False):
         
         #### ON-CHIP FILTERING
         if(standardid != None):
+            comment = comment+"Filtering for SID ";
+            
             self.client.MCPreqstatConfiguration();  
             self.client.poke8(0x60,0x26); # set RXB0 CTRL register to ONLY accept STANDARD messages with filter match (RXM1=0, RMX0=1, BUKT=1)
             self.client.poke8(0x20,0xFF); #set buffer 0 mask 1 (SID 10:3) to FF
@@ -110,11 +117,21 @@ class GoodFETMCPCANCommunication:
                self.client.poke8(RXFSIDL, SIDlow);
         
                if (verbose == True):
-                   print "Filtering for SID %d (0x%02xh) with filter #%d"%(ID, ID, filter);
+                print "Filtering for SID %d (0x%02xh) with filter #%d"%(ID, ID, filter);
+            comment = comment + ("%d " %(ID))
         
         
         self.client.MCPsetrate(freq);
         
+        # This will handle the files so that we do not loose them. each day we will create a new csv file
+        if( filename==None):
+            #get folder information (based on today's date)
+            now = datetime.datetime.now()
+            datestr = now.strftime("%Y%m%d")
+            path = self.DATALOCATION+datestr+".csv"
+            filename = path
+            
+        
         outfile = open(filename,'a');
         dataWriter = csv.writer(outfile,delimiter=',');
         dataWriter.writerow(['# Time     Error        Bytes 1-13']);
@@ -176,13 +193,33 @@ class GoodFETMCPCANCommunication:
                     row.append(0);  #since we don't check for errors if we're not in debug mode...
                             
                 row.append(comment)
+                #write packet to file
                 for byte in packet:
                     row.append("%02x"%ord(byte));
                 dataWriter.writerow(row);
         
         outfile.close()
         print "Listened for %d seconds, captured %d packets." %(duration,packetcount);
-     
+        return packetcount
+        
+        
+    def filterStdSweep(self, freq = freq, time = 5):
+        msgIDs = []
+       niff(self,freq,duration,description, verbose=True, comment=None, filename=None, standardid=None, debug = False):
+        for i in range(0, 2047, 6):
+            print "sniffing id: %d, %d, %d, %d, %d, %d" % (i,i+1,i+2,i+3,i+4,i+5)
+            comment = "sweepFilter_%d_%d_%d_%d_%d_%d" % (i,i+1,i+2,i+3,i+4,i+5)
+            description = "Running a sweep filer for all the possible standard IDs. This run filters for: %d, %d, %d, %d, %d, %d" % (i,i+1,i+2,i+3,i+4,i+5)
+            count = self.sniff(freq=freq, duration = time, description = description,comment = comment, standardid = [i, i+1, i+2, i+3, i+4, i+5])
+            if( count != 0):
+                for j in range(i,i+5):
+                    comment = "sweepFilter: %d" % (j)
+                    description = "Running a sweep filer for all the possible standard IDs. This run filters for: %d " % j
+                    count = self.sniff(freq=freq, duration = time, description = description,comment = comment, standardid = [j])
+                    if( count != 0):
+                        msgIDs.append(j)
+        return msgIDs
+    
     def sniffTest(self, freq):
         
         rate = freq;
@@ -286,23 +323,26 @@ class GoodFETMCPCANCommunication:
                 break;
     
         
-    def spit(self,freq):
+    def spit(self,freq, standardid,debug):
+        
         self.client.MCPsetrate(freq);
         self.client.MCPreqstatNormal();
         
-        print "Tx Errors:  %3d" % self.client.peek8(0x1c);
-        print "Rx Errors:  %3d" % self.client.peek8(0x1d);
-        print "Error Flags:  %02x\n" % self.client.peek8(0x2d);
-        print "TXB0CTRL: %02x" %self.client.peek8(0x30);
-        print "CANINTF: %02x"  %self.client.peek8(0x2C);
+        if(debug==true):
+            print "Tx Errors:  %3d" % self.client.peek8(0x1c);
+            print "Rx Errors:  %3d" % self.client.peek8(0x1d);
+            print "Error Flags:  %02x\n" % self.client.peek8(0x2d);
+            print "TXB0CTRL: %02x" %self.client.peek8(0x30);
+            print "CANINTF: %02x"  %self.client.peek8(0x2C);
     
+        #### split SID into different regs
+        SIDlow = (standardid & 0x03) << 5;  # get SID bits 2:0, rotate them to bits 7:5
+        SIDhigh = (standardid >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
         
-        packet = [0x00, 
-                   0x08, # LOWER nibble must be 8 or greater to set EXTENDED ID 
-                   0x00, 0x00,
-                   0x08, # UPPER nibble must be 0 to set RTR bit for DATA FRAME
-                      # LOWER nibble is DLC
-                   0x01,0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xFF]    
+        packet = [SIDhigh, SIDlow,
+                  0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
+                  # lower nibble is DLC                   
+                  0x01,0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xFF]    
         
         self.client.txpacket(packet);
     
@@ -314,11 +354,12 @@ class GoodFETMCPCANCommunication:
             print "TXB0CTRL: %02x" %self.client.peek8(0x30);
             self.client.MCPbitmodify(0x30,0x08,0x00);
             print "TXB0CTRL modified to: %02x\n" %self.client.peek8(0x30);
-    
-        print "message sending attempted.";
-        print "Tx Errors:  %02x" % self.client.peek8(0x1c);
-        print "Rx Errors:  %02x" % self.client.peek8(0x1d);
-        print "Error Flags:  %02x" % self.client.peek8(0x2d);        
+        
+        if (debug==true):
+            print "message sending attempted.";
+            print "Tx Errors:  %02x" % self.client.peek8(0x1c);
+            print "Rx Errors:  %02x" % self.client.peek8(0x1d);
+            print "Error Flags:  %02x" % self.client.peek8(0x2d);        
         
 
 
@@ -343,7 +384,7 @@ if __name__ == "__main__":
     parser.add_argument('verb', choices=['info', 'test','peek', 'reset', 'sniff', 'freqtest','snifftest', 'spit']);
     parser.add_argument('-f', '--freq', type=int, default=500, help='The desired frequency (kHz)', choices=[100, 125, 250, 500, 1000]);
     parser.add_argument('-t','--time', type=int, default=15, help='The duration to run the command (s)');
-    parser.add_argument('-o', '--output', default="../../contrib/ted/sniff_out.csv",help='Output file');
+    parser.add_argument('-o', '--output', default=None,help='Output file');
     parser.add_argument("-d", "--description", help='Description of experiment (included in the output file)', default="");
     parser.add_argument('-v',"--verbose",action='store_false',help='-v will stop packet output to terminal', default=True);
     parser.add_argument('-c','--comment', help='Comment attached to ech packet uploaded',default=None);
@@ -391,7 +432,7 @@ if __name__ == "__main__":
     #
     
     if(args.verb=="sniff"):
-        comm.sniff(freq=freq,duration=duration,filename=filename,description=description,verbose=verbose,comment=comments, standardid=standardid)    
+        comm.sniff(freq=freq,duration=duration,description=description,verbose=verbose,comment=comments,filename=filename, standardid=standardid)    
                     
     ##########################
     #   SNIFF TEST
@@ -404,6 +445,7 @@ if __name__ == "__main__":
     if(args.verb=="snifftest"):
         comm.sniffTest(freq=freq)
         
+        
     ##########################
     #   FREQ TEST
     ##########################
@@ -417,6 +459,8 @@ if __name__ == "__main__":
     if(args.verb=="freqtest"):
         comm.freqtest(freq=freq)
 
+
+
     ##########################
     #   iSniff
     ##########################
@@ -462,7 +506,7 @@ if __name__ == "__main__":
     #   transmission (travis thinks this is because we're sniffing in listen-only
     #   and thus not generating an ack bit on the recieving board)
     if(args.verb=="spit"):
-        comm.spit(freq=freq)
+        comm.spit(freq=freq, standardid=standardid, debug=debug)