Skip to content

Commit 008f9f8

Browse files
committed
asc2log: add option to disable direction info
The ASC file provides an information whether the CAN frame is sent (Tx) or received (Rx) on the local CAN node. With the new '-d' option the generation of this information is disabled for the log file generation to simplify the comparision of log files. E.g. log1 -> asc -> log2 always created the T/R information in the log2 file which is then hard to compare to the log1 file which might not have the T/R information at all. Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
1 parent 66e7c08 commit 008f9f8

1 file changed

Lines changed: 50 additions & 54 deletions

File tree

asc2log.c

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include "lib.h"
6060

6161
#define BUFLEN 6500 /* CAN XL mode lines can be pretty long */
62+
#define NO_DIR '.'
6263

6364
/* relevant flags in Flags field */
6465
#define ASC_F_RTR 0x00000010
@@ -78,10 +79,12 @@ static void print_usage(char *prg)
7879
fprintf(stderr, "Options:\n");
7980
fprintf(stderr, "\t-I <infile>\t(default stdin)\n");
8081
fprintf(stderr, "\t-O <outfile>\t(default stdout)\n");
82+
fprintf(stderr, "\t-d (disable direction information R/T)\n");
83+
fprintf(stderr, "\t-v (verbose)\n");
8184
}
8285

8386
static void prframe(FILE *file, struct timeval *tv, int dev,
84-
cu_t *cf, char *extra_info)
87+
cu_t *cf, char dir)
8588
{
8689
static char abuf[BUFLEN];
8790

@@ -93,7 +96,11 @@ static void prframe(FILE *file, struct timeval *tv, int dev,
9396
fprintf(file, "canX ");
9497

9598
snprintf_canframe(abuf, sizeof(abuf), cf, 0);
96-
fprintf(file, "%s%s", abuf, extra_info);
99+
100+
if (dir == NO_DIR)
101+
fprintf(file, "%s\n", abuf);
102+
else
103+
fprintf(file, "%s %c\n", abuf, dir);
97104
}
98105

99106
static void get_can_id(canid_t *can_id, char *idstring, int base)
@@ -140,7 +147,7 @@ static void calc_tv(struct timeval *tv, struct timeval *read_tv,
140147
}
141148

142149
static void eval_can(char* buf, struct timeval *date_tvp, char timestamps,
143-
char base, int dplace, FILE *outfile)
150+
char base, int dplace, int disable_dir, FILE *outfile)
144151
{
145152
int interface;
146153
static struct timeval tv; /* current frame timestamp */
@@ -153,7 +160,6 @@ static void eval_can(char* buf, struct timeval *date_tvp, char timestamps,
153160
int data[8];
154161
char idstr[21];
155162
char dir[5]; /* 'Rx'/'Tx'/'TxRq' plus terminating zero */
156-
char *extra_info;
157163
int i, items;
158164
unsigned long long sec, usec;
159165

@@ -171,7 +177,7 @@ static void eval_can(char* buf, struct timeval *date_tvp, char timestamps,
171177
cf.len = CAN_ERR_DLC;
172178

173179
calc_tv(&tv, &read_tv, date_tvp, timestamps, dplace);
174-
prframe(outfile, &tv, interface, (cu_t *)&cf, "\n");
180+
prframe(outfile, &tv, interface, (cu_t *)&cf, NO_DIR);
175181
fflush(outfile);
176182
return;
177183
}
@@ -225,18 +231,16 @@ static void eval_can(char* buf, struct timeval *date_tvp, char timestamps,
225231
if (strlen(dir) != 2) /* "Rx" or "Tx" */
226232
return;
227233

234+
if (disable_dir)
235+
dir[0] = NO_DIR;
236+
228237
/* check for signed integer overflow */
229238
if (dplace == 4 && read_tv.tv_usec >= INT_MAX / 100)
230239
return;
231240

232241
if (dplace == 5 && read_tv.tv_usec >= INT_MAX / 10)
233242
return;
234243

235-
if (dir[0] == 'R')
236-
extra_info = " R\n";
237-
else
238-
extra_info = " T\n";
239-
240244
cf.len = len;
241245
if (rtr == 'r')
242246
cf.can_id |= CAN_RTR_FLAG;
@@ -245,13 +249,13 @@ static void eval_can(char* buf, struct timeval *date_tvp, char timestamps,
245249
cf.data[i] = data[i] & 0xFFU;
246250

247251
calc_tv(&tv, &read_tv, date_tvp, timestamps, dplace);
248-
prframe(outfile, &tv, interface, (cu_t *)&cf, extra_info);
252+
prframe(outfile, &tv, interface, (cu_t *)&cf, dir[0]);
249253
fflush(outfile);
250254
}
251255
}
252256

253257
static void eval_canfd(char* buf, struct timeval *date_tvp, char timestamps,
254-
int dplace, FILE *outfile)
258+
int dplace, int disable_dir, FILE *outfile)
255259
{
256260
int interface;
257261
static struct timeval tv; /* current frame timestamp */
@@ -262,7 +266,6 @@ static void eval_canfd(char* buf, struct timeval *date_tvp, char timestamps,
262266
int dlc, dlen = 0;
263267
char idstr[21];
264268
char dir[5]; /* 'Rx'/'Tx'/'TxRq' plus terminating zero */
265-
char *extra_info;
266269
char *ptr;
267270
int i;
268271
int n = 0; /* sscanf consumed characters */
@@ -304,6 +307,9 @@ static void eval_canfd(char* buf, struct timeval *date_tvp, char timestamps,
304307
if (strlen(dir) != 2) /* "Rx" or "Tx" */
305308
return;
306309

310+
if (disable_dir)
311+
dir[0] = NO_DIR;
312+
307313
/* check for signed integer overflow */
308314
if (dplace == 4 && read_tv.tv_usec >= INT_MAX / 100)
309315
return;
@@ -312,11 +318,6 @@ static void eval_canfd(char* buf, struct timeval *date_tvp, char timestamps,
312318
if (dplace == 5 && read_tv.tv_usec >= INT_MAX / 10)
313319
return;
314320

315-
if (dir[0] == 'R')
316-
extra_info = " R\n";
317-
else
318-
extra_info = " T\n";
319-
320321
/* don't trust ASCII content - sanitize data length */
321322
if (dlen != can_fd_dlc2len(can_fd_len2dlc(dlen)))
322323
return;
@@ -373,14 +374,14 @@ static void eval_canfd(char* buf, struct timeval *date_tvp, char timestamps,
373374
}
374375

375376
calc_tv(&tv, &read_tv, date_tvp, timestamps, dplace);
376-
prframe(outfile, &tv, interface, (cu_t *)&cf, extra_info);
377+
prframe(outfile, &tv, interface, (cu_t *)&cf, dir[0]);
377378
fflush(outfile);
378379

379380
/* No support for really strange CANFD ErrorFrames format m( */
380381
}
381382

382383
static void eval_canxl_cc(char* buf, struct timeval *date_tvp, char timestamps,
383-
int dplace, FILE *outfile)
384+
int dplace, int disable_dir, FILE *outfile)
384385
{
385386
int interface;
386387
static struct timeval tv; /* current frame timestamp */
@@ -391,7 +392,6 @@ static void eval_canxl_cc(char* buf, struct timeval *date_tvp, char timestamps,
391392
int dlc, dlen = 0;
392393
char idstr[21];
393394
char dir[5]; /* 'Rx'/'Tx'/'TxRq' plus terminating zero */
394-
char *extra_info;
395395
char *ptr;
396396
int i;
397397
int n = 0; /* sscanf consumed characters */
@@ -435,6 +435,9 @@ static void eval_canxl_cc(char* buf, struct timeval *date_tvp, char timestamps,
435435
if (strlen(dir) != 2) /* "Rx" or "Tx" */
436436
return;
437437

438+
if (disable_dir)
439+
dir[0] = NO_DIR;
440+
438441
/* check for signed integer overflow */
439442
if (dplace == 4 && read_tv.tv_usec >= INT_MAX / 100)
440443
return;
@@ -443,11 +446,6 @@ static void eval_canxl_cc(char* buf, struct timeval *date_tvp, char timestamps,
443446
if (dplace == 5 && read_tv.tv_usec >= INT_MAX / 10)
444447
return;
445448

446-
if (dir[0] == 'R')
447-
extra_info = " R\n";
448-
else
449-
extra_info = " T\n";
450-
451449
get_can_id(&cf.can_id, idstr, 16);
452450

453451
ptr = buf + n; /* start of ASCII hex frame data */
@@ -489,12 +487,12 @@ static void eval_canxl_cc(char* buf, struct timeval *date_tvp, char timestamps,
489487
cf.len8_dlc = dlc;
490488

491489
calc_tv(&tv, &read_tv, date_tvp, timestamps, dplace);
492-
prframe(outfile, &tv, interface, (cu_t *)&cf, extra_info);
490+
prframe(outfile, &tv, interface, (cu_t *)&cf, dir[0]);
493491
fflush(outfile);
494492
}
495493

496494
static void eval_canxl_fd(char* buf, struct timeval *date_tvp, char timestamps,
497-
int dplace, FILE *outfile)
495+
int dplace, int disable_dir, FILE *outfile)
498496
{
499497
int interface;
500498
static struct timeval tv; /* current frame timestamp */
@@ -505,7 +503,6 @@ static void eval_canxl_fd(char* buf, struct timeval *date_tvp, char timestamps,
505503
int dlc, dlen = 0;
506504
char idstr[21];
507505
char dir[5]; /* 'Rx'/'Tx'/'TxRq' plus terminating zero */
508-
char *extra_info;
509506
char *ptr;
510507
int i;
511508
int n = 0; /* sscanf consumed characters */
@@ -549,6 +546,9 @@ static void eval_canxl_fd(char* buf, struct timeval *date_tvp, char timestamps,
549546
if (strlen(dir) != 2) /* "Rx" or "Tx" */
550547
return;
551548

549+
if (disable_dir)
550+
dir[0] = NO_DIR;
551+
552552
/* check for signed integer overflow */
553553
if (dplace == 4 && read_tv.tv_usec >= INT_MAX / 100)
554554
return;
@@ -557,11 +557,6 @@ static void eval_canxl_fd(char* buf, struct timeval *date_tvp, char timestamps,
557557
if (dplace == 5 && read_tv.tv_usec >= INT_MAX / 10)
558558
return;
559559

560-
if (dir[0] == 'R')
561-
extra_info = " R\n";
562-
else
563-
extra_info = " T\n";
564-
565560
get_can_id(&cf.can_id, idstr, 16);
566561

567562
ptr = buf + n; /* start of ASCII hex frame data */
@@ -599,12 +594,12 @@ static void eval_canxl_fd(char* buf, struct timeval *date_tvp, char timestamps,
599594
cf.flags |= CANFD_ESI;
600595

601596
calc_tv(&tv, &read_tv, date_tvp, timestamps, dplace);
602-
prframe(outfile, &tv, interface, (cu_t *)&cf, extra_info);
597+
prframe(outfile, &tv, interface, (cu_t *)&cf, dir[0]);
603598
fflush(outfile);
604599
}
605600

606601
static void eval_canxl_xl(char* buf, struct timeval *date_tvp, char timestamps,
607-
int dplace, FILE *outfile)
602+
int dplace, int disable_dir, FILE *outfile)
608603
{
609604
int interface;
610605
static struct timeval tv; /* current frame timestamp */
@@ -615,7 +610,6 @@ static void eval_canxl_xl(char* buf, struct timeval *date_tvp, char timestamps,
615610
int dlc, dlen = 0;
616611
char idstr[21];
617612
char dir[5]; /* 'Rx'/'Tx'/'TxRq' plus terminating zero */
618-
char *extra_info;
619613
char *ptr;
620614
int i;
621615
int n = 0; /* sscanf consumed characters */
@@ -668,6 +662,9 @@ static void eval_canxl_xl(char* buf, struct timeval *date_tvp, char timestamps,
668662
if (strlen(dir) != 2) /* "Rx" or "Tx" */
669663
return;
670664

665+
if (disable_dir)
666+
dir[0] = NO_DIR;
667+
671668
/* check for signed integer overflow */
672669
if (dplace == 4 && read_tv.tv_usec >= INT_MAX / 100)
673670
return;
@@ -676,11 +673,6 @@ static void eval_canxl_xl(char* buf, struct timeval *date_tvp, char timestamps,
676673
if (dplace == 5 && read_tv.tv_usec >= INT_MAX / 10)
677674
return;
678675

679-
if (dir[0] == 'R')
680-
extra_info = " R\n";
681-
else
682-
extra_info = " T\n";
683-
684676
/* don't trust ASCII content - sanitize data length */
685677
if (dlen != dlc + 1)
686678
return;
@@ -731,14 +723,14 @@ static void eval_canxl_xl(char* buf, struct timeval *date_tvp, char timestamps,
731723
cf.flags |= CANXL_RRS;
732724

733725
calc_tv(&tv, &read_tv, date_tvp, timestamps, dplace);
734-
prframe(outfile, &tv, interface, (cu_t *)&cf, extra_info);
726+
prframe(outfile, &tv, interface, (cu_t *)&cf, dir[0]);
735727
fflush(outfile);
736728

737729
/* No support for CAN XL ErrorFrames */
738730
}
739731

740732
static void eval_canxl(char* buf, struct timeval *date_tvp, char timestamps,
741-
int dplace, FILE *outfile)
733+
int dplace, int disable_dir, FILE *outfile)
742734
{
743735
int interface;
744736
char dir[5]; /* 'Rx'/'Tx'/'TxRq' plus terminating zero */
@@ -773,15 +765,15 @@ static void eval_canxl(char* buf, struct timeval *date_tvp, char timestamps,
773765
return;
774766

775767
if (!strncmp(frfo, "XLFF", 4))
776-
eval_canxl_xl(buf, date_tvp, timestamps, dplace, outfile);
768+
eval_canxl_xl(buf, date_tvp, timestamps, dplace, disable_dir, outfile);
777769
else if (!strncmp(frfo, "FBFF", 4))
778-
eval_canxl_fd(buf, date_tvp, timestamps, dplace, outfile);
770+
eval_canxl_fd(buf, date_tvp, timestamps, dplace, disable_dir, outfile);
779771
else if (!strncmp(frfo, "FEFF", 4))
780-
eval_canxl_fd(buf, date_tvp, timestamps, dplace, outfile);
772+
eval_canxl_fd(buf, date_tvp, timestamps, dplace, disable_dir, outfile);
781773
else if (!strncmp(frfo, "CBFF", 4))
782-
eval_canxl_cc(buf, date_tvp, timestamps, dplace, outfile);
774+
eval_canxl_cc(buf, date_tvp, timestamps, dplace, disable_dir, outfile);
783775
else if (!strncmp(frfo, "CEFF", 4))
784-
eval_canxl_cc(buf, date_tvp, timestamps, dplace, outfile);
776+
eval_canxl_cc(buf, date_tvp, timestamps, dplace, disable_dir, outfile);
785777
}
786778

787779
static int get_date(struct timeval *tv, char *date)
@@ -847,15 +839,15 @@ int main(int argc, char **argv)
847839

848840
FILE *infile = stdin;
849841
FILE *outfile = stdout;
850-
static int verbose;
842+
static int verbose, disable_dir;
851843
static struct timeval date_tv; /* date of the ASC file */
852844
static int dplace; /* decimal place 4, 5 or 6 or uninitialized */
853845
static char base; /* 'd'ec or 'h'ex */
854846
static char timestamps; /* 'a'bsolute or 'r'elative */
855847
int opt;
856848
unsigned long long sec, usec;
857849

858-
while ((opt = getopt(argc, argv, "I:O:v?")) != -1) {
850+
while ((opt = getopt(argc, argv, "I:O:dv?")) != -1) {
859851
switch (opt) {
860852
case 'I':
861853
infile = fopen(optarg, "r");
@@ -873,6 +865,10 @@ int main(int argc, char **argv)
873865
}
874866
break;
875867

868+
case 'd':
869+
disable_dir = 1;
870+
break;
871+
876872
case 'v':
877873
verbose = 1;
878874
break;
@@ -954,11 +950,11 @@ int main(int argc, char **argv)
954950
/* check classic CAN format or the CANFD/CANXL tag which can take different types */
955951
if (sscanf(buf, "%llu.%llu %9s ", &sec, &usec, tmp1) == 3) {
956952
if (!strncmp(tmp1, "CANXL", 5))
957-
eval_canxl(buf, &date_tv, timestamps, dplace, outfile);
953+
eval_canxl(buf, &date_tv, timestamps, dplace, disable_dir, outfile);
958954
else if (!strncmp(tmp1, "CANFD", 5))
959-
eval_canfd(buf, &date_tv, timestamps, dplace, outfile);
955+
eval_canfd(buf, &date_tv, timestamps, dplace, disable_dir, outfile);
960956
else
961-
eval_can(buf, &date_tv, timestamps, base, dplace, outfile);
957+
eval_can(buf, &date_tv, timestamps, base, dplace, disable_dir, outfile);
962958
}
963959
}
964960
fclose(outfile);

0 commit comments

Comments
 (0)