]> rtime.felk.cvut.cz Git - can-utils.git/blob - slcand.c
autotools: fix syntax error
[can-utils.git] / slcand.c
1 /*
2  * slcand.c - userspace daemon for serial line CAN interface driver SLCAN
3  *
4  * Copyright (c) 2009 Robert Haddon <robert.haddon@verari.com>
5  * Copyright (c) 2009 Verari Systems Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  * Send feedback to <linux-can@vger.kernel.org>
22  *
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/socket.h>
32 #include <fcntl.h>
33 #include <syslog.h>
34 #include <errno.h>
35 #include <pwd.h>
36 #include <signal.h>
37 #include <sys/ioctl.h>
38 #include <net/if.h>
39 #include <termios.h>
40 #include <linux/tty.h>
41 #include <linux/sockios.h>
42
43 /* Change this to whatever your daemon is called */
44 #define DAEMON_NAME "slcand"
45
46 /* Change this to the user under which to run */
47 #define RUN_AS_USER "root"
48
49 /* The length of ttypath buffer */
50 #define TTYPATH_LENGTH  64
51
52 /* UART flow control types */
53 #define FLOW_NONE 0
54 #define FLOW_HW 1
55 #define FLOW_SW 2
56
57 void print_usage(char *prg)
58 {
59         fprintf(stderr, "\nUsage: %s [options] <tty> [canif-name]\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, "         -S <speed> (set UART speed in baud)\n");
65         fprintf(stderr, "         -t <type>  (set UART flow control type 'hw' or 'sw')\n");
66         fprintf(stderr, "         -b <btr>   (set bit time register value)\n");
67         fprintf(stderr, "         -F         (stay in foreground; no daemonize)\n");
68         fprintf(stderr, "         -h         (show this help page)\n");
69         fprintf(stderr, "\nExamples:\n");
70         fprintf(stderr, "slcand -o -c -f -s6 ttyUSB0\n");
71         fprintf(stderr, "slcand -o -c -f -s6 ttyUSB0 can0\n");
72         fprintf(stderr, "slcand -o -c -f -s6 /dev/ttyUSB0\n");
73         fprintf(stderr, "\n");
74         exit(EXIT_FAILURE);
75 }
76
77 static int slcand_running;
78 static int exit_code;
79 static char ttypath[TTYPATH_LENGTH];
80
81 static void child_handler(int signum)
82 {
83         switch (signum) {
84
85         case SIGUSR1:
86                 /* exit parent */
87                 exit(EXIT_SUCCESS);
88                 break;
89         case SIGALRM:
90         case SIGCHLD:
91                 syslog(LOG_NOTICE, "received signal %i on %s", signum, ttypath);
92                 exit_code = EXIT_FAILURE;
93                 slcand_running = 0;
94                 break;
95         case SIGINT:
96         case SIGTERM:
97                 syslog(LOG_NOTICE, "received signal %i on %s", signum, ttypath);
98                 exit_code = EXIT_SUCCESS;
99                 slcand_running = 0;
100                 break;
101         }
102 }
103
104 static int look_up_uart_speed(long int s)
105 {
106         switch (s) {
107
108         case 9600:
109                 return B9600;
110         case 19200:
111                 return B19200;
112         case 38400:
113                 return B38400;
114         case 57600:
115                 return B57600;
116         case 115200:
117                 return B115200;
118         case 230400:
119                 return B230400;
120         case 460800:
121                 return B460800;
122         case 500000:
123                 return B500000;
124         case 576000:
125                 return B576000;
126         case 921600:
127                 return B921600;
128         case 1000000:
129                 return B1000000;
130         case 1152000:
131                 return B1152000;
132         case 1500000:
133                 return B1500000;
134         case 2000000:
135                 return B2000000;
136 #ifdef B2500000
137         case 2500000:
138                 return B2500000;
139 #endif
140 #ifdef B3000000
141         case 3000000:
142                 return B3000000;
143 #endif
144 #ifdef B3500000
145         case 3500000:
146                 return B3500000;
147 #endif
148 #ifdef B3710000
149         case 3710000
150                 return B3710000;
151 #endif
152 #ifdef B4000000
153         case 4000000:
154                 return B4000000;
155 #endif
156         default:
157                 return -1;
158         }
159 }
160
161 int main(int argc, char *argv[])
162 {
163         char *tty = NULL;
164         char const *devprefix = "/dev/";
165         char *name = NULL;
166         char buf[IFNAMSIZ+1];
167         struct termios tios;
168         speed_t old_ispeed;
169         speed_t old_ospeed;
170
171         int opt;
172         int send_open = 0;
173         int send_close = 0;
174         int send_read_status_flags = 0;
175         char *speed = NULL;
176         char *uart_speed_str = NULL;
177         long int uart_speed = 0;
178         int flow_type = FLOW_NONE;
179         char *btr = NULL;
180         int run_as_daemon = 1;
181         char *pch;
182         int ldisc = N_SLCAN;
183         int fd;
184
185         ttypath[0] = '\0';
186
187         while ((opt = getopt(argc, argv, "ocfs:S:t:b:?hF")) != -1) {
188                 switch (opt) {
189                 case 'o':
190                         send_open = 1;
191                         break;
192                 case 'c':
193                         send_close = 1;
194                         break;
195                 case 'f':
196                         send_read_status_flags = 1;
197                         break;
198                 case 's':
199                         speed = optarg;
200                         if (strlen(speed) > 1)
201                                 print_usage(argv[0]);
202                         break;
203                 case 'S':
204                         uart_speed_str = optarg;
205                         errno = 0;
206                         uart_speed = strtol(uart_speed_str, NULL, 10);
207                         if (errno)
208                                 print_usage(argv[0]);
209                         if (look_up_uart_speed(uart_speed) == -1) {
210                                 fprintf(stderr, "Unsupported UART speed (%lu)\n", uart_speed);
211                                 exit(EXIT_FAILURE);
212                         }
213                         break;
214                 case 't':
215                         if (!strcmp(optarg, "hw")) {
216                                 flow_type = FLOW_HW;
217                         } else if (!strcmp(optarg, "sw")) {
218                                 flow_type = FLOW_SW;
219                         } else {
220                                 fprintf(stderr, "Unsupported flow type (%s)\n", optarg);
221                                 exit(EXIT_FAILURE);
222                         }
223                         break;
224                 case 'b':
225                         btr = optarg;
226                         if (strlen(btr) > 6)
227                                 print_usage(argv[0]);
228                         break;
229                 case 'F':
230                         run_as_daemon = 0;
231                         break;
232                 case 'h':
233                 case '?':
234                 default:
235                         print_usage(argv[0]);
236                         break;
237                 }
238         }
239
240         /* Initialize the logging interface */
241         openlog(DAEMON_NAME, LOG_PID, LOG_LOCAL5);
242
243         /* Parse serial device name and optional can interface name */
244         tty = argv[optind];
245         if (NULL == tty)
246                 print_usage(argv[0]);
247
248         name = argv[optind + 1];
249
250         /* Prepare the tty device name string */
251         pch = strstr(tty, devprefix);
252         if (pch != tty)
253                 snprintf(ttypath, TTYPATH_LENGTH, "%s%s", devprefix, tty);
254         else
255                 snprintf(ttypath, TTYPATH_LENGTH, "%s", tty);
256
257         syslog(LOG_INFO, "starting on TTY device %s", ttypath);
258
259         /* Daemonize */
260         if (run_as_daemon) {
261                 if (daemon(0, 0)) {
262                         syslog(LOG_ERR, "failed to daemonize");
263                         exit(EXIT_FAILURE);
264                 }
265         }
266         else {
267                 /* Trap signals that we expect to receive */
268                 signal(SIGINT, child_handler);
269                 signal(SIGTERM, child_handler);
270         }
271
272         /* */
273         slcand_running = 1;
274
275         /* Now we are a daemon -- do the work for which we were paid */
276         fd = open(ttypath, O_RDWR | O_NONBLOCK | O_NOCTTY);
277         if (fd < 0) {
278                 syslog(LOG_NOTICE, "failed to open TTY device %s\n", ttypath);
279                 perror(ttypath);
280                 exit(EXIT_FAILURE);
281         }
282
283         /* Configure baud rate */
284         memset(&tios, 0, sizeof(struct termios));
285         if (tcgetattr(fd, &tios) < 0) {
286                 syslog(LOG_NOTICE, "failed to get attributes for TTY device %s: %s\n", ttypath, strerror(errno));
287                 exit(EXIT_FAILURE);
288         }
289
290         /* Get old values for later restore */
291         old_ispeed = cfgetispeed(&tios);
292         old_ospeed = cfgetospeed(&tios);
293
294         /* Reset UART settings */
295         cfmakeraw(&tios);
296         tios.c_iflag &= ~IXOFF;
297         tios.c_cflag &= ~CRTSCTS;
298
299         /* Baud Rate */
300         cfsetispeed(&tios, look_up_uart_speed(uart_speed));
301         cfsetospeed(&tios, look_up_uart_speed(uart_speed));
302
303         /* Flow control */
304         if (flow_type == FLOW_HW)
305                 tios.c_cflag |= CRTSCTS;
306         else if (flow_type == FLOW_SW)
307                 tios.c_iflag |= (IXON | IXOFF);
308
309         /* apply changes */
310         if (tcsetattr(fd, TCSADRAIN, &tios) < 0)
311                 syslog(LOG_NOTICE, "Cannot set attributes for device \"%s\": %s!\n", ttypath, strerror(errno));
312
313         if (speed) {
314                 sprintf(buf, "C\rS%s\r", speed);
315                 write(fd, buf, strlen(buf));
316         }
317
318         if (btr) {
319                 sprintf(buf, "C\rs%s\r", btr);
320                 write(fd, buf, strlen(buf));
321         }
322
323         if (send_read_status_flags) {
324                 sprintf(buf, "F\r");
325                 write(fd, buf, strlen(buf));
326         }
327
328         if (send_open) {
329                 sprintf(buf, "O\r");
330                 write(fd, buf, strlen(buf));
331         }
332
333         /* set slcan like discipline on given tty */
334         if (ioctl(fd, TIOCSETD, &ldisc) < 0) {
335                 perror("ioctl TIOCSETD");
336                 exit(EXIT_FAILURE);
337         }
338         
339         /* retrieve the name of the created CAN netdevice */
340         if (ioctl(fd, SIOCGIFNAME, buf) < 0) {
341                 perror("ioctl SIOCGIFNAME");
342                 exit(EXIT_FAILURE);
343         }
344
345         syslog(LOG_NOTICE, "attached TTY %s to netdevice %s\n", ttypath, buf);
346         
347         /* try to rename the created netdevice */
348         if (name) {
349                 struct ifreq ifr;
350                 int s = socket(PF_INET, SOCK_DGRAM, 0);
351
352                 if (s < 0)
353                         perror("socket for interface rename");
354                 else {
355                         strncpy(ifr.ifr_name, buf, IFNAMSIZ);
356                         strncpy(ifr.ifr_newname, name, IFNAMSIZ);
357
358                         if (ioctl(s, SIOCSIFNAME, &ifr) < 0) {
359                                 syslog(LOG_NOTICE, "netdevice %s rename to %s failed\n", buf, name);
360                                 perror("ioctl SIOCSIFNAME rename");
361                                 exit(EXIT_FAILURE);
362                         } else
363                                 syslog(LOG_NOTICE, "netdevice %s renamed to %s\n", buf, name);
364
365                         close(s);
366                 }       
367         }
368
369         /* The Big Loop */
370         while (slcand_running)
371                 sleep(1); /* wait 1 second */
372
373         /* Reset line discipline */
374         syslog(LOG_INFO, "stopping on TTY device %s", ttypath);
375         ldisc = N_TTY;
376         if (ioctl(fd, TIOCSETD, &ldisc) < 0) {
377                 perror("ioctl TIOCSETD");
378                 exit(EXIT_FAILURE);
379         }
380
381         if (send_close) {
382                 sprintf(buf, "C\r");
383                 write(fd, buf, strlen(buf));
384         }
385
386         /* Reset old rates */
387         cfsetispeed(&tios, old_ispeed);
388         cfsetospeed(&tios, old_ospeed);
389
390         /* apply changes */
391         if (tcsetattr(fd, TCSADRAIN, &tios) < 0)
392                 syslog(LOG_NOTICE, "Cannot set attributes for device \"%s\": %s!\n", ttypath, strerror(errno));
393
394         /* Finish up */
395         syslog(LOG_NOTICE, "terminated on %s", ttypath);
396         closelog();
397         return exit_code;
398 }