b2fdc337b21b40626a9f810d64af8995ef41e59c
[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):
16     
17     def __init__(self, dataLocation = "../../contrib/ThayerData/"):
18         GoodFETMCPCANCommunication.__init__(self, dataLocation)
19         #super(FordExperiments,self).__init__(self) #initialize chip
20         self.freq = 500;
21
22     def mimic1056(self,packetData,runTime):
23         #setup chip
24         self.client.serInit()
25         self.spitSetup(self.freq)
26         #FIGURE out how to clear buffers
27         self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
28         packet1 = self.client.rxpacket();
29         if(packet1 != None):
30             packetParsed = self.client.packet2parsed(packet1);
31         #keep sniffing till we read a packet
32         while( packet1 == None or packetParsed.get('sID') != 1056 ):
33             packet1 = self.client.rxpacket()
34             if(packet1 != None):
35                 packetParsed = self.client.packet2parsed(packet1)
36         recieveTime = time.time()
37         packetParsed = self.client.packet2parsed(packet1)
38         if( packetParsed['sID'] != 1056):
39             print "Sniffed wrong packet"
40             return
41         countInitial = ord(packetParsed['db3']) #initial count value
42         packet = []
43         #set data packet to match what was sniffed or at least what was input
44         for i in range(0,8):
45             idx = "db%d"%i
46             if(packetData.get(idx) == None):
47                 packet.append(ord(packetParsed.get(idx)))
48             else:
49                 packet.append(packetData.get(idx))
50         print packet
51         #### split SID into different regs
52         SIDlow = (1056 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
53         SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
54         packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
55                   0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
56                   # lower nibble is DLC                   
57                  packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
58         packetCount = 1;
59         self.client.txpacket(packet);
60         tpast = time.time()
61         while( (time.time()-recieveTime) < runTime):
62             #care about db3 or packet[8] that we want to count at the rate that it is
63             dT = time.time()-tpast
64             if( dT/0.2 >= 1):
65                 db3 = (countInitial + math.floor((time.time()-recieveTime)/0.2))%255
66                 packet[8] = db3
67                 self.client.txpacket(packet)
68                 packetCount += 1
69             else:
70                 packetCount += 1
71                 self.client.MCPrts(TXB0=True)
72             tpast = time.time()  #update our transmit time on the one before   
73             
74                 
75          
76     def cycledb1_1056(self,runTime):
77         #setup chip
78         self.client.serInit()
79         self.spitSetup(500)
80         #FIGURE out how to clear buffers
81         self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
82         packet1 = self.client.rxpacket();
83         if(packet1 != None):
84             packetParsed = self.client.packet2parsed(packet1);
85         #keep sniffing till we read a packet
86         while( packet1 == None or packetParsed.get('sID') != 1056 ):
87             time.sleep(.1)
88             packet1 = self.client.rxpacket()
89             if(packet1 != None):
90                 packetParsed = self.client.packet2parsed(packet1)
91         recieveTime = time.time()
92         packetParsed = self.client.packet2parsed(packet1)
93         if( packetParsed['sID'] != 1056):
94             print "Sniffed wrong packet"
95             return
96         packet = []
97         #set data packet to match what was sniffed or at least what was input
98         for i in range(0,8):
99             idx = "db%d"%i
100             packet.append(ord(packetParsed.get(idx)))
101         packetValue = 0
102         packet[1] = packetValue;
103         
104         print packet
105         #### split SID into different regs
106         SIDlow = (1056 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
107         SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
108         packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
109                   0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
110                   # lower nibble is DLC                   
111                  packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
112         packetCount = 1;
113         self.client.txpacket(packet);
114         tpast = time.time()
115         while( (time.time()-recieveTime) < runTime):
116             #care about db3 or packet[8] that we want to count at the rate that it is
117             dT = time.time()-tpast
118             packetValue += 10
119             pV = packetValue%255
120             #temp = ((packetValue+1))%2
121             #if( temp == 1):
122             #    pV = packetValue%255
123             #else:
124             #    pV = 0
125             packet[6] = pV
126             #packet[6] = 1
127             print packet
128             self.client.txpacket(packet)
129             packetCount += 1
130             tpast = time.time()  #update our transmit time on the one before   
131         print packetCount;
132         
133     def getBackground(self,sId):
134         packet1 = self.client.rxpacket();
135         if(packet1 != None):
136             packetParsed = self.client.packet2parsed(packet1);
137         #keep sniffing till we read a packet
138         while( packet1 == None or packetParsed.get('sID') != sId ):
139             packet1 = self.client.rxpacket()
140             if(packet1 != None):
141                 packetParsed = self.client.packet2parsed(packet1)
142             
143         #recieveTime = time.time()
144         return packetParsed
145
146     def cycle4packets1279(self):
147         self.client.serInit()
148         self.spitSetup(500)
149         # filter on 1279
150         self.addFilter([1279, 1279, 1279, 1279, 1279, 1279], verbose = False)
151         packetParsed = self.getBackground(1279)
152         packet = []
153         if (packetParsed[db0] == 16):
154             # if it's the first of the four packets, replace the value in db7  with 83
155             packetParsed[db7] = 83
156             # transmit new packet
157             self.client.txpacket(packetParsed)
158         else:
159         # otherwise, leave it alone
160             # transmit same pakcet we read in
161             self.client.txpacket(packetParsed)
162         # print the packet we are transmitting
163         print packetParsed
164         
165         
166     def oscillateTemperature(self,time):
167         #setup chip
168         self.client.serInit()
169         self.spitSetup(500)
170         #FIGURE out how to clear buffers
171         self.addFilter([1056, 1056, 1056, 1056,1056, 1056], verbose=False)
172         packetParsed = self.getBackground(1056)
173         packet = []
174         #set data packet to match what was sniffed or at least what was input
175         for i in range(0,8):
176             idx = "db%d"%i
177             packet.append(ord(packetParsed.get(idx)))
178         packetValue = 0
179         packet[1] = packetValue;
180         
181         print packet
182         #### split SID into different regs
183         SIDlow = (1056 & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
184         SIDhigh = (1056 >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
185         packet = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
186                   0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
187                   # lower nibble is DLC                   
188                  packet[0],packet[1],packet[2],packet[3],packet[4],packet[5],packet[6],packet[7]]
189         packetCount = 1;
190         self.client.txpacket(packet);
191         startTime = tT.time()
192         while( (tT.time()-startTime) < runTime):
193             dt = tT.time()-startTime
194             inputValue = ((2.0*math.pi)/20.0)*dt
195             value = 30*math.sin(inputValue)+130
196             print value
197             #packet[5] = int(value)
198             if( value > 130 ):
199                 packet[5] = 160
200             else:
201                 packet[5] = 100
202             #packet[6] = 1
203             print packet
204             self.client.txpacket(packet)
205             packetCount += 1
206             #tpast = time.time()  #update our transmit time on the one before   
207         print packetCount;
208         
209         
210     def fakeVIN(self):
211        #reset eveything on the chip
212        self.client.serInit() 
213        self.reset()
214        duration = 20; #seconds 
215        
216        listenID = 2015
217        listenPacket = [2, 9, 6, 153, 153, 153, 153, 153]
218        responseID = 2024
219        #actual response by the car
220        #r1 = [34, 88, 0, 0, 0, 0, 0, 0]
221        #r2 = [33, 75, 50, 78, 51, 46, 72, 69 ]
222        #r3 = [16, 19, 73, 4, 1, 70, 65, 66]
223        
224        r1 = [34, 88, 0, 0, 0, 0, 0, 0]
225        r2 = [33, 75, 50, 78, 51, 46, 72, 69 ]
226        r3 = [16, 19, 73, 160, 159, 70, 65, 66]
227        
228        #format
229        SIDlow = (responseID & 0x07) << 5;  # get SID bits 2:0, rotate them to bits 7:5
230        SIDhigh = (responseID >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
231        packet1 = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
232                   0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
233                   # lower nibble is DLC                   
234                  r1[0],r1[1],r1[2],r1[3],r1[4],r1[5],r1[6],r1[7]]
235        packet2 = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
236               0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
237                   # lower nibble is DLC                   
238                  r2[0],r2[1],r2[2],r2[3],r2[4],r2[5],r2[6],r2[7]]
239        packet3 = [SIDhigh, SIDlow, 0x00,0x00, # pad out EID regs
240                   0x08, # bit 6 must be set to 0 for data frame (1 for RTR) 
241                   # lower nibble is DLC                   
242                  r3[0],r3[1],r3[2],r3[3],r3[4],r3[5],r3[6],r3[7]]
243
244        self.multiPacketSpit(packet0 = r1, packet1 = r2, packet2 = r3, packet0rts = True, packet1rts = True, packet2rts = True)
245
246        #filter for the correct packet
247        self.filterForPacket(listenID, listenPacket[0],listenPacket[1], verbose = True)
248        self.client.rxpacket()
249        self.client.rxpacket() # flush buffers if there is anything
250        startTime = tT.time()
251        while( (tT.time() -startTime) < duration):
252            packet = self.client.rxpacket()
253            if( packet != None):
254                sid =  ord(packet[0])<<3 | ord(packet[1])>>5
255                if( sid == listenID):
256                    byte3 = ord(packet[6])
257                    if( byte3 == listenPacket[3]):
258                        print "SendingPackets!"
259                        #send packets
260                        self.multpackSpit(packet0rts=True,packet1rts=True,packet2rts=True)
261                        
262        
263        
264         
265 if __name__ == "__main__":
266     fe = FordExperiments("asdsf");
267     #packetData = {}
268     #packetData['db4'] = 4;
269     #runTime = 100;
270     #fe.mimic1056(packetData, runTime)
271     #fe.cycledb1_1056(runTime)
272     #fe.oscillateTemperature(runTime)
273     fe.fakeVIN()