-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmcu_debug_log_decoder.py
More file actions
269 lines (240 loc) · 11.9 KB
/
mcu_debug_log_decoder.py
File metadata and controls
269 lines (240 loc) · 11.9 KB
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
from LogDecoders import *
class McuLogDecoder(DebugLogDecoder):
def __init__(self, dev_name, filter_dict, config):
super(McuLogDecoder, self).__init__(dev_name, filter_dict)
self.mcu_decode_output_folder = './mcu_log_files/'
self.filter_out_count = 0 # Record the discarded log count
self.filter_in_count = 0 # Record the kept log count
self.config = config
def load_one_file(self, file_name):
# Input: do not include file extension in the filename
with open(self.mcu_decode_output_folder + '{0}.txt'.format(file_name)) as f_t:
one_row = f_t.readline()
f_t.close()
self.f_out = open(self.mcu_decode_output_folder + file_name + '_decoded.txt', 'w') # output handler
return one_row # all the data in the file.
def state_machine(self, row_all):
states = {'UNKNOWN': 0, 'PREAMBLE': 1, 'COUNT': 2, 'TICK': 3,
'DATA': 5, 'LENGTH': 4, 'FINISHED': 6} # UART state machine
str_buf = []
raw_buf = []
st = states['PREAMBLE']
# Initialize local variable to prevent warning.
seq_num = 0
time_tick = 0
parsed_msg = ''
time_stamp = .0
payload_len = 1
max_len = 0
app_rep_flag = False
empty_msg_list = [0, 0, .0] # Order: seq_num, timestamp, time tick,
parsed_log_list = empty_msg_list.copy()
row_buf = row_all.split(' ')
print('Message byte length:', len(row_buf))
while len(row_buf) > 0:
if st == states['PREAMBLE']:
new_byte = row_buf.pop(0)
# print(new_byte)
if new_byte == '25': # '%'
str_buf.append(new_byte)
# TODO: change read byte to traversing.
new_byte = ''
for i in range(4):
new_byte += row_buf.pop(0)
if new_byte == '4442473A': # 'DBG:'
str_buf.append(new_byte)
st = states['COUNT']
time_stamp = time.time()
parsed_log_list[1] = time_stamp
else:
str_buf = []
else:
str_buf = [] # Empty the buf and restart
# str_buf.append(new_byte)
# if len(str_buf) > 200: # read
# print('Read too m')
# self.dbg_run_flag = False
elif st == states['COUNT']:
# 4 bytes' msg counter.
str_buf = []
for i in range(4):
str_buf.append(row_buf.pop(0))
num_temp = self.hex_to_decimal(str_buf)
if num_temp - seq_num != 1:
missing_log_msg = '[Warning] Inconsistent sequence number detected! This: {0}, Prev: {1}'.format(num_temp, seq_num)
print(missing_log_msg)
seq_num = num_temp
parsed_log_list[0] = seq_num # Update the dict
str_buf = []
st = states['TICK']
elif st == states['TICK']:
str_buf = []
for i in range(4):
str_buf.append(row_buf.pop(0))
time_tick = self.hex_to_decimal(str_buf)
parsed_log_list[2] = time_tick # Update the dict
for i in range(4):
dummy = row_buf.pop(0) # Neglect the useless bytes.
if len(dummy) == 0:
st = states['PREAMBLE']
continue
if dummy[0] == 'A': # This is an application report message
app_rep_flag = True
parsed_log_list.append('APPLICATION_REPORT')
else:
app_rep_flag = False
st = states['LENGTH']
elif st == states['LENGTH']:
str_buf = []
for i in range(2):
str_buf.append(row_buf.pop(0))
payload_len = self.hex_to_decimal(str_buf)
# if max_len < payload_len:
# max_len = payload_len
# print('[INFO]Max payload length:', max_len)
if payload_len > 720:
st = states['UNKNOWN']
print('[ERROR] Found unbounded large payload length.')
continue
st = states['DATA']
elif st == states['DATA']:
# Read in the data as the length field specified.
str_buf = []
for i in range(payload_len):
str_buf.append(row_buf.pop(0))
raw_buf = parsed_log_list.copy()
raw_buf.append(self.byte_concatenation(str_buf))
print(str_buf)
if app_rep_flag is True:
str_buf.reverse() # There is another reverse in the hex to ascii function.
parsed_log_list.append(self.hex_to_ascii(str_buf))
self.application_report_export_processing(parsed_log_list)
else:
disp_list = self.parse_one_msg_common(str_buf)
# Output order: msg_id_dec, msg_name, msg_src, msg_dest, msg_length, decoded_msg
disp_list = [str(x) for x in disp_list]
self.f_out.write('\t'.join(disp_list))
print(disp_list)
# TODO: Bookmarked. extract info from log.
# self.extract_info_from_log(disp_list)
parsed_log_list += disp_list # parsed_log_list have time. disp_list only has message info.
self.display_export_processing(parsed_log_list)
if len(parsed_log_list) >= 6 and parsed_log_list[4] != 'N/A': # msg name
self.dy_extract_rsrp_snr_from_log(parsed_log_list)
self.extract_npusch_power_from_log(parsed_log_list)
# print(parsed_log_dict)
st = states['FINISHED']
elif st == states['FINISHED']:
parsed_log_list = empty_msg_list.copy()
self.f_out.flush()
st = states['PREAMBLE'] # Recycle the processing state machine
elif st == states['UNKNOWN']:
print('[ERROR] Something wrong happens. Reset to PREAMBLE state.')
st = states['PREAMBLE']
# All the bytes are processed.
self.f_out.flush()
self.f_close()
def display_export_processing(self, info_list):
# TODO: change this to txt friendly format.
self.res = self.packet_output_formatting(info_list)
if True:
res_disp = self.res.split('\n')[0] + '\n' # Truncate the result and keep only the first line.
else:
res_disp = self.res
if len(info_list) <= 5:
print('[ERROR] Missing element in Info List.')
try:
is_filtered = self.check_filters(info_list[4])
except IndexError:
is_filtered = True
if is_filtered is False:
print(res_disp)
# Apply the filter, exporting
if self.config['Export decoded']:
if self.config['Keep filtered logs'] is True:
# Means write every log
is_filtered = False
if is_filtered is False:
# This log need to be export
if self.config['Export format'] == 'txt':
# self.f_exp.write(self.res)
self.file_io.write_debug_log_formatted(info_list)
elif self.config['Export format'] == 'csv':
# self.f_exp_csv_writer.writerow(info_list)
self.file_io.write_debug_log_formatted(info_list)
def application_report_export_processing(self, info_list):
first_line = '#{0}\t{1}\t{2}\t{3}\t\n'.format(info_list[0], info_list[1],
info_list[2], info_list[3])
whole_app_rep = first_line + info_list[4] + '\n' # The 4th element is the actual msg. add double \n
# Check filter
is_filtered = self.check_filters('APPLICATION_REPORT')
if is_filtered is False:
if self.config['Run in Qt']:
self.transfer_buf.append(whole_app_rep)
self.dbg_uart_trigger.emit()
else:
print(whole_app_rep)
if self.config['Export decoded']:
if self.config['Keep filtered logs'] is True:
# Means write every log
is_filtered = False
if is_filtered is False:
# This log need to be export
if self.config['Export format'] == 'txt':
# self.f_exp.write(whole_app_rep)
self.file_io.write_debug_log_formatted(whole_app_rep)
elif self.config['Export format'] == 'csv':
# self.f_exp_csv_writer.writerow(info_list)
self.file_io.write_debug_log_formatted(info_list)
def check_filters(self, log_name):
is_filtered_flag = False # True: not wanted log; False: wanted log.
# Apply the filter, printing
if self.filter_flag == 1: # Filter out
if log_name in self.filter_dict['FO']: # Message name
is_filtered_flag = True
self.filter_out_count += 1
else:
self.filter_in_count += 1 # The log that is kept.
elif self.filter_flag == 2: # Filter in
if log_name in self.filter_dict['FI']: # Message in the set
self.filter_in_count += 1
else:
is_filtered_flag = True
self.filter_out_count += 1
if self.filter_out_count % 1000 == 0 and self.filter_out_count > 0:
filter_out_count_msg = '[INFO] Excluded log count: {0}'.format(self.filter_out_count)
print(filter_out_count_msg)
if self.config['Run in Qt']:
self.sys_info_buf.append(filter_out_count_msg)
self.dbg_uart_trigger.emit() # Tell the main thread to update the system info monitor.
if self.filter_in_count % 500 == 0 and self.filter_in_count > 0:
filter_in_count_msg = '[INFO] Included log count: {0}'.format(self.filter_in_count)
print(filter_in_count_msg)
if self.config['Run in Qt']:
self.sys_info_buf.append(filter_in_count_msg)
self.dbg_uart_trigger.emit()
return is_filtered_flag
def byte_concatenation(self, b_list):
# Convert ['AA', '3E', '4F'] to 'AA-3E-4F'.
ret = ''
for i in range(len(b_list)):
if i != len(b_list) - 1:
ret += b_list[i] + '-'
else:
ret += b_list[i]
return ret
filter_out_list = {'N/A', 'UICC_DBG_LOG_P0', 'UICC_DBG_LOG_P1', 'UICC_DBG_LOG_P2'} # add item to only one of them!!! E.g. to remove invalid message, use 'N/A'.
filter_in_list = {} # add item to only one of them!!!
filter_dict = {'FO': filter_out_list, 'FI': filter_in_list}
config = {'Device name': 'BC95',
'Dbg port': 'COM3',
'Filter dict': filter_dict,
'Run in Qt': False,
'Export decoded': True,
'Export to file': True, # choose to export the decoded info. The raw log is mandatory.
'Export filename time prefix': '%y%m%d_%H%M%S',
'Keep filtered logs': False, # filtered logs will not be printed, but you can export them to file.
'Time format': '%m-%d %H:%M:%S.%f', # see time.strftime() for detail.
'Export format': 'csv'} # format: txt or csv, need to enable "export to file first".
mcu_decoder = McuLogDecoder('BC95', filter_dict, config)
mcu_decoder.state_machine(mcu_decoder.load_one_file('raw_dbg_log_short'))