X-Git-Url: https://rtime.felk.cvut.cz/gitweb/can-utils.git/blobdiff_plain/afcac3462afc46e581c981c55ec1b42c01f663de..HEAD:/slcan_attach.c diff --git a/slcan_attach.c b/slcan_attach.c index 3d5bc55..40bddc8 100644 --- a/slcan_attach.c +++ b/slcan_attach.c @@ -1,7 +1,3 @@ -/* - * $Id$ - */ - /* * slcan_attach.c - userspace tool for serial line CAN interface driver SLCAN * @@ -41,64 +37,200 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * - * Send feedback to + * Send feedback to * */ #include #include +#include #include #include #include +#include #include +#include +#include +#include +#include -#define LDISC_N_SLCAN 17 /* default slcan line discipline since Kernel 2.6.25 */ - -void usage(char *name) +void print_usage(char *prg) { - fprintf(stderr, "Usage: %s [-d] [-l ldisc] tty\n", name); + fprintf(stderr, "\nUsage: %s [options] tty\n\n", prg); + fprintf(stderr, "Options: -o (send open command 'O\\r')\n"); + fprintf(stderr, " -l (send listen only command 'L\\r', overrides -o)\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 (set CAN speed 0..8)\n"); + fprintf(stderr, " -b (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 (assign created netdevice name)\n"); + fprintf(stderr, "\nExamples:\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); } 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_listen = 0; + int send_close = 0; + int send_read_status_flags = 0; + char *speed = NULL; + char *btr = NULL; + char buf[IFNAMSIZ+1]; char *tty; + char *name = NULL; int opt; - while ((opt = getopt(argc, argv, "l:d")) != -1) { + while ((opt = getopt(argc, argv, "ldwocfs:b:n:?")) != -1) { switch (opt) { + case 'd': + detach = 1; + break; + + case 'w': + waitkey = 1; + break; + + case 'o': + send_open = 1; + break; + case 'l': - ldisc = atoi(optarg); + send_listen = 1; break; - case 'd': - detach = 1; + case 'c': + send_close = 1; + break; + + case 'f': + send_read_status_flags = 1; + break; + + case 's': + speed = optarg; + if (strlen(speed) > 1) + print_usage(argv[0]); break; + + case 'b': + btr = optarg; + if (strlen(btr) > 6) + print_usage(argv[0]); + break; + + case 'n': + name = optarg; + if (strlen(name) > IFNAMSIZ-1) + print_usage(argv[0]); + break; + + case '?': default: - usage(argv[0]); + print_usage(argv[0]); break; } } if (argc - optind != 1) - usage(argv[0]); + print_usage(argv[0]); tty = argv[optind]; - if ((fd = open (tty, O_RDONLY | O_NOCTTY)) < 0) { + if ((fd = open (tty, O_WRONLY | O_NOCTTY)) < 0) { perror(tty); exit(1); } - if (detach) + if (waitkey || !detach) { + + if (speed) { + sprintf(buf, "C\rS%s\r", speed); + write(fd, buf, strlen(buf)); + } + + if (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)); + } + + 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)); + } + + /* set slcan line discipline on given tty */ + if (ioctl (fd, TIOCSETD, &ldisc) < 0) { + 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) { + printf("Press any key to detach %s ...\n", tty); + getchar(); + } + + if (waitkey || detach) { ldisc = N_TTY; + if (ioctl (fd, TIOCSETD, &ldisc) < 0) { + perror("ioctl"); + exit(1); + } - if (ioctl (fd, TIOCSETD, &ldisc) < 0) { - perror("ioctl"); - exit(1); + if (send_close) { + sprintf(buf, "C\r"); + write(fd, buf, strlen(buf)); + } } close(fd);