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