From 38fb68fb500cbb5ae3a9827ebcf069f4f3f6d931 Mon Sep 17 00:00:00 2001 From: Yegor Yefremov Date: Mon, 13 Jan 2014 09:54:04 +0100 Subject: [PATCH] slcand: add flow control option Add option '-t' to specify flow control type. Two types are supported: 'hw' - RTS/CTS and 'sw' - XON/XOFF. Signed-off-by: Yegor Yefremov Acked-by: Oliver Hartkopp Signed-off-by: Marc Kleine-Budde --- slcand.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/slcand.c b/slcand.c index 30f5127..24af971 100644 --- a/slcand.c +++ b/slcand.c @@ -69,6 +69,11 @@ #define EXIT_SUCCESS 0 #define EXIT_FAILURE 1 +/* UART flow control types */ +#define FLOW_NONE 0 +#define FLOW_HW 1 +#define FLOW_SW 2 + void print_usage(char *prg) { fprintf(stderr, "\nUsage: %s [options] [canif-name]\n\n", prg); @@ -77,6 +82,7 @@ void print_usage(char *prg) fprintf(stderr, " -f (read status flags with 'F\\r' to reset error states)\n"); fprintf(stderr, " -s (set CAN speed 0..8)\n"); fprintf(stderr, " -S (set UART speed in baud)\n"); + fprintf(stderr, " -t (set UART flow control type 'hw' or 'sw')\n"); fprintf(stderr, " -b (set bit time register value)\n"); fprintf(stderr, " -F (stay in foreground; no daemonize)\n"); fprintf(stderr, " -h (show this help page)\n"); @@ -300,6 +306,7 @@ int main(int argc, char *argv[]) char *speed = NULL; char *uart_speed_str = NULL; long int uart_speed = 0; + int flow_type = FLOW_NONE; char *btr = NULL; int run_as_daemon = 1; pid_t parent_pid = 0; @@ -309,7 +316,7 @@ int main(int argc, char *argv[]) ttypath[0] = '\0'; - while ((opt = getopt(argc, argv, "ocfs:S:b:?hF")) != -1) { + while ((opt = getopt(argc, argv, "ocfs:S:t:b:?hF")) != -1) { switch (opt) { case 'o': send_open = 1; @@ -336,6 +343,16 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } break; + case 't': + if (!strcmp(optarg, "hw")) { + flow_type = FLOW_HW; + } else if (!strcmp(optarg, "sw")) { + flow_type = FLOW_SW; + } else { + fprintf(stderr, "Unsupported flow type (%s)\n", optarg); + exit(EXIT_FAILURE); + } + break; case 'b': btr = optarg; if (strlen(btr) > 6) @@ -401,10 +418,21 @@ int main(int argc, char *argv[]) old_ispeed = cfgetispeed(&tios); old_ospeed = cfgetospeed(&tios); + /* Reset UART settings */ + cfmakeraw(&tios); + tios.c_iflag &= ~IXOFF; + tios.c_cflag &= ~CRTSCTS; + /* Baud Rate */ cfsetispeed(&tios, look_up_uart_speed(uart_speed)); cfsetospeed(&tios, look_up_uart_speed(uart_speed)); + /* Flow control */ + if (flow_type == FLOW_HW) + tios.c_cflag |= CRTSCTS; + else if (flow_type == FLOW_SW) + tios.c_iflag |= (IXON | IXOFF); + /* apply changes */ if (tcsetattr(fd, TCSADRAIN, &tios) < 0) syslog(LOG_NOTICE, "Cannot set attributes for device \"%s\": %s!\n", ttypath, strerror(errno));