X-Git-Url: http://git.rot13.org/?p=goodfet;a=blobdiff_plain;f=client%2FGoodFETMCPCAN.py;h=39faace56c18b74fbc0f1201258133900bd6fca5;hp=aa06b260adcd152cd4dc4a2078a7110b01d8687e;hb=0c1cae929512565296940bf6322087a73b2cecd0;hpb=0a4e61c633bfe1cb455f1cd1ecd73afca0306a50 diff --git a/client/GoodFETMCPCAN.py b/client/GoodFETMCPCAN.py index aa06b26..39faace 100644 --- a/client/GoodFETMCPCAN.py +++ b/client/GoodFETMCPCAN.py @@ -234,7 +234,22 @@ class GoodFETMCPCAN(GoodFETSPI): self.SPItrans([0x40|(packbuf<<1)]+packet); + + def txpacket(self,packet): + """Transmits a packet through one of the outbound buffers. + As usual, the packet should begin with SIDH. + For now, only TXB0 is supported.""" + + self.writetxbuffer(packet,0); + self.MCPrts(TXB0=True); + + + ############### UTILITY FUNCTIONS ################# + def simpleParse(self,packet): + """ + This will print a simple parsing of the data with the standard Id and the extended id parsed. + """ dataPt = ord(packet[0]); dataPt2 = ord(packet[1]); # check if we have a standard frame, the msb of the second @@ -256,9 +271,9 @@ class GoodFETMCPCAN(GoodFETSPI): #find the dataLength dataPt5 = packet[4] dataLength = dataPt5 & 0x0e - print "Data Length: "+("%d"%dataLength) + #print "Data Length: "+("%d"%dataLength) # Print the data packet - print "Data:" + #print "Data:" # Temporary, until correct packets are read if( dataLength > 8 ): dataLength = 8 @@ -272,16 +287,6 @@ class GoodFETMCPCAN(GoodFETSPI): # toprint = self.pcket2str(packet[5:(5+dataLength)]) # print toprint - def txpacket(self,packet): - """Transmits a packet through one of the outbound buffers. - As usual, the packet should begin with SIDH. - For now, only TXB0 is supported.""" - - self.writetxbuffer(packet,0); - self.MCPrts(TXB0=True); - - - ############### UTILITY FUNCTIONS ################# def packet2str(self,packet): """Converts a packet from the internal format to a string.""" @@ -290,18 +295,35 @@ class GoodFETMCPCAN(GoodFETSPI): toprint=toprint+("%02x "%ord(bar)) return toprint; + + def packet2parsed(self,data): + """ + This method will parse the packet that was received via L{rxpacket}. + + @type data: List + @param data: data packet read off of the MCP2515 from the CAN bus. format will be + 14 bytes where each element is a character whose unicode integer value corresponds + to the hex value of the byte (use the ord() method). + + @rtype: Dictionary + @return: Dictionary of the packet parsed into various components. The key values will be as follows + + 1. ide : 1 if the message is an extended frame. 0 otherwise + 2. eID : extended ID. Not included if not an extended frame + 3. rtr : 1 if the message is a remote transmission request (RTR) + 4. sID : Standard ID. + 5. length: packet length (between 0 and 8) + 6. db0 : Data byte 0 (not included if RTR message) + + --- + 7. db7 : Data byte 7 (not included if RTR message) + """ dp1 = ord(data[0]) dp2 = ord(data[1]) dp5 = ord(data[4]) - #converts the CAN message to a string - msg=""; - for bar in data: - msg=msg+("%02x"%ord(bar)) - - packet = {'msg':msg} - + packet = {} #get the ide bit. allows us to check to see if we have an extended #frame packet['ide'] = (dp2 & 0x0f)>>3 @@ -310,36 +332,51 @@ class GoodFETMCPCAN(GoodFETSPI): #get lower nibble, last 2 bits eId = dp2 & 0x03 eId = eId<<8 | ord(data[2]) - packet['eID'] = eId<<8 | ord(data[3]) - packet['rtr'] = dp5>>6 & 0x01 + eId = eId<<8 | ord(data[3]) + rtr = dp5>>6 & 0x01 + packet['eID'] = " eID: %06d" %(eId) + packet['rtr'] = " rtr: %d" % (rtr) else: packet['rtr'] = dp2>>4 & 0x01 - - #error check, 2nd msb of the lower nibble of byte 2 should be 0 - if( (dp2 & 0x04) == 4 ): - packet['error'] = 1 - #error check an always 0 bit - if( (dp5 & 0xf0) == 240): - packet['error'] = 1 + # Create the standard ID. from the message packet['sID'] = dp1<<3 | dp2>>5 - - - length = dp5 & 0x0f - packet['length'] = length - - if( length > 8): - packet['error'] = 1 + packet['length'] = dp5 & 0x0f #generate the data section - for i in range(0,length): - idx = 5+i - dbidx = 'db%d' % i - packet[dbidx] = data[idx] + for i in range(0,packet['length']): + idx = 5 + i + dbidx = 'db%d'%i + packet[dbidx] = ord(data[idx]) return packet + + def packet2parsedstr(self,data): + """ + This will return a string that is the parsed CAN message. The bytes will be parsed + for the standard ID, extended ID (if one is present), rtr, length and databytes (if present). + The message will be placed into a string as decimal integers not hex values. This method + calls L{packet2parsed} to do the packet parsing. + @type data: List + @param data: Data packet as returned by the L{rxpacket} + @rtype: String + @return: String that shows the data message in decimal format, parsed. + """ + packet = self.packet2parsed(data) + msg = "sID: %04d" %packet['sId'] + if( packetParsed.get('eID')): + msg += " eID: %d" %packetParsed.get('eID') + msg += " rtr: %d"%packetParsed['rtr'] + length = packetParsed['length'] + msg += " length: %d"%length + msg += " data:" + for i in range(0,length): + dbidx = 'db%d'%i + msg +=" %03d"% ord(packetParsed[dbidx]) # could change this to HEX + #msg = self.client.packet2parsedstr(packet) + return msg def peek8(self,adr): @@ -384,4 +421,4 @@ class GoodFETMCPCAN(GoodFETSPI): # TXRTSCTRL = x0D - \ No newline at end of file +