2019-05-22 22:42:21 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
2019-05-23 02:28:34 -05:00
|
|
|
#include <getopt.h>
|
2019-05-23 22:02:15 -05:00
|
|
|
#include <sysexits.h>
|
2019-05-22 22:42:21 -05:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <hexagram/can.h>
|
|
|
|
#include <hexagram/capture.h>
|
|
|
|
|
|
|
|
#include "can2dump.h"
|
|
|
|
|
2019-05-23 02:28:34 -05:00
|
|
|
enum timestamp_type {
|
|
|
|
TIMESTAMP_NONE,
|
|
|
|
TIMESTAMP_ABSOLUTE,
|
|
|
|
TIMESTAMP_DELTA,
|
|
|
|
TIMESTAMP_ZERO,
|
|
|
|
TIMESTAMP_DATE
|
|
|
|
};
|
|
|
|
|
2019-05-23 22:02:15 -05:00
|
|
|
struct optvalues {
|
|
|
|
enum timestamp_type tstype;
|
|
|
|
};
|
|
|
|
|
2019-05-22 22:42:21 -05:00
|
|
|
char *hexagram_arglist_can2dump(void) {
|
2019-05-23 02:28:34 -05:00
|
|
|
return "[--timestamp=|adzA|] ifname [file.can] [candump.txt]";
|
2019-05-22 22:42:21 -05:00
|
|
|
}
|
|
|
|
|
2019-05-23 01:20:05 -05:00
|
|
|
static int usage(int argc, char **argv, const char *message, ...) {
|
2019-05-22 22:42:21 -05:00
|
|
|
if (message) {
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, message);
|
|
|
|
vfprintf(stderr, message, args);
|
2019-05-23 02:28:34 -05:00
|
|
|
fprintf(stderr, "\n");
|
2019-05-22 22:42:21 -05:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "usage: hexagram %s %s\n", argv[0], hexagram_arglist_can2dump());
|
|
|
|
|
2019-05-23 22:02:15 -05:00
|
|
|
return EX_USAGE;
|
2019-05-22 22:42:21 -05:00
|
|
|
}
|
|
|
|
|
2019-05-23 22:02:15 -05:00
|
|
|
static int parseopts(struct optvalues *v,
|
|
|
|
struct option opts[],
|
|
|
|
int *argc,
|
|
|
|
char **argv[]) {
|
|
|
|
int ch, index;
|
2019-05-23 02:28:34 -05:00
|
|
|
|
2019-05-23 22:02:15 -05:00
|
|
|
while ((ch = getopt_long(*argc, *argv, "t:", opts, &index)) >= 0) {
|
2019-05-23 02:28:34 -05:00
|
|
|
switch (ch) {
|
|
|
|
case 't':
|
|
|
|
switch (optarg[0]) {
|
2019-05-23 22:02:15 -05:00
|
|
|
case 'a': v->tstype = TIMESTAMP_ABSOLUTE; break;
|
|
|
|
case 'd': v->tstype = TIMESTAMP_DELTA; break;
|
|
|
|
case 'z': v->tstype = TIMESTAMP_ZERO; break;
|
|
|
|
case 'A': v->tstype = TIMESTAMP_DATE; break;
|
2019-05-23 02:28:34 -05:00
|
|
|
|
|
|
|
default:
|
2019-05-23 22:02:15 -05:00
|
|
|
return usage(*argc, *argv, "Invalid timestamp type");
|
2019-05-23 02:28:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2019-05-23 22:02:15 -05:00
|
|
|
if (ch == '?') {
|
|
|
|
return usage(*argc, *argv, NULL);
|
|
|
|
} else {
|
|
|
|
return usage(*argc, *argv, "Unknown flag '%c'", ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2019-05-23 02:28:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 22:02:15 -05:00
|
|
|
*argc -= optind;
|
|
|
|
*argv += optind;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hexagram_main_can2dump(int argc, char **argv) {
|
|
|
|
static struct option opts[] = {
|
|
|
|
{ "timestamp", required_argument, NULL, 't' },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct optvalues values = {
|
|
|
|
.tstype = TIMESTAMP_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
int status;
|
|
|
|
|
|
|
|
hexagram_capture *capture;
|
|
|
|
const char *ifname;
|
|
|
|
FILE *fh;
|
|
|
|
|
|
|
|
struct timeval timestamp;
|
|
|
|
struct can_frame frame;
|
|
|
|
|
|
|
|
int argn = argc;
|
|
|
|
char **args = argv;
|
|
|
|
|
|
|
|
if ((status = parseopts(&values, opts, &argn, &args)) != 0) {
|
|
|
|
return status;
|
|
|
|
}
|
2019-05-23 02:28:34 -05:00
|
|
|
|
|
|
|
if (argn == 0) {
|
2019-05-23 01:20:05 -05:00
|
|
|
return usage(argc, argv, "No CAN interface name provided");
|
2019-05-22 22:42:21 -05:00
|
|
|
}
|
|
|
|
|
2019-05-23 02:28:34 -05:00
|
|
|
ifname = args[0];
|
2019-05-22 22:42:21 -05:00
|
|
|
|
2019-05-23 02:28:34 -05:00
|
|
|
if (argn == 1) {
|
2019-05-22 22:42:21 -05:00
|
|
|
if ((capture = hexagram_capture_open_fd(fileno(stdin), O_RDONLY)) == NULL) {
|
|
|
|
perror("hexagram_capture_open_fd()");
|
|
|
|
|
|
|
|
goto error_capture_open;
|
|
|
|
}
|
2019-05-23 01:20:05 -05:00
|
|
|
|
|
|
|
fh = stdout;
|
2019-05-23 22:02:15 -05:00
|
|
|
} else if (argn > 1) {
|
2019-05-23 02:28:34 -05:00
|
|
|
if ((capture = hexagram_capture_open_file(args[1], O_RDONLY)) == NULL) {
|
2019-05-23 01:20:05 -05:00
|
|
|
perror("hexagram_capture_open_file()");
|
|
|
|
|
|
|
|
goto error_capture_open;
|
|
|
|
}
|
2019-05-23 22:02:15 -05:00
|
|
|
}
|
2019-05-23 01:20:05 -05:00
|
|
|
|
2019-05-23 22:02:15 -05:00
|
|
|
if (argn == 3) {
|
2019-05-23 02:28:34 -05:00
|
|
|
if ((fh = fopen(args[2], "w")) == NULL) {
|
2019-05-23 01:20:05 -05:00
|
|
|
goto error_fopen;
|
|
|
|
}
|
2019-05-23 22:02:15 -05:00
|
|
|
} else if (argn == 2) {
|
|
|
|
fh = stdout;
|
2019-05-22 22:42:21 -05:00
|
|
|
} else {
|
2019-05-23 02:28:34 -05:00
|
|
|
return usage(argc, argv, NULL);
|
2019-05-22 22:42:21 -05:00
|
|
|
}
|
|
|
|
|
2019-05-23 01:20:05 -05:00
|
|
|
while (hexagram_capture_read(capture, ×tamp, &frame) >= 0) {
|
2019-05-23 01:49:26 -05:00
|
|
|
uint8_t i;
|
|
|
|
|
2019-05-23 22:02:15 -05:00
|
|
|
switch (values.tstype) {
|
2019-05-23 02:28:34 -05:00
|
|
|
case TIMESTAMP_NONE:
|
2019-05-23 22:02:15 -05:00
|
|
|
fprintf(fh, " ");
|
|
|
|
|
2019-05-23 02:28:34 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TIMESTAMP_ABSOLUTE:
|
|
|
|
fprintf(fh, " (%zu.%zu) ",
|
|
|
|
timestamp.tv_sec, timestamp.tv_usec);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fh, "%6s %03X [%d] ",
|
2019-05-23 01:49:26 -05:00
|
|
|
ifname, frame.can_id, frame.can_dlc);
|
|
|
|
|
|
|
|
for (i=0; i<frame.can_dlc; i++) {
|
|
|
|
fprintf(fh, " %02X", frame.data[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fh, "\n");
|
2019-05-22 22:42:21 -05:00
|
|
|
}
|
|
|
|
|
2019-05-23 02:28:34 -05:00
|
|
|
if (argn == 1) {
|
2019-05-22 22:42:21 -05:00
|
|
|
hexagram_capture_destroy(capture);
|
2019-05-23 02:28:34 -05:00
|
|
|
} else if (argn >= 2) {
|
2019-05-23 01:20:05 -05:00
|
|
|
hexagram_capture_close(capture);
|
2019-05-22 22:42:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2019-05-23 01:20:05 -05:00
|
|
|
error_fopen:
|
2019-05-23 02:28:34 -05:00
|
|
|
if (argn == 1) {
|
2019-05-22 22:42:21 -05:00
|
|
|
hexagram_capture_destroy(capture);
|
2019-05-23 02:28:34 -05:00
|
|
|
} else if (argn >= 2) {
|
2019-05-23 01:20:05 -05:00
|
|
|
hexagram_capture_close(capture);
|
2019-05-22 22:42:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
error_capture_open:
|
|
|
|
return 1;
|
|
|
|
}
|