]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - slcan_attach.c
can-utils: AOSP build clean up
[sojka/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
55 #define LDISC_N_SLCAN 17 /* default slcan line discipline since Kernel 2.6.25 */
56
57 void print_usage(char *prg)
58 {
59         fprintf(stderr, "\nUsage: %s [options] tty\n\n", prg);
60         fprintf(stderr, "Options: -o         (send open command 'O\\r')\n");
61         fprintf(stderr, "         -c         (send close command 'C\\r')\n");
62         fprintf(stderr, "         -f         (read status flags with 'F\\r' to reset error states)\n");
63         fprintf(stderr, "         -s <speed> (set CAN speed 0..8)\n");
64         fprintf(stderr, "         -b <btr>   (set bit time register value)\n");
65         fprintf(stderr, "         -d         (only detach line discipline)\n");
66         fprintf(stderr, "         -w         (attach - wait for keypess - detach)\n");
67         fprintf(stderr, "         -n <name>  (assign created netdevice name)\n");
68         fprintf(stderr, "\nExamples:\n");
69         fprintf(stderr, "slcan_attach -w -o -f -s6 -c /dev/ttyS1\n");
70         fprintf(stderr, "slcan_attach /dev/ttyS1\n");
71         fprintf(stderr, "slcan_attach -d /dev/ttyS1\n");
72         fprintf(stderr, "slcan_attach -w -n can15 /dev/ttyS1\n");
73         fprintf(stderr, "\n");
74         exit(1);
75 }
76
77 int main(int argc, char **argv)
78 {
79         int fd;
80         int ldisc = LDISC_N_SLCAN;
81         int detach = 0;
82         int waitkey = 0;
83         int send_open = 0;
84         int send_close = 0;
85         int send_read_status_flags = 0;
86         char *speed = NULL;
87         char *btr = NULL;
88         char buf[IFNAMSIZ+1];
89         char *tty;
90         char *name = NULL;
91         int opt;
92
93         while ((opt = getopt(argc, argv, "l:dwocfs:b:n:?")) != -1) {
94                 switch (opt) {
95                 case 'l':
96                         fprintf(stderr, "Ignored option '-l'\n");
97                         break;
98
99                 case 'd':
100                         detach = 1;
101                         break;
102
103                 case 'w':
104                         waitkey = 1;
105                         break;
106
107                 case 'o':
108                         send_open = 1;
109                         break;
110
111                 case 'c':
112                         send_close = 1;
113                         break;
114
115                 case 'f':
116                         send_read_status_flags = 1;
117                         break;
118
119                 case 's':
120                         speed = optarg;
121                         if (strlen(speed) > 1)
122                                 print_usage(argv[0]);
123                         break;
124
125                 case 'b':
126                         btr = optarg;
127                         if (strlen(btr) > 6)
128                                 print_usage(argv[0]);
129                         break;
130
131                 case 'n':
132                         name = optarg;
133                         if (strlen(name) > IFNAMSIZ-1)
134                                 print_usage(argv[0]);
135                         break;
136
137                 case '?':
138                 default:
139                         print_usage(argv[0]);
140                         break;
141                 }
142         }
143
144         if (argc - optind != 1)
145                 print_usage(argv[0]);
146
147         tty = argv[optind];
148
149         if ((fd = open (tty, O_WRONLY | O_NOCTTY)) < 0) {
150                 perror(tty);
151                 exit(1);
152         }
153
154         if (waitkey || !detach) {
155
156                 if (speed) {
157                         sprintf(buf, "C\rS%s\r", speed);
158                         write(fd, buf, strlen(buf));
159                 }
160
161                 if (btr) {
162                         sprintf(buf, "C\rs%s\r", btr);
163                         write(fd, buf, strlen(buf));
164                 }
165
166                 if (send_read_status_flags) {
167                         sprintf(buf, "F\r");
168                         write(fd, buf, strlen(buf));
169                 }
170
171                 if (send_open) {
172                         sprintf(buf, "O\r");
173                         write(fd, buf, strlen(buf));
174                 }
175
176                 /* set slcan line discipline on given tty */
177                 if (ioctl (fd, TIOCSETD, &ldisc) < 0) {
178                         perror("ioctl TIOCSETD");
179                         exit(1);
180                 }
181
182                 /* retrieve the name of the created CAN netdevice */
183                 if (ioctl (fd, SIOCGIFNAME, buf) < 0) {
184                         perror("ioctl SIOCGIFNAME");
185                         exit(1);
186                 }
187
188                 printf("attached tty %s to netdevice %s\n", tty, buf);
189
190                 /* try to rename the created device if requested */
191                 if (name) {
192                         struct ifreq ifr;
193                         int s = socket(PF_INET, SOCK_DGRAM, 0);
194  
195                         printf("rename netdevice %s to %s ... ", buf, name);
196
197                         if (s < 0)
198                                 perror("socket for interface rename");
199                         else {
200                                 strncpy (ifr.ifr_name, buf, IFNAMSIZ);
201                                 strncpy (ifr.ifr_newname, name, IFNAMSIZ);
202  
203                                 if (ioctl(s, SIOCSIFNAME, &ifr) < 0)
204                                         printf("failed!\n");
205                                 else
206                                         printf("ok.\n");
207  
208                                 close(s);
209                         }
210                 }
211         }
212
213         if (waitkey) {
214                 printf("Press any key to detach %s ...\n", tty);
215                 getchar();
216         }
217
218         if (waitkey || detach) {
219                 ldisc = N_TTY;
220                 if (ioctl (fd, TIOCSETD, &ldisc) < 0) {
221                         perror("ioctl");
222                         exit(1);
223                 }
224
225                 if (send_close) {
226                         sprintf(buf, "C\r");
227                         write(fd, buf, strlen(buf));
228                 }
229         }
230
231         close(fd);
232
233         return 0;
234 }