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