Fixed bug that prevented hackrf_max2837 and hackrf_rffc5071 from displaying help when HackRF is not found.

This commit is contained in:
Michael Ossmann
2017-01-29 21:27:44 -07:00
parent b97bd4d192
commit 1eb34ac765
2 changed files with 94 additions and 44 deletions

View File

@ -26,6 +26,12 @@
#include <stdlib.h> #include <stdlib.h>
#include <getopt.h> #include <getopt.h>
#ifndef bool
typedef int bool;
#define true 1
#define false 0
#endif
static void usage() { static void usage() {
printf("\nUsage:\n"); printf("\nUsage:\n");
printf("\t-h, --help: this help\n"); printf("\t-h, --help: this help\n");
@ -96,7 +102,7 @@ int dump_registers(hackrf_device* device) {
break; break;
} }
} }
return result; return result;
} }
@ -125,18 +131,14 @@ int main(int argc, char** argv) {
uint16_t register_value; uint16_t register_value;
hackrf_device* device = NULL; hackrf_device* device = NULL;
int option_index = 0; int option_index = 0;
bool read = false;
bool write = false;
int result = hackrf_init(); int result = hackrf_init();
if( result ) { if( result ) {
printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result); printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result);
return EXIT_FAILURE; 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 ) { while( (opt = getopt_long(argc, argv, "n:rw:h?", long_options, &option_index)) != EOF ) {
switch( opt ) { switch( opt ) {
@ -145,20 +147,14 @@ int main(int argc, char** argv) {
break; break;
case 'w': case 'w':
write = true;
result = parse_int(optarg, &register_value); result = parse_int(optarg, &register_value);
if( result == HACKRF_SUCCESS ) {
result = write_register(device, register_number, register_value);
}
break; break;
case 'r': case 'r':
if( register_number == REGISTER_INVALID ) { read = true;
result = dump_registers(device);
} else {
result = dump_register(device, register_number);
}
break; break;
case 'h': case 'h':
case '?': case '?':
usage(); usage();
@ -168,19 +164,50 @@ int main(int argc, char** argv) {
usage(); usage();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if( result != HACKRF_SUCCESS ) { if( result != HACKRF_SUCCESS ) {
printf("argument error: %s (%d)\n", hackrf_error_name(result), result); 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); result = hackrf_close(device);
if( result ) { if( result ) {
printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result); printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
hackrf_exit(); hackrf_exit();
return EXIT_SUCCESS; return EXIT_SUCCESS;

View File

@ -27,6 +27,12 @@
#include <stdlib.h> #include <stdlib.h>
#include <getopt.h> #include <getopt.h>
#ifndef bool
typedef int bool;
#define true 1
#define false 0
#endif
static void usage() { static void usage() {
printf("\nUsage:\n"); printf("\nUsage:\n");
printf("\t-h, --help: this help\n"); printf("\t-h, --help: this help\n");
@ -126,64 +132,81 @@ int main(int argc, char** argv) {
uint16_t register_value; uint16_t register_value;
hackrf_device* device = NULL; hackrf_device* device = NULL;
int option_index = 0; int option_index = 0;
bool read = false;
bool write = false;
int result = hackrf_init(); int result = hackrf_init();
if( result ) { if( result ) {
printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result); printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result);
return EXIT_FAILURE; 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 ) { while( (opt = getopt_long(argc, argv, "n:rw:h?", long_options, &option_index)) != EOF ) {
switch( opt ) { switch( opt ) {
case 'n': case 'n':
result = parse_int(optarg, &register_number); result = parse_int(optarg, &register_number);
break; break;
case 'w': case 'w':
write = true;
result = parse_int(optarg, &register_value); result = parse_int(optarg, &register_value);
if( result == HACKRF_SUCCESS ) {
result = write_register(device, register_number, register_value);
}
break; break;
case 'r': case 'r':
if( register_number == REGISTER_INVALID ) { read = true;
result = dump_registers(device);
} else {
result = dump_register(device, register_number);
}
break; break;
case 'h': case 'h':
case '?': case '?':
usage(); usage();
return EXIT_SUCCESS; return EXIT_SUCCESS;
default: default:
fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg); fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg);
usage(); usage();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if( result != HACKRF_SUCCESS ) { if( result != HACKRF_SUCCESS ) {
printf("argument error: %s (%d)\n", hackrf_error_name(result), result); printf("argument error: %s (%d)\n", hackrf_error_name(result), result);
usage(); 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); result = hackrf_close(device);
if( result ) { if( result ) {
printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result); printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
hackrf_exit(); hackrf_exit();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }