From 1eb34ac765837b998779e5a5b61812cec7c4a9a8 Mon Sep 17 00:00:00 2001 From: Michael Ossmann Date: Sun, 29 Jan 2017 21:27:44 -0700 Subject: [PATCH] Fixed bug that prevented hackrf_max2837 and hackrf_rffc5071 from displaying help when HackRF is not found. --- host/hackrf-tools/src/hackrf_max2837.c | 69 +++++++++++++++++-------- host/hackrf-tools/src/hackrf_rffc5071.c | 69 ++++++++++++++++--------- 2 files changed, 94 insertions(+), 44 deletions(-) diff --git a/host/hackrf-tools/src/hackrf_max2837.c b/host/hackrf-tools/src/hackrf_max2837.c index 38ac01be..6b118e38 100644 --- a/host/hackrf-tools/src/hackrf_max2837.c +++ b/host/hackrf-tools/src/hackrf_max2837.c @@ -26,6 +26,12 @@ #include #include +#ifndef bool +typedef int bool; +#define true 1 +#define false 0 +#endif + static void usage() { printf("\nUsage:\n"); printf("\t-h, --help: this help\n"); @@ -96,7 +102,7 @@ int dump_registers(hackrf_device* device) { break; } } - + return result; } @@ -125,18 +131,14 @@ int main(int argc, char** argv) { uint16_t register_value; hackrf_device* device = NULL; int option_index = 0; + bool read = false; + bool write = false; int result = hackrf_init(); if( result ) { printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result); return EXIT_FAILURE; } - - result = hackrf_open(&device); - if( result ) { - printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); - return EXIT_FAILURE; - } while( (opt = getopt_long(argc, argv, "n:rw:h?", long_options, &option_index)) != EOF ) { switch( opt ) { @@ -145,20 +147,14 @@ int main(int argc, char** argv) { break; case 'w': + write = true; result = parse_int(optarg, ®ister_value); - if( result == HACKRF_SUCCESS ) { - result = write_register(device, register_number, register_value); - } break; - + case 'r': - if( register_number == REGISTER_INVALID ) { - result = dump_registers(device); - } else { - result = dump_register(device, register_number); - } + read = true; break; - + case 'h': case '?': usage(); @@ -168,19 +164,50 @@ int main(int argc, char** argv) { usage(); return EXIT_FAILURE; } - + if( result != HACKRF_SUCCESS ) { printf("argument error: %s (%d)\n", hackrf_error_name(result), result); - break; + usage(); + return EXIT_FAILURE; } } - + + if(write && read) { + fprintf(stderr, "Read and write options are mutually exclusive.\n"); + usage(); + return EXIT_FAILURE; + } + + if(!(write || read)) { + fprintf(stderr, "Specify either read or write option.\n"); + usage(); + return EXIT_FAILURE; + } + + result = hackrf_open(&device); + if(result) { + printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); + return EXIT_FAILURE; + } + + if(write) { + result = write_register(device, register_number, register_value); + } + + if(read) { + if(register_number == REGISTER_INVALID) { + result = dump_registers(device); + } else { + result = dump_register(device, register_number); + } + } + result = hackrf_close(device); if( result ) { printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result); return EXIT_FAILURE; } - + hackrf_exit(); return EXIT_SUCCESS; diff --git a/host/hackrf-tools/src/hackrf_rffc5071.c b/host/hackrf-tools/src/hackrf_rffc5071.c index 7282184a..82b3f552 100644 --- a/host/hackrf-tools/src/hackrf_rffc5071.c +++ b/host/hackrf-tools/src/hackrf_rffc5071.c @@ -27,6 +27,12 @@ #include #include +#ifndef bool +typedef int bool; +#define true 1 +#define false 0 +#endif + static void usage() { printf("\nUsage:\n"); printf("\t-h, --help: this help\n"); @@ -126,64 +132,81 @@ int main(int argc, char** argv) { uint16_t register_value; hackrf_device* device = NULL; int option_index = 0; + bool read = false; + bool write = false; int result = hackrf_init(); if( result ) { printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result); return EXIT_FAILURE; } - - result = hackrf_open(&device); - if( result ) { - printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); - return EXIT_FAILURE; - } while( (opt = getopt_long(argc, argv, "n:rw:h?", long_options, &option_index)) != EOF ) { switch( opt ) { case 'n': result = parse_int(optarg, ®ister_number); break; - case 'w': + write = true; result = parse_int(optarg, ®ister_value); - if( result == HACKRF_SUCCESS ) { - result = write_register(device, register_number, register_value); - } break; - case 'r': - if( register_number == REGISTER_INVALID ) { - result = dump_registers(device); - } else { - result = dump_register(device, register_number); - } + read = true; break; case 'h': case '?': usage(); return EXIT_SUCCESS; - default: fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg); usage(); return EXIT_FAILURE; } - + if( result != HACKRF_SUCCESS ) { printf("argument error: %s (%d)\n", hackrf_error_name(result), result); usage(); - break; + return EXIT_FAILURE; } } - + + if(write && read) { + fprintf(stderr, "Read and write options are mutually exclusive.\n"); + usage(); + return EXIT_FAILURE; + } + + if(!(write || read)) { + fprintf(stderr, "Specify either read or write option.\n"); + usage(); + return EXIT_FAILURE; + } + + result = hackrf_open(&device); + if(result) { + printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); + return EXIT_FAILURE; + } + + if(write) { + result = write_register(device, register_number, register_value); + } + + if(read) { + if(register_number == REGISTER_INVALID) { + result = dump_registers(device); + } else { + result = dump_register(device, register_number); + } + } + result = hackrf_close(device); if( result ) { printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result); return EXIT_FAILURE; } - + hackrf_exit(); - - return EXIT_SUCCESS; + + return EXIT_SUCCESS; }