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
* 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 <format> Set timestamp format (default: 24hour)
--timestamp-timeout <ms> Set timestamp timeout (default: 200)
-L, --list-devices List available serial devices by ID
-l, --log Enable log to file
--log-file <filename> Set log filename

11
TODO
View file

@ -17,16 +17,11 @@
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
Time is in ms.
* Advanced line mode
* 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

View file

@ -121,6 +121,16 @@ ISO8601 format ("YYYY-MM-DDThh:mm:ss.sss")
Default format is \fB24hour\fR
.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
.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"

View file

@ -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

View file

@ -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);

View file

@ -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 <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, --log Enable log to file\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' },
{"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);

View file

@ -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;

View file

@ -22,6 +22,7 @@
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
@ -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,13 +1715,39 @@ 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<bytes_read; i++)
{
input_char = input_buffer[i];
/* 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();
if (now)
@ -1732,6 +1760,7 @@ int tty_connect(void)
next_timestamp = false;
}
}
}
/* Convert MSB to LSB bit order */
if (map_o_msblsb)