minor bug fix on parsing experiments data
[goodfet] / client / experiments.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
16
17 class experiments(GoodFETMCPCANCommunication):
18     
19     def __init__(self):
20         GoodFETMCPCANCommunication.__init__(self)
21         #super(experiments,self).__init(self)
22         self.freq = 500;
23         
24     def filterStdSweep(self, freq, low, high, time = 5):
25         msgIDs = []
26         self.client.serInit()
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])
34             if( count != 0):
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])
40                     if( count != 0):
41                         msgIDs.append(j)
42         return msgIDs
43     
44     
45     def sweepRandom(self, freq, number = 5, time = 200):
46         msgIDs = []
47         ids = []
48         self.client.serInit()
49         self.client.MCPsetup()
50         for i in range(0,number+1,6):
51             idsTemp = []
52             comment = "sweepFilter: "
53             for j in range(0,6,1):
54                 id = randrange(2047)
55                 #comment += "_%d" % id
56                 idsTemp.append(id)
57                 ids.append(id)
58             print comment
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)
61             if( count != 0):
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])
67                     if( count != 0):
68                         msgIDs.append(j)
69         return msgIDs, ids
70     
71     
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
74     
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):
77         #set up file
78         now = datetime.datetime.now()
79         datestr = now.strftime("%Y%m%d")
80         path = self.DATALOCATION+datestr+"_rtr.csv"
81         filename = path
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)])
86         print "started"
87         #self.client.serInit()
88         #self.spitSetup(freq)
89         for i in range(lowID,highID+1, 1):
90             self.client.serInit()
91             self.spitSetup(freq)
92             standardid = [i, i, i, i]
93             #set filters
94             self.addFilter(standardid, verbose = True)
95             
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
99             #create RTR packet
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 
103             #clear buffer
104             packet1 = self.client.rxpacket();
105             packet2 = self.client.rxpacket();
106             #send in rtr request
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()
113                 if( packet == None):
114                     continue
115                 row = []
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
121                 for byte in packet:
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);
131 #                continue
132 #            elif( packet1 != None):
133 #                print self.client.packet2parsedstr(packet1)
134 #            elif( packet2 != None):
135 #                print self.client.packet2parsedstr(packet2)
136             trial= 2;
137             # for each trial
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();
146                     row = []
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
152                     for byte in packet:
153                         row.append("%02x"%ord(byte));
154                     dataWriter.writerow(row)
155                     print self.client.packet2parsedstr(packet)
156 #                    packet2=self.client.rxpacket();
157 #                    
158 #                    if( packet1 != None and packet2 != None):
159 #                        print "packets recieved :\n "
160 #                        print self.client.packet2parsedstr(packet1);
161 #                        print self.client.packet2parsedstr(packet2);
162 #                        #break
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)
169                 trial += 1
170         print "sweep complete"
171         outfile.close()
172         
173     
174         
175     
176     
177     
178     
179