-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.awk
executable file
·275 lines (238 loc) · 6.05 KB
/
parse.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
#!/usr/bin/gawk -f
BEGIN {
FS = "\t";
last_pgn = -1;
n_fields = 0;
c_header_outfile = "j1939_msg.h"
c_decode_outfile = "j1939_decode.c"
c_print_outfile = "j1939_print.c"
# TCL
printf "set ::all_pgn {\n"
printf "#include \"%s\"\n", c_header_outfile > c_decode_outfile;
printf "\n" > c_decode_outfile;
printf "int j1939_decode(unsigned int type, uint64_t data, struct j1939_msg *msg)\n" > c_decode_outfile;
printf "{\n" > c_decode_outfile;
printf "\tmsg->type = type;\n" > c_decode_outfile;
printf "\tswitch (msg->type) {\n" > c_decode_outfile;
printf "#include <stdio.h>\n" > c_print_outfile;
printf "#include \"%s\"\n", c_header_outfile > c_print_outfile;
printf "#include \"j1939_slot.h\"\n" > c_print_outfile;
printf "\n" > c_print_outfile;
printf "\tstatic char nastr[] = \" NA\";" > c_print_outfile;
printf "\n" > c_print_outfile;
printf "void j1939_print(FILE *fp, struct j1939_msg *msg)\n" > c_print_outfile;
printf "{\n" > c_print_outfile;
printf "\tchar *ptr, buf[128];\n" > c_print_outfile;
printf "\tswitch (msg->type) {\n" > c_print_outfile;
}
END {
pgn_footer();
printf "\tdefault:\n" > c_decode_outfile;
printf "\t\treturn -1;\n" > c_decode_outfile;
printf "\t}\n" > c_decode_outfile;
printf "\treturn 0;\n" > c_decode_outfile;
printf "}\n" > c_decode_outfile;
printf "\t}\n" > c_print_outfile;
printf "}\n" > c_print_outfile;
# TCL
printf "}\n"
}
function bitlen(input, num, unit)
{
split(input, tmp, " ");
num = tmp[1];
unit = tmp[2];
if (unit == "bit" || unit == "bits") {
return num;
} else if (unit == "byte" || unit == "bytes") {
return 8 * num;
}
printf "UNEXPECTED SIZE: %s\n", input;
}
function bitpos(input, byte, bit)
{
split(input, tmp, ",");
input = tmp[1];
if (match(input, "\\.")) {
split(input, tmp, "\\.");
byte = tmp[1];
bit = tmp[2];
byte--;
bit--;
} else if (match(input, "-")) {
split(input, tmp, "-");
byte = tmp[1];
byte--;
bit = 0;
} else {
byte = input;
byte--;
bit = 0;
}
return bit + 8 * byte;
}
function print_protect(x, indent, i)
{
for (i = 0; i < indent; i++) {
printf "\t";
}
if (match(x, " ") || x == "") {
printf "{%s}", x;
} else {
printf "%s", x;
}
printf "\n";
}
function pg_print_protect(x)
{
print_protect(x, 1);
}
function sp_print_protect(x)
{
print_protect(x, 2);
}
function pgn_header()
{
# TCL
printf "{\n";
pg_print_protect(pgn);
pg_print_protect(pg_label);
pg_print_protect(pg_name);
pg_print_protect(pg_desc);
pg_print_protect(edp);
pg_print_protect(dp);
pg_print_protect(pf);
pg_print_protect(ps);
pg_print_protect(multipacket);
pg_print_protect(txrate);
pg_print_protect(datalen);
pg_print_protect(priority);
printf "\t{\n";
#
# FOREACH PGN
#
printf "\tcase %s:\n", pg_name > c_decode_outfile;
printf "\tcase %s:\n", pg_name > c_print_outfile;
printf "\t\tfprintf(fp, \"* %s -- %s\\n\");\n", \
pg_name, pg_label > c_print_outfile;
}
function pgn_footer( i, label)
{
if (last_pgn == -1) {
return;
}
# TCL
printf "\t}\n";
printf "}\n";
#
# FOREACH SPN
#
for (i = 0; i < n_fields; i++) {
printf "\t\tmsg->%s.spn%d = (data >> SPN%d_SHIFT) & SPN%d_MASK;\n", \
tolower(pg_name), f_spn[i], f_spn[i], f_spn[i] > c_decode_outfile;
printf "\t\tif (msg->%s.spn%d == SPN%d_MASK) {\n", \
tolower(pg_name), f_spn[i], f_spn[i] > c_print_outfile;
printf "\t\t\tptr = nastr;\n" > c_print_outfile;
printf "\t\t} else {\n" > c_print_outfile;
printf "\t\t\t%s(msg->%s.spn%d, buf, sizeof(buf));\n", \
slot_name, tolower(pg_name), f_spn[i] > c_print_outfile;
printf "\t\t\tptr = buf;\n" > c_print_outfile;
printf "\t\t}\n" > c_print_outfile;
# Truncate label to a reasonable length.
label = substr(f_label[i], 1, 50);
if (label != f_label[i]) {
label = label "..."
}
printf "\t\tfprintf(fp, \" %-54s %%s %s 0x%%x\\n\", ptr, msg->%s.spn%d);\n", \
label, units, tolower(pg_name), f_spn[i] > c_print_outfile;
}
printf "\t\tbreak;\n" > c_decode_outfile;
printf "\t\tbreak;\n" > c_print_outfile;
n_fields = 0;
}
function spn_record( len, pos, mask, shift, hex)
{
len = bitlen(sp_length);
pos = bitpos(sp_position);
# Ignore these two that are larger than eight bytes:
# 1198 2-8 56 Anti-theft Random Number
# 1202 2-8 56 Anti-theft Password Representation
if (len > 32) {
return;
}
mask = sprintf("SPN%d_MASK", spn);
shift = sprintf("SPN%d_SHIFT", spn);
hex = int(lshift(1, len)) - 1;
f_spn[n_fields] = spn;
f_label[n_fields] = sp_label;
n_fields++;
# TCL
printf "\t{\n";
sp_print_protect(sp_position);
sp_print_protect(spn);
sp_print_protect(sp_label);
sp_print_protect(sp_desc);
sp_print_protect(sp_length);
sp_print_protect(resolution);
sp_print_protect(offset);
sp_print_protect(datrange);
sp_print_protect(operange);
sp_print_protect(units);
sp_print_protect(slot_id);
sp_print_protect(slot_name);
sp_print_protect(len);
sp_print_protect(pos);
sp_print_protect(mask);
sp_print_protect(shift);
sp_print_protect(hex);
printf "\t}\n";
}
$1 != "Revised" && $15 == 8 && $18 != "" && $19 != "" {
if ($5 != last_pgn) {
pgn_footer();
}
pgn = $5;
pg_label = $6;
pg_name = $7;
pg_desc = $8;
edp = $9;
dp = $10;
pf = $11;
ps = $12;
multipacket = $13;
txrate = $14; # NB text
datalen = $15;
priority = $16;
ref = $17; # uninteresting
sp_position = $18; # byte.bit or byte-byte
spn = $19;
sp_label = $20;
sp_desc = $21;
sp_length = $22; # "2 bits" or "1 byte"
resolution = $23; # NB text
offset = $24; # NB some are number with unit
datrange = $25; # "0 to 8,000.1 rpm"
operange = $26;
units = $27;
slot_id = $28; # index
slot_name = $29; # internal database name
# Some of the names have a / so remove it.
gsub("/", "_", pg_name);
# Some of the labels have "" so remove them.
gsub("\"", "'", sp_label);
# Some units are empty or have %, so fix it.
if (!units) {
units = ";";
}
gsub("%", "%%", units);
# Sanity check for duplicate SPN numbers.
if (spn in all) {
print NR, spn, sp_position;
}
all[spn] = 1;
if (pgn != last_pgn) {
pgn_header();
}
spn_record();
last_pgn = pgn;
}