diff --git a/examples/Makefile b/examples/Makefile index 361dfd9..a423b64 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -8,7 +8,7 @@ CFLAGS += -I$(INCLUDE_PATH) LDFLAGS = -L../src -lhexagram STATIC = ../src/libhexagram.a -EXAMPLES = capture convert pcapread replay +EXAMPLES = capture convert pcapread replay view RM = /bin/rm @@ -17,5 +17,8 @@ all: $(EXAMPLES) $(EXAMPLES): %: %.c $(STATIC) $(CC) $(CFLAGS) $< -o $@ $(STATIC) +view: view.c $(STATIC) + $(CC) $(CFLAGS) $< -o $@ $(STATIC) -lncurses + clean: $(RM) -f $(EXAMPLES) diff --git a/examples/capture.c b/examples/capture.c index 2bed423..556c76f 100644 --- a/examples/capture.c +++ b/examples/capture.c @@ -61,7 +61,7 @@ int main(int argc, char **argv) { } while (hexagram_can_if_read(can_if, &frame) >= 0) { - if (hexagram_capture_write(capture, &frame, NULL) < 0) { + if (hexagram_capture_write(capture, NULL, &frame) < 0) { goto error_capture_write; } } diff --git a/examples/convert.c b/examples/convert.c index 25cf212..c8b78fb 100644 --- a/examples/convert.c +++ b/examples/convert.c @@ -116,7 +116,7 @@ static ssize_t handle_block_packet(hexagram_pcapng_stream *stream, ((struct can_frame *)body)->can_id = be32toh(((struct can_frame *)body)->can_id); - if (hexagram_capture_write(capture, (struct can_frame *)body, ×tamp) < 0) { + if (hexagram_capture_write(capture, ×tamp, (struct can_frame *)body) < 0) { goto error_io; } diff --git a/examples/view.c b/examples/view.c new file mode 100644 index 0000000..699f91a --- /dev/null +++ b/examples/view.c @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +#include + +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: %s canif\n", argv[0]); + + return 1; +} + +int main(int argc, char **argv) { + WINDOW *win; + hexagram_can_if *can_if; + struct can_frame frame; + uint8_t last_gear = 0; + + if (argc != 2) { + return usage(argc, argv, NULL); + } + + if ((can_if = hexagram_can_if_open(argv[1])) == NULL) { + perror("hexagram_can_if_open()"); + + goto error_can_if_open; + } + + initscr(); + + win = newwin(LINES, COLS, 0, 0); + + while (hexagram_can_if_read(can_if, &frame) >= 0) { + char lever_states[16] = " PRNDS "; + char manual_gears[16] = " 123456 "; + + if (frame.can_id == 0x280) { + + } else if (frame.can_id == 0x540) { + if (frame.data[7] != last_gear) { + wmove(win, 0, 0); + + if ((frame.data[7] & 0xc) == 0xc) { + wprintw(win, " %c\n", + manual_gears[(frame.data[7] & 0xf0) >> 4]); + } else { + wprintw(win, "%c \n", + lever_states[(frame.data[7] & 0xf0) >> 4]); + } + + wrefresh(win); + + last_gear = frame.data[7]; + } + } + } + + hexagram_can_if_close(can_if); + + return 0; + +error_can_if_open: + return 1; +} diff --git a/include/hexagram/capture.h b/include/hexagram/capture.h index 6dc5771..b835314 100644 --- a/include/hexagram/capture.h +++ b/include/hexagram/capture.h @@ -8,8 +8,8 @@ #include -#define HEXAGRAM_CAPTURE_MAGIC "HCAN" -#define HEXAGRAM_CAPTURE_ENDIAN 0x0a0b0c0d +#define HEXAGRAM_CAPTURE_MAGIC "HCAN" +#define HEXAGRAM_CAPTURE_ENDIAN 0x0a0b0c0d #define HEXAGRAM_CAPTURE_ENDIAN_SWAPPED 0x0d0c0b0a typedef struct _hexagram_capture hexagram_capture; @@ -20,8 +20,8 @@ typedef struct _hexagram_capture_header { } hexagram_capture_header; typedef struct _hexagram_capture_frame { - uint32_t timestamp_hi, - timestamp_lo; + uint32_t sec, + usec; struct can_frame frame; } hexagram_capture_frame; @@ -34,13 +34,12 @@ void hexagram_capture_destroy(hexagram_capture *capture); void hexagram_capture_close(hexagram_capture *capture); ssize_t hexagram_capture_read(hexagram_capture *capture, - uint32_t *timestamp_hi, - uint32_t *timestamp_lo, + struct timeval *timestamp, struct can_frame *frame); ssize_t hexagram_capture_write(hexagram_capture *capture, - struct can_frame *frame, - struct timeval *timestamp); + struct timeval *timestamp, + struct can_frame *frame); int hexagram_capture_replay(hexagram_capture *capture, hexagram_can_if *can_if, diff --git a/src/capture.c b/src/capture.c index 85f8d79..3ba5f46 100644 --- a/src/capture.c +++ b/src/capture.c @@ -88,8 +88,7 @@ void hexagram_capture_close(hexagram_capture *capture) { } ssize_t hexagram_capture_read(hexagram_capture *capture, - uint32_t *timestamp_hi, - uint32_t *timestamp_lo, + struct timeval *timestamp, struct can_frame *frame) { ssize_t len; hexagram_capture_frame data; @@ -100,8 +99,8 @@ ssize_t hexagram_capture_read(hexagram_capture *capture, goto error_io; } - if (timestamp_hi) *timestamp_hi = data.timestamp_hi; - if (timestamp_lo) *timestamp_lo = data.timestamp_lo; + timestamp->tv_sec = data.sec; + timestamp->tv_usec = data.usec; memcpy(frame, &data.frame, sizeof(data.frame)); @@ -112,10 +111,9 @@ error_io: } ssize_t hexagram_capture_write(hexagram_capture *capture, - struct can_frame *frame, - struct timeval *timestamp) { + struct timeval *timestamp, + struct can_frame *frame) { hexagram_capture_frame data; - uint64_t usec; if (timestamp == NULL) { struct timeval now; @@ -124,14 +122,13 @@ ssize_t hexagram_capture_write(hexagram_capture *capture, goto error_gettimeofday; } - usec = now.tv_usec + now.tv_sec * 1000000; + data.sec = now.tv_sec; + data.usec = now.tv_usec; } else { - usec = timestamp->tv_usec + timestamp->tv_sec * 1000000; + data.sec = timestamp->tv_sec; + data.usec = timestamp->tv_usec; } - data.timestamp_hi = usec & 0xffffffff00000000 >> 32; - data.timestamp_lo = usec & 0x00000000ffffffff; - memcpy(&data.frame, frame, sizeof(data.frame)); return write(capture->fd, &data, sizeof(data)); @@ -143,15 +140,16 @@ error_gettimeofday: int hexagram_capture_replay(hexagram_capture *capture, hexagram_can_if *can_if, float speed) { - uint32_t timestamp[2], - usec_last = 0; + struct timeval timestamp; + + uint64_t usec_last = 0, + usec; struct can_frame frame; while (1) { ssize_t len = hexagram_capture_read(capture, - ×tamp[0], - ×tamp[1], + ×tamp, &frame); if (len < 0) { @@ -160,15 +158,18 @@ int hexagram_capture_replay(hexagram_capture *capture, break; } + usec = timestamp.tv_sec * 1000000 + + timestamp.tv_usec; + if (usec_last) { - usleep(speed * (useconds_t)(timestamp[1] - usec_last)); + usleep(speed * (useconds_t)(usec - usec_last)); } if (write(can_if->sock, &frame, sizeof(struct can_frame)) < 0) { goto error_io; } - usec_last = timestamp[1]; + usec_last = usec; } return 0;