From a23be7f2c2bacef428b0e50aa93e37e5b52bdd56 Mon Sep 17 00:00:00 2001 From: Martin Lund Date: Tue, 12 Jul 2022 12:09:09 +0200 Subject: [PATCH] Simplify arbitrary baudrate code --- src/meson.build | 5 ++--- src/{setspeed2.c => setspeed.c} | 24 +++++++++++++++++++++++- src/{iossiospeed.c => setspeed.h} | 10 +++------- src/tty.c | 29 ++++------------------------- 4 files changed, 32 insertions(+), 36 deletions(-) rename src/{setspeed2.c => setspeed.c} (77%) rename src/{iossiospeed.c => setspeed.h} (80%) diff --git a/src/meson.build b/src/meson.build index 38d300a..464f4f3 100644 --- a/src/meson.build +++ b/src/meson.build @@ -13,7 +13,8 @@ tio_sources = [ 'print.c', 'configfile.c', 'signals.c', - 'socket.c' + 'socket.c', + 'setspeed.c' ] tio_dep = dependency('inih', required: true, @@ -23,12 +24,10 @@ tio_dep = dependency('inih', required: true, tio_c_args = ['-Wno-unused-result'] if enable_setspeed2 - tio_sources += 'setspeed2.c' tio_c_args += '-DHAVE_TERMIOS2' endif if enable_iossiospeed - tio_sources += 'iossiospeed.c' tio_c_args += '-DHAVE_IOSSIOSPEED' endif diff --git a/src/setspeed2.c b/src/setspeed.c similarity index 77% rename from src/setspeed2.c rename to src/setspeed.c index 64ecb4a..9159a47 100644 --- a/src/setspeed2.c +++ b/src/setspeed.c @@ -19,13 +19,21 @@ * 02110-1301, USA. */ +#ifdef HAVE_TERMIOS2 #define termios asmtermios #include #undef termios #include #include -int setspeed2(int fd, int baudrate) +#elif HAVE_IOSSIOSPEED +#include +#include +#endif + + +#ifdef HAVE_TERMIOS2 +int setspeed(int fd, int baudrate) { struct termios2 tio; int status; @@ -42,3 +50,17 @@ int setspeed2(int fd, int baudrate) return status; } + +#elif HAVE_IOSSIOSPEED +int setspeed(int fd, int baudrate) +{ + return ioctl(fd, IOSSIOSPEED, (char *)&baudrate); +} + +#else +int setspeed(int fd, int baudrate) +{ + errno = EINVAL; + return -1; +} +#endif diff --git a/src/iossiospeed.c b/src/setspeed.h similarity index 80% rename from src/iossiospeed.c rename to src/setspeed.h index 8fa0d5f..c201f6e 100644 --- a/src/iossiospeed.c +++ b/src/setspeed.h @@ -1,7 +1,7 @@ /* * tio - a simple serial terminal I/O tool * - * Copyright (c) 2017 Martin Lund + * Copyright (c) 2022 Martin Lund * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -19,10 +19,6 @@ * 02110-1301, USA. */ -#include -#include +#pragma once -int iossiospeed(int fd, int baudrate) -{ - return ioctl(fd, IOSSIOSPEED, (char *)&baudrate); -} +int setspeed(int fd, int baudrate); diff --git a/src/tty.c b/src/tty.c index bc22fbd..e8696c6 100644 --- a/src/tty.c +++ b/src/tty.c @@ -49,14 +49,7 @@ #include "log.h" #include "error.h" #include "socket.h" - -#ifdef HAVE_TERMIOS2 -extern int setspeed2(int fd, int baudrate); -#endif - -#ifdef HAVE_IOSSIOSPEED -extern int iossiospeed(int fd, int baudrate); -#endif +#include "setspeed.h" #ifdef __APPLE__ #define PATH_SERIAL_DEVICES "/dev/" @@ -928,7 +921,7 @@ int tty_connect(void) #ifdef HAVE_IOSSIOSPEED if (!standard_baudrate) { - /* OS X wants these fields left alone. We'll set baudrate with iossiospeed below. */ + /* OS X wants these fields left alone before setting arbitrary baud rate */ tio.c_ispeed = tio_old.c_ispeed; tio.c_ospeed = tio_old.c_ospeed; } @@ -949,27 +942,15 @@ int tty_connect(void) goto error_tcsetattr; } -#ifdef HAVE_TERMIOS2 + /* Set arbitrary baudrate (only works on supported platforms) */ if (!standard_baudrate) { - if (setspeed2(fd, option.baudrate) != 0) + if (setspeed(fd, option.baudrate) != 0) { tio_error_printf_silent("Could not set baudrate speed (%s)", strerror(errno)); goto error_setspeed; } } -#endif - -#ifdef HAVE_IOSSIOSPEED - if (!standard_baudrate) - { - if (iossiospeed(fd, option.baudrate) != 0) - { - tio_error_printf_silent("Could not set baudrate speed (%s)", strerror(errno)); - goto error_setspeed; - } - } -#endif /* Input loop */ while (true) @@ -1123,9 +1104,7 @@ int tty_connect(void) return TIO_SUCCESS; -#if defined (HAVE_TERMIOS2) || defined (HAVE_IOSSIOSPEED) error_setspeed: -#endif error_tcsetattr: error_tcgetattr: error_read: