+ # assumes 8 byte packets
+ def packetRespond(self,freq, time, repeats, period, responseID, respondPacket,listenID, listenPacket = None):
+ """
+ This method will allow the user to listen for a specific packet and then respond with a given message.
+ If no listening packet is included then the method will only listen for the id and respond with the specified
+ packet when it receives a message from that id. This process will continue for the given amount of time (in seconds).
+ and with each message received that matches the listenPacket and ID the transmit message will be sent the I{repeats} number
+ of times at the specified I{period}. This message assumes a packet length of 8 for both messages, although the listenPacket can be None
+
+ @type freq: number
+ @param freq: Frequency of the CAN bus
+ @type time: number
+ @param time: Length of time to perform the packet listen/response in seconds.
+ @type repeats: Integer
+ @param repeats: The number of times the response packet will be injected onto the bus after the listening
+ criteria has been met.
+ @type period: number
+ @param period: The time interval between messages being injected onto the CAN bus. This will be specified in milliseconds
+ @type responseID: Integer
+ @param responseID: The standard ID of the message that we want to inject
+ @type respondPacket: List of integers
+ @param respondPacket: The data we wish to inject into the bus. In the format where respondPacket[0] = databyte 0 ... respondPacket[7] = databyte 7
+ This assumes a packet length of 8.
+ @type listenID: Integer
+ @param listenID: The standard ID of the messages that we are listening for. When we read the correct message from this ID off of the bus, the method will
+ begin re-injecting the responsePacket on the responseID
+ @type listenPacket: List of Integers
+ @param listenPacket: The data we wish to listen for before we inject packets. This will be a list of the databytes, stored as integers such that
+ listenPacket[0] = data byte 0, ..., listenPacket[7] = databyte 7. This assumes a packet length of 8. This input can be None and this
+ will lead to the program only listening for the standardID and injecting the response as soon as any message from that ID is given
+ """
+
+
+ self.client.serInit()
+ self.spitSetup(freq)
+
+ #formulate response packet
+ SIDhigh = (responseID >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
+ SIDlow = (responseID & 0x07) << 5; # get SID bits 2:0, rotate them to bits 7:5
+ #resPacket[0] = SIDhigh
+ #resPacket[1] = SIDlow
+ resPacket = [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
+ respondPacket[0],respondPacket[1],respondPacket[2],respondPacket[3],respondPacket[4],respondPacket[5],respondPacket[6],respondPacket[7]]
+ #load packet/send once
+ """@todo: make this only load the data onto the chip and not send """
+ self.client.txpacket(resPacket)
+ self.addFilter([listenID,listenID,listenID,listenID, listenID, listenID]) #listen only for this packet
+ startTime = tT.time()
+ packet = None
+ while( (tT.time() - startTime) < time):
+ packet = self.client.rxpacket()
+ if( packet != None):
+ print "packet read in, responding now"
+ # assume the ids already match since we are filtering for the id
+
+ #compare packet received to desired packet
+ if( listenPacket == None): # no packets given, just want the id
+ for i in range(0,repeats):
+ self.client.MCPrts(TXB0=True)
+ tT.sleep(period/1000)
+ else: #compare packets
+ sid = ord(packet[0])<<3 | ord(packet[1])>>5
+ print "standard id of packet recieved: ", sid #standard ID
+ msg = ""
+ for i in range(0,8):
+ idx = 5 + i
+ byteIn = ord(packet[idx])
+ msg += " %d" %byteIn
+ compareIn = listenPacket[i]
+ print byteIn, compareIn
+ if( byteIn != compareIn):
+ packet == None
+ print "packet did not match"
+ break
+ print msg
+ if( packet != None ):
+ self.client.MCPrts(TXB0=True)
+ tT.sleep(period/1000)
+ print "Response Listening Terminated."
+
+
+# def generationFuzzRandomID(self, freq, standardIDs, dbLimits, period, writesPerFuzz, Fuzzes):
+# print "Fuzzing on standard ID: %d" %standardId
+# self.client.serInit()
+# self.spitSetup(freq)
+# packetTemp = [0,0,0,0,0,0,0,0]
+# #form a basic packet
+#
+# #### split SID into different regs
+# 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
+#
+# 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
+# packetTemp[0],packetTemp[1],packetTemp[2],packetTemp[3],packetTemp[4],packetTemp[5],packetTemp[6],packetTemp[7]]
+#
+#
+# #get folder information (based on today's date)
+# now = datetime.datetime.now()
+# datestr = now.strftime("%Y%m%d")
+# path = self.DATA_LOCATION+"InjectedData/"+datestr+"_GenerationFuzzedPackets.csv"
+# filename = path
+# outfile = open(filename,'a');
+# dataWriter = csv.writer(outfile,delimiter=',');
+# #dataWriter.writerow(['# Time Error Bytes 1-13']);
+# #dataWriter.writerow(['#' + description])
+#
+# numIds = len(standardIDs)
+# fuzzNumber = 0;
+# while( fuzzNumber < Fuzzes):
+# id_new = standsardIDs[random.randint(0,numIds-1)]
+# #### split SID into different regs
+# SIDlow = (id_new & 0x07) << 5; # get SID bits 2:0, rotate them to bits 7:5
+# SIDhigh = (id_new >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
+# packet[0] = SIDhigh
+# packet[1] = SIDlow
+#
+# #generate a fuzzed packet
+# for i in range(0,8): # for each databyte, fuzz it
+# idx = "db%d"%i
+# limits = dbLimits[idx]
+# value = random.randint(limits[0],limits[1]) #generate pseudo-random integer value
+# packet[i+5] = value
+#
+# #put a rough time stamp on the data and get all the data bytes
+# row = [time.time(), standardId,8]
+# msg = "Injecting: "
+# for i in range(5,13):
+# row.append(packet[i])
+# msg += " %d"%packet[i]
+# #print msg
+# dataWriter.writerow(row)
+# self.client.txpacket(packet)
+# #inject the packet repeatily
+# for i in range(1,writesPerFuzz):
+# self.client.MCPrts(TXB0=True)
+# time.sleep(period/1000)
+# fuzzNumber += 1
+# print "Fuzzing Complete"
+# outfile.close()