]> rtime.felk.cvut.cz Git - can-utils.git/blobdiff - slcand.c
candump: Enable HW timestamping before using it
[can-utils.git] / slcand.c
index 69d30f7b2064472b841bb31d617b5048aa79a2f2..a8b62f2f5daa7c90b4c223312ef96ab48481f572 100644 (file)
--- a/slcand.c
+++ b/slcand.c
@@ -28,6 +28,7 @@
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/socket.h>
 #include <fcntl.h>
 #include <syslog.h>
 #include <errno.h>
@@ -36,9 +37,8 @@
 #include <sys/ioctl.h>
 #include <net/if.h>
 #include <termios.h>
-
-/* default slcan line discipline since Kernel 2.6.25 */
-#define LDISC_N_SLCAN 17
+#include <linux/tty.h>
+#include <linux/sockios.h>
 
 /* Change this to whatever your daemon is called */
 #define DAEMON_NAME "slcand"
@@ -49,9 +49,6 @@
 /* The length of ttypath buffer */
 #define TTYPATH_LENGTH 64
 
-#define EXIT_SUCCESS 0
-#define EXIT_FAILURE 1
-
 /* UART flow control types */
 #define FLOW_NONE 0
 #define FLOW_HW 1
@@ -63,6 +60,7 @@ void print_usage(char *prg)
        fprintf(stderr, "Options: -o         (send open command 'O\\r')\n");
        fprintf(stderr, "         -c         (send close command 'C\\r')\n");
        fprintf(stderr, "         -f         (read status flags with 'F\\r' to reset error states)\n");
+       fprintf(stderr, "         -l         (send listen only command 'L\\r', overrides -o)\n");
        fprintf(stderr, "         -s <speed> (set CAN speed 0..8)\n");
        fprintf(stderr, "         -S <speed> (set UART speed in baud)\n");
        fprintf(stderr, "         -t <type>  (set UART flow control type 'hw' or 'sw')\n");
@@ -70,8 +68,9 @@ void print_usage(char *prg)
        fprintf(stderr, "         -F         (stay in foreground; no daemonize)\n");
        fprintf(stderr, "         -h         (show this help page)\n");
        fprintf(stderr, "\nExamples:\n");
-       fprintf(stderr, "slcand -o -c -f -s6 ttyslcan0\n");
-       fprintf(stderr, "slcand -o -c -f -s6 ttyslcan0 can0\n");
+       fprintf(stderr, "slcand -o -c -f -s6 ttyUSB0\n");
+       fprintf(stderr, "slcand -o -c -f -s6 ttyUSB0 can0\n");
+       fprintf(stderr, "slcand -o -c -f -s6 /dev/ttyUSB0\n");
        fprintf(stderr, "\n");
        exit(EXIT_FAILURE);
 }
@@ -173,6 +172,7 @@ int main(int argc, char *argv[])
        int opt;
        int send_open = 0;
        int send_close = 0;
+       int send_listen = 0;
        int send_read_status_flags = 0;
        char *speed = NULL;
        char *uart_speed_str = NULL;
@@ -181,12 +181,12 @@ int main(int argc, char *argv[])
        char *btr = NULL;
        int run_as_daemon = 1;
        char *pch;
-       int ldisc = LDISC_N_SLCAN;
+       int ldisc = N_SLCAN;
        int fd;
 
        ttypath[0] = '\0';
 
-       while ((opt = getopt(argc, argv, "ocfs:S:t:b:?hF")) != -1) {
+       while ((opt = getopt(argc, argv, "ocfls:S:t:b:?hF")) != -1) {
                switch (opt) {
                case 'o':
                        send_open = 1;
@@ -197,6 +197,9 @@ int main(int argc, char *argv[])
                case 'f':
                        send_read_status_flags = 1;
                        break;
+               case 'l':
+                       send_listen = 1;
+                       break;
                case 's':
                        speed = optarg;
                        if (strlen(speed) > 1)
@@ -251,10 +254,11 @@ int main(int argc, char *argv[])
 
        /* Prepare the tty device name string */
        pch = strstr(tty, devprefix);
-       if (pch == tty)
-               print_usage(argv[0]);
+       if (pch != tty)
+               snprintf(ttypath, TTYPATH_LENGTH, "%s%s", devprefix, tty);
+       else
+               snprintf(ttypath, TTYPATH_LENGTH, "%s", tty);
 
-       snprintf(ttypath, TTYPATH_LENGTH, "%s%s", devprefix, tty);
        syslog(LOG_INFO, "starting on TTY device %s", ttypath);
 
        /* Daemonize */
@@ -326,7 +330,10 @@ int main(int argc, char *argv[])
                write(fd, buf, strlen(buf));
        }
 
-       if (send_open) {
+       if (send_listen) {
+               sprintf(buf, "L\r");
+               write(fd, buf, strlen(buf));
+       } else if (send_open) {
                sprintf(buf, "O\r");
                write(fd, buf, strlen(buf));
        }
@@ -334,13 +341,13 @@ int main(int argc, char *argv[])
        /* set slcan like discipline on given tty */
        if (ioctl(fd, TIOCSETD, &ldisc) < 0) {
                perror("ioctl TIOCSETD");
-               exit(1);
+               exit(EXIT_FAILURE);
        }
        
        /* retrieve the name of the created CAN netdevice */
        if (ioctl(fd, SIOCGIFNAME, buf) < 0) {
                perror("ioctl SIOCGIFNAME");
-               exit(1);
+               exit(EXIT_FAILURE);
        }
 
        syslog(LOG_NOTICE, "attached TTY %s to netdevice %s\n", ttypath, buf);
@@ -359,7 +366,7 @@ int main(int argc, char *argv[])
                        if (ioctl(s, SIOCSIFNAME, &ifr) < 0) {
                                syslog(LOG_NOTICE, "netdevice %s rename to %s failed\n", buf, name);
                                perror("ioctl SIOCSIFNAME rename");
-                               exit(1);
+                               exit(EXIT_FAILURE);
                        } else
                                syslog(LOG_NOTICE, "netdevice %s renamed to %s\n", buf, name);