hexagram/bin/can2dump.c

101 lines
2.4 KiB
C
Raw Normal View History

2019-05-22 22:42:21 -05:00
#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]";
}
2019-05-23 01:20:05 -05:00
static int usage(int argc, char **argv, const char *message, ...) {
2019-05-22 22:42:21 -05:00
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());
2019-05-23 01:20:05 -05:00
return 1;
2019-05-22 22:42:21 -05:00
}
2019-05-23 01:20:05 -05:00
int hexagram_main_can2dump(int argc, char **argv) {
hexagram_capture *capture;
const char *ifname;
2019-05-22 22:42:21 -05:00
FILE *fh;
2019-05-23 01:20:05 -05:00
struct timeval timestamp;
struct can_frame frame;
2019-05-22 22:42:21 -05:00
2019-05-23 01:20:05 -05:00
if (argc <= 1) {
return usage(argc, argv, "No CAN interface name provided");
2019-05-22 22:42:21 -05:00
}
2019-05-23 01:20:05 -05:00
ifname = argv[1];
2019-05-22 22:42:21 -05:00
if (argc == 2) {
if ((capture = hexagram_capture_open_fd(fileno(stdin), O_RDONLY)) == NULL) {
perror("hexagram_capture_open_fd()");
goto error_capture_open;
}
2019-05-23 01:20:05 -05:00
fh = stdout;
2019-05-22 22:42:21 -05:00
} else if (argc == 3) {
if ((capture = hexagram_capture_open_file(argv[2], O_RDONLY)) == NULL) {
perror("hexagram_capture_open_file()");
goto error_capture_open;
}
2019-05-23 01:20:05 -05:00
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;
}
2019-05-22 22:42:21 -05:00
} else {
usage(argc, argv, NULL);
exit(1);
}
2019-05-23 01:20:05 -05:00
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]);
2019-05-22 22:42:21 -05:00
}
2019-05-23 01:20:05 -05:00
if (argc == 2) {
2019-05-22 22:42:21 -05:00
hexagram_capture_destroy(capture);
2019-05-23 01:20:05 -05:00
} else if (argc >= 3) {
hexagram_capture_close(capture);
2019-05-22 22:42:21 -05:00
}
return 0;
2019-05-23 01:20:05 -05:00
error_fopen:
if (argc == 2) {
2019-05-22 22:42:21 -05:00
hexagram_capture_destroy(capture);
2019-05-23 01:20:05 -05:00
} else if (argc >= 3) {
hexagram_capture_close(capture);
2019-05-22 22:42:21 -05:00
}
error_capture_open:
return 1;
}