From 19285478179bd8c24431dfd5203c0177a0a6122d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20=C3=96hrstr=C3=B6m?= Date: Wed, 16 Aug 2023 16:08:50 +0200 Subject: [PATCH 1/2] Add option -f check if incoming data has stalled, if so force an exit. --- rtl_wmbus.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/rtl_wmbus.c b/rtl_wmbus.c index 32f49a7..a8208a3 100644 --- a/rtl_wmbus.c +++ b/rtl_wmbus.c @@ -48,6 +48,14 @@ #define WINDOWS_BUILD 0 #endif +#if WINDOWS_BUILD == 0 +#define CHECK_FLOW 1 +#include +#include +#else +#define CHECK_FLOW 0 +#endif + #if WINDOWS_BUILD == 1 #include #warning "Compiling for Win discludes network support." @@ -801,6 +809,7 @@ static int opts_accurate_atan = 1; int opts_show_used_algorithm = 0; static int opts_t1_c1_processing_enabled = 1; static int opts_s1_processing_enabled = 1; +static int opts_check_flow = 0; static const unsigned opts_CLOCK_LOCK_THRESHOLD_T1_C1 = 2; // Is not implemented as option yet. static const unsigned opts_CLOCK_LOCK_THRESHOLD_S1 = 2; // Is not implemented as option yet. @@ -817,6 +826,8 @@ static void print_usage(const char *program_name) fprintf(stdout, "\t-V show version\n"); fprintf(stdout, "\t-s receive S1 and T1/C1 datagrams simultaneously. rtl_sdr _MUST_ be set to 868.625MHz (-f 868.625M)\n"); fprintf(stdout, "\t-p [T,S] to disable processing T1/C1 or S1 mode.\n"); + fprintf(stdout, "\t-f exit if flow of incoming data stops.\n"); + fprintf(stdout, "\t-h print this help\n"); } static void print_version(void) @@ -829,10 +840,16 @@ static void process_options(int argc, char *argv[]) { int option; - while ((option = getopt(argc, argv, "ad:p:r:vVst:")) != -1) + while ((option = getopt(argc, argv, "fad:p:r:vVst:")) != -1) { switch (option) { + case 'f': + opts_check_flow = 1; +#if CHECK_FLOW == 0 + fprintf(stderr, "rtl_wmbus: Warning! You supplied the option -f but this build of rtl_wmbus cannot check flow of incoming data!\n"); +#endif + break; case 'a': opts_accurate_atan = 0; break; @@ -1139,13 +1156,48 @@ void s1_signal_chain_empty(float i_s1, float q_s1, { } +#if CHECK_FLOW == 1 +#define START_ALARM if (opts_check_flow) { alarm(2); } +#define STOP_ALARM if (opts_check_flow) { alarm(0); } + +static void sig_alarm_handler(int signo) +{ + fprintf(stderr, "rtl_wmbus: exiting since incoming data stopped flowing!\n"); + exit(1); +} +#else +#define START_ALARM +#define STOP_ALARM +#endif + int main(int argc, char *argv[]) { #if WINDOWS_BUILD == 1 _setmode(_fileno(stdin), _O_BINARY); + #else + if (isatty(0)) + { + // Standard input is a terminal, print help. + print_usage(argv[0]); + exit(0); + } #endif process_options(argc, argv); +#if CHECK_FLOW == 1 + struct sigaction old_alarm; + struct sigaction new_alarm; + + if (opts_check_flow) + { + new_alarm.sa_handler = sig_alarm_handler; + sigemptyset(&new_alarm.sa_mask); + new_alarm.sa_flags = 0; + + fprintf(stderr, "rtl_wmbus: monitoring flow\n"); + sigaction(SIGALRM, &new_alarm, &old_alarm); + } +#endif __attribute__((__aligned__(16))) uint8_t samples[4096]; const int fs_kHz = opts_decimation_rate*800; // Sample rate [kHz] as a multiple of 800 kHz. @@ -1197,7 +1249,9 @@ int main(int argc, char *argv[]) while (!feof(input)) { + START_ALARM; size_t read_items = fread(samples, sizeof(samples), 1, input); + STOP_ALARM; if (1 != read_items) { // End of file?.. @@ -1253,6 +1307,13 @@ int main(int argc, char *argv[]) } } + #if CHECK_FLOW == 1 + if (opts_check_flow) + { + sigaction(SIGALRM, &old_alarm, NULL); + } + #endif + if (input != stdin) fclose(input); free(LUT_FREQUENCY_TRANSLATION_PLUS_COSINE); free(LUT_FREQUENCY_TRANSLATION_PLUS_SINE); From 325a2eb8e2c520d312eb2d064fa000c0c1354e12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20=C3=96hrstr=C3=B6m?= Date: Wed, 16 Aug 2023 16:44:48 +0200 Subject: [PATCH 2/2] Only print help if no arguments given and stdin is a tty. --- rtl_wmbus.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rtl_wmbus.c b/rtl_wmbus.c index a8208a3..f026700 100644 --- a/rtl_wmbus.c +++ b/rtl_wmbus.c @@ -1175,7 +1175,7 @@ int main(int argc, char *argv[]) #if WINDOWS_BUILD == 1 _setmode(_fileno(stdin), _O_BINARY); #else - if (isatty(0)) + if (isatty(0) && argc == 1) { // Standard input is a terminal, print help. print_usage(argv[0]);