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