Simplify arbitrary baudrate code

This commit is contained in:
Martin Lund 2022-07-12 12:09:09 +02:00
parent ac859f41b9
commit a23be7f2c2
4 changed files with 32 additions and 36 deletions

View file

@ -13,7 +13,8 @@ tio_sources = [
'print.c', 'print.c',
'configfile.c', 'configfile.c',
'signals.c', 'signals.c',
'socket.c' 'socket.c',
'setspeed.c'
] ]
tio_dep = dependency('inih', required: true, tio_dep = dependency('inih', required: true,
@ -23,12 +24,10 @@ tio_dep = dependency('inih', required: true,
tio_c_args = ['-Wno-unused-result'] tio_c_args = ['-Wno-unused-result']
if enable_setspeed2 if enable_setspeed2
tio_sources += 'setspeed2.c'
tio_c_args += '-DHAVE_TERMIOS2' tio_c_args += '-DHAVE_TERMIOS2'
endif endif
if enable_iossiospeed if enable_iossiospeed
tio_sources += 'iossiospeed.c'
tio_c_args += '-DHAVE_IOSSIOSPEED' tio_c_args += '-DHAVE_IOSSIOSPEED'
endif endif

View file

@ -19,13 +19,21 @@
* 02110-1301, USA. * 02110-1301, USA.
*/ */
#ifdef HAVE_TERMIOS2
#define termios asmtermios #define termios asmtermios
#include <sys/ioctl.h> #include <sys/ioctl.h>
#undef termios #undef termios
#include <asm-generic/ioctls.h> #include <asm-generic/ioctls.h>
#include <asm-generic/termbits.h> #include <asm-generic/termbits.h>
int setspeed2(int fd, int baudrate) #elif HAVE_IOSSIOSPEED
#include <sys/ioctl.h>
#include <IOKit/serial/ioss.h>
#endif
#ifdef HAVE_TERMIOS2
int setspeed(int fd, int baudrate)
{ {
struct termios2 tio; struct termios2 tio;
int status; int status;
@ -42,3 +50,17 @@ int setspeed2(int fd, int baudrate)
return status; 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

View file

@ -1,7 +1,7 @@
/* /*
* tio - a simple serial terminal I/O tool * 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 * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -19,10 +19,6 @@
* 02110-1301, USA. * 02110-1301, USA.
*/ */
#include <sys/ioctl.h> #pragma once
#include <IOKit/serial/ioss.h>
int iossiospeed(int fd, int baudrate) int setspeed(int fd, int baudrate);
{
return ioctl(fd, IOSSIOSPEED, (char *)&baudrate);
}

View file

@ -49,14 +49,7 @@
#include "log.h" #include "log.h"
#include "error.h" #include "error.h"
#include "socket.h" #include "socket.h"
#include "setspeed.h"
#ifdef HAVE_TERMIOS2
extern int setspeed2(int fd, int baudrate);
#endif
#ifdef HAVE_IOSSIOSPEED
extern int iossiospeed(int fd, int baudrate);
#endif
#ifdef __APPLE__ #ifdef __APPLE__
#define PATH_SERIAL_DEVICES "/dev/" #define PATH_SERIAL_DEVICES "/dev/"
@ -928,7 +921,7 @@ int tty_connect(void)
#ifdef HAVE_IOSSIOSPEED #ifdef HAVE_IOSSIOSPEED
if (!standard_baudrate) 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_ispeed = tio_old.c_ispeed;
tio.c_ospeed = tio_old.c_ospeed; tio.c_ospeed = tio_old.c_ospeed;
} }
@ -949,27 +942,15 @@ int tty_connect(void)
goto error_tcsetattr; goto error_tcsetattr;
} }
#ifdef HAVE_TERMIOS2 /* Set arbitrary baudrate (only works on supported platforms) */
if (!standard_baudrate) 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)); tio_error_printf_silent("Could not set baudrate speed (%s)", strerror(errno));
goto error_setspeed; 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 */ /* Input loop */
while (true) while (true)
@ -1123,9 +1104,7 @@ int tty_connect(void)
return TIO_SUCCESS; return TIO_SUCCESS;
#if defined (HAVE_TERMIOS2) || defined (HAVE_IOSSIOSPEED)
error_setspeed: error_setspeed:
#endif
error_tcsetattr: error_tcsetattr:
error_tcgetattr: error_tcgetattr:
error_read: error_read: