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 <ms> for setting the timeout value in
milliseconds.

Defaults to 200 ms.
This commit is contained in:
Martin Lund 2024-04-18 15:45:50 +02:00
parent a8e0d2693d
commit 6fff4939e4
8 changed files with 76 additions and 20 deletions

View file

@ -43,7 +43,8 @@ when used in combination with [tmux](https://tmux.github.io).
* Pulse serial lines with configurable pulse duration * Pulse serial lines with configurable pulse duration
* Local echo support * Local echo support
* Remapping of characters (nl, cr-nl, bs, lowercase to uppercase, etc.) * 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 character
* Support for delayed output per line * Support for delayed output per line
* Switchable independent input and output mode (normal vs hex) * Switchable independent input and output mode (normal vs hex)
@ -94,6 +95,7 @@ Options:
--output-mode normal|hex Select output mode (default: normal) --output-mode normal|hex Select output mode (default: normal)
-t, --timestamp Enable line timestamp -t, --timestamp Enable line timestamp
--timestamp-format <format> Set timestamp format (default: 24hour) --timestamp-format <format> Set timestamp format (default: 24hour)
--timestamp-timeout <ms> Set timestamp timeout (default: 200)
-L, --list-devices List available serial devices by ID -L, --list-devices List available serial devices by ID
-l, --log Enable log to file -l, --log Enable log to file
--log-file <filename> Set log filename --log-file <filename> Set log filename

11
TODO
View file

@ -17,16 +17,11 @@
With and without timestamp support for each broken up line. 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 Already supported in hex output mode.
defined in the configuration file, e.g.:
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 Current line mode only support backspace editing. Would be nice with arrow
key navigation left/right and insert/overwrite support. Also history browsing key navigation left/right and insert/overwrite support. Also history browsing

View file

@ -121,6 +121,16 @@ ISO8601 format ("YYYY-MM-DDThh:mm:ss.sss")
Default format is \fB24hour\fR Default format is \fB24hour\fR
.RE .RE
.TP
.BR " \-\-timestamp\-timeout \fI<ms>
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 .TP
.BR \-L ", " \-\-list\-devices .BR \-L ", " \-\-list\-devices
@ -479,6 +489,8 @@ Enable local echo
Enable line timestamp Enable line timestamp
.IP "\fBtimestamp-format" .IP "\fBtimestamp-format"
Set timestamp format Set timestamp format
.IP "\fBtimestamp-timeout"
Set timestamp timeout
.IP "\fBmap" .IP "\fBmap"
Map characters on input or output Map characters on input or output
.IP "\fBcolor" .IP "\fBcolor"

View file

@ -28,6 +28,7 @@ _tio()
-m --map \ -m --map \
-t --timestamp \ -t --timestamp \
--timestamp-format \ --timestamp-format \
--timestamp-timeout \
-L --list-devices \ -L --list-devices \
-c --color \ -c --color \
-S --socket \ -S --socket \
@ -118,6 +119,10 @@ _tio()
COMPREPLY=( $(compgen -W "24hour 24hour-start 24hour-delta iso8601" -- ${cur}) ) COMPREPLY=( $(compgen -W "24hour 24hour-start 24hour-delta iso8601" -- ${cur}) )
return 0 return 0
;; ;;
--timestamp-timeout)
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
;;
-L | --list-devices) -L | --list-devices)
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0 return 0

View file

@ -230,6 +230,10 @@ static int data_handler(void *user, const char *section, const char *name,
{ {
option.timestamp = timestamp_option_parse(value); 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")) else if (!strcmp(name, "map"))
{ {
asprintf(&c.map, "%s", value); asprintf(&c.map, "%s", value);

View file

@ -45,6 +45,7 @@ enum opt_t
{ {
OPT_NONE, OPT_NONE,
OPT_TIMESTAMP_FORMAT, OPT_TIMESTAMP_FORMAT,
OPT_TIMESTAMP_TIMEOUT,
OPT_LOG_FILE, OPT_LOG_FILE,
OPT_LOG_DIRECTORY, OPT_LOG_DIRECTORY,
OPT_LOG_STRIP, OPT_LOG_STRIP,
@ -105,6 +106,7 @@ struct option_t option =
.script = NULL, .script = NULL,
.script_filename = NULL, .script_filename = NULL,
.script_run = SCRIPT_RUN_ALWAYS, .script_run = SCRIPT_RUN_ALWAYS,
.timestamp_timeout = 200,
}; };
void print_help(char *argv[]) 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(" --output-mode normal|hex Select output mode (default: normal)\n");
printf(" -t, --timestamp Enable line timestamp\n"); printf(" -t, --timestamp Enable line timestamp\n");
printf(" --timestamp-format <format> Set timestamp format (default: 24hour)\n"); printf(" --timestamp-format <format> Set timestamp format (default: 24hour)\n");
printf(" --timestamp-timeout <ms> Set timestamp timeout (default: 200)\n");
printf(" -L, --list-devices List available serial devices by ID\n"); printf(" -L, --list-devices List available serial devices by ID\n");
printf(" -l, --log Enable log to file\n"); printf(" -l, --log Enable log to file\n");
printf(" --log-file <filename> Set log filename\n"); printf(" --log-file <filename> Set log filename\n");
@ -367,6 +370,7 @@ void options_parse(int argc, char *argv[])
{"local-echo", no_argument, 0, 'e' }, {"local-echo", no_argument, 0, 'e' },
{"timestamp", no_argument, 0, 't' }, {"timestamp", no_argument, 0, 't' },
{"timestamp-format", required_argument, 0, OPT_TIMESTAMP_FORMAT }, {"timestamp-format", required_argument, 0, OPT_TIMESTAMP_FORMAT },
{"timestamp-timeout", required_argument, 0, OPT_TIMESTAMP_TIMEOUT },
{"list-devices", no_argument, 0, 'L' }, {"list-devices", no_argument, 0, 'L' },
{"log", no_argument, 0, 'l' }, {"log", no_argument, 0, 'l' },
{"log-file", required_argument, 0, OPT_LOG_FILE }, {"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); option.timestamp = timestamp_option_parse(optarg);
break; break;
case OPT_TIMESTAMP_TIMEOUT:
option.timestamp_timeout = string_to_long(optarg);
break;
case 'L': case 'L':
list_serial_devices(); list_serial_devices();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);

View file

@ -88,6 +88,7 @@ struct option_t
const char *script; const char *script;
const char *script_filename; const char *script_filename;
enum script_run_t script_run; enum script_run_t script_run;
unsigned int timestamp_timeout;
}; };
extern struct option_t option; extern struct option_t option;

View file

@ -22,6 +22,7 @@
#include "config.h" #include "config.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/time.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
@ -972,7 +973,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
break; break;
case TIMESTAMP_END: case TIMESTAMP_END:
option.timestamp = TIMESTAMP_NONE; option.timestamp = TIMESTAMP_NONE;
tio_printf("Switched timestamp mode to off"); tio_printf("Switched timestamp mode off");
break; break;
} }
break; break;
@ -1545,6 +1546,7 @@ int tty_connect(void)
char* now = NULL; char* now = NULL;
unsigned int line_index = 0; unsigned int line_index = 0;
static char previous_char[2] = {}; static char previous_char[2] = {};
struct timeval tval_before = {}, tval_now, tval_result;
/* Open tty device */ /* Open tty device */
device_fd = open(option.tty_device, O_RDWR | O_NOCTTY | O_NONBLOCK); device_fd = open(option.tty_device, O_RDWR | O_NOCTTY | O_NONBLOCK);
@ -1713,13 +1715,39 @@ int tty_connect(void)
/* Update receive statistics */ /* Update receive statistics */
rx_total += bytes_read; 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 */ /* Process input byte by byte */
for (int i=0; i<bytes_read; i++) for (int i=0; i<bytes_read; i++)
{ {
input_char = input_buffer[i]; input_char = input_buffer[i];
/* Print timestamp on new line if enabled */ /* Print timestamp on new line if enabled */
if ((next_timestamp && input_char != '\n' && input_char != '\r') && (option.output_mode == OUTPUT_MODE_NORMAL)) if (option.output_mode == OUTPUT_MODE_NORMAL)
{
if ((next_timestamp && input_char != '\n' && input_char != '\r'))
{ {
now = timestamp_current_time(); now = timestamp_current_time();
if (now) if (now)
@ -1732,6 +1760,7 @@ int tty_connect(void)
next_timestamp = false; next_timestamp = false;
} }
} }
}
/* Convert MSB to LSB bit order */ /* Convert MSB to LSB bit order */
if (map_o_msblsb) if (map_o_msblsb)