]> rtime.felk.cvut.cz Git - can-utils.git/blob - slcan_attach.c
configure: switch to new libtool-2.0 macro
[can-utils.git] / slcan_attach.c
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
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <fcntl.h>
48 #include <unistd.h>
49 #include <getopt.h>
50 #include <sys/socket.h>
51 #include <sys/ioctl.h>
52 #include <net/if.h>
53 #include <termios.h>
54 #include <linux/tty.h>
55
56 void print_usage(char *prg)
57 {
58         fprintf(stderr, "\nUsage: %s [options] tty\n\n", prg);
59         fprintf(stderr, "Options: -o         (send open command 'O\\r')\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
76 int main(int argc, char **argv)
77 {
78         int fd;
79         int ldisc = N_SLCAN;
80         int detach = 0;
81         int waitkey = 0;
82         int send_open = 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
92         while ((opt = getopt(argc, argv, "l:dwocfs:b:n:?")) != -1) {
93                 switch (opt) {
94                 case 'l':
95                         fprintf(stderr, "Ignored option '-l'\n");
96                         break;
97
98                 case 'd':
99                         detach = 1;
100                         break;
101
102                 case 'w':
103                         waitkey = 1;
104                         break;
105
106                 case 'o':
107                         send_open = 1;
108                         break;
109
110                 case 'c':
111                         send_close = 1;
112                         break;
113
114                 case 'f':
115                         send_read_status_flags = 1;
116                         break;
117
118                 case 's':
119                         speed = optarg;
120                         if (strlen(speed) > 1)
121                                 print_usage(argv[0]);
122                         break;
123
124                 case 'b':
125                         btr = optarg;
126                         if (strlen(btr) > 6)
127                                 print_usage(argv[0]);
128                         break;
129
130                 case 'n':
131                         name = optarg;
132                         if (strlen(name) > IFNAMSIZ-1)
133                                 print_usage(argv[0]);
134                         break;
135
136                 case '?':
137                 default:
138                         print_usage(argv[0]);
139                         break;
140                 }
141         }
142
143         if (argc - optind != 1)
144                 print_usage(argv[0]);
145
146         tty = argv[optind];
147
148         if ((fd = open (tty, O_WRONLY | O_NOCTTY)) < 0) {
149                 perror(tty);
150                 exit(1);
151         }
152
153         if (waitkey || !detach) {
154
155                 if (speed) {
156                         sprintf(buf, "C\rS%s\r", speed);
157                         write(fd, buf, strlen(buf));
158                 }
159
160                 if (btr) {
161                         sprintf(buf, "C\rs%s\r", btr);
162                         write(fd, buf, strlen(buf));
163                 }
164
165                 if (send_read_status_flags) {
166                         sprintf(buf, "F\r");
167                         write(fd, buf, strlen(buf));
168                 }
169
170                 if (send_open) {
171                         sprintf(buf, "O\r");
172                         write(fd, buf, strlen(buf));
173                 }
174
175                 /* set slcan line discipline on given tty */
176                 if (ioctl (fd, TIOCSETD, &ldisc) < 0) {
177                         perror("ioctl TIOCSETD");
178                         exit(1);
179                 }
180
181                 /* retrieve the name of the created CAN netdevice */
182                 if (ioctl (fd, SIOCGIFNAME, buf) < 0) {
183                         perror("ioctl SIOCGIFNAME");
184                         exit(1);
185                 }
186
187                 printf("attached tty %s to netdevice %s\n", tty, buf);
188
189                 /* try to rename the created device if requested */
190                 if (name) {
191                         struct ifreq ifr;
192                         int s = socket(PF_INET, SOCK_DGRAM, 0);
193  
194                         printf("rename netdevice %s to %s ... ", buf, name);
195
196                         if (s < 0)
197                                 perror("socket for interface rename");
198                         else {
199                                 strncpy (ifr.ifr_name, buf, IFNAMSIZ);
200                                 strncpy (ifr.ifr_newname, name, IFNAMSIZ);
201  
202                                 if (ioctl(s, SIOCSIFNAME, &ifr) < 0)
203                                         printf("failed!\n");
204                                 else
205                                         printf("ok.\n");
206  
207                                 close(s);
208                         }
209                 }
210         }
211
212         if (waitkey) {
213                 printf("Press any key to detach %s ...\n", tty);
214                 getchar();
215         }
216
217         if (waitkey || detach) {
218                 ldisc = N_TTY;
219                 if (ioctl (fd, TIOCSETD, &ldisc) < 0) {
220                         perror("ioctl");
221                         exit(1);
222                 }
223
224                 if (send_close) {
225                         sprintf(buf, "C\r");
226                         write(fd, buf, strlen(buf));
227                 }
228         }
229
230         close(fd);
231
232         return 0;
233 }