slcan_attach.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * slcan_attach.c - userspace tool for serial line CAN interface driver SLCAN
  3. *
  4. * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of Volkswagen nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * Alternatively, provided that this notice is retained in full, this
  20. * software may be distributed under the terms of the GNU General
  21. * Public License ("GPL") version 2, in which case the provisions of the
  22. * GPL apply INSTEAD OF those given above.
  23. *
  24. * The provided data structures and external interfaces from this code
  25. * are not restricted to be used by modules with a GPL compatible license.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  38. * DAMAGE.
  39. *
  40. * Send feedback to <linux-can@vger.kernel.org>
  41. *
  42. */
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <fcntl.h>
  47. #include <unistd.h>
  48. #include <getopt.h>
  49. #include <sys/socket.h>
  50. #include <sys/ioctl.h>
  51. #include <net/if.h>
  52. #include <termios.h>
  53. #include <linux/tty.h>
  54. #include <linux/sockios.h>
  55. void print_usage(char *prg)
  56. {
  57. fprintf(stderr, "\nUsage: %s [options] tty\n\n", prg);
  58. fprintf(stderr, "Options: -o (send open command 'O\\r')\n");
  59. fprintf(stderr, " -l (send listen only command 'L\\r', overrides -o)\n");
  60. fprintf(stderr, " -c (send close command 'C\\r')\n");
  61. fprintf(stderr, " -f (read status flags with 'F\\r' to reset error states)\n");
  62. fprintf(stderr, " -s <speed> (set CAN speed 0..8)\n");
  63. fprintf(stderr, " -b <btr> (set bit time register value)\n");
  64. fprintf(stderr, " -d (only detach line discipline)\n");
  65. fprintf(stderr, " -w (attach - wait for keypess - detach)\n");
  66. fprintf(stderr, " -n <name> (assign created netdevice name)\n");
  67. fprintf(stderr, "\nExamples:\n");
  68. fprintf(stderr, "slcan_attach -w -o -f -s6 -c /dev/ttyS1\n");
  69. fprintf(stderr, "slcan_attach /dev/ttyS1\n");
  70. fprintf(stderr, "slcan_attach -d /dev/ttyS1\n");
  71. fprintf(stderr, "slcan_attach -w -n can15 /dev/ttyS1\n");
  72. fprintf(stderr, "\n");
  73. exit(1);
  74. }
  75. int main(int argc, char **argv)
  76. {
  77. int fd;
  78. int ldisc = N_SLCAN;
  79. int detach = 0;
  80. int waitkey = 0;
  81. int send_open = 0;
  82. int send_listen = 0;
  83. int send_close = 0;
  84. int send_read_status_flags = 0;
  85. char *speed = NULL;
  86. char *btr = NULL;
  87. char buf[IFNAMSIZ+1];
  88. char *tty;
  89. char *name = NULL;
  90. int opt;
  91. while ((opt = getopt(argc, argv, "ldwocfs:b:n:?")) != -1) {
  92. switch (opt) {
  93. case 'd':
  94. detach = 1;
  95. break;
  96. case 'w':
  97. waitkey = 1;
  98. break;
  99. case 'o':
  100. send_open = 1;
  101. break;
  102. case 'l':
  103. send_listen = 1;
  104. break;
  105. case 'c':
  106. send_close = 1;
  107. break;
  108. case 'f':
  109. send_read_status_flags = 1;
  110. break;
  111. case 's':
  112. speed = optarg;
  113. if (strlen(speed) > 1)
  114. print_usage(argv[0]);
  115. break;
  116. case 'b':
  117. btr = optarg;
  118. if (strlen(btr) > 6)
  119. print_usage(argv[0]);
  120. break;
  121. case 'n':
  122. name = optarg;
  123. if (strlen(name) > IFNAMSIZ-1)
  124. print_usage(argv[0]);
  125. break;
  126. case '?':
  127. default:
  128. print_usage(argv[0]);
  129. break;
  130. }
  131. }
  132. if (argc - optind != 1)
  133. print_usage(argv[0]);
  134. tty = argv[optind];
  135. if ((fd = open (tty, O_WRONLY | O_NOCTTY)) < 0) {
  136. perror(tty);
  137. exit(1);
  138. }
  139. if (waitkey || !detach) {
  140. if (speed) {
  141. sprintf(buf, "C\rS%s\r", speed);
  142. write(fd, buf, strlen(buf));
  143. }
  144. if (btr) {
  145. sprintf(buf, "C\rs%s\r", btr);
  146. write(fd, buf, strlen(buf));
  147. }
  148. if (send_read_status_flags) {
  149. sprintf(buf, "F\r");
  150. write(fd, buf, strlen(buf));
  151. }
  152. if (send_listen) {
  153. sprintf(buf, "L\r");
  154. write(fd, buf, strlen(buf));
  155. } else if (send_open) {
  156. sprintf(buf, "O\r");
  157. write(fd, buf, strlen(buf));
  158. }
  159. /* set slcan line discipline on given tty */
  160. if (ioctl (fd, TIOCSETD, &ldisc) < 0) {
  161. perror("ioctl TIOCSETD");
  162. exit(1);
  163. }
  164. /* retrieve the name of the created CAN netdevice */
  165. if (ioctl (fd, SIOCGIFNAME, buf) < 0) {
  166. perror("ioctl SIOCGIFNAME");
  167. exit(1);
  168. }
  169. printf("attached tty %s to netdevice %s\n", tty, buf);
  170. /* try to rename the created device if requested */
  171. if (name) {
  172. struct ifreq ifr;
  173. int s = socket(PF_INET, SOCK_DGRAM, 0);
  174. printf("rename netdevice %s to %s ... ", buf, name);
  175. if (s < 0)
  176. perror("socket for interface rename");
  177. else {
  178. strncpy (ifr.ifr_name, buf, IFNAMSIZ);
  179. strncpy (ifr.ifr_newname, name, IFNAMSIZ);
  180. if (ioctl(s, SIOCSIFNAME, &ifr) < 0)
  181. printf("failed!\n");
  182. else
  183. printf("ok.\n");
  184. close(s);
  185. }
  186. }
  187. }
  188. if (waitkey) {
  189. printf("Press any key to detach %s ...\n", tty);
  190. getchar();
  191. }
  192. if (waitkey || detach) {
  193. ldisc = N_TTY;
  194. if (ioctl (fd, TIOCSETD, &ldisc) < 0) {
  195. perror("ioctl");
  196. exit(1);
  197. }
  198. if (send_close) {
  199. sprintf(buf, "C\r");
  200. write(fd, buf, strlen(buf));
  201. }
  202. }
  203. close(fd);
  204. return 0;
  205. }