From 78e1a845154a58e36f1606d161d0375d0eeadb16 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 17 Oct 2023 14:54:44 +0200 Subject: [PATCH] added the timeout to preferences, so its stored in the pref file but... there need to be adaptations to the uart_p*/uart_w* files to use the the session value.. --- CHANGELOG.md | 1 + client/src/cmdhw.c | 15 ++-- client/src/preferences.c | 151 ++++++++++++++++++++++++++++----------- client/src/ui.h | 1 + 4 files changed, 118 insertions(+), 50 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6043e3440..0ebf42904 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] + - Added client communication timeout to preferences (@iceman1001) - Added IPv6 support (@wh201906) - Fixed `lf hid clone --bin` - now correctly handles sentinel bits (@iceman1001) - Experimental UDP support in linux (@iceman1001, @wh201906) diff --git a/client/src/cmdhw.c b/client/src/cmdhw.c index 4b26bc00e..d47cec050 100644 --- a/client/src/cmdhw.c +++ b/client/src/cmdhw.c @@ -932,13 +932,12 @@ static int CmdTimeout(const char *Cmd) { CLIParserInit(&ctx, "hw timeout", "Set the communication timeout on the client side", "hw timeout --> Show current timeout\n" - "hw timeout -t 20 --> Set the timeout to 20ms\n" - "hw timeout -t 500 --> Set the timeout to 500ms\n" + "hw timeout --ms 500\n" ); void *argtable[] = { arg_param_begin, - arg_int0("t", "timeout", "", "timeout in ms"), + arg_int0(NULL, "ms", "", "timeout in micro seconds"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, true); @@ -949,20 +948,20 @@ static int CmdTimeout(const char *Cmd) { // timeout is not given/invalid, just show the current timeout then return if (arg < 0) { - PrintAndLogEx(INFO, "Current communication timeout: %ums", oldTimeout); + PrintAndLogEx(INFO, "Current communication timeout... " _GREEN_("%u") " ms", oldTimeout); return PM3_SUCCESS; } uint32_t newTimeout = arg; // UART_USB_CLIENT_RX_TIMEOUT_MS is considered as the minimum required timeout. if (newTimeout < UART_USB_CLIENT_RX_TIMEOUT_MS) { - PrintAndLogEx(WARNING, "Timeout less than %ums might cause errors.", UART_USB_CLIENT_RX_TIMEOUT_MS); + PrintAndLogEx(WARNING, "Timeout less than %u ms might cause errors.", UART_USB_CLIENT_RX_TIMEOUT_MS); } else if (newTimeout > 5000) { - PrintAndLogEx(WARNING, "Timeout greater than 5000ms makes the client unresponsive."); + PrintAndLogEx(WARNING, "Timeout greater than 5000 ms makes the client unresponsive."); } uart_reconfigure_timeouts(newTimeout); - PrintAndLogEx(INFO, "Old communication timeout: %ums", oldTimeout); - PrintAndLogEx(INFO, "New communication timeout: %ums", newTimeout); + PrintAndLogEx(INFO, "Old communication timeout... %u ms", oldTimeout); + PrintAndLogEx(INFO, "New communication timeout... " _GREEN_("%u") " ms", newTimeout); return PM3_SUCCESS; } diff --git a/client/src/preferences.c b/client/src/preferences.c index e3aef4ccb..cbdeb69f1 100644 --- a/client/src/preferences.c +++ b/client/src/preferences.c @@ -34,6 +34,7 @@ #include #include #include "cliparser.h" +#include "uart/uart.h" // uart_reconfigure_timeouts static int CmdHelp(const char *Cmd); static int setCmdHelp(const char *Cmd); @@ -52,6 +53,8 @@ int preferences_load(void) { // Set all defaults g_session.client_debug_level = cdbOFF; // g_session.device_debug_level = ddbOFF; + g_session.timeout = uart_get_timeouts(); + g_session.window_changed = false; g_session.plot.x = 10; g_session.plot.y = 30; @@ -260,7 +263,7 @@ void preferences_save_callback(json_t *root) { } */ JsonSaveInt(root, "client.exe.delay", g_session.client_exe_delay); - + JsonSaveInt(root, "client.timeout", g_session.timeout); } void preferences_load_callback(json_t *root) { json_error_t up_error = {0}; @@ -355,6 +358,10 @@ void preferences_load_callback(json_t *root) { // client command execution delay if (json_unpack_ex(root, &up_error, 0, "{s:i}", "client.exe.delay", &i1) == 0) g_session.client_exe_delay = i1; + + // client command timeout + if (json_unpack_ex(root, &up_error, 0, "{s:i}", "client.timeout", &i1) == 0) + g_session.timeout = i1; } // Help Functions @@ -379,44 +386,44 @@ static void showEmojiState(prefShowOpt_t opt) { switch (g_session.emoji_mode) { case EMO_ALIAS: - PrintAndLogEx(INFO, " %s emoji.................. "_GREEN_("alias"), prefShowMsg(opt)); + PrintAndLogEx(INFO, " %s emoji................... "_GREEN_("alias"), prefShowMsg(opt)); break; case EMO_EMOJI: - PrintAndLogEx(INFO, " %s emoji.................. "_GREEN_("emoji"), prefShowMsg(opt)); + PrintAndLogEx(INFO, " %s emoji................... "_GREEN_("emoji"), prefShowMsg(opt)); break; case EMO_ALTTEXT: - PrintAndLogEx(INFO, " %s emoji.................. "_GREEN_("alttext"), prefShowMsg(opt)); + PrintAndLogEx(INFO, " %s emoji................... "_GREEN_("alttext"), prefShowMsg(opt)); break; case EMO_NONE: - PrintAndLogEx(INFO, " %s emoji.................. "_GREEN_("none"), prefShowMsg(opt)); + PrintAndLogEx(INFO, " %s emoji................... "_GREEN_("none"), prefShowMsg(opt)); break; default: - PrintAndLogEx(INFO, " %s emoji.................. "_RED_("unknown"), prefShowMsg(opt)); + PrintAndLogEx(INFO, " %s emoji................... "_RED_("unknown"), prefShowMsg(opt)); } } static void showColorState(prefShowOpt_t opt) { if (g_session.supports_colors) - PrintAndLogEx(INFO, " %s color.................. "_GREEN_("ansi"), prefShowMsg(opt)); + PrintAndLogEx(INFO, " %s color................... "_GREEN_("ansi"), prefShowMsg(opt)); else - PrintAndLogEx(INFO, " %s color.................. "_WHITE_("off"), prefShowMsg(opt)); + PrintAndLogEx(INFO, " %s color................... "_WHITE_("off"), prefShowMsg(opt)); } static void showClientDebugState(prefShowOpt_t opt) { switch (g_session.client_debug_level) { case cdbOFF: - PrintAndLogEx(INFO, " %s client debug........... "_WHITE_("off"), prefShowMsg(opt)); + PrintAndLogEx(INFO, " %s client debug............ "_WHITE_("off"), prefShowMsg(opt)); break; case cdbSIMPLE: - PrintAndLogEx(INFO, " %s client debug........... "_GREEN_("simple"), prefShowMsg(opt)); + PrintAndLogEx(INFO, " %s client debug............ "_GREEN_("simple"), prefShowMsg(opt)); break; case cdbFULL: - PrintAndLogEx(INFO, " %s client debug........... "_GREEN_("full"), prefShowMsg(opt)); + PrintAndLogEx(INFO, " %s client debug............ "_GREEN_("full"), prefShowMsg(opt)); break; default: - PrintAndLogEx(INFO, " %s client debug........... "_RED_("unknown"), prefShowMsg(opt)); + PrintAndLogEx(INFO, " %s client debug............ "_RED_("unknown"), prefShowMsg(opt)); } } /* @@ -448,17 +455,17 @@ static void showSavePathState(savePaths_t path_index, prefShowOpt_t opt) { char s[50]; switch (path_index) { case spDefault: - strcpy(s, "default save path......"); + strcpy(s, "default save path......."); break; case spDump: - strcpy(s, "dump save path........."); + strcpy(s, "dump save path.........."); break; case spTrace: - strcpy(s, "trace save path........"); + strcpy(s, "trace save path........."); break; case spItemCount: default: - strcpy(s, _RED_("unknown")" save path......"); + strcpy(s, _RED_("unknown")" save path......."); } if (path_index < spItemCount) { @@ -478,7 +485,7 @@ static void showSavePathState(savePaths_t path_index, prefShowOpt_t opt) { } static void showPlotPosState(void) { - PrintAndLogEx(INFO, " Plot window............ X "_GREEN_("%4d")" Y "_GREEN_("%4d")" H "_GREEN_("%4d")" W "_GREEN_("%4d"), + PrintAndLogEx(INFO, " plot window............. X "_GREEN_("%4d")" Y "_GREEN_("%4d")" H "_GREEN_("%4d")" W "_GREEN_("%4d"), g_session.plot.x, g_session.plot.y, g_session.plot.h, @@ -487,7 +494,7 @@ static void showPlotPosState(void) { } static void showOverlayPosState(void) { - PrintAndLogEx(INFO, " Slider/Overlay window.. X "_GREEN_("%4d")" Y "_GREEN_("%4d")" H "_GREEN_("%4d")" W "_GREEN_("%4d"), + PrintAndLogEx(INFO, " slider/overlay window... X "_GREEN_("%4d")" Y "_GREEN_("%4d")" H "_GREEN_("%4d")" W "_GREEN_("%4d"), g_session.overlay.x, g_session.overlay.y, g_session.overlay.h, @@ -497,44 +504,47 @@ static void showOverlayPosState(void) { static void showHintsState(prefShowOpt_t opt) { if (g_session.show_hints) - PrintAndLogEx(INFO, " %s hints.................. "_GREEN_("on"), prefShowMsg(opt)); + PrintAndLogEx(INFO, " %s hints................... "_GREEN_("on"), prefShowMsg(opt)); else - PrintAndLogEx(INFO, " %s hints.................. "_WHITE_("off"), prefShowMsg(opt)); + PrintAndLogEx(INFO, " %s hints................... "_WHITE_("off"), prefShowMsg(opt)); } static void showPlotSliderState(prefShowOpt_t opt) { if (g_session.overlay_sliders) - PrintAndLogEx(INFO, " %s show plot sliders...... "_GREEN_("on"), prefShowMsg(opt)); + PrintAndLogEx(INFO, " %s show plot sliders....... "_GREEN_("on"), prefShowMsg(opt)); else - PrintAndLogEx(INFO, " %s show plot sliders...... "_WHITE_("off"), prefShowMsg(opt)); + PrintAndLogEx(INFO, " %s show plot sliders....... "_WHITE_("off"), prefShowMsg(opt)); } static void showBarModeState(prefShowOpt_t opt) { switch (g_session.bar_mode) { case STYLE_BAR: - PrintAndLogEx(INFO, " %s barmode................ "_GREEN_("bar"), prefShowMsg(opt)); + PrintAndLogEx(INFO, " %s barmode................. "_GREEN_("bar"), prefShowMsg(opt)); break; case STYLE_MIXED: - PrintAndLogEx(INFO, " %s barmode................ "_GREEN_("mixed"), prefShowMsg(opt)); + PrintAndLogEx(INFO, " %s barmode................. "_GREEN_("mixed"), prefShowMsg(opt)); break; case STYLE_VALUE: - PrintAndLogEx(INFO, " %s barmode................ "_GREEN_("value"), prefShowMsg(opt)); + PrintAndLogEx(INFO, " %s barmode................. "_GREEN_("value"), prefShowMsg(opt)); break; default: - PrintAndLogEx(INFO, " %s barmode............... "_RED_("unknown"), prefShowMsg(opt)); + PrintAndLogEx(INFO, " %s barmode................ "_RED_("unknown"), prefShowMsg(opt)); } } static void showOutputState(prefShowOpt_t opt) { - PrintAndLogEx(INFO, " %s output................. %s", prefShowMsg(opt), + PrintAndLogEx(INFO, " %s output.................. %s", prefShowMsg(opt), g_session.dense_output ? _GREEN_("dense") : _WHITE_("normal")); } static void showClientExeDelayState(void) { - PrintAndLogEx(INFO, " Cmd execution delay.... "_GREEN_("%u"), g_session.client_exe_delay); + PrintAndLogEx(INFO, " cmd execution delay..... "_GREEN_("%u"), g_session.client_exe_delay); } +static void showClientTimeoutState(void) { + PrintAndLogEx(INFO, " communication timeout... " _GREEN_("%u") " ms", g_session.timeout); +} static int setCmdEmoji(const char *Cmd) { CLIParserContext *ctx; @@ -636,9 +646,9 @@ static int setCmdColor(const char *Cmd) { static int setCmdDebug(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "prefs set clientdebug ", + CLIParserInit(&ctx, "prefs set client.debug ", "Set persistent preference of using clientside debug level", - "prefs set clientdebug --simple" + "prefs set client.debug --simple" ); void *argtable[] = { @@ -798,10 +808,10 @@ static int setCmdOutput(const char *Cmd) { static int setCmdExeDelay(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "prefs set clientdelay", + CLIParserInit(&ctx, "prefs set client.delay", "Set persistent preference of delay before executing a command in the client", - "prefs set clientdelay --ms 0 --> unsets any delay\n" - "prefs set clientdelay --ms 1000 --> sets 1000ms delay" + "prefs set client.delay --ms 0 --> unsets any delay\n" + "prefs set client.delay --ms 1000 --> sets 1000ms delay" ); void *argtable[] = { @@ -824,9 +834,46 @@ static int setCmdExeDelay(const char *Cmd) { return PM3_SUCCESS; } +static int setClientTimeout(const char *Cmd) { + CLIParserContext *ctx; + CLIParserInit(&ctx, "prefs set client.timeout", + "Set persistent preference of client communication timeout", + "prefs set client.timeout --ms 0 --> unsets any timeout\n" + "prefs set client.timeout --ms 500\n" + ); + + void *argtable[] = { + arg_param_begin, + arg_int0(NULL, "ms", "", "timeout in micro seconds"), + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, true); + uint32_t new_value = (uint32_t)arg_get_int_def(ctx, 1, 0); + CLIParserFree(ctx); + + // UART_USB_CLIENT_RX_TIMEOUT_MS is considered as the minimum required timeout. + if (new_value < UART_USB_CLIENT_RX_TIMEOUT_MS) { + PrintAndLogEx(WARNING, "Timeout less than %u ms might cause errors.", UART_USB_CLIENT_RX_TIMEOUT_MS); + } else if (new_value > 5000) { + PrintAndLogEx(WARNING, "Timeout greater than 5000 ms makes the client unresponsive."); + } + + if (g_session.timeout != new_value) { + showClientTimeoutState(); + g_session.timeout = new_value; + uart_reconfigure_timeouts(new_value); + showClientTimeoutState(); + preferences_save(); + } else { + showClientTimeoutState(); + } + return PM3_SUCCESS; +} + + static int setCmdHint(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "prefs set hints ", + CLIParserInit(&ctx, "prefs set hints", "Set persistent preference of showing hint messages in the client", "prefs set hints --on" ); @@ -1087,9 +1134,9 @@ static int getCmdColor(const char *Cmd) { static int getCmdDebug(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "prefs get clientdebug", + CLIParserInit(&ctx, "prefs get client.debug", "Get preference of using clientside debug level", - "prefs get clientdebug" + "prefs get client.debug" ); void *argtable[] = { arg_param_begin, @@ -1169,9 +1216,9 @@ static int getCmdSavePaths(const char *Cmd) { static int getCmdExeDelay(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "prefs get clientdelay", + CLIParserInit(&ctx, "prefs get client.delay", "Get preference of delay time before execution of a command in the client", - "prefs get clientdelay" + "prefs get client.delay" ); void *argtable[] = { arg_param_begin, @@ -1183,10 +1230,27 @@ static int getCmdExeDelay(const char *Cmd) { return PM3_SUCCESS; } +static int getClientTimeout(const char *Cmd) { + CLIParserContext *ctx; + CLIParserInit(&ctx, "prefs get client.timeout", + "Get preference of delay time before execution of a command in the client", + "prefs get client.timeout" + ); + void *argtable[] = { + arg_param_begin, + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, true); + CLIParserFree(ctx); + showClientTimeoutState(); + return PM3_SUCCESS; +} + static command_t CommandTableGet[] = { {"barmode", getCmdBarMode, AlwaysAvailable, "Get bar mode preference"}, - {"clientdebug", getCmdDebug, AlwaysAvailable, "Get client debug level preference"}, - {"clientdelay", getCmdExeDelay, AlwaysAvailable, "Get client execution delay preference"}, + {"client.debug", getCmdDebug, AlwaysAvailable, "Get client debug level preference"}, + {"client.delay", getCmdExeDelay, AlwaysAvailable, "Get client execution delay preference"}, + {"client.timeout", getClientTimeout, AlwaysAvailable, "Get client execution delay preference"}, {"color", getCmdColor, AlwaysAvailable, "Get color support preference"}, {"savepaths", getCmdSavePaths, AlwaysAvailable, "Get file folder "}, // {"devicedebug", getCmdDeviceDebug, AlwaysAvailable, "Get device debug level"}, @@ -1200,8 +1264,10 @@ static command_t CommandTableGet[] = { static command_t CommandTableSet[] = { {"help", setCmdHelp, AlwaysAvailable, "This help"}, {"barmode", setCmdBarMode, AlwaysAvailable, "Set bar mode"}, - {"clientdebug", setCmdDebug, AlwaysAvailable, "Set client debug level"}, - {"clientdelay", setCmdExeDelay, AlwaysAvailable, "Set client execution delay"}, + {"client.debug", setCmdDebug, AlwaysAvailable, "Set client debug level"}, + {"client.delay", setCmdExeDelay, AlwaysAvailable, "Set client execution delay"}, + {"client.timeout", setClientTimeout, AlwaysAvailable, "Set client communication timeout"}, + {"color", setCmdColor, AlwaysAvailable, "Set color support"}, {"emoji", setCmdEmoji, AlwaysAvailable, "Set emoji display"}, {"hints", setCmdHint, AlwaysAvailable, "Set hint display"}, @@ -1265,6 +1331,7 @@ static int CmdPrefShow(const char *Cmd) { showBarModeState(prefShowNone); showClientExeDelayState(); showOutputState(prefShowNone); + showClientTimeoutState(); PrintAndLogEx(NORMAL, ""); return PM3_SUCCESS; diff --git a/client/src/ui.h b/client/src/ui.h index 4042550b3..4cfd75660 100644 --- a/client/src/ui.h +++ b/client/src/ui.h @@ -62,6 +62,7 @@ typedef struct { uint16_t client_exe_delay; char *history_path; pm3_device_t *current_device; + uint32_t timeout; } session_arg_t; extern session_arg_t g_session;