Remove options --response-wait, --response-timeout

Remove options and rework input handling so it is possible to do the
same thing but via script which is much more flexible.

These options were always a bit of a hardcoded solution. With the new
script expect feature we can wait for any type of response.

For example, pipe command to serial device and wait for line response within 1 second:

$ echo "*IDN?" | tio /dev/ttyACM0 --script "expect('\r\n', 1000)" --mute
This commit is contained in:
Martin Lund 2024-04-13 23:16:31 +02:00
parent e1e3e298bf
commit 97537853a8
9 changed files with 46 additions and 116 deletions

View file

@ -33,8 +33,6 @@ _tio()
-S --socket \
--input-mode \
--output-mode \
-r --response-wait \
--response-timeout \
--rs-485 \
--rs-485-config \
--alert \
@ -140,14 +138,6 @@ _tio()
COMPREPLY=( $(compgen -W "normal hex" -- ${cur}) )
return 0
;;
-r | --response-wait)
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
;;
--response-timeout)
COMPREPLY=( $(compgen -W "1 10 100" -- ${cur}) )
return 0
;;
--rs-485)
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0

View file

@ -276,14 +276,6 @@ static int data_handler(void *user, const char *section, const char *name,
option.prefix_key = value[0];
}
}
else if (!strcmp(name, "response-wait"))
{
option.response_wait = read_boolean(value, name);
}
else if (!strcmp(name, "response-timeout"))
{
option.response_timeout = read_integer(value, name, 0, LONG_MAX);
}
else if (!strcmp(name, "rs-485"))
{
option.rs485 = read_boolean(value, name);

View file

@ -69,12 +69,6 @@ int main(int argc, char *argv[])
{
// Enter non interactive mode
interactive_mode = false;
// Mute tio text in response mode
if (option.response_wait)
{
option.mute = true;
}
}
/* Configure output terminal */

View file

@ -50,7 +50,6 @@ enum opt_t
OPT_LOG_STRIP,
OPT_LOG_APPEND,
OPT_LINE_PULSE_DURATION,
OPT_RESPONSE_TIMEOUT,
OPT_RS485,
OPT_RS485_CONFIG,
OPT_ALERT,
@ -96,8 +95,6 @@ struct option_t option =
.prefix_code = 20, // ctrl-t
.prefix_key = 't',
.prefix_enabled = true,
.response_wait = false,
.response_timeout = 100,
.mute = false,
.rs485 = false,
.rs485_config_flags = 0,
@ -142,8 +139,6 @@ void print_help(char *argv[])
printf(" -m, --map <flags> Map characters\n");
printf(" -c, --color 0..255|bold|none|list Colorize tio text (default: bold)\n");
printf(" -S, --socket <socket> Redirect I/O to socket\n");
printf(" -r, --response-wait Wait for line response then quit\n");
printf(" --response-timeout <ms> Response timeout (default: 100)\n");
printf(" --rs-485 Enable RS-485 mode\n");
printf(" --rs-485-config <config> Set RS-485 configuration\n");
printf(" --alert bell|blink|none Alert on connect/disconnect (default: none)\n");
@ -375,8 +370,6 @@ void options_parse(int argc, char *argv[])
{"color", required_argument, 0, 'c' },
{"input-mode", required_argument, 0, OPT_INPUT_MODE },
{"output-mode", required_argument, 0, OPT_OUTPUT_MODE },
{"response-wait", no_argument, 0, 'r' },
{"response-timeout", required_argument, 0, OPT_RESPONSE_TIMEOUT },
{"rs-485", no_argument, 0, OPT_RS485 },
{"rs-485-config", required_argument, 0, OPT_RS485_CONFIG },
{"alert", required_argument, 0, OPT_ALERT },
@ -531,14 +524,6 @@ void options_parse(int argc, char *argv[])
option.output_mode = output_mode_option_parse(optarg);
break;
case 'r':
option.response_wait = true;
break;
case OPT_RESPONSE_TIMEOUT:
option.response_timeout = string_to_long(optarg);
break;
case OPT_RS485:
option.rs485 = true;
break;

View file

@ -77,8 +77,6 @@ struct option_t
unsigned char prefix_code;
unsigned char prefix_key;
bool prefix_enabled;
bool response_wait;
int response_timeout;
bool mute;
bool rs485;
uint32_t rs485_config_flags;

View file

@ -1536,9 +1536,6 @@ int tty_connect(void)
int status;
bool next_timestamp = false;
char* now = NULL;
struct timeval tv;
struct timeval *tv_p = &tv;
bool ignore_stdin = false;
/* Open tty device */
fd = open(option.tty_device, O_RDWR | O_NOCTTY | O_NONBLOCK);
@ -1629,6 +1626,35 @@ int tty_connect(void)
}
}
/* If stdin is a pipe forward all input to tty device */
if (interactive_mode == false)
{
while (true)
{
int ret = read(pipefd[0], &input_char, 1);
if (ret < 0)
{
tio_error_printf("Could not read from pipe (%s)", strerror(errno));
exit(EXIT_FAILURE);
}
else if (ret > 0)
{
// Forward to tty device
ret = write(fd, &input_char, 1);
if (ret < 0)
{
tio_error_printf("Could not write to serial device (%s)", strerror(errno));
exit(EXIT_FAILURE);
}
}
else
{
// EOF - finished forwarding
break;
}
}
}
/* Manage script activation */
if (option.script_run != SCRIPT_RUN_NEVER)
{
@ -1640,33 +1666,24 @@ int tty_connect(void)
}
}
// Exit if piped input
if (interactive_mode == false)
{
exit(EXIT_SUCCESS);
}
/* Input loop */
while (true)
{
FD_ZERO(&rdfs);
FD_SET(fd, &rdfs);
if (!ignore_stdin)
{
FD_SET(pipefd[0], &rdfs);
}
FD_SET(pipefd[0], &rdfs);
maxfd = MAX(fd, pipefd[0]);
maxfd = MAX(maxfd, socket_add_fds(&rdfs, true));
/* Manage timeout */
if ((option.response_wait) && (option.response_timeout != 0))
{
// Set response timeout
tv_p->tv_sec = 0;
tv_p->tv_usec = option.response_timeout * 1000;
}
else
{
// No timeout
tv_p = NULL;
}
/* Block until input becomes available */
status = select(maxfd + 1, &rdfs, NULL, NULL, tv_p);
status = select(maxfd + 1, &rdfs, NULL, NULL, NULL);
if (status > 0)
{
bool forward = false;
@ -1750,15 +1767,6 @@ int tty_connect(void)
{
next_timestamp = true;
}
if (option.response_wait)
{
if (input_char == '\n')
{
tty_sync(fd);
exit(EXIT_SUCCESS);
}
}
}
}
else if (FD_ISSET(pipefd[0], &rdfs))
@ -1772,22 +1780,9 @@ int tty_connect(void)
}
else if (bytes_read == 0)
{
/* Reached EOF (when piping to stdin) */
if (option.response_wait)
{
/* Stdin pipe closed but not blocking so stop listening
* to stdin in response mode.
*
* Note: select() really indicates not if data is ready
* but if file descriptor is non-blocking for I/O
* operation. */
ignore_stdin = true;
}
else
{
tty_sync(fd);
exit(EXIT_SUCCESS);
}
/* Reached EOF (when piping to stdin, never reached) */
tty_sync(fd);
exit(EXIT_SUCCESS);
}
/* Process input byte by byte */