From ef764dabc6bf5cef74ae9228f81e661dbd701fe0 Mon Sep 17 00:00:00 2001 From: Aaron Cook Date: Sat, 13 May 2023 14:11:22 -0500 Subject: [PATCH] #11 Implement inter command timeout --- Makefile | 2 +- src/bytenuts.c | 15 +++++++++++++++ src/bytenuts.h | 5 ++++- src/ingest.c | 31 ++++++++++++++++++++++++++++--- src/ingest.h | 2 ++ 5 files changed, 50 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 7302347..2c57a52 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC=clang -CFLAGS=-Wall -MMD -pthread -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE +CFLAGS=-Wall -MMD -pthread -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=199309L LDFLAGS=-lpthread -lncurses DIR_SRC=src diff --git a/src/bytenuts.c b/src/bytenuts.c index b4b158d..02db1ea 100644 --- a/src/bytenuts.c +++ b/src/bytenuts.c @@ -177,6 +177,8 @@ bytenuts_print_stats() cheerios_insert(st_line, strlen(st_line)); sprintf(st_line, "serial_path: %s\r\n", bytenuts.config.serial_path); cheerios_insert(st_line, strlen(st_line)); + sprintf(st_line, "inter_cmd_to: %d\r\n", bytenuts.config.inter_cmd_to); + cheerios_insert(st_line, strlen(st_line)); return 0; } @@ -357,6 +359,13 @@ parse_args(int argc, char **argv) bytenuts.config.escape = argv[i][9]; bytenuts.config_overrides[3] = 1; } + else if (arg_len > 15 && !memcmp(argv[i], "--inter_cmd_to=", 15)) { + long cmd_to = strtol(&argv[i][15], NULL, 10); + if (cmd_to >= 0) { + bytenuts.config.inter_cmd_to = cmd_to; + bytenuts.config_overrides[4] = 1; + } + } else if (!strcmp(argv[i], "--resume") || !strcmp(argv[i], "-r")) { bytenuts.resume = 1; } @@ -404,6 +413,12 @@ load_configs() else if (!bytenuts.config_overrides[3] && !memcmp(line, "escape=", 7)) { bytenuts.config.escape = line[7]; } + else if (!bytenuts.config_overrides[4] && !memcmp(line, "inter_cmd_to=", 13)) { + long cmd_to = strtol(&line[13], NULL, 10); + if (cmd_to >= 0) { + bytenuts.config.inter_cmd_to = cmd_to; + } + } } return 0; diff --git a/src/bytenuts.h b/src/bytenuts.h index a8f470a..389047e 100644 --- a/src/bytenuts.h +++ b/src/bytenuts.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "serial.h" @@ -27,6 +28,7 @@ typedef struct bytenuts_config_struct { char *config_path; /* config file path */ char *log_path; /* path to the log file (if it exists) */ char *serial_path; /* path to the target serial device */ + uint32_t inter_cmd_to; /* inter command timeout in ms */ } bytenuts_config_t; #define CONFIG_DEFAULT (bytenuts_config_t){ \ @@ -38,12 +40,13 @@ typedef struct bytenuts_config_struct { .config_path = NULL, \ .log_path = NULL, \ .serial_path = NULL, \ + .inter_cmd_to = 10, \ } typedef struct bytenuts_struct { int serial_fd; bytenuts_config_t config; - int config_overrides[4]; + int config_overrides[5]; int resume; bytenuts_state_t state; WINDOW *status_win; diff --git a/src/ingest.c b/src/ingest.c index 1d043ac..4189730 100644 --- a/src/ingest.c +++ b/src/ingest.c @@ -8,10 +8,12 @@ #include #include #include +#include #include "bytenuts.h" #include "cheerios.h" #include "ingest.h" +#include "timer_math.h" static void *ingest_thread(void *arg); @@ -21,10 +23,11 @@ static int mode_normal(int ch); static int mode_xmodem(int block_sz); static int mode_hex(int ch); static int handle_functions(int ch); -static int print_stats(); -static int auto_complete(); +static int print_stats(void); +static int auto_complete(void); static int read_cmd_page(int idx); -static void update_cmd_pg_status(); +static void update_cmd_pg_status(void); +static void inter_command_wait(void); int ingest_start(bytenuts_t *bytenuts) @@ -70,6 +73,10 @@ ingest_start(bytenuts_t *bytenuts) } update_cmd_pg_status(); + clock_gettime(CLOCK_REALTIME, &ingest.cmd_ts); + /* ensure first command is not delayed */ + timer_sub_ms(&ingest.cmd_ts, ingest.config->inter_cmd_to); + ingest.running = 1; if (pthread_create(&ingest.thr, NULL, ingest_thread, NULL)) { return -1; @@ -471,6 +478,7 @@ mode_normal(int ch) else ending = "\r\n"; + inter_command_wait(); cheerios_input(ingest.inbuf, inlen); cheerios_input(ending, strlen(ending)); if (ingest.config->echo) { @@ -688,6 +696,7 @@ mode_hex(int ch) hexlen++; } + inter_command_wait(); cheerios_input(hexbuf, hexlen); if (ingest.config->echo) { cheerios_insert(">> (hex)\r\n", 10); @@ -1029,3 +1038,19 @@ update_cmd_pg_status() bytenuts_set_status(STATUS_CMDPAGE, "n/a"); } } + +static void +inter_command_wait() +{ + struct timespec target; + struct timespec now; + + clock_gettime(CLOCK_REALTIME, &now); + memcpy(&target, &ingest.cmd_ts, sizeof(struct timespec)); + + timer_add_ms(&target, ingest.config->inter_cmd_to); + timer_sub(&target, &now); + + nanosleep(&target, NULL); + clock_gettime(CLOCK_REALTIME, &ingest.cmd_ts); +} diff --git a/src/ingest.h b/src/ingest.h index 5f39b06..e8e4474 100644 --- a/src/ingest.h +++ b/src/ingest.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "bytenuts.h" @@ -40,6 +41,7 @@ typedef struct ingest_struct { int cmd_pgs_n; /* what command page is currently selected */ int cmd_pg_cur; + struct timespec cmd_ts; /* last command sent timestamp */ } ingest_t; /* startup the input window thread */