updated some new experiment methods. you can now fuzz on all ids randomly as well...
[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 GoodFETMCPCANCommunication import GoodFETMCPCANCommunication
10 from intelhex import IntelHex;
11 import Queue
12 import math
13
14 tT = time
15 class FordExperiments(GoodFETMCPCANCommunication, dataLocation):
16     
17     def __init__(self):
18         GoodFETMCPCANCommunication.__init__(self, dataLocation)
19         #super(FordExperiments,self).__init__(self) #initialize chip
20         self.freq = 500;
21
22     
23
24
25     def mimic1056(self,packetData,runTime):
26         #setup chip
27         self.client.serInit()
28         self.spitSetup(self.freq)
29         #FIGURE out how to clear buffers
30         self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
31         packet1 = self.client.rxpacket();
32         if(packet1 != None):
33             packetParsed = self.client.packet2parsed(packet1);
34         #keep sniffing till we read a packet
35         while( packet1 == None or packetParsed.get('sID') != 1056 ):
36             packet1 = self.client.rxpacket()
37             if(packet1 != None):
38                 packetParsed = self.client.packet2parsed(packet1)
39         recieveTime = time.time()
40         packetParsed = self.client.packet2parsed(packet1)
41         if( packetParsed['sID'] != 1056):
42             print "Sniffed wrong packet"
43             return
44         countInitial = ord(packetParsed['db3']) #initial count value
45         packet = []
46         #set data packet to match what was sniffed or at least what was input
47         for i in range(0,8):
48             idx = "db%d"%i
49             if(packetData.get(idx) == None):
50                 packet.append(ord(packetParsed.get(idx)))
51             else:
52                 packet.append(packetData.get(idx))
53         print packet
54         #### split SID into different regs
55         SIDlow = (1056 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
56         SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
57         packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
58                   0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
59                   # lower nibble is DLC                   
60                  packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
61         packetCount = 1;
62         self.client.txpacket(packet);
63         tpast = time.time()
64         while( (time.time()-recieveTime) < runTime):
65             #care about db3 or packet[8] that we want to count at the rate that it is
66             dT = time.time()-tpast
67             if( dT/0.2 >= 1):
68                 db3 = (countInitial + math.floor((time.time()-recieveTime)/0.2))%255
69                 packet[8] = db3
70                 self.client.txpacket(packet)
71                 packetCount += 1
72             else:
73                 packetCount += 1
74                 self.client.MCPrts(TXB0=True)
75             tpast = time.time()  #update our transmit time on the one before   
76             
77                 
78          
79     def cycledb1_1056(self,runTime):
80         #setup chip
81         self.client.serInit()
82         self.spitSetup(500)
83         #FIGURE out how to clear buffers
84         self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
85         packet1 = self.client.rxpacket();
86         if(packet1 != None):
87             packetParsed = self.client.packet2parsed(packet1);
88         #keep sniffing till we read a packet
89         while( packet1 == None or packetParsed.get('sID') != 1056 ):
90             time.sleep(.1)
91             packet1 = self.client.rxpacket()
92             if(packet1 != None):
93                 packetParsed = self.client.packet2parsed(packet1)
94         recieveTime = time.time()
95         packetParsed = self.client.packet2parsed(packet1)
96         if( packetParsed['sID'] != 1056):
97             print "Sniffed wrong packet"
98             return
99         packet = []
100         #set data packet to match what was sniffed or at least what was input
101         for i in range(0,8):
102             idx = "db%d"%i
103             packet.append(ord(packetParsed.get(idx)))
104         packetValue = 0
105         packet[1] = packetValue;
106         
107         print packet
108         #### split SID into different regs
109         SIDlow = (1056 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
110         SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
111         packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
112                   0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
113                   # lower nibble is DLC                   
114                  packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
115         packetCount = 1;
116         self.client.txpacket(packet);
117         tpast = time.time()
118         while( (time.time()-recieveTime) < runTime):
119             #care about db3 or packet[8] that we want to count at the rate that it is
120             dT = time.time()-tpast
121             packetValue += 1
122             pV = packetValue%255
123             #temp = ((packetValue+1))%2
124             #if( temp == 1):
125             #    pV = packetValue%255
126             #else:
127             #    pV = 0
128             packet[6] = pV
129             #packet[6] = 1
130             print packet
131             self.client.txpacket(packet)
132             packetCount += 1
133             tpast = time.time()  #update our transmit time on the one before   
134         print packetCount;
135         
136     def getBackground(self,sId):
137         packet1 = self.client.rxpacket();
138         if(packet1 != None):
139             packetParsed = self.client.packet2parsed(packet1);
140         #keep sniffing till we read a packet
141         while( packet1 == None or packetParsed.get('sID') != sId ):
142             packet1 = self.client.rxpacket()
143             if(packet1 != None):
144                 packetParsed = self.client.packet2parsed(packet1)
145             
146         #recieveTime = time.time()
147         return packetParsed
148
149     def cycle4packets1279(self):
150         self.client.serInit()
151         self.spitSetup(500)
152         # filter on 1279
153         self.addFilter([1279, 1279, 1279, 1279, 1279, 1279], verbose = False)
154         packetParsed = self.getBackground(1279)
155         packet = []
156         if (packetParsed[db0] == 16):
157             # if it's the first of the four packets, replace the value in db7  with 83
158             packetParsed[db7] = 83
159             # transmit new packet
160             self.client.txpacket(packetParsed)
161         else:
162         # otherwise, leave it alone
163             # transmit same pakcet we read in
164             self.client.txpacket(packetParsed)
165         # print the packet we are transmitting
166         print packetParsed
167         
168         
169     def oscillateTemperature(self,time):
170         #setup chip
171         self.client.serInit()
172         self.spitSetup(500)
173         #FIGURE out how to clear buffers
174         self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
175         packetParsed = self.getBackground(1056)
176         packet = []
177         #set data packet to match what was sniffed or at least what was input
178         for i in range(0,8):
179             idx = "db%d"%i
180             packet.append(ord(packetParsed.get(idx)))
181         packetValue = 0
182         packet[1] = packetValue;
183         
184         print packet
185         #### split SID into different regs
186         SIDlow = (1056 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
187         SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
188         packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
189                   0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
190                   # lower nibble is DLC                   
191                  packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
192         packetCount = 1;
193         self.client.txpacket(packet);
194         startTime = tT.time()
195         while( (tT.time()-startTime) < runTime):
196             dt = tT.time()-startTime
197             inputValue = ((2.0*math.pi)/20.0)*dt
198             value = 30*math.sin(inputValue)+130
199             print value
200             #packet[5] = int(value)
201             if( value > 130 ):
202                 packet[5] = 160
203             else:
204                 packet[5] = 100
205             #packet[6] = 1
206             print packet
207             self.client.txpacket(packet)
208             packetCount += 1
209             #tpast = time.time()  #update our transmit time on the one before   
210         print packetCount;
211         
212         
213 if __name__ == "__main__":
214     fe = FordExperiments();
215     packetData = {}
216     packetData['db4'] = 4;
217     runTime = 100;
218     #fe.mimic1056(packetData, runTime)
219     #fe.cycledb1_1056(runTime)
220     fe.oscillateTemperature(runTime)