2 * slcand.c - userspace daemon for serial line CAN interface driver SLCAN
4 * Copyright (c) 2009 Robert Haddon <robert.haddon@verari.com>
5 * Copyright (c) 2009 Verari Systems Inc.
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.
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.
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
21 * Send feedback to <linux-can@vger.kernel.org>
29 #include <sys/types.h>
31 #include <sys/socket.h>
37 #include <sys/ioctl.h>
41 /* default slcan line discipline since Kernel 2.6.25 */
42 #define LDISC_N_SLCAN 17
44 /* Change this to whatever your daemon is called */
45 #define DAEMON_NAME "slcand"
47 /* Change this to the user under which to run */
48 #define RUN_AS_USER "root"
50 /* The length of ttypath buffer */
51 #define TTYPATH_LENGTH 64
53 #define EXIT_SUCCESS 0
54 #define EXIT_FAILURE 1
56 /* UART flow control types */
61 void print_usage(char *prg)
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");
81 static int slcand_running;
83 static char ttypath[TTYPATH_LENGTH];
85 static void child_handler(int signum)
95 syslog(LOG_NOTICE, "received signal %i on %s", signum, ttypath);
96 exit_code = EXIT_FAILURE;
101 syslog(LOG_NOTICE, "received signal %i on %s", signum, ttypath);
102 exit_code = EXIT_SUCCESS;
108 static int look_up_uart_speed(long int s)
165 int main(int argc, char *argv[])
168 char const *devprefix = "/dev/";
170 char buf[IFNAMSIZ+1];
178 int send_read_status_flags = 0;
180 char *uart_speed_str = NULL;
181 long int uart_speed = 0;
182 int flow_type = FLOW_NONE;
184 int run_as_daemon = 1;
186 int ldisc = LDISC_N_SLCAN;
191 while ((opt = getopt(argc, argv, "ocfs:S:t:b:?hF")) != -1) {
200 send_read_status_flags = 1;
204 if (strlen(speed) > 1)
205 print_usage(argv[0]);
208 uart_speed_str = optarg;
210 uart_speed = strtol(uart_speed_str, NULL, 10);
212 print_usage(argv[0]);
213 if (look_up_uart_speed(uart_speed) == -1) {
214 fprintf(stderr, "Unsupported UART speed (%lu)\n", uart_speed);
219 if (!strcmp(optarg, "hw")) {
221 } else if (!strcmp(optarg, "sw")) {
224 fprintf(stderr, "Unsupported flow type (%s)\n", optarg);
231 print_usage(argv[0]);
239 print_usage(argv[0]);
244 /* Initialize the logging interface */
245 openlog(DAEMON_NAME, LOG_PID, LOG_LOCAL5);
247 /* Parse serial device name and optional can interface name */
250 print_usage(argv[0]);
252 name = argv[optind + 1];
254 /* Prepare the tty device name string */
255 pch = strstr(tty, devprefix);
257 snprintf(ttypath, TTYPATH_LENGTH, "%s%s", devprefix, tty);
259 snprintf(ttypath, TTYPATH_LENGTH, "%s", tty);
261 syslog(LOG_INFO, "starting on TTY device %s", ttypath);
266 syslog(LOG_ERR, "failed to daemonize");
271 /* Trap signals that we expect to receive */
272 signal(SIGINT, child_handler);
273 signal(SIGTERM, child_handler);
279 /* Now we are a daemon -- do the work for which we were paid */
280 fd = open(ttypath, O_RDWR | O_NONBLOCK | O_NOCTTY);
282 syslog(LOG_NOTICE, "failed to open TTY device %s\n", ttypath);
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));
294 /* Get old values for later restore */
295 old_ispeed = cfgetispeed(&tios);
296 old_ospeed = cfgetospeed(&tios);
298 /* Reset UART settings */
300 tios.c_iflag &= ~IXOFF;
301 tios.c_cflag &= ~CRTSCTS;
304 cfsetispeed(&tios, look_up_uart_speed(uart_speed));
305 cfsetospeed(&tios, look_up_uart_speed(uart_speed));
308 if (flow_type == FLOW_HW)
309 tios.c_cflag |= CRTSCTS;
310 else if (flow_type == FLOW_SW)
311 tios.c_iflag |= (IXON | IXOFF);
314 if (tcsetattr(fd, TCSADRAIN, &tios) < 0)
315 syslog(LOG_NOTICE, "Cannot set attributes for device \"%s\": %s!\n", ttypath, strerror(errno));
318 sprintf(buf, "C\rS%s\r", speed);
319 write(fd, buf, strlen(buf));
323 sprintf(buf, "C\rs%s\r", btr);
324 write(fd, buf, strlen(buf));
327 if (send_read_status_flags) {
329 write(fd, buf, strlen(buf));
334 write(fd, buf, strlen(buf));
337 /* set slcan like discipline on given tty */
338 if (ioctl(fd, TIOCSETD, &ldisc) < 0) {
339 perror("ioctl TIOCSETD");
343 /* retrieve the name of the created CAN netdevice */
344 if (ioctl(fd, SIOCGIFNAME, buf) < 0) {
345 perror("ioctl SIOCGIFNAME");
349 syslog(LOG_NOTICE, "attached TTY %s to netdevice %s\n", ttypath, buf);
351 /* try to rename the created netdevice */
354 int s = socket(PF_INET, SOCK_DGRAM, 0);
357 perror("socket for interface rename");
359 strncpy(ifr.ifr_name, buf, IFNAMSIZ);
360 strncpy(ifr.ifr_newname, name, IFNAMSIZ);
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");
367 syslog(LOG_NOTICE, "netdevice %s renamed to %s\n", buf, name);
374 while (slcand_running)
375 sleep(1); /* wait 1 second */
377 /* Reset line discipline */
378 syslog(LOG_INFO, "stopping on TTY device %s", ttypath);
380 if (ioctl(fd, TIOCSETD, &ldisc) < 0) {
381 perror("ioctl TIOCSETD");
387 write(fd, buf, strlen(buf));
390 /* Reset old rates */
391 cfsetispeed(&tios, old_ispeed);
392 cfsetospeed(&tios, old_ospeed);
395 if (tcsetattr(fd, TCSADRAIN, &tios) < 0)
396 syslog(LOG_NOTICE, "Cannot set attributes for device \"%s\": %s!\n", ttypath, strerror(errno));
399 syslog(LOG_NOTICE, "terminated on %s", ttypath);