hexagram/bin/can2dump.c
XANTRONIX Development f1388e93bd Thanks Gemini!
2019-05-23 01:20:05 -05:00

100 lines
2.4 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <inttypes.h>
#include <fcntl.h>
#include <hexagram/can.h>
#include <hexagram/capture.h>
#include "can2dump.h"
char *hexagram_arglist_can2dump(void) {
return "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);
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;
if (argc <= 1) {
return usage(argc, argv, "No CAN interface name provided");
}
ifname = argv[1];
if (argc == 2) {
if ((capture = hexagram_capture_open_fd(fileno(stdin), O_RDONLY)) == NULL) {
perror("hexagram_capture_open_fd()");
goto error_capture_open;
}
fh = stdout;
} else if (argc == 3) {
if ((capture = hexagram_capture_open_file(argv[2], 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) {
perror("hexagram_capture_open_file()");
goto error_capture_open;
}
if ((fh = fopen(argv[3], "w")) == NULL) {
goto error_fopen;
}
} else {
usage(argc, argv, NULL);
exit(1);
}
while (hexagram_capture_read(capture, &timestamp, &frame) >= 0) {
fprintf(fh, "%s 0x%x %02x %02x %02x %02x %02x %02x %02x %02x\n",
ifname, frame.can_id,
frame.data[0], frame.data[1], frame.data[2], frame.data[3],
frame.data[4], frame.data[5], frame.data[6], frame.data[7]);
}
if (argc == 2) {
hexagram_capture_destroy(capture);
} else if (argc >= 3) {
hexagram_capture_close(capture);
}
return 0;
error_fopen:
if (argc == 2) {
hexagram_capture_destroy(capture);
} else if (argc >= 3) {
hexagram_capture_close(capture);
}
error_capture_open:
return 1;
}