]> rtime.felk.cvut.cz Git - can-utils.git/blobdiff - slcan_attach.c
use line discipline number from linux/tty.h
[can-utils.git] / slcan_attach.c
index ed6afc7f2bc2bb19f683a6fae46e16784b0a74df..ab8ff0ac30f1c87ad76c3df0e66533386668391c 100644 (file)
@@ -1,7 +1,3 @@
-/*
- *  $Id$
- */
-
 /*
  * slcan_attach.c - userspace tool for serial line CAN interface driver SLCAN
  *
@@ -41,7 +37,7 @@
  * 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 <fcntl.h>
 #include <unistd.h>
 #include <getopt.h>
+#include <sys/socket.h>
 #include <sys/ioctl.h>
-
-#define LDISC_N_SLCAN 17 /* default slcan line discipline since Kernel 2.6.25 */
+#include <net/if.h>
+#include <termios.h>
+#include <linux/tty.h>
 
 void print_usage(char *prg)
 {
        fprintf(stderr, "\nUsage: %s [options] tty\n\n", 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, "         -s <speed> (set CAN speed 0..8)\n");
        fprintf(stderr, "         -b <btr>   (set bit time register value)\n");
        fprintf(stderr, "         -d         (only detach line discipline)\n");
        fprintf(stderr, "         -w         (attach - wait for keypess - detach)\n");
+       fprintf(stderr, "         -n <name>  (assign created netdevice name)\n");
        fprintf(stderr, "\nExamples:\n");
-       fprintf(stderr, "slcan_attach -w -o -s6 -c /dev/ttyS1\n");
+       fprintf(stderr, "slcan_attach -w -o -f -s6 -c /dev/ttyS1\n");
        fprintf(stderr, "slcan_attach /dev/ttyS1\n");
        fprintf(stderr, "slcan_attach -d /dev/ttyS1\n");
+       fprintf(stderr, "slcan_attach -w -n can15 /dev/ttyS1\n");
        fprintf(stderr, "\n");
        exit(1);
 }
@@ -75,18 +76,20 @@ void print_usage(char *prg)
 int main(int argc, char **argv)
 {
        int fd;
-       int ldisc = LDISC_N_SLCAN;
+       int ldisc = N_SLCAN;
        int detach = 0;
        int waitkey = 0;
        int send_open = 0;
        int send_close = 0;
+       int send_read_status_flags = 0;
        char *speed = NULL;
        char *btr = NULL;
-       char buf[10];
+       char buf[IFNAMSIZ+1];
        char *tty;
+       char *name = NULL;
        int opt;
 
-       while ((opt = getopt(argc, argv, "l:dwocs:b:?")) != -1) {
+       while ((opt = getopt(argc, argv, "l:dwocfs:b:n:?")) != -1) {
                switch (opt) {
                case 'l':
                        fprintf(stderr, "Ignored option '-l'\n");
@@ -108,6 +111,10 @@ int main(int argc, char **argv)
                        send_close = 1;
                        break;
 
+               case 'f':
+                       send_read_status_flags = 1;
+                       break;
+
                case 's':
                        speed = optarg;
                        if (strlen(speed) > 1)
@@ -120,6 +127,12 @@ int main(int argc, char **argv)
                                print_usage(argv[0]);
                        break;
 
+               case 'n':
+                       name = optarg;
+                       if (strlen(name) > IFNAMSIZ-1)
+                               print_usage(argv[0]);
+                       break;
+
                case '?':
                default:
                        print_usage(argv[0]);
@@ -140,12 +153,17 @@ int main(int argc, char **argv)
        if (waitkey || !detach) {
 
                if (speed) {
-                       sprintf(buf, "S%s\r", speed);
+                       sprintf(buf, "C\rS%s\r", speed);
                        write(fd, buf, strlen(buf));
                }
 
                if (btr) {
-                       sprintf(buf, "s%s\r", btr);
+                       sprintf(buf, "C\rs%s\r", btr);
+                       write(fd, buf, strlen(buf));
+               }
+
+               if (send_read_status_flags) {
+                       sprintf(buf, "F\r");
                        write(fd, buf, strlen(buf));
                }
 
@@ -154,10 +172,41 @@ int main(int argc, char **argv)
                        write(fd, buf, strlen(buf));
                }
 
+               /* set slcan line discipline on given tty */
                if (ioctl (fd, TIOCSETD, &ldisc) < 0) {
-                       perror("ioctl");
+                       perror("ioctl TIOCSETD");
                        exit(1);
                }
+
+               /* retrieve the name of the created CAN netdevice */
+               if (ioctl (fd, SIOCGIFNAME, buf) < 0) {
+                       perror("ioctl SIOCGIFNAME");
+                       exit(1);
+               }
+
+               printf("attached tty %s to netdevice %s\n", tty, buf);
+
+               /* try to rename the created device if requested */
+               if (name) {
+                       struct ifreq ifr;
+                       int s = socket(PF_INET, SOCK_DGRAM, 0);
+                       printf("rename netdevice %s to %s ... ", buf, name);
+
+                       if (s < 0)
+                               perror("socket for interface rename");
+                       else {
+                               strncpy (ifr.ifr_name, buf, IFNAMSIZ);
+                               strncpy (ifr.ifr_newname, name, IFNAMSIZ);
+                               if (ioctl(s, SIOCSIFNAME, &ifr) < 0)
+                                       printf("failed!\n");
+                               else
+                                       printf("ok.\n");
+                               close(s);
+                       }
+               }
        }
 
        if (waitkey) {