From 9181cbe0493fea5cfc8d0feed89eaac4e42e1909 Mon Sep 17 00:00:00 2001 From: Michael Ossmann Date: Sun, 18 Sep 2022 05:07:09 -0400 Subject: [PATCH 1/4] hackrf_transfer: send signal to own PID Instead of sending a signal to the process group, get our own process ID and send the signal to it. This fixes a bug that prevented termination when called from a script. --- host/hackrf-tools/src/hackrf_transfer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/host/hackrf-tools/src/hackrf_transfer.c b/host/hackrf-tools/src/hackrf_transfer.c index 5d1adbc9..d7903f60 100644 --- a/host/hackrf-tools/src/hackrf_transfer.c +++ b/host/hackrf-tools/src/hackrf_transfer.c @@ -397,7 +397,7 @@ void stop_main_loop(void) #ifdef _WIN32 SetEvent(interrupt_handle); #else - kill(0, SIGALRM); + kill(getpid(), SIGALRM); #endif } From 22635bf07d8f2353703a69356989f0e1d8c7092b Mon Sep 17 00:00:00 2001 From: Michael Ossmann Date: Sun, 18 Sep 2022 05:13:25 -0400 Subject: [PATCH 2/4] libhackrf: update hackrf_transfer struct comment --- host/libhackrf/src/hackrf.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/host/libhackrf/src/hackrf.h b/host/libhackrf/src/hackrf.h index 68bfd1cd..3f4f010c 100644 --- a/host/libhackrf/src/hackrf.h +++ b/host/libhackrf/src/hackrf.h @@ -154,7 +154,9 @@ typedef struct hackrf_device hackrf_device; /** * USB transfer information passed to RX or TX callback. - * A callback should treat all these fields as read-only except that a TX callback should write to the data buffer. + * A callback should treat all these fields as read-only except that a TX + * callback should write to the data buffer and may write to valid_length to + * indicate that a smaller number of bytes is to be transmitted. */ typedef struct { hackrf_device* device; /**< HackRF USB device for this transfer */ From 82ecfce41455ae66e09ba2fbaa4bb1ef1f979a6b Mon Sep 17 00:00:00 2001 From: Michael Ossmann Date: Sun, 18 Sep 2022 05:49:03 -0400 Subject: [PATCH 3/4] hackrf_transfer: simplify hardware sync mode logic The -H option now requires no argument. --- host/hackrf-tools/src/hackrf_transfer.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/host/hackrf-tools/src/hackrf_transfer.c b/host/hackrf-tools/src/hackrf_transfer.c index d7903f60..d1e723bb 100644 --- a/host/hackrf-tools/src/hackrf_transfer.c +++ b/host/hackrf-tools/src/hackrf_transfer.c @@ -335,7 +335,6 @@ bool signalsource = false; uint32_t amplitude = 0; bool hw_sync = false; -uint32_t hw_sync_enable = 0; bool receive = false; bool receive_wav = false; @@ -639,7 +638,7 @@ static void usage() printf("\t[-b baseband_filter_bw_hz] # Set baseband filter bandwidth in Hz.\n"); printf("\tPossible values: 1.75/2.5/3.5/5/5.5/6/7/8/9/10/12/14/15/20/24/28MHz, default <= 0.75 * sample_rate_hz.\n"); printf("\t[-C ppm] # Set Internal crystal clock error in ppm.\n"); - printf("\t[-H hw_sync_enable] # Synchronise USB transfer using GPIO pins.\n"); + printf("\t[-H hw_sync] # Synchronize RX/TX to external input signal.\n"); } static hackrf_device* device = NULL; @@ -693,15 +692,12 @@ int main(int argc, char** argv) stats_t stats = {0, 0}; static int32_t preload_bytes = 0; - while ((opt = - getopt(argc, - argv, - "H:wr:t:f:i:o:m:a:p:s:Fn:b:l:g:x:c:d:C:RS:Bh?")) != EOF) { + while ((opt = getopt(argc, argv, "Hwr:t:f:i:o:m:a:p:s:Fn:b:l:g:x:c:d:C:RS:Bh?")) != + EOF) { result = HACKRF_SUCCESS; switch (opt) { case 'H': hw_sync = true; - result = parse_u32(optarg, &hw_sync_enable); break; case 'w': receive_wav = true; @@ -1166,10 +1162,10 @@ int main(int argc, char** argv) } } - fprintf(stderr, "call hackrf_set_hw_sync_mode(%d)\n", hw_sync_enable); + fprintf(stderr, "call hackrf_set_hw_sync_mode(%d)\n", hw_sync ? 1 : 0); result = hackrf_set_hw_sync_mode( device, - hw_sync_enable ? HW_SYNC_MODE_ON : HW_SYNC_MODE_OFF); + hw_sync ? HW_SYNC_MODE_ON : HW_SYNC_MODE_OFF); if (result != HACKRF_SUCCESS) { fprintf(stderr, "hackrf_set_hw_sync_mode() failed: %s (%d)\n", @@ -1356,8 +1352,7 @@ int main(int argc, char** argv) time_difference = TimevalDiff(&time_now, &time_start); rate = (float) byte_count_now / time_difference; - if (byte_count_now == 0 && hw_sync == true && - hw_sync_enable != 0) { + if ((byte_count_now == 0) && (hw_sync)) { fprintf(stderr, "Waiting for sync...\n"); } else { double full_scale_ratio = (double) stream_power_now / @@ -1395,8 +1390,7 @@ int main(int argc, char** argv) time_start = time_now; - if (byte_count_now == 0 && - (hw_sync == false || hw_sync_enable == 0)) { + if ((byte_count_now == 0) && (!hw_sync)) { exit_code = EXIT_FAILURE; fprintf(stderr, "\nCouldn't transfer any bytes for one second.\n"); From f4202d1163aacd88c26714ea578f42da94fb91ee Mon Sep 17 00:00:00 2001 From: Michael Ossmann Date: Sun, 18 Sep 2022 13:27:51 -0400 Subject: [PATCH 4/4] hackrf_transfer: Don't skip preload stats in RX Fixes a bug introduced in 8a9af7a: Statistics skipped for initial buffers not only when preloaded in TX mode but also in RX mode with no preloading. The total bytes transferred may appear inflated in RX mode when receiving a small number of samples with -n, but the stats represent data transferred over USB, some of which may have been discarded by the host. --- host/hackrf-tools/src/hackrf_transfer.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/host/hackrf-tools/src/hackrf_transfer.c b/host/hackrf-tools/src/hackrf_transfer.c index d1e723bb..bc52776a 100644 --- a/host/hackrf-tools/src/hackrf_transfer.c +++ b/host/hackrf-tools/src/hackrf_transfer.c @@ -1249,6 +1249,8 @@ int main(int argc, char** argv) result |= hackrf_set_lna_gain(device, lna_gain); result |= hackrf_start_rx(device, rx_callback, NULL); } else { + preload_bytes = hackrf_get_transfer_queue_depth(device) * + hackrf_get_transfer_buffer_size(device); result = hackrf_set_txvga_gain(device, txvga_gain); result |= hackrf_enable_tx_flush(device, 1); result |= hackrf_start_tx(device, tx_callback, NULL); @@ -1279,9 +1281,6 @@ int main(int argc, char** argv) .it_value = {.tv_sec = 1, .tv_usec = 0}}; setitimer(ITIMER_REAL, &interval_timer, NULL); #endif - preload_bytes = hackrf_get_transfer_queue_depth(device) * - hackrf_get_transfer_buffer_size(device); - while ((hackrf_is_streaming(device) == HACKRF_TRUE) && (do_exit == false)) { uint64_t byte_count_now; struct timeval time_now;