-
Notifications
You must be signed in to change notification settings - Fork 22
/
dbc2c.awk
executable file
·2183 lines (2100 loc) · 56.6 KB
/
dbc2c.awk
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
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/awk -f
#
# This script parses Vector CAN DBs (.dbc files), such as can be created
# using Vector CANdb++.
#
# A subset of the parsed information is output using a set of templates.
#
# @note
# Pipe the input through "iconv -f CP1252" so GNU AWK doesn't choke
# on non-UTF-8 characters in comments.
# @warning
# Templates are subject to change, which may break the output for
# your use case. To prevent this retain your own copy of the
# templates directory and set the \ref dbc2c_env_TEMPLTES variable.
# Old templates will continue working, though they might cause
# deprecation warnings.
#
# \section dbc2c_env Environment
#
# The script uses certain environment variables.
#
# \subsection dbc2c_env_DEBUG DEBUG
#
# | Value | Effect
# |---------------------|-----------------------------------------
# | 0, "" | Debugging output is deactivated
# | 1, any string != "" | Debugging output to stderr is activated
# | > 1 | Additionally any string read is output
#
# \subsection dbc2c_env_TEMPLTES TEMPLATES
#
# This variable can be used to pass the template directory to the script.
#
# If the \c LIBPROJDIR environment variable is set it defaults to
# <tt>${LIBPROJDIR}/scripts/templates.dbc2c</tt>, otherwise it defaults
# to the relative path <tt>scripts/templates.dbc2c</tt>.
#
# \subsection dbc2c_env_DATE DATE
#
# This can be used to define the date string provided to <tt>header.tpl</tt>.
#
# It defaults to the output of the \c date command.
#
# \section dbc2c_vts Value Tables
#
# Since values in value tables only consist of a number and description,
# the first word of this description is used as a symbolic name for a given
# value.
#
# All non-alhpanumeric characters of this first word will be converted to
# underscores. Redundancies will be resolved by appending the value to the
# word that signifies the name.
#
# This functionality is implemented in the function getUniqueEnum().
#
# \section dbc2c_templates Templates
#
# This section describes the templates that are used by the script
# and the arguments passed to them. Templates are listed in the
# chronological order of use.
#
# \subsection dbc2c_templates_attributes Special Attributes
#
# Some of the arguments provided depend on custom attributes:
# | Template | Argument | Attribute | Object
# |-------------|----------|---------------------|--------------------------
# | sig.tpl | start | GenSigStartValue | Signal
# | msg.tpl | fast | GenMsgCycleTimeFast | Message
# | msg.tpl | cycle | GenMsgCycleTime | Message
# | msg.tpl | delay | GenMsgDelayTime | Message
# | msg.tpl | send | GenMsgSendType | Message
# | timeout.tpl | timeout | GenSigTimeoutTime | Relation (ECU to Signal)
#
# These and more attributes are specified by the
# <b>Vector Interaction Layer</b>.
#
# \subsection dbc2c_templates_data Inserting Data
#
# Templates are arbitrary text files that are provided with a set of
# arguments. Arguments have a symbolic name through which they can be used.
# In the following sections they are called fields, because they are provided
# to the template() function in an associative array.
#
# Inserting data into a template is simple:
#
# <:name:>
#
# The previous example adds the data in the field \c name into the file.
# It can be surrounded by additional context:
#
# #define <:name:> <:value:>
#
# If \c name is "FOO_BAR" and \c value is 1337, this line would be resolved
# to:
#
# #define FOO_BAR 1337
#
# It may be desired to reformat some of those values. A number of special
# filters (see filter()) as well es printf(3) style formatting is available.
# E.g. \c name can be converted to camel case and \c value to hex:
#
# #define <:name:camel:%-16s:> <:value:%#x:>
#
# The output would look like this:
#
# #define fooBar 0x539
#
# An important property of templates is that arguments may contain multiple
# lines. In that case the surrounding text is preserved for every line, which
# is useful to format multiline text or lists. This can be used to create
# lists or provide visual sugar around text:
#
# +----[ <:title:%-50s:> ]----+
# | <:text:%-60s:> |
# +--------------------------------------------------------------+
#
# Output could look like this:
#
# +----[ Racecar by Matt Brown ]----+
# | 'Racecar - Searching for the Limit in Formula SAE' |
# | is available for download from: |
# | http://www.superfastmatt.com/2011/11/book.html |
# +--------------------------------------------------------------+
#
# Multi line data is treated as an array of individual lines. Besides
# descriptions in DBC files multiline data can also originate from lists
# provided by this script in order to allow describing the relations
# between ECUs, messages, signals etc..
#
# In some cases it is prudent to print lines conditionally. For that
# conditionals are provided:
#
# <?name?>
#
# If the reverenced field evaluates to \c true, the conditional is removed
# from the line. If it evaluates to \c false, the entire template line is
# omitted.
#
# \subsection dbc2c_templates_header header.tpl
#
# Used once with the following arguments:
# | Field | Type | Description
# |----------|----------|-------------
# | date | string | The current date
# | db | string[] | A list of identifiers for the parsed DBCs
#
# \subsection dbc2c_templates_file file.tpl
#
# Used for each input file with the following arguments:
# | Field | Type | Description
# |----------|----------|-------------
# | db | string | An identifier for this input file
# | file | string | The file name
# | comment | string[] | The comment text for this CANdb
# | ecu | string[] | A list of ECUs provided with this file
#
# \subsection dbc2c_templates_sigid sigid.tpl
#
# This template should only contain a single line that produces a unique
# identifier string for a signal, using the following arguments:
# | Field | Type | Description
# |----------|----------|-------------
# | msg | int | The message ID
# | msgname | string | The message name
# | sig | string | The signal name
#
# Signal names are not globally unique, thus an identifier must contain
# a message reference to avoid name collisions.
#
# \subsection dbc2c_templates_ecu ecu.tpl
#
# Used for each ECU with the following arguments:
# | Field | Type | Description
# |----------|----------|-------------
# | ecu | string | An identifier for the ECU
# | comment | string[] | The comment text for this ECU
# | db | string | The input file identifier
# | txid | int[] | A list of message IDs belonging to messages sent by this ECU
# | txname | string[] | A list of message names sent by this ECU
# | rx | string[] | A list of signals received by this ECU
# | rxid | string[] | A list of unique signal identifiers received by this ECU
#
# \subsection dbc2c_templates_msg msg.tpl
#
# Used for each message with the following arguments:
# | Field | Type | Description
# |----------|----------|-------------
# | msg | int | The message ID
# | name | string | The message name
# | comment | string[] | The comment text for this message
# | sig | string[] | A list of signal names contained in this message
# | sigid | string[] | A list of signal identifiers contained in this message
# | ecu | string | The ECU sending this message
# | ext | bool | Message ID is extended
# | dlc | int | The data length count
# | cycle | int | The cycle time of this message
# | fast | int | The fast cycle time of this message
# | delay | int | The minimum delay time between two transmissions
# | send | string | The send type (cyclic, spontaneous etc.)
# | sgid | string[] | A list of signal group ids
# | sgname | string[] | A list of signal group names
#
# \subsection dbc2c_templates_siggrp siggrp.tpl
#
# Used for each signal group with the following arguments:
# | Field | Type | Description
# |----------|----------|-------------
# | id | string | The ID of the signal group (created using sigid.tpl)
# | name | string | The name of the signal group
# | msg | int | The ID of the message containing this signal group
# | msgname | string | The name of the message containing this signal group
# | sig | string[] | A list of signals belonging to this signal group
# | sigid | string[] | A list of signal identifers belonging to this signal group
#
# \subsection dbc2c_templates_sig sig.tpl
#
# Used for each signal with the following arguments:
# | Field | Type | Description
# |----------|----------|-------------
# | name | string | The signal name
# | id | string | The unique signal identifier created with sigid.tpl
# | comment | string[] | The comment text for this signal
# | enum | bool | Indicates whether this signal has a value table
# | msg | int | The ID of the message sending this signal
# | sgid | string[] | The signal groups containing this signal
# | sgname | string[] | The names of the signal groups containing this signal
# | ecu | string[] | A list of the ECUs receiving this signal
# | intel | bool | Intel (little endian) style signal
# | motorola | bool | Motorola (big endian) style signal
# | signed | bool | The signal is signed
# | sbit | int | The start bit (meaning depends on endianess)
# | len | int | The signal length
# | start | int | The initial (default) signal value (raw)
# | calc16 | string[] | A rational conversion function (see \ref dbc2c_templates_sig_calc16)
# | min | int | The raw minimum value
# | max | int | The raw maximum value
# | off | int | The raw offset value
# | getbuf | string[] | The output of <tt>sig_getbuf.tpl</tt>
# | setbuf | string[] | The output of <tt>sig_setbuf.tpl</tt>
#
# \subsubsection dbc2c_templates_sig_calc16 calc16
#
# A rational conversion function for the raw signal value \c x
# and formatting factor \c fmt into a real value as defined
# by the linear factor and offset in the DBC, this function
# uses up to 16bit integers.
#
# \subsubsection dbc2c_templates_sig_buf sig_getbuf.tpl, sig_setbuf.tpl
#
# These templates can be used to construct static byte wise signal getters
# and setters.
#
# For signed signals <tt>sig_getbuf.tpl</tt> is first called with the
# following arguments:
# | Field | Type | Description
# |----------|----------|-------------
# | sign | string | "-"
# | byte | int | The byte containing the most significant bit
# | align | int | The position of the most significant bit in the byte
# | msk | int | 1
# | pos | int | The position in front of the entire read signal
# | int8 | bool | Indicates whether an 8 bit integer suffices to contain the signal
# | int16 | bool | Indicates whether a 16 bit integer suffices to contain the signal
# | int32 | bool | Indicates whether a 32 bit integer suffices to contain the signal
#
# These arguments can be used to duplicate the signed bit and shift it
# in front.
#
# Both templates are used for each touched signal byte with the following
# arguments:
# | Field | Type | Description
# |----------|----------|-------------
# | sign | string | "+"
# | byte | int | The signal byte
# | align | int | The least significant bit within the byte belonging to the signal
# | msk | int | A bit mask to mask the aligned signal bits
# | pos | int | The position to shift the resulting bits to
# | int8 | bool | Indicates whether an 8 bit integer suffices to address the desired bit
# | int16 | bool | Indicates whether a 16 bit integer suffices to address the desired bit
# | int32 | bool | Indicates whether a 32 bit integer suffices to address the desired bit
#
# \subsubsection dbc2c_templates_sig_enum sig_enum.tpl, sig_enumval.tpl
#
# In case a value table is assigned to the signal, <tt>sig_enum.tpl</tt> is
# called with all the arguments provided to <tt>sig.tpl</tt>.
#
# For each entry in the value table <tt>sig_enumval.tpl</tt> is called
# with these additional arguments:
# | Field | Type | Description
# |----------|----------|-------------
# | enumval | int | The value
# | enumname | string | The name of the value
# | comment | string[] | The comment part of the value description
#
# \subsection dbc2c_templates_timeout timeout.tpl
#
# Used for each timeout with the following arguments:
# | Field | Type | Description
# |----------|----------|-------------
# | ecu | string | The ECU that times out
# | sig | string | The signal that is expected by the ECU
# | sigid | string | The unique identifier for the expected signal
# | timeout | int | The timeout time
# | msg | int | The ID of the CAN message containing the signal
# | msgname | string | The name of the CAN message containing the signal
#
# \subsection dbc2c_templates_enum enum.tpl
#
# Invoked for every value table with the following arguments:
# | Field | Type | Description
# |----------|----------|-------------
# | enum | string | The name of the value table
# | db | string | The name of the CAN DB this enum was defined in
#
# \subsubsection dbc2c_templates_enumval enumval.tpl
#
# Invoked for every value defined in a value table. All the template arguments
# for \c enum.tpl are available in addition to the following arguments:
# | Field | Type | Description
# |----------|----------|-------------
# | val | int | The value
# | name | string | The symbolic name for the value
# | comment | string[] | The comment part of the value description
#
##
# Initialises globals.
#
BEGIN {
# Environment variables
DEBUG = (DEBUG ? DEBUG : ENVIRON["DEBUG"])
TEMPLATES = (TEMPLATES ? TEMPLATES : ENVIRON["TEMPLATES"])
DATE = (DATE ? DATE : ENVIRON["DATE"])
# Template directory
if (!TEMPLATES) {
path = ENVIRON["LIBPROJDIR"]
sub(/.+/, "&/", path)
TEMPLATES = path "scripts/templates.dbc2c"
}
sub(/\/?$/, "/", TEMPLATES)
# Generating date
if (!DATE) {
"date" | getline DATE
close("date")
}
FILENAME = "/dev/stdin"
# Regexes for different types of data
rLF = "\n"
rFLOAT = "-?[0-9]+(\\.[0-9]+)?([eE][-+]?[0-9]+)?"
rINT = "-?[0-9]+"
rID = "[0-9]+"
rDLC = "[0-9]+"
rSEP = "[:;,]"
rSYM = "[a-zA-Z0-9_]+"
rSYMS = "(" rSYM ",)*" rSYM
rSIG = "[0-9]+\\|[0-9]+@[0-9]+[-+]"
rVEC = "\\(" rFLOAT "," rFLOAT "\\)"
rBND = "\\[" rFLOAT "\\|" rFLOAT "\\]"
rSTR = "\"([^\"]|\\\\.)*\"" # CANdb++ does not support escaping
# characters like ", however it parses
# them just fine.
# Type strings
tDISCARD["BS_"]
t["SIG_ENUM"] = "VAL_"
t["ENUM"] = "VAL_TABLE_"
t["SYMBOLS"] = "NS_"
t["VER"] = "VERSION"
t["ECU"] = "BU_"
t["MSG"] = "BO_"
t["SIG"] = "SG_"
t["ENV"] = "EV_"
t["COM"] = "CM_"
t["ATTRDEFAULT"] = "BA_DEF_DEF_"
t["RELATTRDEFAULT"] = "BA_DEF_DEF_REL_"
t["ATTRRANGE"] = "BA_DEF_"
t["RELATTRRANGE"] = "BA_DEF_REL_"
t["ATTR"] = "BA_"
t["RELATTR"] = "BA_REL_"
t["EDLC"] = "ENVVAR_DATA_"
t["TX"] = "BO_TX_BU_"
t["SIG_GRP"] = "SIG_GROUP_"
# Relationship symbols
t["ECU_SIG"] = "BU_SG_REL_"
t["ECU_ENV"] = "BU_EV_REL_"
t["ECU_MSG"] = "BU_BO_REL_"
# Not implemented!
# This is marked by the symbol being indexed with its literal name.
t["NS_DESC_"] = "NS_DESC_"
t["CAT_DEF_"] = "CAT_DEF_"
t["CAT_"] = "CAT_"
t["FILTER"] = "FILTER"
t["EV_DATA_"] = "EV_DATA_"
t["SGTYPE_"] = "SGTYPE_"
t["SGTYPE_VAL_"] = "SGTYPE_VAL_"
t["BA_DEF_SGTYPE_"] = "BA_DEF_SGTYPE_"
t["BA_SGTYPE_"] = "BA_SGTYPE_"
t["SIG_TYPE_REF_"] = "SIG_TYPE_REF_"
t["SIG_VALTYPE_"] = "SIG_VALTYPE_"
t["SIGTYPE_VALTYPE_"] = "SIGTYPE_VALTYPE_"
t["SG_MUL_VAL_"] = "SG_MUL_VAL_"
# Attribute types
atSTR = "STRING"
atENUM = "ENUM"
atINT = "INT"; atNUM[atINT]
atFLOAT = "FLOAT"; atNUM[atFLOAT]
atHEX = "HEX"; atNUM[atHEX]
# Environment variable types
etINT = "INT"
etFLOAT = "FLOAT"
etDATA = "DATA"
eTYPE[0] = etINT
eTYPE[1] = etFLOAT
# Prominent attributes
aSTART = "GenSigStartValue"
aFCYCLE = "GenMsgCycleTimeFast"
aCYCLE = "GenMsgCycleTime"
aDELAY = "GenMsgDelayTime"
aSEND = "GenMsgSendType"
aTIMEOUT = "GenSigTimeoutTime"
# Global error indicator
errno = 0
}
##
# Strip DOS line endings and make sure there is a new line symbol at the
# end of the line, so multiline definitions can be parsed.
#
{
gsub(/[\r\n]*$/, "\n")
}
##
# Prints an error message on stderr and exits.
#
# @param no
# The number to set errno to
# @param msg
# The error message
#
function error(no, msg) {
errno = no
print "dbc2c.awk: ERROR: " FILENAME "(" NR "): " msg > "/dev/stderr"
exit
}
##
# Prints a warning message on stderr.
#
# @param msg
# The message to print
#
function warn(msg) {
print "dbc2c.awk: WARNING: " FILENAME "(" NR "): " msg > "/dev/stderr"
}
##
# Prints a debugging message on stderr.
#
# The debugging message is only printed if DEBUG is set.
#
# @param msg
# The message to print
#
function debug(msg) {
if (DEBUG) {
print "dbc2c.awk: " msg > "/dev/stderr"
}
}
##
# Makes sure $0 is not empty.
#
function buffer() {
sub(/^[ ]*/, "")
if (!$0) {
getline
gsub(/[\r\n]*$/, "\n")
sub(/^[ ]*/, "")
}
}
##
# Special function to fetch a string from the buffer.
#
# This is a special case, because strings may span multiple lines.
# This function supports strings with up to 256 lines.
#
# @return
# The fetched string
#
function fetchStr(dummy,
str,i) {
buffer()
if ($0 !~ /^"/) {
return ""
}
# Assume strings are no longer than 256 lines
while ($0 !~ "^(" rSTR ")" && i++ < 256) {
getline str
gsub(/[\r\n]*$/, "\n", str)
$0 = $0 str
}
return strip(fetch(rSTR))
}
##
# Fetch the next token from the input buffer, matching a given type.
#
# @param types
# A regular expression describing the type of data to be fetched
# @return
# The fetched string of data
#
function fetch(types,
str, re) {
buffer()
if (match($0, "^(" types ")")) {
str = substr($0, RSTART, RLENGTH)
# Cut str from the beginning of $0
$0 = substr($0, RSTART + RLENGTH)
}
if (DEBUG > 1 && str !~ /^[ \t\n]*$/) {
debug("fetch: " str)
}
if (str) {
fetch_loop_detect = 0
} else if (fetch_loop_detect++ >= 100) {
error(11, "infinite loop detected!")
}
return str
}
##
# Returns the expresion with ^ and $ at beginning and end to make ~ match
# entire strings only.
#
# @param re
# The expression to wrap
# @return
# An expression for matching entire strings
#
function whole(re) {
return "^(" re ")$"
}
##
# Remove quotes and escapes from strings.
#
# This function is used by fetchStr().
#
# @param str
# The string to unescape
# @return
# The litreal string
#
function strip(str) {
sub(/^"/, "", str)
sub(/"$/, "", str)
# Unescape "
gsub(/\\"/, "\"", str)
return str
}
##
# Returns the context type for a string.
#
# @param str
# The string to interpret
# @retval "sig"
# The context is a signal
# @retval "msg"
# The context is a message
# @retval "ecu"
# The context is an ECU
# @retval "env"
# The context is an environment variable
# @retval "db"
# The context is the DB
#
function getContext(str) {
if (str == t["SIG"]) {
return "sig"
}
if (str == t["MSG"]) {
return "msg"
}
if (str == t["ECU"]) {
return "ecu"
}
if (str == t["ENV"]) {
return "env"
}
if (str == "") {
return "db"
}
error(2, "unknown context " str " encountered")
}
##
# Generates a unique name for a value table entry.
#
# Updates:
# - obj_enum_count[enum, name] = (int)
#
# Sets the following fields in the given array:
# - name: A unique identifier
# - desc: The description
# - invalid: No valid identifier was in the description (bool)
# - duplicate: The identifier was already in use (bool)
#
# @param ret
# An array to return the data set in
# @param enum
# The identifier of the value table
# @param val
# The value
# @param desc
# The description string to fetch a name from
#
function getUniqueEnum(ret, enum, val, desc,
name) {
name = desc
ret["invalid"] = 0
ret["duplicate"] = 0
sub(/[ \t\r\n].*/, "", name)
if (name !~ /^[a-zA-Z0-9_]*$/) {
warn("Invalid identifier '" name "' for value " sprintf("%#x", val) " in table " enum)
gsub(/[^a-zA-Z0-9_]/, "_", name)
warn("Replaced with '" name "'")
ret["invalid"] = 1
} else {
# Name is valid, remove it from the description
sub(/^[^ \t\r\n]+[ \t\r\n]*/, "", desc)
}
while (obj_enum_count[enum, name]++) {
warn("Identifier '" name "' for value " sprintf("%#x", val) " in table " enum " already in use")
name = name "_" sprintf("%X", val)
warn("Replaced with '" name "'")
ret["duplicate"] = 1
}
ret["name"] = name
ret["desc"] = desc
}
##
# Discards buffered symbols until an empty line is encountered.
#
# This is used to skip the list of supported symbols at the beginning of a
# dbc file.
#
function fsm_discard() {
fetch(":")
fetch(rLF)
while(fetch(rSYM "|" rLF) !~ whole(rLF)) {
fetch(rLF)
}
}
##
# Parse an ECU definition.
#
# Token: BU_
#
# Creates:
# - * ind_ecu[cnt_ecu++] = ecu
# - * obj_ecu[ecu]
# - * obj_ecu_db[ecu] = FILENAME
# - * obj_db_ecu[FILENAME, p] = ecu
#
function fsm_ecu(dummy,
ecu, p) {
fetch(":")
ecu = fetch(rSYM "|" rLF)
while (ecu !~ whole(rLF)) {
ind_ecu[cnt_ecu++] = ecu
obj_ecu[ecu]
obj_ecu_db[ecu] = FILENAME
p = 0
while (obj_db_ecu[FILENAME, p++]);
obj_db_ecu[FILENAME, --p] = ecu
ecu = fetch(rSYM "|" rLF)
}
}
##
# Parse a value table.
#
# Token: VAL_TABLE_
#
# Creates:
# - 1 ind_enum[cnt_enum++] = enum
# - 1 obj_enum_db[enum] = FILENAME
# - * obj_enum_val[enum, i] = val
# - * obj_enum_name[enum, i] = name
# - * obj_enum_desc[enum, i] = desc
# - * obj_enum_invalid[enum, i] = (bool)
# - * obj_enum_duplicate[enum, i] = (bool)
#
function fsm_enum(dummy,
enum,
val, a,
i) {
enum = fetch(rSYM)
ind_enum[cnt_enum++] = enum
obj_enum_db[enum] = FILENAME
val = fetch(rINT "|;")
i = 0
while (val != ";") {
obj_enum_val[enum, i] = val
delete a
getUniqueEnum(a, enum, val, fetchStr())
obj_enum_name[enum, i] = a["name"]
obj_enum_desc[enum, i] = a["desc"]
obj_enum_invalid[enum, i] = a["invalid"]
obj_enum_duplicate[enum, i] = a["duplicate"]
++i
val = fetch(rINT "|;")
}
fetch(rLF)
}
##
# Parse a value table bound to a signal.
#
# Token: VAL_
#
# Creates:
# - 1 obj_sig_enum[msgid, sig]
# - * obj_sig_enum_val[msgid, sig, i] = val
# - * obj_sig_enum_name[msgid, sig, i] = name
# - * obj_sig_enum_desc[enum, i] = desc
# - * obj_sig_enum_invalid[enum, i] = (bool)
# - * obj_sig_enum_duplicate[enum, i] = (bool)
#
function fsm_sig_enum(dummy,
msgid, sig,
val, a,
i) {
msgid = fetch(rID)
sig = fetch(rSYM)
obj_sig_enum[msgid, sig]
val = fetch(rINT "|;")
i = 0
while (val != ";") {
obj_sig_enum_val[msgid, sig, i] = val
delete a
getUniqueEnum(a, msgid SUBSEP sig, val, fetchStr())
obj_sig_enum_name[msgid, sig, i] = a["name"]
obj_sig_enum_desc[msgid, sig, i] = a["desc"]
obj_sig_enum_invalid[msgid, sig, i] = a["invalid"]
obj_sig_enum_duplicate[msgid, sig, i] = a["duplicate"]
++i
val = fetch(rINT "|;")
}
fetch(rLF)
}
##
# Parse an environment variable.
#
# Token: EV_
#
# Creates:
# - 1 ind_env[cnt_env++] = name
# - 1 obj_env[name] = val
# - 1 obj_env_type[name] = ("INT"|"FLOAT"|"DATA")
# - 1 obj_env_min[name] = (float)
# - 1 obj_env_max[name] = (float)
# - 1 obj_env_unit[name] = (string)
#
function fsm_env(dummy,
name, a) {
name = fetch(rSYM)
debug("obj_env[" name "]")
ind_env[cnt_env++] = name
fetch(":")
obj_env_type[name] = eTYPE[fetch(rID)]
split(fetch(rBND), a, /[][|]/)
obj_env_min[name] = a[2]
obj_env_max[name] = a[3]
obj_env_unit[name] = fetchStr()
if (obj_env_type[name] == etINT) {
obj_env[name] = int(fetch(rFLOAT))
} else if (obj_env_type[name] == etFLOAT) {
obj_env[name] = fetch(rFLOAT)
} else {
error(3, "environment variable type '" obj_env_type[name] "' not implemented!")
}
fetch(rID) # Just a counter of environment variables
fetch(rSYM) # DUMMY_NODE_VECTOR0
fetch(rSYM) # Vector__XXX TODO add support for ECUs
fetch(";")
}
##
# Parse the data length count of DATA type environment variables.
#
# Token: ENVVAR_DATA_
#
# Creates:
# - 1 obj_env_dlc[name] = (int)
#
function fsm_env_data(dummy,
name) {
name = fetch(rSYM)
debug("obj_env_dlc[" name "]")
fetch(":")
obj_env_dlc[name] = int(fetch(rDLC))
fetch(";")
}
##
# Parse a message definition.
#
# Token: BO_
#
# Creates:
# - 1 ind_msg[cnt_msg++] = id
# - 1 obj_msg_name[id] = name
# - 1 obj_msg_dlc[id] = dlc
# - 1 obj_msg_tx[id] = ecu
# - 1 obj_ecu_tx[ecu, i] = id
#
function fsm_msg(dummy,
id,
name,
dlc,
ecu,
i) {
id = fetch(rID) # Do not cast to int to preserve full value
debug("obj_msg[" id "]")
name = fetch(rSYM)
fetch(":")
dlc = int(fetch(rDLC))
ecu = fetch(rSYM)
fetch(rLF)
ind_msg[cnt_msg++] = id
obj_msg_name[id] = name
obj_msg_dlc[id] = dlc
if (ecu in obj_ecu) {
obj_msg_tx[id] = ecu
while (obj_ecu_tx[ecu, i++]);
obj_ecu_tx[ecu, --i] = id
}
while (fetch(rSYM) == t["SIG"]) {
fsm_sig(id)
}
}
##
# Parse a signal definition.
#
# Token: SG_
#
# Creates:
# - 1 ind_sig[cnt_sig++] = msgid, name
# - 1 obj_sig_name[msgid, name] = name
# - 1 obj_sig_msgid[msgid, name] = msgid
# - 1 obj_sig_multiplexor[msgid, name] = (bool)
# - 1 obj_sig_multiplexed[msgid, name] = (int)
# - 1 obj_sig_sbit[msgid, name] = (uint)
# - 1 obj_sig_len[msgid, name] = (uint)
# - 1 obj_sig_intel[msgid, name] = (bool)
# - 1 obj_sig_signed[msgid, name] = (bool)
# - 1 obj_sig_fac[msgid, name] = (float)
# - 1 obj_sig_off[msgid, name] = (float)
# - 1 obj_sig_min[msgid, name] = (float)
# - 1 obj_sig_max[msgid, name] = (float)
# - 1 obj_sig_unit[msgid, name] = (string)
# - * obj_sig_rx[msgid, name, i] = ecu
# - * obj_ecu_rx[ecu, p] = msgid, name
# - * obj_msg_sig[msgid, p] = msgid, name
#
function fsm_sig(msgid,
name,
multiplexing,
a,
ecu, count, i, p) {
name = fetch(rSYM)
debug("obj_sig[" msgid ", " name "]")
ind_sig[cnt_sig++] = msgid SUBSEP name
obj_sig_name[msgid, name] = name
obj_sig_msgid[msgid, name] = msgid
while (obj_msg_sig[msgid, p++]);
obj_msg_sig[msgid, --p] = msgid SUBSEP name
multiplexing = fetch("m[0-9]+|M")
obj_sig_multiplexor[msgid, name] = (multiplexing == "M")
gsub(/[mM]/, "", multiplexing)
obj_sig_multiplexed[msgid, name] = multiplexing
fetch(":")
split(fetch(rSIG), a, /[|@]/)
obj_sig_sbit[msgid, name] = a[1]
obj_sig_len[msgid, name] = a[2]
obj_sig_intel[msgid, name] = (a[3] ~ /^1/)
obj_sig_signed[msgid, name] = (a[3] ~ /-$/)
split(fetch(rVEC), a, /[(),]/)
obj_sig_fac[msgid, name] = a[2]
obj_sig_off[msgid, name] = a[3]
split(fetch(rBND), a, /[][|]/)
obj_sig_min[msgid, name] = a[2]
obj_sig_max[msgid, name] = a[3]
obj_sig_unit[msgid, name] = fetchStr()
count = split(fetch(rSYMS), a, /,/)
for (ecu = 1; ecu <= count; ++ecu) {
if (a[ecu] in obj_ecu) {
obj_sig_rx[msgid, name, i++] = a[ecu]
p = 0
while (obj_ecu_rx[a[ecu], p++]);
obj_ecu_rx[a[ecu], --p] = msgid SUBSEP name
}
}
fetch(rLF)
}
##
# Parse comments.
#
# Token: CM_
#
# Creates one of:
# - 1 obj_db_comment[FILENAME]
# - 1 obj_ecu_comment[name]
# - 1 obj_env_comment[name]
# - 1 obj_msg_comment[msgid]
# - 1 obj_sig_comment[msgid, name]
#
function fsm_comment(dummy,
context, name, msgid, str) {
context = getContext(fetch(rSYM))
if (context == "db") {
obj_db_comment[FILENAME] = fetchStr()
} else if (context == "env") {
name = fetch(rSYM)
obj_env_comment[name] = fetchStr()
} else if (context == "ecu") {
name = fetch(rSYM)
obj_ecu_comment[name] = fetchStr()
} else if (context == "msg") {
msgid = fetch(rID)
obj_msg_comment[msgid] = fetchStr()
} else if (context == "sig") {
msgid = fetch(rID)
name = fetch(rSYM)
obj_sig_comment[msgid, name] = fetchStr()
} else {
error(10, "comment for '" context "' not implemented!")
}
fetch(";")
fetch(rLF)
}
##
# Parse a custom attribute definition.
#
# Token: BA_DEF_
#
# Creates:
# - 1 ind_attr[cnt_attr++] = name
# - 1 obj_attr_context[name] = ("sig"|"msg"|"ecu"|"env"|"db")
# - 1 obj_attr_type[name] = ("INT"|"ENUM"|"STRING")
# - ? obj_attr_min[name] = (float)
# - ? obj_attr_max[name] = (float)
# - * obj_attr_enum[name, i] = (string)
# - ? obj_attr_str[name] = (string)
#
function fsm_attrrange(dummy,
context,
name,
type,
val,
i) {
context = getContext(fetch(rSYM))
name = fetchStr()
obj_attr_context[name] = context
ind_attr[cnt_attr++] = name
type = fetch(rSYM)
obj_attr_type[name] = type
if (type in atNUM) {
obj_attr_min[name] = fetch(rFLOAT)
obj_attr_max[name] = fetch(rFLOAT)
} else if (type == atENUM) {
val = fetchStr()
while (val != "") {
obj_attr_enum[name, ++i] = val
fetch(",")