#include #include #include #include #include #include #include #include #include #include "can2dump.h" enum timestamp_type { TIMESTAMP_NONE, TIMESTAMP_ABSOLUTE, TIMESTAMP_DELTA, TIMESTAMP_ZERO, TIMESTAMP_DATE }; char *hexagram_arglist_can2dump(void) { return "[--timestamp=|adzA|] ifname [file.can] [candump.txt]"; } static int usage(int argc, char **argv, const char *message, ...) { if (message) { va_list args; va_start(args, message); vfprintf(stderr, message, args); fprintf(stderr, "\n"); va_end(args); } fprintf(stderr, "usage: hexagram %s %s\n", argv[0], hexagram_arglist_can2dump()); return 1; } int hexagram_main_can2dump(int argc, char **argv) { hexagram_capture *capture; const char *ifname; FILE *fh; struct timeval timestamp; struct can_frame frame; 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 = args[0]; if (argn == 1) { if ((capture = hexagram_capture_open_fd(fileno(stdin), O_RDONLY)) == NULL) { perror("hexagram_capture_open_fd()"); goto error_capture_open; } fh = stdout; } 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 (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(args[2], "w")) == NULL) { goto error_fopen; } } else { return usage(argc, argv, NULL); } while (hexagram_capture_read(capture, ×tamp, &frame) >= 0) { uint8_t i; 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= 2) { hexagram_capture_close(capture); } return 0; error_fopen: if (argn == 1) { hexagram_capture_destroy(capture); } else if (argn >= 2) { hexagram_capture_close(capture); } error_capture_open: return 1; }