(i just want to be super good)
This commit is contained in:
parent
66f4990a1e
commit
4ae8dd7339
8 changed files with 245 additions and 62 deletions
|
@ -7,7 +7,7 @@ INCLUDE_PATH = ../include
|
|||
CFLAGS += -I$(INCLUDE_PATH)
|
||||
LDFLAGS = -L../src -lhexagram
|
||||
|
||||
EXAMPLES = convert pcapread replay
|
||||
EXAMPLES = capture convert pcapread replay
|
||||
|
||||
RM = /bin/rm
|
||||
|
||||
|
|
91
examples/capture.c
Normal file
91
examples/capture.c
Normal file
|
@ -0,0 +1,91 @@
|
|||
#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 <net/if.h>
|
||||
#include <linux/can.h>
|
||||
#include <linux/can/raw.h>
|
||||
|
||||
#include <hexagram/can.h>
|
||||
#include <hexagram/capture.h>
|
||||
|
||||
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: %s canif [file.can]\n", argv[0]);
|
||||
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
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) {
|
||||
perror("hexagram_capture_open_fd()");
|
||||
|
||||
goto error_capture_open;
|
||||
}
|
||||
} else if (argc == 3) {
|
||||
if ((capture = hexagram_capture_open_file(argv[2], O_CREAT | O_WRONLY)) == 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;
|
||||
}
|
||||
|
||||
while (hexagram_can_if_read(can_if, &frame) >= 0) {
|
||||
if (hexagram_capture_write(capture, &frame, NULL) < 0) {
|
||||
goto error_capture_write;
|
||||
}
|
||||
}
|
||||
|
||||
hexagram_can_if_close(can_if);
|
||||
|
||||
if (argc == 3) {
|
||||
hexagram_capture_close(capture);
|
||||
} else {
|
||||
hexagram_capture_destroy(capture);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
error_capture_write:
|
||||
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;
|
||||
}
|
|
@ -13,6 +13,7 @@
|
|||
#include <linux/can.h>
|
||||
#include <linux/can/raw.h>
|
||||
|
||||
#include <hexagram/can.h>
|
||||
#include <hexagram/capture.h>
|
||||
|
||||
static void usage(int argc, char **argv, const char *message, ...) {
|
||||
|
@ -29,48 +30,14 @@ static void usage(int argc, char **argv, const char *message, ...) {
|
|||
exit(1);
|
||||
}
|
||||
|
||||
static int capture_replay(hexagram_capture *capture, int sock) {
|
||||
ssize_t len;
|
||||
uint32_t timestamp[2],
|
||||
usec_last = 0;
|
||||
struct can_frame frame;
|
||||
|
||||
while (1) {
|
||||
if ((len = hexagram_capture_read(capture,
|
||||
×tamp[0],
|
||||
×tamp[1],
|
||||
&frame)) < 0) {
|
||||
goto error_io;
|
||||
} else if (len == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (usec_last) {
|
||||
usleep((useconds_t)(timestamp[1] - usec_last));
|
||||
}
|
||||
|
||||
if (write(sock, &frame, sizeof(struct can_frame)) < 0) {
|
||||
goto error_io;
|
||||
}
|
||||
|
||||
usec_last = timestamp[1];
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
error_io:
|
||||
return -1;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
struct sockaddr_can addr;
|
||||
struct ifreq ifr;
|
||||
int sock;
|
||||
|
||||
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) {
|
||||
|
@ -84,46 +51,36 @@ int main(int argc, char **argv) {
|
|||
exit(1);
|
||||
}
|
||||
|
||||
if ((sock = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
|
||||
perror("socket()");
|
||||
if ((can_if = hexagram_can_if_open(argv[1])) == NULL) {
|
||||
perror("hexagram_can_if_open()");
|
||||
|
||||
goto error_socket;
|
||||
goto error_can_if_open;
|
||||
}
|
||||
|
||||
strcpy(ifr.ifr_name, argv[1]);
|
||||
|
||||
ioctl(sock, SIOCGIFINDEX, &ifr);
|
||||
|
||||
addr.can_family = AF_CAN;
|
||||
addr.can_ifindex = ifr.ifr_ifindex;
|
||||
|
||||
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||
perror("bind()");
|
||||
|
||||
goto error_bind;
|
||||
}
|
||||
|
||||
if (capture_replay(capture, sock) < 0) {
|
||||
perror("capture_replay()");
|
||||
if (hexagram_capture_replay(capture, can_if, 1) < 0) {
|
||||
perror("hexagram_capture_replay()");
|
||||
|
||||
goto error_capture_replay;
|
||||
}
|
||||
|
||||
close(sock);
|
||||
hexagram_can_if_close(can_if);
|
||||
|
||||
if (argc == 3) {
|
||||
hexagram_capture_close(capture);
|
||||
} else {
|
||||
hexagram_capture_destroy(capture);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
error_capture_replay:
|
||||
error_bind:
|
||||
close(sock);
|
||||
hexagram_can_if_close(can_if);
|
||||
|
||||
error_socket:
|
||||
error_can_if_open:
|
||||
if (argc == 3) {
|
||||
hexagram_capture_close(capture);
|
||||
} else {
|
||||
hexagram_capture_destroy(capture);
|
||||
}
|
||||
|
||||
error_capture_open:
|
||||
|
|
25
include/hexagram/can.h
Normal file
25
include/hexagram/can.h
Normal file
|
@ -0,0 +1,25 @@
|
|||
#ifndef _HEXAGRAM_CAN_H
|
||||
#define _HEXAGRAM_CAN_H
|
||||
|
||||
#include <net/if.h>
|
||||
|
||||
#include <linux/can.h>
|
||||
#include <linux/can/raw.h>
|
||||
|
||||
typedef struct _hexagram_can_if {
|
||||
struct sockaddr_can addr;
|
||||
struct ifreq ifr;
|
||||
int sock;
|
||||
} hexagram_can_if;
|
||||
|
||||
hexagram_can_if *hexagram_can_if_open(const char *name);
|
||||
|
||||
void hexagram_can_if_close(hexagram_can_if *can_if);
|
||||
|
||||
int hexagram_can_if_read(hexagram_can_if *can_if,
|
||||
struct can_frame *frame);
|
||||
|
||||
int hexagram_can_if_write(hexagram_can_if *can_if,
|
||||
struct can_frame *frame);
|
||||
|
||||
#endif /* _HEXAGRAM_CAN_H */
|
|
@ -6,6 +6,8 @@
|
|||
#include <sys/time.h>
|
||||
#include <linux/can.h>
|
||||
|
||||
#include <hexagram/can.h>
|
||||
|
||||
#define HEXAGRAM_CAPTURE_MAGIC "HCAN"
|
||||
#define HEXAGRAM_CAPTURE_ENDIAN 0x0a0b0c0d
|
||||
#define HEXAGRAM_CAPTURE_ENDIAN_SWAPPED 0x0d0c0b0a
|
||||
|
@ -40,4 +42,8 @@ ssize_t hexagram_capture_write(hexagram_capture *capture,
|
|||
struct can_frame *frame,
|
||||
struct timeval *timestamp);
|
||||
|
||||
int hexagram_capture_replay(hexagram_capture *capture,
|
||||
hexagram_can_if *can_if,
|
||||
float speed);
|
||||
|
||||
#endif /* _HEXAGRAM_CAPTURE_H */
|
||||
|
|
|
@ -7,10 +7,10 @@ CC = $(CROSS)cc
|
|||
CFLAGS += -fPIC -I$(INCLUDE_PATH)
|
||||
LDFLAGS =
|
||||
|
||||
HEADERS = capture.h pcapng.h
|
||||
HEADERS = can.h capture.h pcapng.h
|
||||
HEADERS_LOCAL = util.h
|
||||
|
||||
OBJS = capture.o pcapng.o
|
||||
OBJS = can.o capture.o pcapng.o
|
||||
|
||||
VERSION_MAJOR = 0
|
||||
VERSION_MINOR = 0.1
|
||||
|
|
67
src/can.c
Normal file
67
src/can.c
Normal file
|
@ -0,0 +1,67 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include <hexagram/can.h>
|
||||
|
||||
hexagram_can_if *hexagram_can_if_open(const char *name) {
|
||||
hexagram_can_if *can_if;
|
||||
|
||||
if (name == NULL) {
|
||||
errno = EINVAL;
|
||||
|
||||
goto error_no_name;
|
||||
}
|
||||
|
||||
if ((can_if = malloc(sizeof(*can_if))) == NULL) {
|
||||
goto error_malloc;
|
||||
}
|
||||
|
||||
if ((can_if->sock = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
|
||||
goto error_socket;
|
||||
}
|
||||
|
||||
strcpy(can_if->ifr.ifr_name, name);
|
||||
|
||||
if (ioctl(can_if->sock, SIOCGIFINDEX, &can_if->ifr) < 0) {
|
||||
goto error_ioctl_siocgifindex;
|
||||
}
|
||||
|
||||
can_if->addr.can_family = AF_CAN;
|
||||
can_if->addr.can_ifindex = can_if->ifr.ifr_ifindex;
|
||||
|
||||
if (bind(can_if->sock, (struct sockaddr *)&can_if->addr, sizeof(struct sockaddr)) < 0) {
|
||||
goto error_bind;
|
||||
}
|
||||
|
||||
return can_if;
|
||||
|
||||
error_bind:
|
||||
error_ioctl_siocgifindex:
|
||||
close(can_if->sock);
|
||||
|
||||
error_socket:
|
||||
free(can_if);
|
||||
|
||||
error_malloc:
|
||||
error_no_name:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void hexagram_can_if_close(hexagram_can_if *can_if) {
|
||||
close(can_if->sock);
|
||||
|
||||
free(can_if);
|
||||
}
|
||||
|
||||
int hexagram_can_if_read(hexagram_can_if *can_if,
|
||||
struct can_frame *frame) {
|
||||
return read(can_if->sock, frame, sizeof(*frame));
|
||||
}
|
||||
|
||||
int hexagram_can_if_write(hexagram_can_if *can_if,
|
||||
struct can_frame *frame) {
|
||||
return write(can_if->sock, frame, sizeof(*frame));
|
||||
}
|
|
@ -139,3 +139,40 @@ ssize_t hexagram_capture_write(hexagram_capture *capture,
|
|||
error_gettimeofday:
|
||||
return -1;
|
||||
}
|
||||
|
||||
int hexagram_capture_replay(hexagram_capture *capture,
|
||||
hexagram_can_if *can_if,
|
||||
float speed) {
|
||||
ssize_t len;
|
||||
|
||||
uint32_t timestamp[2],
|
||||
usec_last = 0;
|
||||
|
||||
struct can_frame frame;
|
||||
|
||||
while (1) {
|
||||
if ((len = hexagram_capture_read(capture,
|
||||
×tamp[0],
|
||||
×tamp[1],
|
||||
&frame)) < 0) {
|
||||
goto error_io;
|
||||
} else if (len == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (usec_last) {
|
||||
usleep(speed * (useconds_t)(timestamp[1] - usec_last));
|
||||
}
|
||||
|
||||
if (write(can_if->sock, &frame, sizeof(struct can_frame)) < 0) {
|
||||
goto error_io;
|
||||
}
|
||||
|
||||
usec_last = timestamp[1];
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
error_io:
|
||||
return -1;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue