From e563a6b5daf250147c455633c1a3ec519cb3b411 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Thu, 23 May 2019 02:28:34 -0500 Subject: [PATCH] Wish me luck getting to sleep this century --- bin/can2dump.c | 87 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 71 insertions(+), 16 deletions(-) diff --git a/bin/can2dump.c b/bin/can2dump.c index 00bf317..dcf9c1c 100644 --- a/bin/can2dump.c +++ b/bin/can2dump.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -10,8 +11,16 @@ #include "can2dump.h" +enum timestamp_type { + TIMESTAMP_NONE, + TIMESTAMP_ABSOLUTE, + TIMESTAMP_DELTA, + TIMESTAMP_ZERO, + TIMESTAMP_DATE +}; + char *hexagram_arglist_can2dump(void) { - return "ifname [file.can] [candump.txt]"; + return "[--timestamp=|adzA|] ifname [file.can] [candump.txt]"; } static int usage(int argc, char **argv, const char *message, ...) { @@ -20,6 +29,7 @@ static int usage(int argc, char **argv, const char *message, ...) { va_start(args, message); vfprintf(stderr, message, args); + fprintf(stderr, "\n"); va_end(args); } @@ -36,13 +46,48 @@ int hexagram_main_can2dump(int argc, char **argv) { struct timeval timestamp; struct can_frame frame; - if (argc <= 1) { + static struct option opts[] = { + { "timestamp", required_argument, NULL, 't' }, + { NULL, 0, NULL, 0 } + }; + + enum timestamp_type tstype = TIMESTAMP_NONE; + + int argn; + char **args; + + int ch; + + while ((ch = getopt_long(argc, argv, "t:", opts, NULL)) >= 0) { + switch (ch) { + case 't': + switch (optarg[0]) { + case 'a': tstype = TIMESTAMP_ABSOLUTE; break; + case 'd': tstype = TIMESTAMP_DELTA; break; + case 'z': tstype = TIMESTAMP_ZERO; break; + case 'A': tstype = TIMESTAMP_DATE; break; + + default: + return usage(argc, argv, "Invalid timestamp type"); + } + + break; + + default: + return usage(argc, argv, "Unknown flag '%c'", ch); + } + } + + argn = argc - optind; + args = argv + optind; + + if (argn == 0) { return usage(argc, argv, "No CAN interface name provided"); } - ifname = argv[1]; + ifname = args[0]; - if (argc == 2) { + if (argn == 1) { if ((capture = hexagram_capture_open_fd(fileno(stdin), O_RDONLY)) == NULL) { perror("hexagram_capture_open_fd()"); @@ -50,33 +95,43 @@ int hexagram_main_can2dump(int argc, char **argv) { } fh = stdout; - } else if (argc == 3) { - if ((capture = hexagram_capture_open_file(argv[2], O_RDONLY)) == NULL) { + } else if (argn == 2) { + if ((capture = hexagram_capture_open_file(args[1], O_RDONLY)) == NULL) { perror("hexagram_capture_open_file()"); goto error_capture_open; } fh = stdout; - } else if (argc == 4) { - if ((capture = hexagram_capture_open_file(argv[2], O_RDONLY)) == NULL) { + } else if (argn == 3) { + if ((capture = hexagram_capture_open_file(args[1], O_RDONLY)) == NULL) { perror("hexagram_capture_open_file()"); goto error_capture_open; } - if ((fh = fopen(argv[3], "w")) == NULL) { + if ((fh = fopen(args[2], "w")) == NULL) { goto error_fopen; } } else { - usage(argc, argv, NULL); - exit(1); + return usage(argc, argv, NULL); } while (hexagram_capture_read(capture, ×tamp, &frame) >= 0) { uint8_t i; - fprintf(fh, "%7s %03X [%d] ", + switch (tstype) { + case TIMESTAMP_NONE: + break; + + case TIMESTAMP_ABSOLUTE: + fprintf(fh, " (%zu.%zu) ", + timestamp.tv_sec, timestamp.tv_usec); + + break; + } + + fprintf(fh, "%6s %03X [%d] ", ifname, frame.can_id, frame.can_dlc); for (i=0; i= 3) { + } else if (argn >= 2) { hexagram_capture_close(capture); } return 0; error_fopen: - if (argc == 2) { + if (argn == 1) { hexagram_capture_destroy(capture); - } else if (argc >= 3) { + } else if (argn >= 2) { hexagram_capture_close(capture); }