Wish me luck getting to sleep this century

This commit is contained in:
XANTRONIX Development 2019-05-23 02:28:34 -05:00
parent 8c0af77ba9
commit e563a6b5da

View file

@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdarg.h> #include <stdarg.h>
#include <getopt.h>
#include <inttypes.h> #include <inttypes.h>
#include <fcntl.h> #include <fcntl.h>
@ -10,8 +11,16 @@
#include "can2dump.h" #include "can2dump.h"
enum timestamp_type {
TIMESTAMP_NONE,
TIMESTAMP_ABSOLUTE,
TIMESTAMP_DELTA,
TIMESTAMP_ZERO,
TIMESTAMP_DATE
};
char *hexagram_arglist_can2dump(void) { 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, ...) { 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); va_start(args, message);
vfprintf(stderr, message, args); vfprintf(stderr, message, args);
fprintf(stderr, "\n");
va_end(args); va_end(args);
} }
@ -36,13 +46,48 @@ int hexagram_main_can2dump(int argc, char **argv) {
struct timeval timestamp; struct timeval timestamp;
struct can_frame frame; 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"); 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) { if ((capture = hexagram_capture_open_fd(fileno(stdin), O_RDONLY)) == NULL) {
perror("hexagram_capture_open_fd()"); perror("hexagram_capture_open_fd()");
@ -50,33 +95,43 @@ int hexagram_main_can2dump(int argc, char **argv) {
} }
fh = stdout; fh = stdout;
} else if (argc == 3) { } else if (argn == 2) {
if ((capture = hexagram_capture_open_file(argv[2], O_RDONLY)) == NULL) { if ((capture = hexagram_capture_open_file(args[1], O_RDONLY)) == NULL) {
perror("hexagram_capture_open_file()"); perror("hexagram_capture_open_file()");
goto error_capture_open; goto error_capture_open;
} }
fh = stdout; fh = stdout;
} else if (argc == 4) { } else if (argn == 3) {
if ((capture = hexagram_capture_open_file(argv[2], O_RDONLY)) == NULL) { if ((capture = hexagram_capture_open_file(args[1], O_RDONLY)) == NULL) {
perror("hexagram_capture_open_file()"); perror("hexagram_capture_open_file()");
goto error_capture_open; goto error_capture_open;
} }
if ((fh = fopen(argv[3], "w")) == NULL) { if ((fh = fopen(args[2], "w")) == NULL) {
goto error_fopen; goto error_fopen;
} }
} else { } else {
usage(argc, argv, NULL); return usage(argc, argv, NULL);
exit(1);
} }
while (hexagram_capture_read(capture, &timestamp, &frame) >= 0) { while (hexagram_capture_read(capture, &timestamp, &frame) >= 0) {
uint8_t i; 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); ifname, frame.can_id, frame.can_dlc);
for (i=0; i<frame.can_dlc; i++) { for (i=0; i<frame.can_dlc; i++) {
@ -86,18 +141,18 @@ int hexagram_main_can2dump(int argc, char **argv) {
fprintf(fh, "\n"); fprintf(fh, "\n");
} }
if (argc == 2) { if (argn == 1) {
hexagram_capture_destroy(capture); hexagram_capture_destroy(capture);
} else if (argc >= 3) { } else if (argn >= 2) {
hexagram_capture_close(capture); hexagram_capture_close(capture);
} }
return 0; return 0;
error_fopen: error_fopen:
if (argc == 2) { if (argn == 1) {
hexagram_capture_destroy(capture); hexagram_capture_destroy(capture);
} else if (argc >= 3) { } else if (argn >= 2) {
hexagram_capture_close(capture); hexagram_capture_close(capture);
} }