-
Notifications
You must be signed in to change notification settings - Fork 1
/
frame-counts.py
339 lines (307 loc) · 13.8 KB
/
frame-counts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#Author : Abhinav Narain
#Date : Aug 26, 2013
#Purpose : To read the binary files with data from BISmark deployment in homes
# To count the types of frames in the dataset; from transmitted to received or FCS correct/failed
import os,sys,re
import gzip
import struct
from header import *
from mac_parser import *
from utils import *
try:
import cPickle as pickle
except ImportError:
import pickle
rx_data_fc_correct= 0
rx_data_fc_incorrect =0
tx_data_fc_incorrect =0
tx_data_fc_correct =0
tx_ctrl_fc_incorrect =0
tx_ctrl_fc_correct =0
rx_ctrl_fc_incorrect =0
rx_ctrl_fc_correct =0
rx_mgmt_fc_incorrect =0
tx_mgmt_fc_incorrect =0
rx_mgmt_fc_correct =0
tx_mgmt_fc_correct =0
missing_files=[]
tx_time_series= []
rx_time_series= []
if len(sys.argv) !=5:
print len(sys.argv)
print "Usage : python station-process.py data/<data.gz> mgmt/<mgmt.gz> ctrl/<ctrl.gz> <output station filename> "
sys.exit(1)
data_f_dir=sys.argv[1]
mgmt_f_dir=sys.argv[2]
ctrl_f_dir=sys.argv[3]
output_station_filename=sys.argv[4]
data_fs=os.listdir(data_f_dir)
ctrl_fs=os.listdir(ctrl_f_dir)
data_file_header_byte_count=0
ctrl_file_header_byte_count=0
mgmt_file_header_byte_count=0
file_counter=0
file_timestamp=0
filename_list=[]
unix_time=set()
for data_f_n in data_fs :
filename_list.append(data_f_n.split('-'))
unix_time.add(int(data_f_n.split('-')[1]))
if not (data_f_n.split('-')[2]=='d'):
print "its not a data file ; skip "
continue
filename_list.sort(key=lambda x : int(x[3]))
filename_list.sort(key=lambda x : int(x[1]))
for data_f_name_list in filename_list : #data_fs :
data_f_name="-".join(data_f_name_list)
data_f= gzip.open(data_f_dir+data_f_name,'rb')
data_file_content=data_f.read()
data_f.close()
data_file_current_timestamp=0
data_file_seq_n=0
bismark_id_data_file=0
start_64_timestamp_data_file=0
for i in xrange(len(data_file_content )):
if data_file_content[i]=='\n':
bismark_data_file_header = str(data_file_content[0:i])
ents= bismark_data_file_header.split(' ')
bismark_id_data_file=ents[0]
start_64_timestamp_data_file= int(ents[1])
data_file_seq_no= int(ents[2])
data_file_current_timestamp=int(ents[3])
data_file_header_byte_count =i
break
data_contents=data_file_content.split('\n----\n')
header_and_correct_data_frames = data_contents[0]
err_data_frames = data_contents[1]
correct_data_frames_missed=data_contents[2]
err_data_frames_missed=data_contents[3]
ctrl_f_name = data_f_name
ctrl_f_name =re.sub("-d-","-c-",ctrl_f_name)
try :
ctrl_f= gzip.open(ctrl_f_dir+ctrl_f_name,'rb')
ctrl_file_content=ctrl_f.read()
except :
print "CTRL file not present ", ctrl_f_name
missing_files.append([ctrl_f_name,data_file_current_timestamp])
continue
ctrl_f.close()
mgmt_f_name = data_f_name
mgmt_f_name = re.sub("-d-","-m-",mgmt_f_name)
try :
mgmt_f= gzip.open(mgmt_f_dir+mgmt_f_name,'rb')
mgmt_file_content=mgmt_f.read()
except :
print "MGMT file not present ",mgmt_f_name
missing_files.append([mgmt_f_name,data_file_current_timestamp])
continue
mgmt_f.close()
mgmt_file_current_timestamp=0
mgmt_file_seq_no=0
bismark_id_mgmt_file=0
start_64_timestamp_mgmt_file=0
ctrl_file_current_timestamp=0
ctrl_file_seq_no=0
bismark_id_ctrl_file=0
start_64_timestamp_ctrl_file=0
for i in xrange(len(mgmt_file_content )):
if mgmt_file_content[i]=='\n':
bismark_mgmt_file_header = str(mgmt_file_content[0:i])
ents= bismark_mgmt_file_header.split(' ')
bismark_id_mgmt_file=ents[0]
start_64_timestamp_mgmt_file=int(ents[1])
mgmt_file_seq_no= int(ents[2])
mgmt_file_current_timestamp= int(ents[3])
mgmt_file_header_byte_count =i
break
mgmt_contents=mgmt_file_content.split('\n----\n')
header_and_beacon_mgmt_frames = mgmt_contents[0]
common_mgmt_frames = mgmt_contents[1]
err_mgmt_frames=mgmt_contents[2]
beacon_mgmt_frames_missed=mgmt_contents[3]
common_mgmt_frames_missed=mgmt_contents[4]
err_mgmt_frames_missed=mgmt_contents[5]
for i in xrange(len(ctrl_file_content )):
if ctrl_file_content[i]=='\n':
bismark_ctrl_file_header = str(ctrl_file_content[0:i])
ents= bismark_ctrl_file_header.split(' ')
bismark_id_ctrl_file= ents[0]
start_64_timestamp_ctrl_file= int(ents[1])
ctrl_file_seq_no= int(ents[2])
ctrl_file_current_timestamp=int(ents[3])
ctrl_file_header_byte_count =i
break
ctrl_contents=ctrl_file_content.split('\n----\n')
header_and_correct_ctrl_frames = ctrl_contents[0]
err_ctrl_frames = ctrl_contents[1]
correct_ctrl_frames_missed=ctrl_contents[2]
err_ctrl_frames_missed=ctrl_contents[3]
#done with reading the binary blobs from file ; now check for timestamps are correct
if (not (ctrl_file_current_timestamp == mgmt_file_current_timestamp == data_file_current_timestamp )) :
print "timestamps don't match "
sys.exit(1)
else :
file_timestamp=ctrl_file_current_timestamp
if (not (ctrl_file_seq_no == mgmt_file_seq_no == data_file_seq_no)):
print "sequence number don't match "
sys.exit(1)
if (len(ctrl_contents) != 4 or len(data_contents) != 4 or len(mgmt_contents) !=6) :
print "for ctrl " ,len (ctrl_contents) ,"for data", len(data_contents), "for mgmt", len(mgmt_contents)
print "file is malformed or the order of input folders is wrong "
continue
#The following code block parses the data file
#print "----------done with missed .. now with actual data "
correct_data_frames=header_and_correct_data_frames[data_file_header_byte_count+1:]
data_index=0
for idx in xrange(0,len(correct_data_frames)-DATA_STRUCT_SIZE ,DATA_STRUCT_SIZE ):
global file_timestamp
frame=correct_data_frames[data_index:data_index+DATA_STRUCT_SIZE]
offset,success,tsf= 8,-1,0
header = frame[:offset]
frame_elem=defaultdict(list)
monitor_elem=defaultdict(list)
(version,pad,radiotap_len,present_flag)=struct.unpack('<BBHI',header)
(success,frame_elem,monitor_elem)=parse_radiotap(frame,radiotap_len,present_flag,offset,monitor_elem,frame_elem)
if success:
for key in frame_elem.keys():
tsf=key
parse_data_frame(frame,radiotap_len,frame_elem)
#print frame_elem[tsf]
temp=frame_elem[tsf]
temp.insert(0,tsf)
if radiotap_len == RADIOTAP_RX_LEN:
rx_data_fc_correct= rx_data_fc_correct + 1
elif radiotap_len ==RADIOTAP_TX_LEN :
tx_data_fc_correct= tx_data_fc_correct + 1
else :
print "impossible radiotap len detected ; Report CERN", radiotap_len
else:
print "success denied; correct data frame"
data_index=data_index+DATA_STRUCT_SIZE
del frame_elem
del monitor_elem
data_index=0
for idx in xrange(0,len(err_data_frames)-DATA_ERR_STRUCT_SIZE,DATA_ERR_STRUCT_SIZE ):
frame=err_data_frames[data_index:data_index+DATA_ERR_STRUCT_SIZE]
offset,success,tsf= 8,-1,0
header = frame[:offset]
frame_elem=defaultdict(list)
monitor_elem=defaultdict(list)
(version,pad,radiotap_len,present_flag)=struct.unpack('<BBHI',header)
(success,frame_elem,monitor_elem)=parse_radiotap(frame,radiotap_len,present_flag,offset,monitor_elem,frame_elem)
if success:
for key in frame_elem.keys():
tsf=key
parse_err_data_frame(frame,radiotap_len,frame_elem)
temp=frame_elem[tsf]
temp.insert(0,tsf)
if radiotap_len == RADIOTAP_RX_LEN:
rx_data_fc_incorrect= rx_data_fc_incorrect + 1
elif radiotap_len ==RADIOTAP_TX_LEN :
tx_data_fc_incorrect= tx_data_fc_incorrect + 1
else :
print "impossible radiotap len detected ; Report CERN"
else :
print "success denied; incorrect data frame"
data_index= data_index+DATA_ERR_STRUCT_SIZE
del frame_elem
del monitor_elem
#The following code block parses the mgmt files
beacon_mgmt_frames=header_and_beacon_mgmt_frames[mgmt_file_header_byte_count+1:]
mgmt_index=0
for idx in xrange(0,len(beacon_mgmt_frames)-MGMT_BEACON_STRUCT_SIZE ,MGMT_BEACON_STRUCT_SIZE ):
global file_timestamp
frame=beacon_mgmt_frames[mgmt_index:mgmt_index+MGMT_BEACON_STRUCT_SIZE]
offset,success,tsf= 8,-1,0
header = frame[:offset]
frame_elem,monitor_elem=defaultdict(list),defaultdict(list)
(version,pad,radiotap_len,present_flag)=struct.unpack('<BBHI',header)
if not( radiotap_len ==RADIOTAP_RX_LEN or radiotap_len == RADIOTAP_TX_LEN) :
print "the radiotap header is not correct "
sys.exit(1)
(success,frame_elem,monitor_elem)=parse_radiotap(frame,radiotap_len,present_flag,offset,monitor_elem,frame_elem)
if success==1 :
if radiotap_len == RADIOTAP_RX_LEN:
rx_mgmt_fc_correct= rx_mgmt_fc_correct + 1
elif radiotap_len == RADIOTAP_TX_LEN:
tx_mgmt_fc_correct= tx_mgmt_fc_correct + 1
else :
print "success denied"
mgmt_index=mgmt_index+MGMT_BEACON_STRUCT_SIZE
del frame_elem
del monitor_elem
mgmt_index=0
for idx in xrange(0,len(err_mgmt_frames)-MGMT_ERR_STRUCT_SIZE,MGMT_ERR_STRUCT_SIZE ):
global file_timestamp
frame=err_mgmt_frames[mgmt_index:mgmt_index+MGMT_ERR_STRUCT_SIZE]
offset,success,tsf= 8,-1,0
header = frame[:offset]
frame_elem,monitor_elem=defaultdict(list),defaultdict(list)
(version,pad,radiotap_len,present_flag)=struct.unpack('<BBHI',header)
if not( radiotap_len ==RADIOTAP_RX_LEN or radiotap_len == RADIOTAP_TX_LEN) :
print "the radiotap header is not correct "
sys.exit(1)
(success,frame_elem,monitor_elem)=parse_radiotap(frame,radiotap_len,present_flag,offset,monitor_elem,frame_elem)
if success==1 :
if radiotap_len == RADIOTAP_RX_LEN:
rx_mgmt_fc_incorrect= rx_mgmt_fc_incorrect + 1
elif radiotap_len == RADIOTAP_TX_LEN:
tx_mgmt_fc_incorrect= tx_mgmt_fc_incorrect + 1
else:
print "success denied"
mgmt_index= mgmt_index+MGMT_ERR_STRUCT_SIZE
del frame_elem
del monitor_elem
correct_ctrl_frames=header_and_correct_ctrl_frames[ctrl_file_header_byte_count+1:]
ctrl_index=0
for idx in xrange(0,len(correct_ctrl_frames)-CTRL_STRUCT_SIZE ,CTRL_STRUCT_SIZE ):
global file_timestamp
frame=correct_ctrl_frames[ctrl_index:ctrl_index+CTRL_STRUCT_SIZE]
offset,success,tsf= 8,-1,0
header = frame[:offset]
frame_elem, monitor_elem=defaultdict(list),defaultdict(list)
(version,pad,radiotap_len,present_flag)=struct.unpack('<BBHI',header)
if not( radiotap_len ==RADIOTAP_RX_LEN or radiotap_len == RADIOTAP_TX_LEN) :
print "the radiotap header is not correct "
sys.exit(1)
(success,frame_elem,monitor_elem)=parse_radiotap(frame,radiotap_len,present_flag,offset,monitor_elem,frame_elem)
if success==1 :
if radiotap_len == RADIOTAP_RX_LEN:
rx_ctrl_fc_correct= rx_ctrl_fc_correct + 1
elif radiotap_len ==RADIOTAP_TX_LEN :
tx_ctrl_fc_correct= tx_ctrl_fc_correct + 1
else :
print "success denied"
ctrl_index=ctrl_index+CTRL_STRUCT_SIZE
del frame_elem
del monitor_elem
ctrl_index=0
for idx in xrange(0,len(err_ctrl_frames)-CTRL_ERR_STRUCT_SIZE,CTRL_ERR_STRUCT_SIZE):
global file_timestamp
frame=err_ctrl_frames[ctrl_index:ctrl_index+CTRL_ERR_STRUCT_SIZE]
offset,success,tsf= 8,-1,0
header = frame[:offset]
frame_elem,monitor_elem=defaultdict(list),defaultdict(list)
(version,pad,radiotap_len,present_flag)=struct.unpack('<BBHI',header)
if not( radiotap_len ==RADIOTAP_RX_LEN or radiotap_len == RADIOTAP_TX_LEN) :
print "the radiotap header is not correct "
sys.exit(1)
(success,frame_elem,monitor_elem)=parse_radiotap(frame,radiotap_len,present_flag,offset,monitor_elem,frame_elem)
if success ==1:
if radiotap_len == RADIOTAP_RX_LEN:
rx_ctrl_fc_incorrect= rx_ctrl_fc_incorrect + 1
elif radiotap_len ==RADIOTAP_TX_LEN :
tx_ctrl_fc_incorrect= tx_ctrl_fc_incorrect + 1
else :
print "success denied"
ctrl_index= ctrl_index+CTRL_ERR_STRUCT_SIZE
del frame_elem
del monitor_elem
file_counter +=1
if file_counter %10 == 0:
print file_counter
print "Statistics "
print "Number of Mgmt frames ", rx_mgmt_fc_incorrect + rx_mgmt_fc_correct+tx_mgmt_fc_incorrect + tx_mgmt_fc_correct
print "Number of Data frames ", tx_data_fc_incorrect+ tx_data_fc_correct + rx_data_fc_incorrect+ rx_data_fc_correct
print "Number of Ctrl frames ", tx_ctrl_fc_incorrect + tx_ctrl_fc_correct + rx_ctrl_fc_incorrect + rx_ctrl_fc_correct
print "Percentage of Mgmt frames ", ( rx_mgmt_fc_incorrect + rx_mgmt_fc_correct+tx_mgmt_fc_incorrect + tx_mgmt_fc_correct) *100.0/(rx_mgmt_fc_incorrect+ rx_mgmt_fc_correct+tx_mgmt_fc_correct + tx_mgmt_fc_incorrect + tx_data_fc_correct+ tx_data_fc_incorrect+rx_data_fc_correct + rx_data_fc_incorrect + rx_ctrl_fc_correct + rx_ctrl_fc_incorrect + tx_ctrl_fc_correct+ tx_ctrl_fc_incorrect)