added filterForPacket method, as discussed with Hoder
[goodfet] / client / GoodFETMCPCANCommunication.py
index 09519d2..f5fc9c9 100644 (file)
@@ -24,16 +24,18 @@ import Queue
 
 class GoodFETMCPCANCommunication:
     
-    def __init__(self):
-       self.client=GoodFETMCPCAN();
+    def __init__(self, dataLocation):
+       self.client=GoodFETMCPCAN(); """ Communication with the bus"""
        self.client.serInit()
        self.client.MCPsetup();
-       self.DATALOCATION = "../../contrib/ThayerData/"
+       #self.DATA_LOCATION = "../../contrib/ThayerData/"
+       self.DATA_LOCATION = dataLocation; """ Stores file data location. This is the root folder where basic sniffs will be stored"""
+       self.INJECT_DATA_LOCATION  = self.DATA_LOCATION+"InjectedData/" """ stores the sub folder path where injected data will be stored"""
        
 
     
     def printInfo(self):
-        
+        """ This method will print information about the board to the termina. It is usefull for diagnostics"""
         self.client.MCPreqstatConfiguration();
         
         print "MCP2515 Info:\n\n";
@@ -66,6 +68,9 @@ class GoodFETMCPCANCommunication:
            print self.client.packet2str(foo);
            
     def reset(self):
+        """ 
+        Reset the chip
+        """
         self.client.MCPsetup();
     
     
@@ -74,7 +79,9 @@ class GoodFETMCPCANCommunication:
     ##########################
          
     def sniff(self,freq,duration,description, verbose=True, comment=None, filename=None, standardid=None, debug=False, faster=False, parsed=True, data = None,writeToFile=True):
+        """
         
+        """
         #reset eveything on the chip
         self.client.serInit() 
         self.reset()
@@ -133,7 +140,7 @@ class GoodFETMCPCANCommunication:
             #get folder information (based on today's date)
             now = datetime.datetime.now()
             datestr = now.strftime("%Y%m%d")
-            path = self.DATALOCATION+datestr+".csv"
+            path = self.DATA_LOCATION+datestr+".csv"
             filename = path
             
         if( writeToFile == True):
@@ -364,7 +371,9 @@ class GoodFETMCPCANCommunication:
                 print "Data: " + self.client.packet2str(data);
 
     def test(self):
-        
+        """ This will perform a test on the GOODTHOPTER10. Diagnostic messages will be printed
+        out to the terminal
+        """
         comm.reset();
         print "Just reset..."
         print "EFLG register:  %02x" % self.client.peek8(0x2d);
@@ -399,7 +408,20 @@ class GoodFETMCPCANCommunication:
     
     
     def addFilter(self,standardid, verbose= True):
-        comment = None
+        """ This method will configure filters on the board. Filters are positive filters meaning that they will only 
+        store messages that match the ids provided in the list of standardid. Since there are 2 buffers and due to the configuration
+        of how the filtering works (see MCP2515 documentation), at least 3 filters must be set to guarentee you do not get any
+        unwanted messages. However even with only 1 filter set you should get all messages from that ID but the other buffer will store 
+        any additional messages.
+        @type standardid: list of integers
+        @param standardid: List of standard ids that need to be set. There can be at most 6 filters set.
+        @type verbose: Boolean
+        @param verbose: If true it will print out messages and diagnostics to terminal.
+        
+        @rtype: None
+        @return: This method does not return anything
+        """
+       
         ### ON-CHIP FILTERING
         if(standardid != None):
             self.client.MCPreqstatConfiguration();  
@@ -445,21 +467,167 @@ class GoodFETMCPCANCommunication:
                
         self.client.MCPreqstatNormal();
     
+    def filterForPacket(self, standardid, DB0, DB1, verbose= True):
+        """ This method will configure filters on the board to listen for a specific packet originating from standardid with data bytes 0 and 1. It will configure all six filters, so you will not recieve any other packets.
+            @type standardid: integer
+            @param standardid: standardID to listen for
+            @type DB0: integer
+            @param standardid: DB0 contents to filter for
+            @type DB1: integer
+            @param standardid: DB1 contents to filter for
+            @type verbose: Boolean
+            @param verbose: If true it will print out messages and diagnostics to terminal.
+            
+            @rtype: None
+            @return: This method does not return anything
+            """
+        
+        ### ON-CHIP FILTERING
+       
+        self.client.MCPreqstatConfiguration();  
+        
+        # SID filtering: set CTRL registers to only accept standard messages
+        self.client.poke8(0x60,0x26); # set RXB0 CTRL register to ONLY accept STANDARD messages with filter match (RXM1=0, RXM=1, BUKT=1)
+        self.client.poke8(0x70,0x20); # set RXB1 CTRL register to ONLY accept STANDARD messages with filter match (RXM1=0, RXM0=1)
+
+        # Mask buffer 0 to match SID, DB0, DB1
+        self.client.poke8(0x20,0xFF); #set buffer 0 mask 1 (SID 10:3) to FF
+        self.client.poke8(0x21,0xE0); #set buffer 0 mask 2 bits 7:5 (SID 2:0) to 1s
+        self.client.poke8(0x22,0xFF); #set buffer 0 mask 3 (DB0) to FF 
+        self.client.poke8(0x23,0xFF); #set buffer 0 mask 4 (DB0) to FF
+
+        # Mask buffer 1 to match SID, DB0, DB1
+        self.client.poke8(0x24,0xFF); #set buffer 1 mask 1 (SID 10:3) to FF
+        self.client.poke8(0x25,0xE0); #set buffer 1 mask 2 bits 7:5 (SID 2:0) to 1s
+        self.client.poke8(0x26,0xFF); #set buffer 1 mask 3 (DB0) to FF
+        self.client.poke8(0x27,0xFF); #set buffer 1 mask 4 (DB1) to FF
+        
+        # Split SID into high and low bytes
+        SIDlow = (standardid & 0x07) << 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
+
+            
+        for filter in range(0,5):
+            if (filter==0):
+                RXFSIDH = 0x00;
+                RXFSIDL = 0x01;
+                RXFDB0 = 0x02;
+                RXFDB1 = 0x03;
+            elif (filter==1):
+                RXFSIDH = 0x04;
+                RXFSIDL = 0x05;
+                RXFDB0 = 0x06;
+                RXFDB1 = 0x07;
+            elif (filter==2):
+                RXFSIDH = 0x08;
+                RXFSIDL = 0x09;
+                RXFDB0 = 0x0A;
+                RXFDB1 = 0x0B;
+            elif (filter==3):
+                RXFSIDH = 0x10;
+                RXFSIDL = 0x11;
+                RXFDB0 = 0x12;
+                RXFDB1 = 0x13;
+            elif (filter==4):
+                RXFSIDH = 0x14;
+                RXFSIDL = 0x15;
+                RXFDB0 = 0x16;
+                RXFDB1 = 0x17;
+            else:
+                RXFSIDH = 0x18;
+                RXFSIDL = 0x19;
+                RXFDB0 = 0x1A;
+                RXFDB1 = 0x1B;
+            
+
+            self.client.poke8(RXFSIDH, SIDhigh);
+            self.client.poke8(RXFSIDL, SIDlow);
+            self.client.poke8(RXFDB0, DB0);
+            self.client.poke8(RXFDB1, DB1);
+                
+            if (verbose == True):
+                print "Filtering for SID %d DB0 0x%02xh DB1 0x%02xh with filter #%d"%(ID, DB0, DB1, filter);
+        
+        self.client.MCPreqstatNormal();
+    
     
    
         
     def spitSetup(self,freq):
+        """ 
+        This method sets up the chip for transmitting messages, but does not transmit anything itself.
+        """
         self.reset();
         self.client.MCPsetrate(freq);
         self.client.MCPreqstatNormal();
         
     
-    def spitSingle(self,freq, standardid, repeat, duration = None, debug = False, packet = None):
+    def spitSingle(self,freq, standardid, repeat,writes, period = None, debug = False, packet = None):
+        """ 
+        This method will spit a single message onto the bus. If there is no packet information provided then the 
+        message will be sent as a remote transmission request (RTR). The packet length is assumed to be 8 bytes The message can be repeated a given number of times with
+        a gap of period (milliseconds) between each message. This will continue for the the number of times specified in the writes input.
+        This method will setup the bus and call the spit method, L{spit}. This method includes a bus reset and initialization.
+        
+        @type freq: number
+        @param freq: The frequency of the bus
+        
+        @type standardid: list of integer
+        @param standardid: This is a single length list with one integer elment that corresponds to the standard id you wish to write to
+        
+        @type repeat: Boolean
+        @param repeat: If true the message will be repeatedly injected. if not the message will only be injected 1 time
+        
+        @type writes: Integer
+        @param writes: Number of writes of the packet
+        
+        @type period: Integer
+        @param period: Time delay between injections of the packet in Milliseconds
+        
+        @type debug: Boolean
+        @param debug: When true debug status messages will be printed to the terminal
+        
+        @type packet: List
+        @param packet: Contains the data bytes for the packet which is assumed to be of length 8. Each byte is stored as
+                       an integer and can range from 0 to 255 (8 bits). If packet == None then an RTR will be sent on the given
+                       standard id.
+        
+        """
         self.spitSetup(freq);
-        spit(self,freq, standardid, repeat, duration = None, debug = False, packet = None)
+        spit(self,freq, standardid, repeat,writes,  period, debug , packet)
 
     def spit(self,freq, standardid, repeat,writes, period = None, debug = False, packet = None):
-    
+        """ 
+        This method will spit a single message onto the bus. If there is no packet information provided then the 
+        message will be sent as a remote transmission request (RTR). The packet length is assumed to be 8 bytes The message can be repeated a given number of times with
+        a gap of period (milliseconds) between each message. This will continue for the the number of times specified in the writes input.
+        This method does not include bus setup, it must be done before the method call.
+        
+        
+        @type freq: number
+        @param freq: The frequency of the bus
+        
+        @type standardid: list of integer
+        @param standardid: This is a single length list with one integer elment that corresponds to the standard id you wish to write to
+        
+        @type repeat: Boolean
+        @param repeat: If true the message will be repeatedly injected. if not the message will only be injected 1 time
+        
+        @type writes: Integer
+        @param writes: Number of writes of the packet
+        
+        @type period: Integer
+        @param period: Time delay between injections of the packet in Milliseconds
+        
+        @type debug: Boolean
+        @param debug: When true debug status messages will be printed to the terminal
+        
+        @type packet: List
+        @param packet: Contains the data bytes for the packet which is assumed to be of length 8. Each byte is stored as
+                       an integer and can range from 0 to 255 (8 bits). If packet == None then an RTR will be sent on the given
+                       standard id.
+        
+        """
 
         #### split SID into different regs
         SIDlow = (standardid[0] & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
@@ -472,14 +640,11 @@ class GoodFETMCPCANCommunication:
             packet = [SIDhigh, SIDlow, 0x00,0x00,0x40] 
         
         
-                #packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
-                #         0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
-                #        # lower nibble is DLC                   
-                #        0x00,0x01,0x02,0x03,0x04,0x05,0x06,0xFF]
         else:
 
             # if we do have a packet, packet is SID + padding out EID registers + DLC of 8 + packet
             #
+            """@todo: allow for variable-length packets"""
             #    TODO: allow for variable-length packets
             #
             packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
@@ -506,6 +671,7 @@ class GoodFETMCPCANCommunication:
         self.client.txpacket(packet);
             
         if repeat:
+            """@todo: the repeat variable is no longer needed and can be removed """
             print "\nNow looping on transmit. "
             if period != None:
                 for i in range(0,writes):
@@ -545,6 +711,12 @@ class GoodFETMCPCANCommunication:
 
 
     def setRate(self,freq):
+        """ 
+        This method will reset the frequency that the MCP2515 expects the CAN bus to be on.
+        
+        @type freq: Number
+        @param freq: Frequency of the CAN bus
+        """
         self.client.MCPsetrate(freq);
         
 
@@ -569,8 +741,10 @@ class GoodFETMCPCANCommunication:
             SIDhigh = (sID >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
             packet = [SIDhigh,SIDlow,0x00,0x00,0x08]
             #dlc = row[2]
-            for i in range(4,dlc+4):
+            dlc = 8
+            for i in range(3,dlc+3):
                 packet.append(row[i])
+            print packet
             self.client.txpacket(packet)