From e0da9b0cfb5384982aef1e0c0d2c0cc46686fe0c Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Mon, 4 Feb 2019 22:15:33 -0600 Subject: [PATCH] Well, I suppose we all have mommies and daddies --- include/hexagram/can.h | 40 ++++++++++++++++++++ src/Makefile | 4 +- src/can.c | 85 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 include/hexagram/can.h create mode 100644 src/can.c diff --git a/include/hexagram/can.h b/include/hexagram/can.h new file mode 100644 index 0000000..55fa34e --- /dev/null +++ b/include/hexagram/can.h @@ -0,0 +1,40 @@ +#ifndef _HEXAGRAM_CAN_H +#define _HEXAGRAM_CAN_H + +#include +#include +#include + +#define HEXAGRAM_CAN_DUMP_MAGIC "CAAN" +#define HEXAGRAM_CAN_DUMP_ENDIAN 0x0a0b0c0d +#define HEXAGRAM_CAN_DUMP_ENDIAN_SWAPPED 0x0d0c0b0a + +#define HEXAGRAM_CAN_DUMP_TYPE_UNKNOWN 0 +#define HEXAGRAM_CAN_DUMP_TYPE_SOCKETCAN 29 + +typedef struct _hexagram_can_dump { + char magic[4]; + uint32_t endian; + uint8_t type, + tsresol; + char iface[38]; +} hexagram_can_dump; + +typedef struct _hexagram_can_frame { + uint32_t timestamp_hi, + timestamp_lo; + struct can_frame frame; +} hexagram_can_frame; + +typedef struct _hexagram_can_stream hexagram_can_stream; + +hexagram_can_stream *hexagram_can_stream_open_fd(int fd); + +void hexagram_can_stream_destroy(hexagram_can_stream *stream); + +int hexagram_can_stream_read(hexagram_can_stream *stream, + uint32_t *timestamp_hi, + uint32_t *timestamp_lo, + struct can_frame *frame); + +#endif /* _HEXAGRAM_CAN_H */ diff --git a/src/Makefile b/src/Makefile index 6c581bc..0cff3b6 100644 --- a/src/Makefile +++ b/src/Makefile @@ -7,10 +7,10 @@ CC = $(CROSS)cc CFLAGS += -fPIC -I$(INCLUDE_PATH) LDFLAGS = -HEADERS = pcapng.h +HEADERS = can.h pcapng.h HEADERS_LOCAL = util.h -OBJS = pcapng.o +OBJS = can.o pcapng.o VERSION_MAJOR = 0 VERSION_MINOR = 0.1 diff --git a/src/can.c b/src/can.c new file mode 100644 index 0000000..41375ca --- /dev/null +++ b/src/can.c @@ -0,0 +1,85 @@ +#include +#include +#include + +#include + +struct _hexagram_can_stream { + int fd; + uint32_t endian; + uint8_t tsresol; +}; + +hexagram_can_stream *hexagram_can_stream_open_fd(int fd) { + hexagram_can_stream *stream; + hexagram_can_dump dump; + ssize_t readlen; + + if ((stream = malloc(sizeof(*stream))) == NULL) { + goto error_malloc; + } + + if ((readlen = read(fd, &dump, sizeof(dump))) < 0) { + goto error_read; + } + + if (memcmp(dump.magic, HEXAGRAM_CAN_DUMP_MAGIC, sizeof(dump.magic)) != 0) { + goto error_invalid_format; + } + + switch (dump.endian) { + case HEXAGRAM_CAN_DUMP_ENDIAN: + case HEXAGRAM_CAN_DUMP_ENDIAN_SWAPPED: + break; + + default: + goto error_invalid_format; + } + + if (dump.type != HEXAGRAM_CAN_DUMP_TYPE_SOCKETCAN) { + goto error_invalid_format; + } + + stream->fd = fd; + stream->endian = dump.endian; + stream->tsresol = dump.tsresol; + + return stream; + +error_invalid_format: +error_read: + free(stream); + +error_malloc: + return NULL; +} + +void hexagram_can_stream_destroy(hexagram_can_stream *stream) { + memset(stream, '\0', sizeof(*stream)); + + free(stream); +} + +int hexagram_can_stream_read(hexagram_can_stream *stream, + uint32_t *timestamp_hi, + uint32_t *timestamp_lo, + struct can_frame *frame) { + ssize_t len; + hexagram_can_frame data; + + if ((len = read(stream->fd, &data, sizeof(data))) < 0) { + goto error_io; + } else if (len < sizeof(data)) { + goto error_io; + } + + *timestamp_hi = data.timestamp_hi; + *timestamp_lo = data.timestamp_lo; + + memcpy(frame, &data.frame, sizeof(data.frame)); + + return 0; + +error_io: + return -1; +}