From c1d1a483aa541de8c31ed766a8b24b48af3aeb9f Mon Sep 17 00:00:00 2001 From: Mike Davis Date: Tue, 21 Jun 2016 14:15:16 +0200 Subject: [PATCH] Added flags + code to open device by index or serial number --- host/hackrf-tools/src/hackrf_si5351c.c | 77 ++++++++++++++++++++------ 1 file changed, 60 insertions(+), 17 deletions(-) diff --git a/host/hackrf-tools/src/hackrf_si5351c.c b/host/hackrf-tools/src/hackrf_si5351c.c index 41146f2c..fae59409 100644 --- a/host/hackrf-tools/src/hackrf_si5351c.c +++ b/host/hackrf-tools/src/hackrf_si5351c.c @@ -25,12 +25,20 @@ #include #include +#ifndef bool +typedef int bool; +#define true 1 +#define false 0 +#endif + static void usage() { printf("\nUsage:\n"); printf("\t-c, --config: print textual configuration information\n"); printf("\t-n, --register : set register number for subsequent read/write operations\n"); printf("\t-r, --read: read register specified by last -n argument, or all registers\n"); printf("\t-w, --write : write register specified by last -n argument with value \n"); + printf("\t-s, --device : specify a particular device by serial number\n"); + printf("\t-d, --device : specify a particular device by number\n"); printf("\nExamples:\n"); printf("\t -n 12 -r # reads from register 12\n"); printf("\t -r # reads all registers\n"); @@ -42,6 +50,8 @@ static struct option long_options[] = { { "register", required_argument, 0, 'n' }, { "write", required_argument, 0, 'w' }, { "read", no_argument, 0, 'r' }, + { "device", no_argument, 0, 'd' }, + { "serial", no_argument, 0, 's' }, { 0, 0, 0, 0 }, }; @@ -184,8 +194,15 @@ int dump_configuration(hackrf_device* device) { int main(int argc, char** argv) { int opt; + uint16_t register_number = REGISTER_INVALID; uint16_t register_value; + bool read = false; + bool write = false; + bool dump_config = false; + const char* serial_number = NULL; + int device_index = 0; + hackrf_device* device = NULL; int option_index = 0; @@ -194,36 +211,31 @@ int main(int argc, char** argv) { printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result); return -1; } - - result = hackrf_open(&device); - if( result ) { - printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); - return -1; - } - while( (opt = getopt_long(argc, argv, "cn:rw:", long_options, &option_index)) != EOF ) { + while( (opt = getopt_long(argc, argv, "d:s:cn:rw:", long_options, &option_index)) != EOF ) { switch( opt ) { case 'n': result = parse_int(optarg, ®ister_number); break; case 'w': - result = parse_int(optarg, ®ister_value); - if( result == HACKRF_SUCCESS ) { - result = write_register(device, register_number, register_value); - } + write = true; break; case 'r': - if( register_number == REGISTER_INVALID ) { - result = dump_registers(device); - } else { - result = dump_register(device, register_number); - } + read = true; break; case 'c': - dump_configuration(device); + dump_config = true; + break; + + case 'd': + device_index = atoi(optarg); + break; + + case 's': + serial_number = optarg; break; default: @@ -235,6 +247,37 @@ int main(int argc, char** argv) { break; } } + + if(serial_number != NULL) { + result = hackrf_open_by_serial(serial_number, &device); + } else { + hackrf_device_list_t* device_list = hackrf_device_list(); + if(device_list->devicecount <= 0) { + result = HACKRF_ERROR_NOT_FOUND; + } else { + result = hackrf_device_list_open(device_list, device_index, &device); + } + } + + if( result ) { + printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); + return -1; + } + + if(write) { + result = parse_int(optarg, ®ister_value); + if( result == HACKRF_SUCCESS ) { + result = write_register(device, register_number, register_value); + } + } else if(read) { + if( register_number == REGISTER_INVALID ) { + result = dump_registers(device); + } else { + result = dump_register(device, register_number); + } + } else if(dump_config) { + dump_configuration(device); + } result = hackrf_close(device); if( result ) {