]> rtime.felk.cvut.cz Git - can-utils.git/blobdiff - canlogserver.c
candump: Enable HW timestamping before using it
[can-utils.git] / canlogserver.c
index b5a9d73339bfa8a0544bf66ccfc29cc9ea6fa685..e3350b739afb0c2e64d4930726988d32f4f551d2 100644 (file)
@@ -1,7 +1,3 @@
-/*
- *  $Id$
- */
-
 /*
  * canlogserver.c
  *
@@ -55,6 +51,7 @@
 #include <time.h>
 
 #include <sys/time.h>
+#include <sys/wait.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
@@ -65,7 +62,6 @@
 #include <linux/can.h>
 #include <linux/can/raw.h>
 #include <signal.h>
-#include <wait.h>
 #include <errno.h>
 
 #include "lib.h"
@@ -74,6 +70,9 @@
 #define ANYDEV "any"
 #define ANL "\r\n" /* newline in ASC mode */
 
+#define COMMENTSZ 200
+#define BUFSZ (sizeof("(1345212884.318850)") + IFNAMSIZ + 4 + CL_CFSZ + COMMENTSZ) /* for one line in the logfile */
+
 #define DEFPORT 28700
 
 static char devname[MAXDEV][IFNAMSIZ+1];
@@ -98,7 +97,7 @@ void print_usage(char *prg)
        fprintf(stderr, "       <received_can_id> & mask == value & mask\n");
        fprintf(stderr, "\n");
        fprintf(stderr, "When using more than one CAN interface the options\n");
-       fprintf(stderr, "m/v/i/e have comma seperated values e.g. '-m 0,7FF,0'\n");
+       fprintf(stderr, "m/v/i/e have comma separated values e.g. '-m 0,7FF,0'\n");
        fprintf(stderr, "\nUse interface name '%s' to receive from all CAN interfaces.\n\n", ANYDEV);
 }
 
@@ -183,15 +182,16 @@ int main(int argc, char **argv)
        int currmax = 1; /* we assume at least one can bus ;-) */
        struct sockaddr_can addr;
        struct can_filter rfilter;
-       struct can_frame frame;
-       int nbytes, i, j;
+       struct canfd_frame frame;
+       const int canfd_on = 1;
+       int nbytes, i, j, maxdlen;
        struct ifreq ifr;
-       struct timeval tv, last_tv;
+       struct timeval tv;
        int port = DEFPORT;
        struct sockaddr_in inaddr;
        struct sockaddr_in clientaddr;
        socklen_t sin_size = sizeof(clientaddr);
-       char temp[128];
+       char temp[BUFSZ];
 
        sigemptyset(&sigset);
        signalaction.sa_handler = &childdied;
@@ -204,10 +204,6 @@ int main(int argc, char **argv)
        sigaction(SIGTERM, &signalaction, NULL); /* install Signal for termination */
        sigaction(SIGINT, &signalaction, NULL); /* install Signal for termination */
 
-
-       last_tv.tv_sec  = 0;
-       last_tv.tv_usec = 0;
-
        while ((opt = getopt(argc, argv, "m:v:i:e:p:?")) != -1) {
 
                switch (opt) {
@@ -339,6 +335,9 @@ int main(int argc, char **argv)
                        setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_ERR_FILTER,
                                   &err_mask[i], sizeof(err_mask[i]));
 
+               /* try to switch the socket into CAN FD mode */
+               setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));
+
                j = strlen(argv[optind+i]);
 
                if (!(j < IFNAMSIZ)) {
@@ -387,14 +386,17 @@ int main(int argc, char **argv)
                                socklen_t len = sizeof(addr);
                                int idx;
 
-                               if ((nbytes = recvfrom(s[i], &frame,
-                                                      sizeof(struct can_frame), 0,
+                               if ((nbytes = recvfrom(s[i], &frame, CANFD_MTU, 0,
                                                       (struct sockaddr*)&addr, &len)) < 0) {
                                        perror("read");
                                        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;
                                }
@@ -407,7 +409,7 @@ int main(int argc, char **argv)
 
                                sprintf(temp, "(%ld.%06ld) %*s ",
                                        tv.tv_sec, tv.tv_usec, max_devname_len, devname[idx]);
-                               sprint_canframe(temp+strlen(temp), &frame, 0); 
+                               sprint_canframe(temp+strlen(temp), &frame, 0, maxdlen); 
                                strcat(temp, "\n");
 
                                if (write(accsocket, temp, strlen(temp)) < 0) {
@@ -415,13 +417,11 @@ int main(int argc, char **argv)
                                        return 1;
                                }
                    
-                               /* printf("%s\n",temp2); */
-
 #if 0
                                /* 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);
+                               fprint_canframe(stdout, &frame, "\n", 0, maxdlen);
 #endif
                        }