I'm just gonna use struct timeval for everything, disregard the local constabulary

OH ALSO I ADDED examples/view.c AS A DATA DECODING TEST BED YEAHHH SON
This commit is contained in:
XANTRONIX Development 2019-02-19 19:26:33 -06:00
parent b82884f23c
commit 1112cb45a0
6 changed files with 117 additions and 29 deletions

View file

@ -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)

View file

@ -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;
}
}

View file

@ -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, &timestamp) < 0) {
if (hexagram_capture_write(capture, &timestamp, (struct can_frame *)body) < 0) {
goto error_io;
}

85
examples/view.c Normal file
View file

@ -0,0 +1,85 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <inttypes.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <ncurses.h>
#include <net/if.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <hexagram/can.h>
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;
}

View file

@ -8,8 +8,8 @@
#include <hexagram/can.h>
#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,

View file

@ -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,
&timestamp[0],
&timestamp[1],
&timestamp,
&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;