4 import csv, time, argparse;
7 from random import randrange
8 from GoodFETMCPCAN import GoodFETMCPCAN;
9 from GoodFETMCPCANCommunication import GoodFETMCPCANCommunication
10 from intelhex import IntelHex;
17 class experiments(GoodFETMCPCANCommunication):
20 GoodFETMCPCANCommunication.__init__(self)
21 #super(experiments,self).__init(self)
24 def filterStdSweep(self, freq, low, high, time = 5):
27 self.client.MCPsetup()
28 for i in range(low, high+1, 6):
29 print "sniffing id: %d, %d, %d, %d, %d, %d" % (i,i+1,i+2,i+3,i+4,i+5)
30 comment= "sweepFilter: "
31 #comment = "sweepFilter_%d_%d_%d_%d_%d_%d" % (i,i+1,i+2,i+3,i+4,i+5)
32 description = "Running a sweep filer for all the possible standard IDs. This run filters for: %d, %d, %d, %d, %d, %d" % (i,i+1,i+2,i+3,i+4,i+5)
33 count = self.sniff(freq=freq, duration = time, description = description,comment = comment, standardid = [i, i+1, i+2, i+3, i+4, i+5])
35 for j in range(i,i+5):
36 comment = "sweepFilter: "
37 #comment = "sweepFilter: %d" % (j)
38 description = "Running a sweep filer for all the possible standard IDs. This run filters for: %d " % j
39 count = self.sniff(freq=freq, duration = time, description = description,comment = comment, standardid = [j, j, j, j])
45 def sweepRandom(self, freq, number = 5, time = 200):
49 self.client.MCPsetup()
50 for i in range(0,number+1,6):
52 comment = "sweepFilter: "
53 for j in range(0,6,1):
55 #comment += "_%d" % id
59 description = "Running a sweep filer for all the possible standard IDs. This runs the following : " + comment
60 count = self.sniff(freq=freq, duration=time, description=description, comment = comment, standardid = idsTemp)
62 for element in idsTemp:
63 #comment = "sweepFilter: %d" % (element)
64 comment="sweepFilter: "
65 description = "Running a sweep filer for all the possible standard IDs. This run filters for: %d " % element
66 count = self.sniff(freq=freq, duration = time, description = description,comment = comment, standardid = [element, element, element])
72 # this will sweep through the given ids to request a packet and then sniff on that
73 # id for a given amount duration. This will be repeated the number of attempts time
75 #at the moment this is set to switch to the next id once a message is identified
76 def rtrSweep(self,freq,lowID,highID, attempts = 1,duration = 1, verbose = True):
78 now = datetime.datetime.now()
79 datestr = now.strftime("%Y%m%d")
80 path = self.DATALOCATION+datestr+"_rtr.csv"
82 outfile = open(filename,'a');
83 dataWriter = csv.writer(outfile,delimiter=',');
84 dataWriter.writerow(['# Time Error Bytes 1-13']);
85 dataWriter.writerow(['#' + "rtr sweep from %d to %d"%(lowID,highID)])
87 #self.client.serInit()
89 for i in range(lowID,highID+1, 1):
92 standardid = [i, i, i, i]
94 self.addFilter(standardid, verbose = True)
96 #### split SID into different areas
97 SIDlow = (standardid[0] & 0x07) << 5; # get SID bits 2:0, rotate them to bits 7:5
98 SIDhigh = (standardid[0] >> 3) & 0xFF; # get SID bits 10:3, rotate them to bits 7:0
100 packet = [SIDhigh, SIDlow, 0x00,0x00,0x40]
101 dataWriter.writerow(["#requested id %d"%i])
102 #self.client.poke8(0x2C,0x00); #clear the CANINTF register; we care about bits 0 and 1 (RXnIF flags) which indicate a message is being held
104 packet1 = self.client.rxpacket();
105 packet2 = self.client.rxpacket();
107 self.client.txpacket(packet)
108 ## listen for 2 packets. one should be the rtr we requested the other should be
109 ## a new packet response
110 starttime = time.time()
111 while ((time.time() - starttime) < duration):
112 packet = self.client.rxpacket()
116 row.append("%f"%time.time()) #timestamp
117 row.append(0) #error flag (not checkign)
118 row.append("rtrRequest_%d"%i) #comment
119 row.append(duration) #sniff time
120 row.append(1) # filtering boolean
122 row.append("%02x"%ord(byte));
123 dataWriter.writerow(row)
124 print self.client.packet2parsedstr(packet)
125 # packet1=self.client.rxpacket();
126 # packet2=self.client.rxpacket();
127 # if( packet1 != None and packet2 != None):
128 # print "packets recieved :\n "
129 # print self.client.packet2parsedstr(packet1);
130 # print self.client.packet2parsedstr(packet2);
132 # elif( packet1 != None):
133 # print self.client.packet2parsedstr(packet1)
134 # elif( packet2 != None):
135 # print self.client.packet2parsedstr(packet2)
138 while( trial <= attempts):
139 print "trial: ", trial
140 self.client.MCPrts(TXB0=True);
141 starttime = time.time()
142 # this time we will sniff for the given amount of time to see if there is a
143 # time till the packets come in
144 while( (time.time()-starttime) < duration):
145 packet=self.client.rxpacket();
147 row.append("%f"%time.time()) #timestamp
148 row.append(0) #error flag (not checking)
149 row.append("rtrRequest_%d"%i) #comment
150 row.append(duration) #sniff time
151 row.append(1) # filtering boolean
153 row.append("%02x"%ord(byte));
154 dataWriter.writerow(row)
155 print self.client.packet2parsedstr(packet)
156 # packet2=self.client.rxpacket();
158 # if( packet1 != None and packet2 != None):
159 # print "packets recieved :\n "
160 # print self.client.packet2parsedstr(packet1);
161 # print self.client.packet2parsedstr(packet2);
163 # elif( packet1 != None):
164 # print "just packet1"
165 # print self.client.packet2parsedstr(packet1)
166 # elif( packet2 != None):
167 # print "just packet2"
168 # print self.client.packet2parsedstr(packet2)
170 print "sweep complete"