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>
40 #include <linux/tty.h>
41 #include <linux/sockios.h>
43 /* Change this to whatever your daemon is called */
44 #define DAEMON_NAME "slcand"
46 /* Change this to the user under which to run */
47 #define RUN_AS_USER "root"
49 /* The length of ttypath buffer */
50 #define TTYPATH_LENGTH 64
52 /* UART flow control types */
57 void print_usage(char *prg)
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, " -s <speed> (set CAN speed 0..8)\n");
64 fprintf(stderr, " -S <speed> (set UART speed in baud)\n");
65 fprintf(stderr, " -t <type> (set UART flow control type 'hw' or 'sw')\n");
66 fprintf(stderr, " -b <btr> (set bit time register value)\n");
67 fprintf(stderr, " -F (stay in foreground; no daemonize)\n");
68 fprintf(stderr, " -h (show this help page)\n");
69 fprintf(stderr, "\nExamples:\n");
70 fprintf(stderr, "slcand -o -c -f -s6 ttyUSB0\n");
71 fprintf(stderr, "slcand -o -c -f -s6 ttyUSB0 can0\n");
72 fprintf(stderr, "slcand -o -c -f -s6 /dev/ttyUSB0\n");
73 fprintf(stderr, "\n");
77 static int slcand_running;
79 static char ttypath[TTYPATH_LENGTH];
81 static void child_handler(int signum)
91 syslog(LOG_NOTICE, "received signal %i on %s", signum, ttypath);
92 exit_code = EXIT_FAILURE;
97 syslog(LOG_NOTICE, "received signal %i on %s", signum, ttypath);
98 exit_code = EXIT_SUCCESS;
104 static int look_up_uart_speed(long int s)
161 int main(int argc, char *argv[])
164 char const *devprefix = "/dev/";
166 char buf[IFNAMSIZ+1];
174 int send_read_status_flags = 0;
176 char *uart_speed_str = NULL;
177 long int uart_speed = 0;
178 int flow_type = FLOW_NONE;
180 int run_as_daemon = 1;
187 while ((opt = getopt(argc, argv, "ocfs:S:t:b:?hF")) != -1) {
196 send_read_status_flags = 1;
200 if (strlen(speed) > 1)
201 print_usage(argv[0]);
204 uart_speed_str = optarg;
206 uart_speed = strtol(uart_speed_str, NULL, 10);
208 print_usage(argv[0]);
209 if (look_up_uart_speed(uart_speed) == -1) {
210 fprintf(stderr, "Unsupported UART speed (%lu)\n", uart_speed);
215 if (!strcmp(optarg, "hw")) {
217 } else if (!strcmp(optarg, "sw")) {
220 fprintf(stderr, "Unsupported flow type (%s)\n", optarg);
227 print_usage(argv[0]);
235 print_usage(argv[0]);
240 /* Initialize the logging interface */
241 openlog(DAEMON_NAME, LOG_PID, LOG_LOCAL5);
243 /* Parse serial device name and optional can interface name */
246 print_usage(argv[0]);
248 name = argv[optind + 1];
250 /* Prepare the tty device name string */
251 pch = strstr(tty, devprefix);
253 snprintf(ttypath, TTYPATH_LENGTH, "%s%s", devprefix, tty);
255 snprintf(ttypath, TTYPATH_LENGTH, "%s", tty);
257 syslog(LOG_INFO, "starting on TTY device %s", ttypath);
262 syslog(LOG_ERR, "failed to daemonize");
267 /* Trap signals that we expect to receive */
268 signal(SIGINT, child_handler);
269 signal(SIGTERM, child_handler);
275 /* Now we are a daemon -- do the work for which we were paid */
276 fd = open(ttypath, O_RDWR | O_NONBLOCK | O_NOCTTY);
278 syslog(LOG_NOTICE, "failed to open TTY device %s\n", ttypath);
283 /* Configure baud rate */
284 memset(&tios, 0, sizeof(struct termios));
285 if (tcgetattr(fd, &tios) < 0) {
286 syslog(LOG_NOTICE, "failed to get attributes for TTY device %s: %s\n", ttypath, strerror(errno));
290 /* Get old values for later restore */
291 old_ispeed = cfgetispeed(&tios);
292 old_ospeed = cfgetospeed(&tios);
294 /* Reset UART settings */
296 tios.c_iflag &= ~IXOFF;
297 tios.c_cflag &= ~CRTSCTS;
300 cfsetispeed(&tios, look_up_uart_speed(uart_speed));
301 cfsetospeed(&tios, look_up_uart_speed(uart_speed));
304 if (flow_type == FLOW_HW)
305 tios.c_cflag |= CRTSCTS;
306 else if (flow_type == FLOW_SW)
307 tios.c_iflag |= (IXON | IXOFF);
310 if (tcsetattr(fd, TCSADRAIN, &tios) < 0)
311 syslog(LOG_NOTICE, "Cannot set attributes for device \"%s\": %s!\n", ttypath, strerror(errno));
314 sprintf(buf, "C\rS%s\r", speed);
315 write(fd, buf, strlen(buf));
319 sprintf(buf, "C\rs%s\r", btr);
320 write(fd, buf, strlen(buf));
323 if (send_read_status_flags) {
325 write(fd, buf, strlen(buf));
330 write(fd, buf, strlen(buf));
333 /* set slcan like discipline on given tty */
334 if (ioctl(fd, TIOCSETD, &ldisc) < 0) {
335 perror("ioctl TIOCSETD");
339 /* retrieve the name of the created CAN netdevice */
340 if (ioctl(fd, SIOCGIFNAME, buf) < 0) {
341 perror("ioctl SIOCGIFNAME");
345 syslog(LOG_NOTICE, "attached TTY %s to netdevice %s\n", ttypath, buf);
347 /* try to rename the created netdevice */
350 int s = socket(PF_INET, SOCK_DGRAM, 0);
353 perror("socket for interface rename");
355 strncpy(ifr.ifr_name, buf, IFNAMSIZ);
356 strncpy(ifr.ifr_newname, name, IFNAMSIZ);
358 if (ioctl(s, SIOCSIFNAME, &ifr) < 0) {
359 syslog(LOG_NOTICE, "netdevice %s rename to %s failed\n", buf, name);
360 perror("ioctl SIOCSIFNAME rename");
363 syslog(LOG_NOTICE, "netdevice %s renamed to %s\n", buf, name);
370 while (slcand_running)
371 sleep(1); /* wait 1 second */
373 /* Reset line discipline */
374 syslog(LOG_INFO, "stopping on TTY device %s", ttypath);
376 if (ioctl(fd, TIOCSETD, &ldisc) < 0) {
377 perror("ioctl TIOCSETD");
383 write(fd, buf, strlen(buf));
386 /* Reset old rates */
387 cfsetispeed(&tios, old_ispeed);
388 cfsetospeed(&tios, old_ospeed);
391 if (tcsetattr(fd, TCSADRAIN, &tios) < 0)
392 syslog(LOG_NOTICE, "Cannot set attributes for device \"%s\": %s!\n", ttypath, strerror(errno));
395 syslog(LOG_NOTICE, "terminated on %s", ttypath);