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