From 55d7e7f2d27367670a7ef254434b42f0ab592b5a Mon Sep 17 00:00:00 2001 From: Dominic Spill Date: Fri, 3 Feb 2017 10:08:41 -0700 Subject: [PATCH 1/3] Standardize device selection options in hackrf-tools Fixes issue #318 --- host/hackrf-tools/src/hackrf_operacake.c | 23 ++++++----------------- host/hackrf-tools/src/hackrf_si5351c.c | 12 ++++++------ 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/host/hackrf-tools/src/hackrf_operacake.c b/host/hackrf-tools/src/hackrf_operacake.c index 91e1e0d2..5162d060 100644 --- a/host/hackrf-tools/src/hackrf_operacake.c +++ b/host/hackrf-tools/src/hackrf_operacake.c @@ -34,7 +34,6 @@ typedef int bool; static void usage() { printf("\nUsage:\n"); printf("\t-h, --help: this help\n"); - printf("\t-s, --serial : specify a particular device by serial number\n"); printf("\t-d, --device : specify a particular device by number\n"); printf("\t-o, --address : specify a particular operacake by address [default: 0x00]\n"); printf("\t-a : set port A connection\n"); @@ -44,7 +43,6 @@ static void usage() { static struct option long_options[] = { { "device", no_argument, 0, 'd' }, - { "serial", no_argument, 0, 's' }, { "address", no_argument, 0, 'o' }, { "list", no_argument, 0, 'v' }, { "help", no_argument, 0, 'h' }, @@ -65,7 +63,6 @@ int parse_int(char* const s, uint16_t* const value) { int main(int argc, char** argv) { int opt; const char* serial_number = NULL; - int device_index = 0; int operacake_address = 0; int port_a = 0; int port_b = 0; @@ -83,13 +80,9 @@ int main(int argc, char** argv) { return -1; } - while( (opt = getopt_long(argc, argv, "d:s:o:a:b:lh?", long_options, &option_index)) != EOF ) { + while( (opt = getopt_long(argc, argv, "d:o:a:b:lh?", long_options, &option_index)) != EOF ) { switch( opt ) { case 'd': - device_index = atoi(optarg); - break; - - case 's': serial_number = optarg; break; @@ -132,15 +125,11 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } - 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); - } + result = hackrf_open_by_serial(serial_number, &device); + if (result != HACKRF_SUCCESS) { + fprintf(stderr, "hackrf_open() failed: %s (%d)\n", + hackrf_error_name(result), result); + return EXIT_FAILURE; } if( result ) { diff --git a/host/hackrf-tools/src/hackrf_si5351c.c b/host/hackrf-tools/src/hackrf_si5351c.c index c4fc0cdb..7c8712c4 100644 --- a/host/hackrf-tools/src/hackrf_si5351c.c +++ b/host/hackrf-tools/src/hackrf_si5351c.c @@ -38,8 +38,8 @@ static void usage() { printf("\t-n, --register : set register number for 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, --serial : specify a particular device by serial number\n"); - printf("\t-d, --device : specify a particular device by number\n"); + printf("\t-d, --device : specify a particular device by serial number\n"); + printf("\t-D, --device-idx : 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"); @@ -52,7 +52,7 @@ static struct option long_options[] = { { "write", required_argument, 0, 'w' }, { "read", no_argument, 0, 'r' }, { "device", no_argument, 0, 'd' }, - { "serial", no_argument, 0, 's' }, + { "device-idx", no_argument, 0, 'D' }, { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 }, }; @@ -214,7 +214,7 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } - while( (opt = getopt_long(argc, argv, "d:s:cn:rw:h?", long_options, &option_index)) != EOF ) { + while( (opt = getopt_long(argc, argv, "d:D:cn:rw:h?", long_options, &option_index)) != EOF ) { switch( opt ) { case 'n': result = parse_int(optarg, ®ister_number); @@ -233,11 +233,11 @@ int main(int argc, char** argv) { dump_config = true; break; - case 'd': + case 'D': device_index = atoi(optarg); break; - case 's': + case 'd': serial_number = optarg; break; case 'h': From feeb7a9c95e3bdfb416305b1c70e232fdf5b2fc4 Mon Sep 17 00:00:00 2001 From: Dominic Spill Date: Fri, 3 Feb 2017 10:20:23 -0700 Subject: [PATCH 2/3] Add selection by device serial number to rffc5071 and max2837 tools --- host/hackrf-tools/src/hackrf_max2837.c | 11 +++++++++-- host/hackrf-tools/src/hackrf_operacake.c | 5 ----- host/hackrf-tools/src/hackrf_rffc5071.c | 10 ++++++++-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/host/hackrf-tools/src/hackrf_max2837.c b/host/hackrf-tools/src/hackrf_max2837.c index d09a7557..f52ca9e1 100644 --- a/host/hackrf-tools/src/hackrf_max2837.c +++ b/host/hackrf-tools/src/hackrf_max2837.c @@ -38,6 +38,7 @@ static void usage() { printf("\t-n, --register : set register number for 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-d, --device : specify a particular device by serial number\n"); printf("\nExamples:\n"); printf("\t -n 12 -r # reads from register 12\n"); printf("\t -r # reads all registers\n"); @@ -48,6 +49,7 @@ 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' }, { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 }, }; @@ -133,6 +135,7 @@ int main(int argc, char** argv) { int option_index = 0; bool read = false; bool write = false; + const char* serial_number = NULL; int result = hackrf_init(); if( result ) { @@ -140,7 +143,7 @@ int main(int argc, char** argv) { 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:d:h?", long_options, &option_index)) != EOF ) { switch( opt ) { case 'n': result = parse_int(optarg, ®ister_number); @@ -155,6 +158,10 @@ int main(int argc, char** argv) { read = true; break; + case 'd': + serial_number = optarg; + break; + case 'h': case '?': usage(); @@ -184,7 +191,7 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } - result = hackrf_open(&device); + result = hackrf_open_by_serial(serial_number, &device); if(result) { printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); return EXIT_FAILURE; diff --git a/host/hackrf-tools/src/hackrf_operacake.c b/host/hackrf-tools/src/hackrf_operacake.c index 5162d060..649f9902 100644 --- a/host/hackrf-tools/src/hackrf_operacake.c +++ b/host/hackrf-tools/src/hackrf_operacake.c @@ -132,11 +132,6 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } - if( result ) { - printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); - return -1; - } - if(list) { result = hackrf_get_operacake_boards(device, operacakes); if (result != HACKRF_SUCCESS) { diff --git a/host/hackrf-tools/src/hackrf_rffc5071.c b/host/hackrf-tools/src/hackrf_rffc5071.c index 11285d83..867bc379 100644 --- a/host/hackrf-tools/src/hackrf_rffc5071.c +++ b/host/hackrf-tools/src/hackrf_rffc5071.c @@ -39,6 +39,7 @@ static void usage() { printf("\t-n, --register : set register number for 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-d, --device : specify a particular device by serial number\n"); printf("\nExamples:\n"); printf("\t -n 12 -r # reads from register 12\n"); printf("\t -r # reads all registers\n"); @@ -49,6 +50,7 @@ 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' }, { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 }, }; @@ -134,6 +136,7 @@ int main(int argc, char** argv) { int option_index = 0; bool read = false; bool write = false; + const char* serial_number = NULL; int result = hackrf_init(); if( result ) { @@ -141,7 +144,7 @@ int main(int argc, char** argv) { 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:d:h?", long_options, &option_index)) != EOF ) { switch( opt ) { case 'n': result = parse_int(optarg, ®ister_number); @@ -153,6 +156,9 @@ int main(int argc, char** argv) { case 'r': read = true; break; + case 'd': + serial_number = optarg; + break; case 'h': case '?': usage(); @@ -182,7 +188,7 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } - result = hackrf_open(&device); + result = hackrf_open_by_serial(serial_number, &device); if(result) { printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); return EXIT_FAILURE; From b8aaee6e720c9267f8a9436f847f1df633b2e609 Mon Sep 17 00:00:00 2001 From: Dominic Spill Date: Fri, 3 Feb 2017 12:07:07 -0700 Subject: [PATCH 3/3] Correct usage for -d --- host/hackrf-tools/src/hackrf_operacake.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/host/hackrf-tools/src/hackrf_operacake.c b/host/hackrf-tools/src/hackrf_operacake.c index 649f9902..0b8405b6 100644 --- a/host/hackrf-tools/src/hackrf_operacake.c +++ b/host/hackrf-tools/src/hackrf_operacake.c @@ -34,7 +34,7 @@ typedef int bool; static void usage() { printf("\nUsage:\n"); printf("\t-h, --help: this help\n"); - printf("\t-d, --device : specify a particular device by number\n"); + printf("\t-d, --device : specify a particular device by serial number\n"); printf("\t-o, --address : specify a particular operacake by address [default: 0x00]\n"); printf("\t-a : set port A connection\n"); printf("\t-b : set port B connection\n");