diff --git a/examples/capture.c b/examples/capture.c index 6b3234e..d5a74f3 100644 --- a/examples/capture.c +++ b/examples/capture.c @@ -27,7 +27,6 @@ static void usage(int argc, char **argv, const char *message, ...) { int main(int argc, char **argv) { hexagram_can_if *can_if; hexagram_capture *capture; - struct can_frame frame; if (argc == 2) { if ((capture = hexagram_capture_open_fd(fileno(stdin), O_WRONLY)) == NULL) { @@ -52,10 +51,8 @@ int main(int argc, char **argv) { goto error_can_if_open; } - while (hexagram_can_if_read(can_if, &frame) >= 0) { - if (hexagram_capture_write(capture, NULL, &frame) < 0) { - goto error_capture_write; - } + if (hexagram_capture_save(capture, can_if) < 0) { + goto error_capture_save; } hexagram_can_if_close(can_if); @@ -68,7 +65,7 @@ int main(int argc, char **argv) { return 0; -error_capture_write: +error_capture_save: hexagram_can_if_close(can_if); error_can_if_open: diff --git a/include/hexagram/capture.h b/include/hexagram/capture.h index b835314..06faec1 100644 --- a/include/hexagram/capture.h +++ b/include/hexagram/capture.h @@ -41,6 +41,9 @@ ssize_t hexagram_capture_write(hexagram_capture *capture, struct timeval *timestamp, struct can_frame *frame); +int hexagram_capture_save(hexagram_capture *capture, + hexagram_can_if *can_if); + int hexagram_capture_replay(hexagram_capture *capture, hexagram_can_if *can_if, float speed); diff --git a/src/capture.c b/src/capture.c index 3ba5f46..8849eca 100644 --- a/src/capture.c +++ b/src/capture.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -137,6 +138,28 @@ error_gettimeofday: return -1; } +int hexagram_capture_save(hexagram_capture *capture, + hexagram_can_if *can_if) { + struct can_frame frame; + + while (hexagram_can_if_read(can_if, &frame) >= 0) { + struct timeval timestamp; + + if (ioctl(can_if->sock, SIOCGSTAMP, ×tamp) < 0) { + goto error_io; + } + + if (hexagram_capture_write(capture, ×tamp, &frame) < 0) { + goto error_io; + } + } + + return 0; + +error_io: + return -1; +} + int hexagram_capture_replay(hexagram_capture *capture, hexagram_can_if *can_if, float speed) {