-
Notifications
You must be signed in to change notification settings - Fork 38
/
candb.py
executable file
·954 lines (856 loc) · 39.9 KB
/
candb.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
"""\
This module provides CAN database(*dbc) and matrix(*xls) operation functions.
"""
import re
import xlrd
import sys
import traceback
import os
reload(sys)
sys.setdefaultencoding('utf-8')
# enable or disable debug info display, this switch is controlled by -d option.
debug_enable = False
# symbol definition of DBC format file
NEW_SYMBOLS = [
'NS_DESC_', 'CM_', 'BA_DEF_', 'BA_', 'VAL_', 'CAT_DEF_', 'CAT_',
'FILTER', 'BA_DEF_DEF_', 'EV_DATA_', 'ENVVAR_DATA_', 'SGTYPE_',
'SGTYPE_VAL_', 'BA_DEF_SGTYPE_', 'BA_SGTYPE_', 'SIG_TYPE_REF_',
'VAL_TABLE_', 'SIG_GROUP_', 'SIG_VALTYPE_', 'SIGTYPE_VALTYPE_',
'BO_TX_BU_', 'BA_DEF_REL_', 'BA_REL_', 'BA_DEF_DEF_REL_',
'BU_SG_REL_', 'BU_EV_REL_', 'BU_BO_REL_', 'SG_MUL_VAL_'
]
# pre-defined attribution definitions
# object type name value type min max default value range
ATTR_DEFS_INIT = [
["Message", "DiagRequest", "Enumeration", "", "", "No", ["No", "Yes"]],
["Message", "DiagResponse", "Enumeration", "", "", "No", ["No", "Yes"]],
["Message", "DiagState", "Enumeration", "", "", "No", ["No", "Yes"]],
["Message", "GenMsgCycleTime", "Integer", 0, 0, 0, []],
["Message", "GenMsgCycleTimeActive", "Integer", 0, 0, 0, []],
["Message", "GenMsgCycleTimeFast", "Integer", 0, 0, 0, []],
["Message", "GenMsgDelayTime", "Integer", 0, 0, 0, []],
["Message", "GenMsgILSupport", "Enumeration", "", "", "No", ["No", "Yes"]],
["Message", "GenMsgNrOfRepetition", "Integer", 0, 0, 0, []],
["Message", "GenMsgSendType", "Enumeration", "", "", "cycle", ["cycle", "NoSendType", "IfActive"]],
["Message", "GenMsgStartDelayTime", "Integer", 0, 65535, 0, []],
["Message", "NmMessage", "Enumeration", "", "", "No", ["No", "Yes"]],
["Network", "BusType", "String", "", "", "CAN", []],
["Network", "Manufacturer", "String", "", "", "", []],
["Network", "NmBaseAddress", "Hex", 0x0, 0x7FF, 0x400, []],
["Network", "NmMessageCount", "Integer", 0, 255, 128, []],
["Network", "NmType", "String", "", "", "", []],
["Network", "DBName", "String", "", "", "", []],
["Node", "DiagStationAddress", "Hex", 0x0, 0xFF, 0x0, []],
["Node", "ILUsed", "Enumeration", "", "", "No", ["No", "Yes"]],
["Node", "NmCAN", "Integer", 0, 2, 0, []],
["Node", "NmNode", "Enumeration", "", "", "Not", ["Not", "Yes"]],
["Node", "NmStationAddress", "Hex", 0x0, 0xFF, 0x0, []],
["Node", "NodeLayerModules", "String", "", "", "CANoeILNVector.dll", []],
["Signal", "GenSigInactiveValue", "Integer", 0, 0, 0, []],
["Signal", "GenSigSendType", "Enumeration", "", "", "cycle", ["cycle", "OnChange", "OnWrite", "IfActive", "OnChangeWithRepetition", "OnWriteWithRepetition", "IfActiveWithRepetition"]],
["Signal", "GenSigStartValue", "Integer", 0, 0, 0, []],
["Signal", "GenSigTimeoutValue", "Integer", 0, 1000000000, 0, []],
]
# matrix template dict
# Matrix parser use this dict to parse can network information from excel. The
# 'key's are used in this module and the 'ValueTable' are used to match excel
# column header. 'ValueTable' may include multi string.
MATRIX_TEMPLATE_MAP = {
"msg_name_col": ["MsgName"],
"msg_type_col": ["MsgType"],
"msg_id_col": ["MsgID"],
"msg_send_type_col": ["MsgSendType"],
"msg_cycle_col": ["MsgCycleTime"],
"msg_len_col": ["MsgLength"],
"sig_name_col": ["SignalName"],
"sig_comment_col": ["SignalDescription"],
"sig_byte_order_col": ["ByteOrder"],
"sig_start_bit_col": ["StartBit"],
"sig_len_col": ["BitLength"],
"sig_value_type_col": ["DateType"],
"sig_factor_col": ["Resolution"],
"sig_offset_col": ["Offset"],
"sig_min_phys_col": ["SignalMin.Value(phys)"],
"sig_max_phys_col": ["SignalMax.Value(phys)"],
"sig_init_val_col": ["InitialValue(Hex)"],
"sig_unit_col": ["Unit"],
"sig_val_col": ["SignalValueDescription"],
}
# excel workbook sheets with name in this list are ignored
MATRIX_SHEET_IGNORE = ["Cover", "History", "Legend", "ECU Version", ]
NODE_NAME_MAX = 8
class MatrixTemplate(object):
def __init__(self):
self.msg_name_col = 0
self.msg_type_col = 0
self.msg_id_col = 0
self.msg_send_type_col = 0
self.msg_cycle_col = 0
self.msg_len_col = 0
self.sig_name_col = 0
self.sig_comment_col = 0
self.sig_byte_order_col = 0
self.sig_start_bit_col = 0
self.sig_len_col = 0
self.sig_value_type_col = 0
self.sig_factor_col = 0
self.sig_offset_col = 0
self.sig_min_phys_col = 0
self.sig_max_phys_col = 0
self.sig_init_val_col = 0
self.sig_unit_col = 0
self.sig_val_col = 0
self.nodes = {}
self.start_row = 0 # start row number of valid data
def members(self):
return sorted(vars(self).items(), key=lambda item:item[1])
def __str__(self):
s = []
for key, var in self.members():
if type(var) == int:
s.append(" %s : %d (%s)" % (key, var, get_xls_col(var)))
return '\n'.join(s)
def get_xls_col(val):
"""
get_xls_col(col) --> string
Convert int column number to excel column symbol like A, B, AB .etc
"""
if type(val) == type(0):
if val <= 25:
s = chr(val+0x41)
elif val <100:
s = chr(val/26-1+0x41)+chr(val%26+0x41)
else:
raise ValueError("column number too large: ", str(val))
return s
else:
raise TypeError("column number only support int: ", str(val))
def get_list_item(list_object):
"""
get_list_item(list_object) --> object(string)
Show object list to terminal and get selection object from ternimal.
"""
list_index = 0
for item in list_object:
print " %2d %s" %(list_index, item)
list_index += 1
while True:
user_input = raw_input()
if user_input.isdigit():
select_index = int(user_input)
if select_index < len(list_object):
return list_object[select_index]
else:
print "input over range"
else:
print "input invalid"
def parse_sheetname(workbook):
"""
Get sheet name of can matrix in the xls workbook.
Only informations in this sheet are used.
"""
sheets = []
for sheetname in workbook.sheet_names():
if sheetname == "Matrix":
return sheetname
if sheetname not in MATRIX_SHEET_IGNORE:
sheets.append(sheetname)
if len(sheets)==1:
return sheets[0]
elif len(sheets)>=2:
print "Select one sheet blow:"
# print " "," ".join(sheets)
# return raw_input()
return get_list_item(sheets)
else:
print "Select one sheet blow:"
# print " "," ".join(workbook.sheet_names())
# return raw_input()
return get_list_item(workbook.sheet_names())
def parse_template(sheet):
"""
parse_template(sheet) -> MatrixTemplate
Parse column headers of xls sheet and the result of column numbers is
returned as MatrixTemplate object
"""
# find table header row
header_row_num = 0xFFFF
for row_num in range(0, sheet.nrows):
if sheet.row_values(row_num)[0].find("Msg Name") != -1:
#print "table header row number: %d" % row_num
header_row_num = row_num
if header_row_num == 0xFFFF:
raise ValueError("Can't find \"Msg Name\" in this sheet")
# get header info
template = MatrixTemplate()
for col_num in range(0, sheet.ncols):
value = sheet.row_values(header_row_num)[col_num]
if value is not None:
value = value.replace(" ","")
for col_name in MATRIX_TEMPLATE_MAP.keys():
for col_header in MATRIX_TEMPLATE_MAP[col_name]:
if col_header in value and getattr(template,col_name)==0:
setattr(template, col_name, col_num)
break
template.start_row = header_row_num + 1
# get ECU nodes
node_start_col = template.sig_val_col
for col in range(node_start_col, sheet.ncols):
value = sheet.row_values(header_row_num)[col]
if value is not None and value != '':
value = value.replace(" ","")
if len(value) <= NODE_NAME_MAX:
template.nodes[value] = col
# print "detected nodes: ", template.nodes
return template
def parse_sig_vals(val_str):
"""
parse_sig_vals(val_str) -> {valuetable}
Get signal key:value pairs from string. Returns None if failed.
"""
vals = {}
if val_str is not None and val_str != '':
token = re.split('[\;\:\\n]+', val_str.strip())
if len(token) >= 2:
if len(token) % 2 == 0:
for i in range(0, len(token), 2):
try:
val = getint(token[i])
desc = token[i + 1].replace('\"','').strip()
vals[desc] = val
except ValueError:
# print "waring: ignored signal value definition: " ,token[i], token[i+1]
raise
return vals
else:
# print "waring: ignored signal value description: ", val_str
raise ValueError()
else:
raise ValueError(val_str)
else:
return None
def getint(str, default=None):
"""
getint(str) -> int
Convert string to int number. If default is given, default value is returned
while str is None.
"""
if str == '':
if default==None:
raise ValueError("None type object is unexpected")
else:
return default
else:
try:
val = int(str)
return val
except (ValueError, TypeError):
try:
val = int(str, 16)
return val
except:
raise
def motorola_msb_2_motorola_backward(start_bit, sig_size, frame_size):
msb_bytes = start_bit//8
msb_byte_bit = start_bit%8
byte_remain_bits = msb_byte_bit + 1
remain_bits = sig_size
backward_start_bit = (frame_size *8 -1) - (8 * msb_bytes) - (7 - msb_byte_bit)
while remain_bits > 0:
if remain_bits > byte_remain_bits:
remain_bits -= byte_remain_bits
backward_start_bit -= byte_remain_bits
byte_remain_bits = 8
else:
backward_start_bit = backward_start_bit - remain_bits + 1
remain_bits = 0
return backward_start_bit
class CanNetwork(object):
def __init__(self, init=True):
self.nodes = []
self.messages = []
self.name = 'CAN'
self.val_tables = []
self.version = ''
self.new_symbols = NEW_SYMBOLS
self.attr_defs = []
self._filename = ''
if init:
self._init_attr_defs()
def _init_attr_defs(self):
for attr_def in ATTR_DEFS_INIT:
self.attr_defs.append(CanAttribution(attr_def[1], attr_def[0], attr_def[2], attr_def[3], attr_def[4],
attr_def[5], attr_def[6]))
def __str__(self):
# ! version
lines = ['VERSION ' + r'""']
lines.append('\n\n\n')
# ! new_symbols
lines.append('NS_ :\n')
for symbol in self.new_symbols:
lines.append(' ' + symbol + '\n')
lines.append('\n')
# ! bit_timming
lines.append("BS_:\n")
lines.append('\n')
# ! nodes
line = ["BU_:"]
for node in self.nodes:
line.append(node)
lines.append(' '.join(line) + '\n\n\n')
# ! messages
for msg in self.messages:
lines.append(str(msg) + '\n\n')
lines.append('\n\n')
# ! comments
lines.append('''CM_ " "''' + ";" + "\n")
for msg in self.messages:
# <message comment are ignored> # todo
for sig in msg.signals:
comment = sig.comment
if comment != "":
line = ["CM_", "SG_", str(msg.msg_id), sig.name, "\"" + comment + "\"" + ";"]
lines.append(" ".join(line) + '\n')
# ! attribution defines
for attr_def in self.attr_defs:
line = ["BA_DEF_"]
obj_type = attr_def.object_type
if (obj_type == "Node"):
line.append("BU_")
elif (obj_type == "Message"):
line.append("BO_")
elif (obj_type == "Signal"):
line.append("SG_")
##elif (obj_type == "Network")
line.append(" \"" + attr_def.name + "\"")
val_type = attr_def.value_type
if (val_type == "Enumeration"):
line.append("ENUM")
val_range = []
for val in attr_def.values:
val_range.append("\"" + val + "\"")
line.append(",".join(val_range) + ";")
elif (val_type == "String"):
line.append("STRING" + " " + ";")
elif (val_type == "Hex"):
line.append("HEX")
line.append(str(attr_def.min))
line.append(str(attr_def.max) + ";")
elif (val_type == "Integer"):
line.append("INT")
line.append(str(attr_def.min))
line.append(str(attr_def.max) + ";")
lines.append(" ".join(line) + '\n')
# ! attribution default values
for attr_def in self.attr_defs:
line = ["BA_DEF_DEF_"]
line.append(" \"" + attr_def.name + "\"")
val_type = attr_def.value_type
if (val_type == "Enumeration"):
line.append("\"" + attr_def.default + "\"" + ";")
elif (val_type == "String"):
line.append("\"" + attr_def.default + "\"" + ";")
elif (val_type == "Hex"):
line.append(str(attr_def.default) + ";")
elif (val_type == "Integer"):
line.append(str(attr_def.default) + ";")
lines.append(" ".join(line) + '\n')
# ! attribution value of object
# build-in value "DBName"
line = ["BA_"]
line.append('''"DBName"''')
line.append(self.name + ";")
lines.append(" ".join(line) + '\n')
# ! message attribution values
for msg in self.messages:
for attr_def in self.attr_defs:
if (msg.attrs.has_key(attr_def.name)):
if (msg.attrs[attr_def.name] != attr_def.default):
line = ["BA_"]
line.append("\"" + attr_def.name + "\"")
line.append("BO_")
line.append(str(msg.msg_id))
if (attr_def.value_type == "Enumeration"):
# write enum index instead of enum value
line.append(str(attr_def.values.index(str(msg.attrs[attr_def.name]))) + ";")
else:
line.append(str(msg.attrs[attr_def.name]) + ";")
lines.append(" ".join(line) + '\n')
# ! signal attribution values
for msg in self.messages:
for sig in msg.signals:
if sig.init_val is not None and sig.init_val is not 0:
line = ["BA_"]
line.append('''"GenSigStartValue"''')
line.append("SG_")
line.append(str(msg.msg_id))
line.append(sig.name)
line.append(str(sig.init_val))
line.append(';')
lines.append(' '.join(line) + '\n')
# ! Value table define
for msg in self.messages:
for sig in msg.signals:
if sig.values is not None and len(sig.values) >= 1:
line = ['VAL_']
line.append(str(msg.msg_id))
line.append(sig.name)
for key in sig.values:
line.append(str(sig.values[key]))
line.append('"'+ key +'"')
line.append(';')
lines.append(' '.join(line) + '\n')
return ''.join(lines)
def add_attr_def(self, name, object_type, value_type, minvalue, maxvalue, default, values=None):
index = None
for i in range (0, len(self.attr_defs)):
if self.attr_defs[i].name == name:
index = i
break
if index is None:
attr_def = CanAttribution(name, object_type, value_type, minvalue, maxvalue, default, values)
self.attr_defs.append(attr_def)
else:
print("info: override default attribution definition \'{}\'".format(name))
self.attr_defs[index].object_type = object_type
self.attr_defs[index].value_type = value_type
self.attr_defs[index].min = minvalue
self.attr_defs[index].max = maxvalue
self.attr_defs[index].default = default
self.attr_defs[index].values = values
#print(self.attr_defs[index])
def get_attr_def(self, name):
ret = None
for attr_def in self.attr_defs:
if attr_def.name == name:
ret = attr_def
return ret
def set_msg_attr(self, msg_id, attr_name, value):
for msg in self.messages:
if msg.msg_id == msg_id:
msg.set_attr(attr_name, value)
def get_msg_attr(self, msg_id, attr_name):
value = None
for msg in self.messages:
if msg.msg_id == msg_id:
if msg.attrs.has_key(attr_name):
value = msg.attrs[attr_name]
else:
attr_def = self.get_attr_def(attr_name)
if attr_def is not None:
value = attr_def.default
return value
def set_sig_attr(self, msg_id, sig_name, attr_name, attr_value):
for msg in self.messages:
if msg.msg_id == msg_id:
for sig in msg.signals:
if sig.name == sig_name:
sig.set_attr(attr_name, attr_value)
def convert_attr_def_value(self, attr_def_name, value_str):
'''
Convert string to a value with AttributionDefinition's data type.
Except:
ValueError: attribuiton definition is not exit, or value type
of AttrDef is not support yet.
'''
attr_def_value_type = None
for attr_def in self.attr_defs:
if attr_def.name == attr_def_name:
attr_def_value_type = attr_def.value_type
attr_def_values = attr_def.values
break
if attr_def_value_type == 'Integer':
value = int(value_str)
elif attr_def_value_type == 'Float':
value = float(value_str)
elif attr_def_value_type == 'String':
value = value_str
elif attr_def_value_type == 'Enumeration':
if value_str.isdigit():
value = attr_def_values[int(value_str)]
else:
value = value_str[1:-1]
elif attr_def_value_type == 'Hex':
value = int(value_str)
elif attr_def_value_type is None:
raise ValueError("Undefined attribution definition: {}".format(attr_def_name))
else:
raise ValueError("Unkown attribution definition value type: {}".format(attr_def_value_type))
return value
def sort(self, option='id'):
messages = self.messages
if option == 'id':
# sort by msg_id, id is treated as string, NOT numbers, to keep the same with candb++
messages.sort(key=lambda msg: str(msg.msg_id))
for msg in messages:
signals = msg.signals
signals.sort(key=lambda sig: sig.start_bit)
elif option == 'name':
messages.sort(key=lambda msg: msg.name)
else:
raise ValueError("Invalid sort option \'{}\'".format(option))
def load(self, path):
dbc = open(path, 'r')
for line in dbc:
line_trimmed = line.strip()
line_split = re.split('[\s\(\)\[\]\|\,\:\@\;]+', line_trimmed)
if len(line_split) >= 2:
# Node
if line_split[0] == 'BU_':
self.nodes.extend(line_split[1:])
# Message
elif line_split[0] == 'BO_':
msg = CanMessage()
msg.msg_id = int(line_split[1])
msg.name = line_split[2]
msg.dlc = int(line_split[3])
msg.sender = line_split[4]
self.messages.append(msg)
# Signal
elif line_split[0] == 'SG_':
sig = CanSignal()
sig.name = line_split[1]
sig.start_bit = int(line_split[2])
sig.sig_len = int(line_split[3])
sig.byte_order = line_split[4][:1]
sig.value_type = line_split[4][1:]
sig.factor = line_split[5]
sig.offset = line_split[6]
sig.min = line_split[7]
sig.max = line_split[8]
sig.unit = line_split[9][1:-1] # remove quotation makes
sig.receivers = line_split[10:] # receiver is a list
msg.signals.append(sig)
# Attribution Definition
elif line_split[0] == 'BA_DEF_':
attr_def_object = line_split[1]
if attr_def_object == 'BO_':
attr_def_object_type = 'Message'
attr_def_name_offset = 2
elif attr_def_object == 'SG_':
attr_def_object_type = 'Signal'
attr_def_name_offset = 2
else: # Network
attr_def_object_type = 'Network'
attr_def_name_offset = 1
attr_def_name = line_split[attr_def_name_offset][1:-1] # remove quotation makes
attr_def_value_type = line_split[attr_def_name_offset + 1]
attr_def_default = ''
if attr_def_value_type == 'ENUM':
attr_def_value_type_str = 'Enumeration'
attr_def_value_min = ''
attr_def_value_max = ''
attr_def_values = map(lambda val:val[1:-1], line_split[attr_def_name_offset + 2:]) #remove "
elif attr_def_value_type == 'FLOAT':
attr_def_value_type_str = 'Float'
attr_def_value_min = float(line_split[attr_def_name_offset + 2])
attr_def_value_max = float(line_split[attr_def_name_offset + 3])
attr_def_values = []
elif attr_def_value_type == 'INT':
attr_def_value_type_str = 'Integer'
attr_def_value_min = int(float(line_split[attr_def_name_offset + 2]))
attr_def_value_max = int(float(line_split[attr_def_name_offset + 3]))
attr_def_values = []
elif attr_def_value_type == 'HEX':
attr_def_value_type_str = 'Hex'
attr_def_value_min = int(line_split[attr_def_name_offset + 2])
attr_def_value_max = int(line_split[attr_def_name_offset + 3])
attr_def_values = []
elif attr_def_value_type == 'STRING':
attr_def_value_type_str = 'String'
attr_def_value_min = ''
attr_def_value_max = ''
attr_def_values = []
else:
raise ValueError("Unkown attribution definition value type: {}".format(attr_def_value_type))
self.add_attr_def(attr_def_name, attr_def_object_type, attr_def_value_type_str, \
attr_def_value_min, attr_def_value_max, attr_def_default, attr_def_values)
# Attribution Definition Default Value
elif line_split[0] == 'BA_DEF_DEF_':
attr_def_name = line_split[1][1:-1] #remove "
attr_def_value_type_str = None
attr_def_default = self.convert_attr_def_value(attr_def_name, line_split[2])
attr_def = self.get_attr_def(attr_def_name)
if attr_def is not None:
attr_def.default = attr_def_default
# Object Attribution definition
elif line_split[0] == 'BA_':
attr_name = line_split[1][1:-1] # remove "
attr_object = line_split[2]
if attr_object == 'BO_':
attr_msg_id = int(line_split[3])
attr_value = self.convert_attr_def_value(attr_name, line_split[4])
if attr_value is not None:
self.set_msg_attr(attr_msg_id, attr_name, attr_value)
else:
raise ValueError("Msg \'{}\' attribuition \'{}\' value is {}".format(attr_msg_id, attr_name, line_split[4]))
elif attr_object == 'SG_':
pass
# Value Tables
elif line_split[0] == 'VAL_':
quote_split = re.split('\s*\"\s*', line_trimmed)
line_split = re.split('\s+', quote_split[0])
line_split.extend(quote_split[1:-1])
val_msg_id = int(line_split[1])
val_sig_name = line_split[2]
values = {}
for i in range(3, len(line_split)-1, 2):
try:
values[line_split[i+1]] = int(line_split[i])
except:
print(line_split)
raise
self.set_sig_attr(val_msg_id, val_sig_name, 'values', values)
dbc.close()
def save(self, path=None):
if (path == None):
file = open(self._filename + ".dbc", "w")
else:
file = open(path, 'w')
# file.write(unicode.encode(str(self), "utf-8"))
file.write(str(self))
def import_excel(self, path, sheetname=None, template=None):
# Open file
book = xlrd.open_workbook(path)
# open sheet
if sheetname is not None:
print "use specified sheet: ", sheetname
sheet = book.sheet_by_name(sheetname)
else:
sheetname = parse_sheetname(book)
print "select sheet: ", sheetname
sheet = book.sheet_by_name(sheetname)
# import template
if template is not None:
print 'use specified template: ', template
import_string = "import templates." + template + " as template"
exec import_string
else:
print "parse template"
template = parse_template(sheet)
if debug_enable:
print template
# ! load network information
filename = os.path.basename(path).split(".")
self._filename = ".".join(filename[:-1])
#self.name = ".".join(filename[:-1]).replace(" ", "_").replace('.', '_').replace('-', '_') # use filename as default DBName
self.name = "CAN"
# ! load nodes information
self.nodes = template.nodes.keys()
# ! load messages
messages = self.messages
nrows = sheet.nrows
for row in range(template.start_row, nrows):
row_values = sheet.row_values(row)
msg_name = row_values[template.msg_name_col]
if (msg_name != ''):
# This row defines a message!
message = CanMessage()
signals = message.signals
message.name = msg_name.replace(' ', '')
# message.type = row_values[template.msg_type_col] # todo: should set candb attribution instead
message.msg_id = getint(row_values[template.msg_id_col])
message.dlc = getint(row_values[template.msg_len_col])
send_type = row_values[template.msg_send_type_col].upper().strip()
if (send_type == "CYCLE") or (send_type == "CE"): # todo: CE is treated as cycle
try:
msg_cycle = getint(row_values[template.msg_cycle_col])
except ValueError:
print "warning: message %s\'s cycle time \"%s\" is invalid, auto set to \'0\'" % (message.name, row_values[template.msg_cycle_col])
msg_cycle = 0
message.set_attr("GenMsgCycleTime", msg_cycle)
message.set_attr("GenMsgSendType", "cycle")
else:
message.set_attr("GenMsgSendType", "NoSendType")
# message sender
message.sender = None
for nodename in template.nodes:
nodecol = template.nodes[nodename]
sender = row_values[nodecol].strip().upper()
if sender == "S":
message.sender = nodename
elif sender == "R":
message.receivers.append(nodename)
if message.sender is None:
message.sender = 'Vector__XXX'
messages.append(message)
else:
sig_name = row_values[template.sig_name_col]
if (sig_name != ''):
# This row defines a signal!
signal = CanSignal()
signal.name = sig_name.replace(' ', '')
signal.start_bit = getint(row_values[template.sig_start_bit_col])
signal.sig_len = getint(row_values[template.sig_len_col])
byte_order_type = row_values[template.sig_byte_order_col]
if (byte_order_type.upper() == "MOTOROLA LSB"): # todo
signal.byte_order = '1'
elif (byte_order_type.upper() == "MOTOROLA MSB"):
signal.byte_order = '0'
else:
signal.byte_order = '0'
raise ValueError("Unknown signal byte order type: \"%s\""%byte_order_type) # todo: intel
if (row_values[template.sig_value_type_col].upper() == "UNSIGNED"):
signal.value_type = '+'
else:
signal.value_type = '-'
signal.factor = row_values[template.sig_factor_col]
signal.offset = row_values[template.sig_offset_col]
signal.min = row_values[template.sig_min_phys_col]
signal.max = row_values[template.sig_max_phys_col]
signal.unit = row_values[template.sig_unit_col]
signal.init_val = getint((row_values[template.sig_init_val_col]), 0)
try:
signal.values = parse_sig_vals(row_values[template.sig_val_col])
except ValueError:
if debug_enable:
print "warning: signal %s\'s value table is ignored" % signal.name
else:
pass
signal.comment = row_values[template.sig_comment_col].replace("\"", "\'").replace("\r", '\n') # todo
# get signal receivers
signal.receivers = []
for nodename in template.nodes:
nodecol = template.nodes[nodename]
receiver = row_values[nodecol].strip().upper()
if receiver == "R":
signal.receivers.append(nodename)
elif receiver == "S":
if message.sender == 'Vector__XXX':
message.sender = nodename
print "warning: message %s\'s sender is set to \"%s\" via signal" %(message.name, nodename)
else:
print "warning: message %s\'s sender is conflict to signal \"%s\"" %(message.name, nodename)
else:
pass
if len(signal.receivers) == 0:
if len(message.receivers) > 0:
signal.receivers = message.receivers
else:
signal.receivers.append('Vector__XXX')
signals.append(signal)
self.sort();
class CanMessage(object):
def __init__(self, name='', msg_id=0, dlc=8, sender='Vector__XXX'):
'''
name: message name
id: message id (11bit or 29 bit)
dlc: message data length
sender: message send node
'''
self.name = name
self.msg_id = msg_id
self.send_type = ''
self.dlc = dlc
self.sender = sender
self.signals = []
self.attrs = {}
self.receivers = []
def __str__(self):
para = []
line = ["BO_", str(self.msg_id), self.name + ":", str(self.dlc), self.sender]
para.append(" ".join(line))
for sig in self.signals:
para.append(str(sig))
return '\n '.join(para)
def set_attr(self, name, value):
self.attrs[name] = value
class CanSignal(object):
def __init__(self, name='', start_bit=0, sig_len=1, init_val=0):
self.name = name
self.start_bit = start_bit
self.sig_len = sig_len
self.init_val = init_val
self.byte_order = '0'
self.value_type = '+'
self.factor = 1
self.offset = 0
self.min = 0
self.max = 1
self.unit = ''
self.values = {}
self.receivers = []
self.comment = ''
def __str__(self):
line = ["SG_", self.name, ":",
str(self.start_bit) + "|" + str(self.sig_len) + "@" + self.byte_order + self.value_type,
"(" + str(self.factor) + "," + str(self.offset) + ")", "[" + str(self.min) + "|" + str(self.max) + "]",
"\"" + str(self.unit) + "\"", ','.join(self.receivers)]
return " ".join(line)
def set_attr(self, name, value):
if name == 'values':
self.values = value
else:
raise ValueError("Unsupport set attr of \'{}\'".format(name))
def get_attr(self, name):
if name == 'values':
return self.values
else:
raise ValueError("Unsupport set attr of \'{}\'".format(name))
class CanAttribution(object):
def __init__(self, name, object_type, value_type, minvalue, maxvalue, default, values=None):
self.name = name
self.object_type = object_type
self.value_type = value_type
self.min = minvalue
self.max = maxvalue
self.default = default
self.values = values
def __str__(self):
line = ["name: {}".format(self.name)]
line.append("object type: {}".format(self.object_type))
line.append("value type: {}".format(self.value_type))
line.append("minimum: {}".format(self.min))
line.append("maximum: {}".format(self.max))
line.append("default: {}".format(self.default))
line.append("values:")
if self.value_type == "Enumeration" and self.values is not None:
for i in range(0, len(self.values)):
line.append(" {:d}: {}".format(i, self.values[i]))
return '\n'.join(line)
def parse_args():
"""
Parse command line commands.
"""
import argparse
parse = argparse.ArgumentParser()
subparser = parse.add_subparsers(title="subcommands")
parse_gen = subparser.add_parser("gen", help="Generate dbc from excle file")
parse_gen.add_argument("filename", help="The xls file to generate dbc")
parse_gen.add_argument("-s","--sheetname",help="set sheet name of xls",default=None)
parse_gen.add_argument("-t","--template",help="Choose a template",default=None)
parse_gen.add_argument("-d","--debug",help="show debug info",action="store_true", dest="debug_switch", default=False)
parse_gen.set_defaults(func=cmd_gen)
parse_sort = subparser.add_parser("sort", help="Sort dbc message and signals")
parse_sort.add_argument("filename", help="Dbc filename")
parse_sort.add_argument("-o","--output", help="Specify output file path", default=None)
parse_sort.set_defaults(func=cmd_sort)
parse_cmp = subparser.add_parser("cmp", help="Compare difference bettween two dbc files")
parse_cmp.add_argument("filename1", help="The base file to be compared with")
parse_cmp.add_argument("filename2", help="The new file to be compared")
parse_cmp.set_defaults(func=cmd_cmp)
args = parse.parse_args()
args.func(args)
def cmd_gen(args):
global debug_enable
debug_enable = args.debug_switch
try:
can = CanNetwork()
can.import_excel(args.filename, args.sheetname, args.template)
can.save()
except IOError,e:
print e
except xlrd.biffh.XLRDError,e:
print e
def cmd_sort(args):
can = CanNetwork()
can.load(args.filename)
can.sort()
if args.output is None:
can.save("sorted.dbc")
else:
can.save(args.output)
def cmd_cmp(args):
print "Compare function is comming soon!"
if __name__ == '__main__':
parse_args()