From 6fff4939e4e48ba0b3af60cda7d2b901d3fed569 Mon Sep 17 00:00:00 2001 From: Martin Lund Date: Thu, 18 Apr 2024 15:45:50 +0200 Subject: [PATCH] Add timeout based timestamps in hex output mode This change reintroduces timestamping in hex output mode but based on timeout instead of new lines which made no sense. This means that timestamps will only be printed when timeout time has elapsed with no output activity from serial device. Adds option --timestamp-timeout for setting the timeout value in milliseconds. Defaults to 200 ms. --- README.md | 4 +++- TODO | 17 +++++--------- man/tio.1.in | 12 ++++++++++ src/bash-completion/tio.in | 5 +++++ src/configfile.c | 4 ++++ src/options.c | 8 +++++++ src/options.h | 1 + src/tty.c | 45 +++++++++++++++++++++++++++++++------- 8 files changed, 76 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 8d502cb..6127a1a 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,8 @@ when used in combination with [tmux](https://tmux.github.io). * Pulse serial lines with configurable pulse duration * Local echo support * Remapping of characters (nl, cr-nl, bs, lowercase to uppercase, etc.) - * Line timestamps + * Per line timestamps in normal output mode + * Timeout based timestamps in hex output mode * Support for delayed output per character * Support for delayed output per line * Switchable independent input and output mode (normal vs hex) @@ -94,6 +95,7 @@ Options: --output-mode normal|hex Select output mode (default: normal) -t, --timestamp Enable line timestamp --timestamp-format Set timestamp format (default: 24hour) + --timestamp-timeout Set timestamp timeout (default: 200) -L, --list-devices List available serial devices by ID -l, --log Enable log to file --log-file Set log filename diff --git a/TODO b/TODO index 59ea160..99a1b7b 100644 --- a/TODO +++ b/TODO @@ -17,20 +17,15 @@ With and without timestamp support for each broken up line. - * Add support for activity based time stamping in both normal and hex-mode. + * Add support for activity based time stamping in normal output mode - Will print a new timestamp if there is no input activity within the time - defined in the configuration file, e.g.: + Already supported in hex output mode. - timestamp-timeout = 200 + * Advanced line mode - Time is in ms. - -* Advanced line mode - - Current line mode only support backspace editing. Would be nice with arrow - key navigation left/right and insert/overwrite support. Also history browsing - pressing up/down. + Current line mode only support backspace editing. Would be nice with arrow + key navigation left/right and insert/overwrite support. Also history browsing + pressing up/down. * Support for running external command diff --git a/man/tio.1.in b/man/tio.1.in index 5cf55d7..5b15b8c 100644 --- a/man/tio.1.in +++ b/man/tio.1.in @@ -121,6 +121,16 @@ ISO8601 format ("YYYY-MM-DDThh:mm:ss.sss") Default format is \fB24hour\fR .RE +.TP +.BR " \-\-timestamp\-timeout \fI + +Set timestamp timeout value in milliseconds. + +This value only takes effect in hex output mode where timestamps are only +printed after elapsed timeout time of no output activity from tty device. + +Default value is 200. + .TP .BR \-L ", " \-\-list\-devices @@ -479,6 +489,8 @@ Enable local echo Enable line timestamp .IP "\fBtimestamp-format" Set timestamp format +.IP "\fBtimestamp-timeout" +Set timestamp timeout .IP "\fBmap" Map characters on input or output .IP "\fBcolor" diff --git a/src/bash-completion/tio.in b/src/bash-completion/tio.in index efe9617..6513b1a 100644 --- a/src/bash-completion/tio.in +++ b/src/bash-completion/tio.in @@ -28,6 +28,7 @@ _tio() -m --map \ -t --timestamp \ --timestamp-format \ + --timestamp-timeout \ -L --list-devices \ -c --color \ -S --socket \ @@ -118,6 +119,10 @@ _tio() COMPREPLY=( $(compgen -W "24hour 24hour-start 24hour-delta iso8601" -- ${cur}) ) return 0 ;; + --timestamp-timeout) + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + ;; -L | --list-devices) COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) return 0 diff --git a/src/configfile.c b/src/configfile.c index b123071..62c0edd 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -230,6 +230,10 @@ static int data_handler(void *user, const char *section, const char *name, { option.timestamp = timestamp_option_parse(value); } + else if (!strcmp(name, "timestamp-timeout")) + { + option.timestamp_timeout = read_integer(value, name, 0, LONG_MAX); + } else if (!strcmp(name, "map")) { asprintf(&c.map, "%s", value); diff --git a/src/options.c b/src/options.c index d231cc7..5c49c1d 100644 --- a/src/options.c +++ b/src/options.c @@ -45,6 +45,7 @@ enum opt_t { OPT_NONE, OPT_TIMESTAMP_FORMAT, + OPT_TIMESTAMP_TIMEOUT, OPT_LOG_FILE, OPT_LOG_DIRECTORY, OPT_LOG_STRIP, @@ -105,6 +106,7 @@ struct option_t option = .script = NULL, .script_filename = NULL, .script_run = SCRIPT_RUN_ALWAYS, + .timestamp_timeout = 200, }; void print_help(char *argv[]) @@ -130,6 +132,7 @@ void print_help(char *argv[]) printf(" --output-mode normal|hex Select output mode (default: normal)\n"); printf(" -t, --timestamp Enable line timestamp\n"); printf(" --timestamp-format Set timestamp format (default: 24hour)\n"); + printf(" --timestamp-timeout Set timestamp timeout (default: 200)\n"); printf(" -L, --list-devices List available serial devices by ID\n"); printf(" -l, --log Enable log to file\n"); printf(" --log-file Set log filename\n"); @@ -367,6 +370,7 @@ void options_parse(int argc, char *argv[]) {"local-echo", no_argument, 0, 'e' }, {"timestamp", no_argument, 0, 't' }, {"timestamp-format", required_argument, 0, OPT_TIMESTAMP_FORMAT }, + {"timestamp-timeout", required_argument, 0, OPT_TIMESTAMP_TIMEOUT }, {"list-devices", no_argument, 0, 'L' }, {"log", no_argument, 0, 'l' }, {"log-file", required_argument, 0, OPT_LOG_FILE }, @@ -461,6 +465,10 @@ void options_parse(int argc, char *argv[]) option.timestamp = timestamp_option_parse(optarg); break; + case OPT_TIMESTAMP_TIMEOUT: + option.timestamp_timeout = string_to_long(optarg); + break; + case 'L': list_serial_devices(); exit(EXIT_SUCCESS); diff --git a/src/options.h b/src/options.h index aff99fc..dca5d13 100644 --- a/src/options.h +++ b/src/options.h @@ -88,6 +88,7 @@ struct option_t const char *script; const char *script_filename; enum script_run_t script_run; + unsigned int timestamp_timeout; }; extern struct option_t option; diff --git a/src/tty.c b/src/tty.c index e4e348a..197a431 100644 --- a/src/tty.c +++ b/src/tty.c @@ -22,6 +22,7 @@ #include "config.h" #include #include +#include #include #include #include @@ -972,7 +973,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward) break; case TIMESTAMP_END: option.timestamp = TIMESTAMP_NONE; - tio_printf("Switched timestamp mode to off"); + tio_printf("Switched timestamp mode off"); break; } break; @@ -1545,6 +1546,7 @@ int tty_connect(void) char* now = NULL; unsigned int line_index = 0; static char previous_char[2] = {}; + struct timeval tval_before = {}, tval_now, tval_result; /* Open tty device */ device_fd = open(option.tty_device, O_RDWR | O_NOCTTY | O_NONBLOCK); @@ -1713,23 +1715,50 @@ int tty_connect(void) /* Update receive statistics */ rx_total += bytes_read; + // Manage timeout based timestamping in hex mode + if (option.output_mode == OUTPUT_MODE_HEX) + { + if (option.timestamp != TIMESTAMP_NONE) + { + gettimeofday(&tval_now, NULL); + timersub(&tval_now, &tval_before, &tval_result); + if ((tval_result.tv_sec * 1000 + tval_result.tv_usec / 1000) > option.timestamp_timeout) + { + now = timestamp_current_time(); + if (now) + { + ansi_printf_raw("\r\n[%s] ", now); + if (option.log) + { + log_printf("\r\n[%s] ", now); + } + next_timestamp = false; + } + } + tval_before = tval_now; + } + } + /* Process input byte by byte */ for (int i=0; i