diff --git a/host/libhackrf/examples/hackrf_transfer.c b/host/libhackrf/examples/hackrf_transfer.c index 8f6b8595..f80cd62c 100644 --- a/host/libhackrf/examples/hackrf_transfer.c +++ b/host/libhackrf/examples/hackrf_transfer.c @@ -86,6 +86,30 @@ int parse_u64(char* s, uint64_t* const value) { } } +int parse_int(char* s, uint32_t* const value) { + uint_fast8_t base = 10; + if( strlen(s) > 2 ) { + if( s[0] == '0' ) { + if( (s[1] == 'x') || (s[1] == 'X') ) { + base = 16; + s += 2; + } else if( (s[1] == 'b') || (s[1] == 'B') ) { + base = 2; + s += 2; + } + } + } + + char* s_end = s; + const unsigned long ulong_value = strtoul(s, &s_end, base); + if( (s != s_end) && (*s_end == 0) ) { + *value = ulong_value; + return HACKRF_SUCCESS; + } else { + return HACKRF_ERROR_INVALID_PARAM; + } +} + FILE* fd = NULL; volatile uint32_t byte_count = 0; @@ -96,7 +120,10 @@ struct timeval t_start; bool freq = false; uint64_t freq_hz; - + +bool amp = false; +uint32_t amp_enable; + int rx_callback(hackrf_transfer* transfer) { if( fd != NULL ) { @@ -135,7 +162,8 @@ static void usage() { printf("Usage:\n"); printf("\t-r # Receive data into file.\n"); printf("\t-t # Transmit data from file.\n"); - printf("\t-f # Set Frequency in MHz (between [%lld, %lld[).\n", FREQ_MIN_HZ, FREQ_MAX_HZ); + printf("\t[-f set_freq_hz] # Set Freq in Hz (between [%lld, %lld[).\n", FREQ_MIN_HZ, FREQ_MAX_HZ); + printf("\t[-a set_amp] # Set Amp 1=Enable, 0=Disable.\n"); } static hackrf_device* device = NULL; @@ -201,7 +229,7 @@ int main(int argc, char** argv) { const char* path = NULL; int result; - while( (opt = getopt(argc, argv, "r:t:f:")) != EOF ) { + while( (opt = getopt(argc, argv, "r:t:f:a:")) != EOF ) { result = HACKRF_SUCCESS; switch( opt ) { case 'r': @@ -218,7 +246,12 @@ int main(int argc, char** argv) { freq = true; result = parse_u64(optarg, &freq_hz); break; - + + case 'a': + amp = true; + result = parse_int(optarg, &_enable); + break; + default: usage(); return EXIT_FAILURE; @@ -233,9 +266,22 @@ int main(int argc, char** argv) { if( freq ) { if( (freq_hz >= FREQ_MAX_HZ) || (freq_hz < FREQ_MIN_HZ) ) - printf("argument error: frequency shall be between [%lld, %lld[.\n", FREQ_MIN_HZ, FREQ_MAX_HZ); + { + printf("argument error: set_freq_hz shall be between [%lld, %lld[.\n", FREQ_MIN_HZ, FREQ_MAX_HZ); + usage(); + return EXIT_FAILURE; + } } + if( amp ) { + if( amp_enable > 1 ) + { + printf("argument error: set_amp shall be 0 or 1.\n"); + usage(); + return EXIT_FAILURE; + } + } + if( transmit == receive ) { if( transmit == true ) @@ -319,6 +365,15 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } } + + if( amp ) { + printf("call hackrf_set_amp_enable(%ld)\n", amp_enable); + result = hackrf_set_amp_enable(device, (uint8_t)amp_enable); + if( result != HACKRF_SUCCESS ) { + printf("hackrf_set_amp_enable() failed: %s (%d)\n", hackrf_error_name(result), result); + return EXIT_FAILURE; + } + } gettimeofday(&t_start, NULL); gettimeofday(&time_start, NULL); diff --git a/host/libhackrf/src/hackrf.c b/host/libhackrf/src/hackrf.c index 4964dd3d..5bb77745 100644 --- a/host/libhackrf/src/hackrf.c +++ b/host/libhackrf/src/hackrf.c @@ -571,26 +571,6 @@ int hackrf_version_string_read(hackrf_device* device, char* version, } } -static int hackrf_set_amp_enable(hackrf_device* device, uint8_t value) -{ - int result = libusb_control_transfer( - device->usb_device, - LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, - HACKRF_VENDOR_REQUEST_AMP_ENABLE, - value, - 0, - NULL, - 0, - 0 - ); - - if (result != 0) { - return HACKRF_ERROR_LIBUSB; - } else { - return HACKRF_SUCCESS; - } -} - typedef struct { uint32_t freq_mhz; /* From 30 to 6000MHz */ uint32_t freq_hz; /* From 0 to 999999Hz */ @@ -630,6 +610,26 @@ int hackrf_set_freq(hackrf_device* device, const uint64_t freq_hz) } } +int hackrf_set_amp_enable(hackrf_device* device, const uint8_t value) +{ + int result = libusb_control_transfer( + device->usb_device, + LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, + HACKRF_VENDOR_REQUEST_AMP_ENABLE, + value, + 0, + NULL, + 0, + 0 + ); + + if (result != 0) { + return HACKRF_ERROR_LIBUSB; + } else { + return HACKRF_SUCCESS; + } +} + static void* transfer_threadproc(void* arg) { hackrf_device* device = (hackrf_device*)arg; diff --git a/host/libhackrf/src/hackrf.h b/host/libhackrf/src/hackrf.h index 31775142..c8dea87d 100644 --- a/host/libhackrf/src/hackrf.h +++ b/host/libhackrf/src/hackrf.h @@ -95,6 +95,8 @@ int hackrf_version_string_read(hackrf_device* device, char* version, int hackrf_set_freq(hackrf_device* device, const uint64_t freq_hz); +int hackrf_set_amp_enable(hackrf_device* device, const uint8_t value); + const char* hackrf_error_name(enum hackrf_error errcode); const char* hackrf_board_id_name(enum hackrf_board_id board_id);