-/*
- * $Id$
- */
-
/*
* candump.c
*
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
- * Send feedback to <socketcan-users@lists.berlios.de>
+ * Send feedback to <linux-can@vger.kernel.org>
*
*/
#include <stdio.h>
#include <stdlib.h>
+#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <libgen.h>
#include <time.h>
+#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>
#include <linux/can.h>
#include <linux/can/raw.h>
+#include <linux/net_tstamp.h>
#include "terminal.h"
#include "lib.h"
+#ifndef SIOCSHWTSTAMP
+# define SIOCSHWTSTAMP 0x89b0
+#endif
+
#define MAXSOCK 16 /* max. number of CAN interfaces given on the cmdline */
#define MAXIFNAMES 30 /* size of receive name index to omit ioctls */
#define MAXCOL 6 /* number of different colors for colorized output */
static char devname[MAXIFNAMES][IFNAMSIZ+1];
static int dindex[MAXIFNAMES];
static int max_devname_len; /* to prevent frazzled device name output */
+const int canfd_on = 1;
#define MAXANI 4
const char anichar[MAXANI] = {'|', '/', '-', '\\'};
+const char extra_m_info[4][4] = {"- -", "B -", "- E", "B E"};
extern int optind, opterr, optopt;
fprintf(stderr, "\nUsage: %s [options] <CAN interface>+\n", prg);
fprintf(stderr, " (use CTRL-C to terminate %s)\n\n", prg);
fprintf(stderr, "Options: -t <type> (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)\n");
+ fprintf(stderr, " -H (use hardware timestamping)\n");
fprintf(stderr, " -c (increment color mode level)\n");
fprintf(stderr, " -i (binary output - may exceed 80 chars/line)\n");
fprintf(stderr, " -a (enable additional ASCII output)\n");
fprintf(stderr, " -r <size> (set socket receive buffer to <size>)\n");
fprintf(stderr, " -d (monitor dropped CAN frames)\n");
fprintf(stderr, " -e (dump CAN error frames in human-readable format)\n");
+ fprintf(stderr, " -x (print extra message infos, rx/tx brs esi)\n");
+ fprintf(stderr, " -T <msecs> (terminate after <msecs> without any reception)\n");
fprintf(stderr, "\n");
fprintf(stderr, "Up to %d CAN interfaces with optional filter sets can be specified\n", MAXSOCK);
fprintf(stderr, "on the commandline in the form: <ifname>[,filter]*\n");
fprintf(stderr, " <can_id>:<can_mask> (matches when <received_can_id> & mask == can_id & mask)\n");
fprintf(stderr, " <can_id>~<can_mask> (matches when <received_can_id> & mask != can_id & mask)\n");
fprintf(stderr, " #<error_mask> (set error frame filter, see include/linux/can/error.h)\n");
+ fprintf(stderr, " [j|J] (join the given CAN filters - logical AND semantic)\n");
fprintf(stderr, "\nCAN IDs, masks and data content are given and expected in hexadecimal values.\n");
fprintf(stderr, "When can_id and can_mask are both 8 digits, they are assumed to be 29 bit EFF.\n");
fprintf(stderr, "Without any given filter all data frames are received ('0:0' default filter).\n");
int bridge = 0;
useconds_t bridge_delay = 0;
unsigned char timestamp = 0;
+ int hw_timestamp = 0;
unsigned char dropmonitor = 0;
+ unsigned char extra_msg_info = 0;
unsigned char silent = SILENT_INI;
unsigned char silentani = 0;
unsigned char color = 0;
int rcvbuf_size = 0;
int opt, ret;
int currmax, numfilter;
+ int join_filter;
char *ptr, *nptr;
struct sockaddr_can addr;
char ctrlmsg[CMSG_SPACE(sizeof(struct timeval)) + CMSG_SPACE(sizeof(__u32))];
struct cmsghdr *cmsg;
struct can_filter *rfilter;
can_err_mask_t err_mask;
- struct can_frame frame;
- int nbytes, i;
+ struct canfd_frame frame;
+ int nbytes, i, maxdlen;
struct ifreq ifr;
struct timeval tv, last_tv;
+ struct timeval timeout, timeout_config = { 0, 0 }, *timeout_current = NULL;
FILE *logfile = NULL;
signal(SIGTERM, sigterm);
last_tv.tv_sec = 0;
last_tv.tv_usec = 0;
- while ((opt = getopt(argc, argv, "t:ciaSs:b:B:u:ldLn:r:he?")) != -1) {
+ while ((opt = getopt(argc, argv, "t:ciaSs:b:B:u:ldxLn:r:heT:H?")) != -1) {
switch (opt) {
case 't':
timestamp = optarg[0];
}
break;
+ case 'H':
+ hw_timestamp = 1;
+ break;
+
case 'c':
color++;
break;
dropmonitor = 1;
break;
+ case 'x':
+ extra_msg_info = 1;
+ break;
+
case 'L':
logfrmt = 1;
break;
}
break;
+ case 'T':
+ errno = 0;
+ timeout_config.tv_usec = strtol(optarg, NULL, 0);
+ if (errno != 0) {
+ print_usage(basename(argv[0]));
+ exit(1);
+ }
+ timeout_config.tv_sec = timeout_config.tv_usec / 1000;
+ timeout_config.tv_usec = (timeout_config.tv_usec % 1000) * 1000;
+ timeout_current = &timeout;
+ break;
default:
print_usage(basename(argv[0]));
exit(1);
numfilter = 0;
err_mask = 0;
+ join_filter = 0;
while (nptr) {
rfilter[numfilter].can_id |= CAN_INV_FILTER;
rfilter[numfilter].can_mask &= ~CAN_ERR_FLAG;
numfilter++;
+ } else if (*ptr == 'j' || *ptr == 'J') {
+ join_filter = 1;
} else if (sscanf(ptr, "#%x", &err_mask) != 1) {
fprintf(stderr, "Error in filter option parsing: '%s'\n", ptr);
return 1;
setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_ERR_FILTER,
&err_mask, sizeof(err_mask));
+ if (join_filter && setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_JOIN_FILTERS,
+ &join_filter, sizeof(join_filter)) < 0) {
+ perror("setsockopt CAN_RAW_JOIN_FILTERS not supported by your Linux Kernel");
+ return 1;
+ }
+
if (numfilter)
setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FILTER,
rfilter, numfilter * sizeof(struct can_filter));
} /* if (nptr) */
+ /* try to switch the socket into CAN FD mode */
+ setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));
+
if (rcvbuf_size) {
int curr_rcvbuf_size;
}
if (timestamp || log || logfrmt) {
+ if (hw_timestamp == 0) {
+ const int timestamp_on = 1;
- const int timestamp_on = 1;
+ if (setsockopt(s[i], SOL_SOCKET, SO_TIMESTAMP,
+ ×tamp_on, sizeof(timestamp_on)) < 0) {
+ perror("setsockopt SO_TIMESTAMP");
+ return 1;
+ }
+ } else {
+ struct ifreq hwtstamp;
+ struct hwtstamp_config hwconfig;
+
+ memset(&hwtstamp, 0, sizeof(hwtstamp));
+ strncpy(hwtstamp.ifr_name, ifr.ifr_name, sizeof(hwtstamp.ifr_name));
+ hwtstamp.ifr_data = (void *)&hwconfig;
+ memset(&hwconfig, 0, sizeof(hwconfig));
+ hwconfig.tx_type = HWTSTAMP_TX_OFF;
+ hwconfig.rx_filter = HWTSTAMP_FILTER_ALL;
+
+ if (ioctl(s[i], SIOCSHWTSTAMP, &hwtstamp) < 0) {
+ perror("SIOCSHWTSTAMP");
+ return 1;
+ }
- if (setsockopt(s[i], SOL_SOCKET, SO_TIMESTAMP,
- ×tamp_on, sizeof(timestamp_on)) < 0) {
- perror("setsockopt SO_TIMESTAMP");
- return 1;
+ const int so_timestamping_flags = SOF_TIMESTAMPING_RAW_HARDWARE;
+
+ if (setsockopt(s[i], SOL_SOCKET, SO_TIMESTAMPING,
+ &so_timestamping_flags,
+ sizeof(so_timestamping_flags)) < 0)
+ perror("setsockopt SO_TIMESTAMPING");
}
}
for (i=0; i<currmax; i++)
FD_SET(s[i], &rdfs);
- if ((ret = select(s[currmax-1]+1, &rdfs, NULL, NULL, NULL)) < 0) {
+ if (timeout_current)
+ *timeout_current = timeout_config;
+
+ if ((ret = select(s[currmax-1]+1, &rdfs, NULL, NULL, timeout_current)) <= 0) {
//perror("select");
running = 0;
continue;
return 1;
}
- if (nbytes < sizeof(struct can_frame)) {
+ if ((size_t)nbytes == CAN_MTU)
+ maxdlen = CAN_MAX_DLEN;
+ else if ((size_t)nbytes == CANFD_MTU)
+ maxdlen = CANFD_MAX_DLEN;
+ else {
fprintf(stderr, "read: incomplete CAN frame\n");
return 1;
}
if (bridge_delay)
usleep(bridge_delay);
- nbytes = write(bridge, &frame, sizeof(struct can_frame));
+ nbytes = write(bridge, &frame, nbytes);
if (nbytes < 0) {
perror("bridge write");
return 1;
- } else if (nbytes < sizeof(struct can_frame)) {
+ } else if ((size_t)nbytes != CAN_MTU && (size_t)nbytes != CANFD_MTU) {
fprintf(stderr,"bridge write: incomplete CAN frame\n");
return 1;
}
tv = *(struct timeval *)CMSG_DATA(cmsg);
else if (cmsg->cmsg_type == SO_RXQ_OVFL)
dropcnt[i] = *(__u32 *)CMSG_DATA(cmsg);
+ else if (cmsg->cmsg_type == SO_TIMESTAMPING) {
+ struct timespec *ts = (struct timespec *) CMSG_DATA(cmsg);
+ tv.tv_sec = ts[2].tv_sec;
+ tv.tv_usec = ts[2].tv_nsec / 1000;
+ }
+
}
/* check for (unlikely) dropped frames on this specific socket */
if (dropcnt[i] != last_dropcnt[i]) {
- __u32 frames;
-
- if (dropcnt[i] > last_dropcnt[i])
- frames = dropcnt[i] - last_dropcnt[i];
- else
- frames = 4294967295U - last_dropcnt[i] + dropcnt[i]; /* 4294967295U == UINT32_MAX */
+ __u32 frames = dropcnt[i] - last_dropcnt[i];
if (silent != SILENT_ON)
printf("DROPCOUNT: dropped %d CAN frame%s on '%s' socket (total drops %d)\n",
idx = idx2dindex(addr.can_ifindex, s[i]);
+ /* once we detected a EFF frame indent SFF frames accordingly */
+ if (frame.can_id & CAN_EFF_FLAG)
+ view |= CANLIB_VIEW_INDENT_SFF;
+
if (log) {
+ char buf[CL_CFSZ]; /* max length */
+
/* log CAN frame with absolute timestamp & device */
- fprintf(logfile, "(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
- fprintf(logfile, "%*s ", max_devname_len, devname[idx]);
- /* without seperator as logfile use-case is parsing */
- fprint_canframe(logfile, &frame, "\n", 0);
+ sprint_canframe(buf, &frame, 0, maxdlen);
+ fprintf(logfile, "(%010ld.%06ld) %*s %s\n",
+ tv.tv_sec, tv.tv_usec,
+ max_devname_len, devname[idx], buf);
}
if (logfrmt) {
+ char buf[CL_CFSZ]; /* max length */
+
/* print CAN frame in log file style to stdout */
- printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
- printf("%*s ", max_devname_len, devname[idx]);
- fprint_canframe(stdout, &frame, "\n", 0);
+ sprint_canframe(buf, &frame, 0, maxdlen);
+ printf("(%010ld.%06ld) %*s %s\n",
+ tv.tv_sec, tv.tv_usec,
+ max_devname_len, devname[idx], buf);
goto out_fflush; /* no other output to stdout */
}
switch (timestamp) {
case 'a': /* absolute with timestamp */
- printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
+ printf("(%010ld.%06ld) ", tv.tv_sec, tv.tv_usec);
break;
case 'A': /* absolute with date */
printf(" %s", (color && (color<3))?col_on[idx%MAXCOL]:"");
printf("%*s", max_devname_len, devname[idx]);
+
+ if (extra_msg_info) {
+
+ if (msg.msg_flags & MSG_DONTROUTE)
+ printf (" TX %s", extra_m_info[frame.flags & 3]);
+ else
+ printf (" RX %s", extra_m_info[frame.flags & 3]);
+ }
+
printf("%s ", (color==1)?col_off:"");
- fprint_long_canframe(stdout, &frame, NULL, view);
+ fprint_long_canframe(stdout, &frame, NULL, view, maxdlen);
printf("%s", (color>1)?col_off:"");
printf("\n");