diff --git a/bin/Makefile b/bin/Makefile index 58062ee..c93983a 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -7,7 +7,7 @@ INCLUDE_PATH = ../include CFLAGS += -I$(INCLUDE_PATH) LDFLAGS = -L../src -lhexagram STATIC = ../src/libhexagram.a -OBJS = capture.o pcap2can.o replay.o pcapreplay.o main.o +OBJS = capture.o can2dump.o replay.o pcapreplay.o pcap2can.o main.o NAME = hexagram RM = /bin/rm diff --git a/bin/can2dump.c b/bin/can2dump.c new file mode 100644 index 0000000..84e1527 --- /dev/null +++ b/bin/can2dump.c @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "can2dump.h" + +char *hexagram_arglist_can2dump(void) { + return "ifname [file.can] [candump.txt]"; +} + +static void 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()); + + exit(1); +} + +static int convert(hexagram_capture *capture, const char *file, const char *ifname) { + struct timeval timestamp; + struct can_frame frame; + + FILE *fh; + + if ((fh = fopen(file, "w")) == NULL) { + goto error_fopen; + } + + while (hexagram_capture_read(capture, ×tamp, &frame) >= 0) { + + } + + fclose(fh); + + return 0; + +error_fopen: + return -1; +} + +int hexagram_main_can2dump(int argc, char **argv) { + hexagram_can_if *can_if; + hexagram_capture *capture; + + if (argc == 2) { + if ((capture = hexagram_capture_open_fd(fileno(stdin), O_RDONLY)) == NULL) { + perror("hexagram_capture_open_fd()"); + + goto error_capture_open; + } + } else if (argc == 3) { + if ((capture = hexagram_capture_open_file(argv[2], O_RDONLY)) == NULL) { + perror("hexagram_capture_open_file()"); + + goto error_capture_open; + } + } else { + usage(argc, argv, NULL); + exit(1); + } + + if ((can_if = hexagram_can_if_open(argv[1])) == NULL) { + perror("hexagram_can_if_open()"); + + goto error_can_if_open; + } + + if (hexagram_capture_can2dump(capture, can_if, 1.0) < 0) { + perror("hexagram_capture_can2dump()"); + + goto error_capture_can2dump; + } + + hexagram_can_if_close(can_if); + + if (argc == 3) { + hexagram_capture_close(capture); + } else { + hexagram_capture_destroy(capture); + } + + return 0; + +error_capture_can2dump: + hexagram_can_if_close(can_if); + +error_can_if_open: + if (argc == 3) { + hexagram_capture_close(capture); + } else { + hexagram_capture_destroy(capture); + } + +error_capture_open: + return 1; +} diff --git a/bin/can2dump.h b/bin/can2dump.h new file mode 100644 index 0000000..b26f60e --- /dev/null +++ b/bin/can2dump.h @@ -0,0 +1,8 @@ +#ifndef _CAN2DUMP_H +#define _CAN2DUMP_H + +char *hexagram_arglist_can2dump(void); + +int hexagram_main_can2dump(int argc, char **argv); + +#endif /* _CAN2DUMP_H */