diff --git a/host/libhackrf/examples/hackrf_transfer.c b/host/libhackrf/examples/hackrf_transfer.c index 6bdad747..2136eba1 100644 --- a/host/libhackrf/examples/hackrf_transfer.c +++ b/host/libhackrf/examples/hackrf_transfer.c @@ -572,8 +572,8 @@ int main(int argc, char** argv) { gettimeofday(&time_start, NULL); printf("Stop with Ctrl-C\n"); - while( (hackrf_is_streaming(device)) && - (do_exit == false) ) + while( (hackrf_is_streaming(device) == HACKRF_TRUE) && + (do_exit == false) ) { sleep(1); @@ -596,11 +596,14 @@ int main(int argc, char** argv) { break; } } - - if (do_exit) - printf("\nUser cancel, exiting...\n"); - else - printf("\nExiting...\n"); + + result = hackrf_is_streaming(device); + if (do_exit) + { + printf("\nUser cancel, exiting...\n"); + } else { + printf("\nExiting... hackrf_is_streaming() result: %s (%d)\n", hackrf_error_name(result), result); + } struct timeval t_end; gettimeofday(&t_end, NULL); diff --git a/host/libhackrf/src/hackrf.c b/host/libhackrf/src/hackrf.c index e2c8d152..8f340c1d 100644 --- a/host/libhackrf/src/hackrf.c +++ b/host/libhackrf/src/hackrf.c @@ -60,16 +60,15 @@ struct hackrf_device { libusb_device_handle* usb_device; struct libusb_transfer** transfers; hackrf_sample_block_cb_fn callback; - bool transfer_thread_started; + volatile bool transfer_thread_started; /* volatile shared between threads (read only) */ pthread_t transfer_thread; uint32_t transfer_count; uint32_t buffer_size; - bool streaming; + volatile bool streaming; /* volatile shared between threads (read only) */ void* rx_ctx; void* tx_ctx; }; - typedef struct { uint32_t bandwidth_hz; } max2837_ft_t; @@ -91,7 +90,7 @@ static const max2837_ft_t max2837_ft[] = { { 20000000 }, { 24000000 }, { 28000000 }, - { 0 }, + { 0 } }; volatile bool do_exit = false; @@ -101,11 +100,41 @@ static const uint16_t hackrf_usb_pid = 0x604b; static libusb_context* g_libusb_context = NULL; -static int free_transfers(hackrf_device* device) { - if( device->transfers != NULL ) { +static void request_exit(void) +{ + do_exit = true; +} + +static int cancel_transfers(hackrf_device* device) +{ + uint32_t transfer_index; + + if( device->transfers != NULL ) + { + for(transfer_index=0; transfer_indextransfer_count; transfer_index++) + { + if( device->transfers[transfer_index] != NULL ) + { + libusb_cancel_transfer(device->transfers[transfer_index]); + } + } + return HACKRF_SUCCESS; + } else { + return HACKRF_ERROR_OTHER; + } +} + +static int free_transfers(hackrf_device* device) +{ + uint32_t transfer_index; + + if( device->transfers != NULL ) + { // libusb_close() should free all transfers referenced from this array. - for(uint32_t transfer_index=0; transfer_indextransfer_count; transfer_index++) { - if( device->transfers[transfer_index] != NULL ) { + for(transfer_index=0; transfer_indextransfer_count; transfer_index++) + { + if( device->transfers[transfer_index] != NULL ) + { libusb_free_transfer(device->transfers[transfer_index]); device->transfers[transfer_index] = NULL; } @@ -116,19 +145,24 @@ static int free_transfers(hackrf_device* device) { return HACKRF_SUCCESS; } -static int allocate_transfers(hackrf_device* const device) { - if( device->transfers == NULL ) { - device->transfers = (libusb_transfer**) calloc(device->transfer_count, sizeof(struct libusb_transfer)); - if( device->transfers == NULL ) { +static int allocate_transfers(hackrf_device* const device) +{ + if( device->transfers == NULL ) + { + device->transfers = (libusb_transfer**) calloc(device->transfer_count, sizeof(struct libusb_transfer)); + if( device->transfers == NULL ) + { return HACKRF_ERROR_NO_MEM; } - - for(uint32_t transfer_index=0; transfer_indextransfer_count; transfer_index++) { - device->transfers[transfer_index] = libusb_alloc_transfer(0); - if( device->transfers[transfer_index] == NULL ) { - return HACKRF_ERROR_LIBUSB; - } - + + for(uint32_t transfer_index=0; transfer_indextransfer_count; transfer_index++) + { + device->transfers[transfer_index] = libusb_alloc_transfer(0); + if( device->transfers[transfer_index] == NULL ) + { + return HACKRF_ERROR_LIBUSB; + } + libusb_fill_bulk_transfer( device->transfers[transfer_index], device->usb_device, @@ -139,12 +173,12 @@ static int allocate_transfers(hackrf_device* const device) { device, 0 ); - - if( device->transfers[transfer_index]->buffer == NULL ) { - return HACKRF_ERROR_NO_MEM; - } - } - + + if( device->transfers[transfer_index]->buffer == NULL ) + { + return HACKRF_ERROR_NO_MEM; + } + } return HACKRF_SUCCESS; } else { return HACKRF_ERROR_BUSY; @@ -154,16 +188,20 @@ static int allocate_transfers(hackrf_device* const device) { static int prepare_transfers( hackrf_device* device, const uint_fast8_t endpoint_address, - libusb_transfer_cb_fn callback -) { - if( device->transfers != NULL ) { - for(uint32_t transfer_index=0; transfer_indextransfer_count; transfer_index++) { + libusb_transfer_cb_fn callback) +{ + int error; + if( device->transfers != NULL ) + { + for(uint32_t transfer_index=0; transfer_indextransfer_count; transfer_index++) + { device->transfers[transfer_index]->endpoint = endpoint_address; device->transfers[transfer_index]->callback = callback; - - int error = libusb_submit_transfer(device->transfers[transfer_index]); - if( error != 0 ) { - return HACKRF_ERROR_LIBUSB; + + error = libusb_submit_transfer(device->transfers[transfer_index]); + if( error != 0 ) + { + return HACKRF_ERROR_LIBUSB; } } return HACKRF_SUCCESS; @@ -172,81 +210,80 @@ static int prepare_transfers( return HACKRF_ERROR_OTHER; } } -/* -static int cancel_transfers(hackrf_device* device) { - if( device->transfers != NULL ) { - for(uint32_t transfer_index=0; transfer_indextransfer_count; transfer_index++) { - libusb_cancel_transfer(device->transfers[transfer_index]); - } - return HACKRF_SUCCESS; - } else { - // This shouldn't happen. - return HACKRF_ERROR_OTHER; - } -} -*/ + #ifdef __cplusplus extern "C" { #endif -int ADDCALL hackrf_init() { +int ADDCALL hackrf_init(void) +{ const int libusb_error = libusb_init(&g_libusb_context); - if( libusb_error != 0 ) { + if( libusb_error != 0 ) + { return HACKRF_ERROR_LIBUSB; } else { return HACKRF_SUCCESS; } } -int ADDCALL hackrf_exit() { - if( g_libusb_context != NULL ) { +int ADDCALL hackrf_exit(void) +{ + if( g_libusb_context != NULL ) + { libusb_exit(g_libusb_context); g_libusb_context = NULL; } - + return HACKRF_SUCCESS; } -int ADDCALL hackrf_open(hackrf_device** device) { - if( device == NULL ) { +int ADDCALL hackrf_open(hackrf_device** device) +{ + int result; + + if( device == NULL ) + { return HACKRF_ERROR_INVALID_PARAM; } - + // TODO: Do proper scanning of available devices, searching for // unit serial number (if specified?). - libusb_device_handle* usb_device = libusb_open_device_with_vid_pid(g_libusb_context, hackrf_usb_vid, hackrf_usb_pid); - if( usb_device == NULL ) { - return HACKRF_ERROR_NOT_FOUND; - } - - //int speed = libusb_get_device_speed(usb_device); + libusb_device_handle* usb_device = libusb_open_device_with_vid_pid(g_libusb_context, hackrf_usb_vid, hackrf_usb_pid); + if( usb_device == NULL ) + { + return HACKRF_ERROR_NOT_FOUND; + } + + //int speed = libusb_get_device_speed(usb_device); // TODO: Error or warning if not high speed USB? - int result = libusb_set_configuration(usb_device, 1); - if( result != 0 ) { - libusb_close(usb_device); - return HACKRF_ERROR_LIBUSB; - } + result = libusb_set_configuration(usb_device, 1); + if( result != 0 ) + { + libusb_close(usb_device); + return HACKRF_ERROR_LIBUSB; + } - result = libusb_claim_interface(usb_device, 0); - if( result != 0 ) { - libusb_close(usb_device); - return HACKRF_ERROR_LIBUSB; - } + result = libusb_claim_interface(usb_device, 0); + if( result != 0 ) + { + libusb_close(usb_device); + return HACKRF_ERROR_LIBUSB; + } hackrf_device* lib_device = NULL; lib_device = (hackrf_device*)malloc(sizeof(*lib_device)); - if( lib_device == NULL ) { + if( lib_device == NULL ) + { libusb_release_interface(usb_device, 0); - libusb_close(usb_device); + libusb_close(usb_device); return HACKRF_ERROR_NO_MEM; } - + lib_device->usb_device = usb_device; lib_device->transfers = NULL; lib_device->callback = NULL; - //lib_device->transfer_thread = (pthread_t)NULL; lib_device->transfer_thread_started = false; /* lib_device->transfer_count = 1024; @@ -256,22 +293,25 @@ int ADDCALL hackrf_open(hackrf_device** device) { lib_device->buffer_size = 262144; /* 1048576; */ lib_device->streaming = false; do_exit = false; - + result = allocate_transfers(lib_device); - if( result != 0 ) { + if( result != 0 ) + { free(lib_device); libusb_release_interface(usb_device, 0); - libusb_close(usb_device); + libusb_close(usb_device); return HACKRF_ERROR_NO_MEM; } - + *device = lib_device; - + return HACKRF_SUCCESS; } -int ADDCALL hackrf_set_transceiver_mode(hackrf_device* device, hackrf_transceiver_mode value) { - int result = libusb_control_transfer( +int ADDCALL hackrf_set_transceiver_mode(hackrf_device* device, hackrf_transceiver_mode value) +{ + int result; + result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, HACKRF_VENDOR_REQUEST_SET_TRANSCEIVER_MODE, @@ -281,20 +321,25 @@ int ADDCALL hackrf_set_transceiver_mode(hackrf_device* device, hackrf_transceive 0, 0 ); - - if( result != 0 ) { + + if( result != 0 ) + { return HACKRF_ERROR_LIBUSB; } else { return HACKRF_SUCCESS; } } -int ADDCALL hackrf_max2837_read(hackrf_device* device, uint8_t register_number, uint16_t* value) { - if( register_number >= 32 ) { +int ADDCALL hackrf_max2837_read(hackrf_device* device, uint8_t register_number, uint16_t* value) +{ + int result; + + if( register_number >= 32 ) + { return HACKRF_ERROR_INVALID_PARAM; } - - int result = libusb_control_transfer( + + result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, HACKRF_VENDOR_REQUEST_MAX2837_READ, @@ -304,23 +349,29 @@ int ADDCALL hackrf_max2837_read(hackrf_device* device, uint8_t register_number, 2, 0 ); - - if( result < 2 ) { + + if( result < 2 ) + { return HACKRF_ERROR_LIBUSB; } else { return HACKRF_SUCCESS; } } -int ADDCALL hackrf_max2837_write(hackrf_device* device, uint8_t register_number, uint16_t value) { - if( register_number >= 32 ) { - return HACKRF_ERROR_INVALID_PARAM; - } - if( value >= 0x400 ) { - return HACKRF_ERROR_INVALID_PARAM; - } +int ADDCALL hackrf_max2837_write(hackrf_device* device, uint8_t register_number, uint16_t value) +{ + int result; - int result = libusb_control_transfer( + if( register_number >= 32 ) + { + return HACKRF_ERROR_INVALID_PARAM; + } + if( value >= 0x400 ) + { + return HACKRF_ERROR_INVALID_PARAM; + } + + result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, HACKRF_VENDOR_REQUEST_MAX2837_WRITE, @@ -330,21 +381,27 @@ int ADDCALL hackrf_max2837_write(hackrf_device* device, uint8_t register_number, 0, 0 ); - - if( result != 0 ) { + + if( result != 0 ) + { return HACKRF_ERROR_LIBUSB; } else { return HACKRF_SUCCESS; } } -int ADDCALL hackrf_si5351c_read(hackrf_device* device, uint16_t register_number, uint16_t* value) { - if( register_number >= 256 ) { +int ADDCALL hackrf_si5351c_read(hackrf_device* device, uint16_t register_number, uint16_t* value) +{ + uint8_t temp_value; + int result; + + if( register_number >= 256 ) + { return HACKRF_ERROR_INVALID_PARAM; } - uint8_t temp_value = 0; - int result = libusb_control_transfer( + temp_value = 0; + result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, HACKRF_VENDOR_REQUEST_SI5351C_READ, @@ -354,8 +411,9 @@ int ADDCALL hackrf_si5351c_read(hackrf_device* device, uint16_t register_number, 1, 0 ); - - if( result < 1 ) { + + if( result < 1 ) + { return HACKRF_ERROR_LIBUSB; } else { *value = temp_value; @@ -363,15 +421,19 @@ int ADDCALL hackrf_si5351c_read(hackrf_device* device, uint16_t register_number, } } -int ADDCALL hackrf_si5351c_write(hackrf_device* device, uint16_t register_number, uint16_t value) { - if( register_number >= 256 ) { +int ADDCALL hackrf_si5351c_write(hackrf_device* device, uint16_t register_number, uint16_t value) +{ + int result; + + if( register_number >= 256 ) + { return HACKRF_ERROR_INVALID_PARAM; } if( value >= 256 ) { return HACKRF_ERROR_INVALID_PARAM; } - - int result = libusb_control_transfer( + + result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, HACKRF_VENDOR_REQUEST_SI5351C_WRITE, @@ -381,16 +443,19 @@ int ADDCALL hackrf_si5351c_write(hackrf_device* device, uint16_t register_number 0, 0 ); - - if( result != 0 ) { + + if( result != 0 ) + { return HACKRF_ERROR_LIBUSB; } else { return HACKRF_SUCCESS; } } -int ADDCALL hackrf_sample_rate_set(hackrf_device* device, const uint32_t sampling_rate_hz) { - int result = libusb_control_transfer( +int ADDCALL hackrf_sample_rate_set(hackrf_device* device, const uint32_t sampling_rate_hz) +{ + int result; + result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, HACKRF_VENDOR_REQUEST_SAMPLE_RATE_SET, @@ -400,16 +465,19 @@ int ADDCALL hackrf_sample_rate_set(hackrf_device* device, const uint32_t samplin 0, 0 ); - - if( result != 0 ) { + + if( result != 0 ) + { return HACKRF_ERROR_LIBUSB; } else { return HACKRF_SUCCESS; } } -int ADDCALL hackrf_baseband_filter_bandwidth_set(hackrf_device* device, const uint32_t bandwidth_hz) { - int result = libusb_control_transfer( +int ADDCALL hackrf_baseband_filter_bandwidth_set(hackrf_device* device, const uint32_t bandwidth_hz) +{ + int result; + result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, HACKRF_VENDOR_REQUEST_BASEBAND_FILTER_BANDWIDTH_SET, @@ -419,8 +487,9 @@ int ADDCALL hackrf_baseband_filter_bandwidth_set(hackrf_device* device, const ui 0, 0 ); - - if( result != 0 ) { + + if( result != 0 ) + { return HACKRF_ERROR_LIBUSB; } else { return HACKRF_SUCCESS; @@ -430,11 +499,14 @@ int ADDCALL hackrf_baseband_filter_bandwidth_set(hackrf_device* device, const ui int ADDCALL hackrf_rffc5071_read(hackrf_device* device, uint8_t register_number, uint16_t* value) { - if( register_number >= 31 ) { + int result; + + if( register_number >= 31 ) + { return HACKRF_ERROR_INVALID_PARAM; } - - int result = libusb_control_transfer( + + result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, HACKRF_VENDOR_REQUEST_RFFC5071_READ, @@ -444,8 +516,9 @@ int ADDCALL hackrf_rffc5071_read(hackrf_device* device, uint8_t register_number, 2, 0 ); - - if( result < 2 ) { + + if( result < 2 ) + { return HACKRF_ERROR_LIBUSB; } else { return HACKRF_SUCCESS; @@ -454,11 +527,14 @@ int ADDCALL hackrf_rffc5071_read(hackrf_device* device, uint8_t register_number, int ADDCALL hackrf_rffc5071_write(hackrf_device* device, uint8_t register_number, uint16_t value) { - if( register_number >= 31 ) { + int result; + + if( register_number >= 31 ) + { return HACKRF_ERROR_INVALID_PARAM; } - - int result = libusb_control_transfer( + + result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, HACKRF_VENDOR_REQUEST_RFFC5071_WRITE, @@ -468,16 +544,19 @@ int ADDCALL hackrf_rffc5071_write(hackrf_device* device, uint8_t register_number 0, 0 ); - - if( result != 0 ) { + + if( result != 0 ) + { return HACKRF_ERROR_LIBUSB; } else { return HACKRF_SUCCESS; } } -int ADDCALL hackrf_spiflash_erase(hackrf_device* device) { - int result = libusb_control_transfer( +int ADDCALL hackrf_spiflash_erase(hackrf_device* device) +{ + int result; + result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, HACKRF_VENDOR_REQUEST_SPIFLASH_ERASE, @@ -488,7 +567,8 @@ int ADDCALL hackrf_spiflash_erase(hackrf_device* device) { 0 ); - if (result != 0) { + if (result != 0) + { return HACKRF_ERROR_LIBUSB; } else { return HACKRF_SUCCESS; @@ -498,11 +578,14 @@ int ADDCALL hackrf_spiflash_erase(hackrf_device* device) { int ADDCALL hackrf_spiflash_write(hackrf_device* device, const uint32_t address, const uint16_t length, unsigned char* const data) { - if (address > 0x0FFFFF) { + int result; + + if (address > 0x0FFFFF) + { return HACKRF_ERROR_INVALID_PARAM; } - int result = libusb_control_transfer( + result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, HACKRF_VENDOR_REQUEST_SPIFLASH_WRITE, @@ -513,7 +596,8 @@ int ADDCALL hackrf_spiflash_write(hackrf_device* device, const uint32_t address, 0 ); - if (result < length) { + if (result < length) + { return HACKRF_ERROR_LIBUSB; } else { return HACKRF_SUCCESS; @@ -523,11 +607,14 @@ int ADDCALL hackrf_spiflash_write(hackrf_device* device, const uint32_t address, int ADDCALL hackrf_spiflash_read(hackrf_device* device, const uint32_t address, const uint16_t length, unsigned char* data) { - if (address > 0x0FFFFF) { + int result; + + if (address > 0x0FFFFF) + { return HACKRF_ERROR_INVALID_PARAM; } - int result = libusb_control_transfer( + result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, HACKRF_VENDOR_REQUEST_SPIFLASH_READ, @@ -538,7 +625,8 @@ int ADDCALL hackrf_spiflash_read(hackrf_device* device, const uint32_t address, 0 ); - if (result < length) { + if (result < length) + { return HACKRF_ERROR_LIBUSB; } else { return HACKRF_SUCCESS; @@ -548,7 +636,8 @@ int ADDCALL hackrf_spiflash_read(hackrf_device* device, const uint32_t address, int ADDCALL hackrf_cpld_write(hackrf_device* device, const uint16_t length, unsigned char* const data) { - int result = libusb_control_transfer( + int result; + result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, HACKRF_VENDOR_REQUEST_CPLD_WRITE, @@ -559,15 +648,18 @@ int ADDCALL hackrf_cpld_write(hackrf_device* device, const uint16_t length, 0 ); - if (result < length) { + if (result < length) + { return HACKRF_ERROR_LIBUSB; } else { return HACKRF_SUCCESS; } } -int ADDCALL hackrf_board_id_read(hackrf_device* device, uint8_t* value) { - int result = libusb_control_transfer( +int ADDCALL hackrf_board_id_read(hackrf_device* device, uint8_t* value) +{ + int result; + result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, HACKRF_VENDOR_REQUEST_BOARD_ID_READ, @@ -578,7 +670,8 @@ int ADDCALL hackrf_board_id_read(hackrf_device* device, uint8_t* value) { 0 ); - if (result < 1) { + if (result < 1) + { return HACKRF_ERROR_LIBUSB; } else { return HACKRF_SUCCESS; @@ -588,7 +681,8 @@ int ADDCALL hackrf_board_id_read(hackrf_device* device, uint8_t* value) { int ADDCALL hackrf_version_string_read(hackrf_device* device, char* version, uint8_t length) { - int result = libusb_control_transfer( + int result; + result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, HACKRF_VENDOR_REQUEST_VERSION_STRING_READ, @@ -599,7 +693,8 @@ int ADDCALL hackrf_version_string_read(hackrf_device* device, char* version, 0 ); - if (result < 0) { + if (result < 0) + { return HACKRF_ERROR_LIBUSB; } else { version[result] = '\0'; @@ -609,7 +704,7 @@ int ADDCALL hackrf_version_string_read(hackrf_device* device, char* version, typedef struct { uint32_t freq_mhz; /* From 30 to 6000MHz */ - uint32_t freq_hz; /* From 0 to 999999Hz */ + uint32_t freq_hz; /* From 0 to 999999Hz */ /* Final Freq = freq_mhz+freq_hz */ } set_freq_params_t; #define FREQ_ONE_MHZ (1000*1000ull) @@ -620,15 +715,16 @@ int ADDCALL hackrf_set_freq(hackrf_device* device, const uint64_t freq_hz) uint32_t l_freq_hz; set_freq_params_t set_freq_params; uint8_t length; - + int result; + /* Convert Freq Hz 64bits to Freq MHz (32bits) & Freq Hz (32bits) */ l_freq_mhz = (uint32_t)(freq_hz / FREQ_ONE_MHZ); l_freq_hz = (uint32_t)(freq_hz - (((uint64_t)l_freq_mhz) * FREQ_ONE_MHZ)); set_freq_params.freq_mhz = l_freq_mhz; set_freq_params.freq_hz = l_freq_hz; length = sizeof(set_freq_params_t); - - int result = libusb_control_transfer( + + result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, HACKRF_VENDOR_REQUEST_SET_FREQ, @@ -638,8 +734,9 @@ int ADDCALL hackrf_set_freq(hackrf_device* device, const uint64_t freq_hz) length, 0 ); - - if (result < length) { + + if (result < length) + { return HACKRF_ERROR_LIBUSB; } else { return HACKRF_SUCCESS; @@ -648,7 +745,8 @@ int ADDCALL hackrf_set_freq(hackrf_device* device, const uint64_t freq_hz) int ADDCALL hackrf_set_amp_enable(hackrf_device* device, const uint8_t value) { - int result = libusb_control_transfer( + int result; + result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, HACKRF_VENDOR_REQUEST_AMP_ENABLE, @@ -659,19 +757,21 @@ int ADDCALL hackrf_set_amp_enable(hackrf_device* device, const uint8_t value) 0 ); - if (result != 0) { + if (result != 0) + { return HACKRF_ERROR_LIBUSB; } else { return HACKRF_SUCCESS; } } - + int ADDCALL hackrf_board_partid_serialno_read(hackrf_device* device, read_partid_serialno_t* read_partid_serialno) { uint8_t length; + int result; length = sizeof(read_partid_serialno_t); - int result = libusb_control_transfer( + result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, HACKRF_VENDOR_REQUEST_BOARD_PARTID_SERIALNO_READ, @@ -682,223 +782,282 @@ int ADDCALL hackrf_board_partid_serialno_read(hackrf_device* device, read_partid 0 ); - if (result < length) { + if (result < length) + { return HACKRF_ERROR_LIBUSB; } else { return HACKRF_SUCCESS; } } -static void* transfer_threadproc(void* arg) { +static void* transfer_threadproc(void* arg) +{ hackrf_device* device = (hackrf_device*)arg; - - struct timeval timeout = { 0, 500000 }; + int error; + struct timeval timeout = { 0, 500000 }; - while( (device->streaming) && (do_exit == false) ) + while( (device->streaming) && (do_exit == false) ) { - int error = libusb_handle_events_timeout(g_libusb_context, &timeout); - if( error != 0 ) { + error = libusb_handle_events_timeout(g_libusb_context, &timeout); + if( error != 0 ) + { device->streaming = false; - } - } - + } + } + return NULL; } -static void hackrf_libusb_transfer_callback(struct libusb_transfer* usb_transfer) { +static void hackrf_libusb_transfer_callback(struct libusb_transfer* usb_transfer) +{ hackrf_device* device = (hackrf_device*)usb_transfer->user_data; - switch(usb_transfer->status) + if(usb_transfer->status == LIBUSB_TRANSFER_COMPLETED) { - case LIBUSB_TRANSFER_COMPLETED: - { - hackrf_transfer transfer = { - transfer.device = device, - transfer.buffer = usb_transfer->buffer, - transfer.buffer_length = usb_transfer->length, - transfer.valid_length = usb_transfer->actual_length, - transfer.rx_ctx = device->rx_ctx, - transfer.tx_ctx = device->tx_ctx - }; + hackrf_transfer transfer = { + transfer.device = device, + transfer.buffer = usb_transfer->buffer, + transfer.buffer_length = usb_transfer->length, + transfer.valid_length = usb_transfer->actual_length, + transfer.rx_ctx = device->rx_ctx, + transfer.tx_ctx = device->tx_ctx + }; - if( device->callback(&transfer) == 0 ) + if( device->callback(&transfer) == 0 ) + { + if( libusb_submit_transfer(usb_transfer) < 0) { - libusb_submit_transfer(usb_transfer); + request_exit(); + }else { return; - }else - { - device->streaming = false; } + }else { + request_exit(); } - break; - - case LIBUSB_TRANSFER_NO_DEVICE: - device->streaming = false; /* Fatal error stop transfer */ - break; - - case LIBUSB_TRANSFER_ERROR: - case LIBUSB_TRANSFER_TIMED_OUT: - case LIBUSB_TRANSFER_STALL: - case LIBUSB_TRANSFER_OVERFLOW: - case LIBUSB_TRANSFER_CANCELLED: - default: - /* Do nothing and continue */ - break; + } else { + /* Other cases LIBUSB_TRANSFER_NO_DEVICE + LIBUSB_TRANSFER_ERROR, LIBUSB_TRANSFER_TIMED_OUT + LIBUSB_TRANSFER_STALL, LIBUSB_TRANSFER_OVERFLOW + LIBUSB_TRANSFER_CANCELLED ... + */ + request_exit(); /* Fatal error stop transfer */ } } -static int kill_transfer_thread(hackrf_device* device) { - device->streaming = false; - do_exit = true; +static int kill_transfer_thread(hackrf_device* device) +{ + void* value; + int result; - if( device->transfer_thread_started != false ) { - void* value = NULL; - int result = pthread_join(device->transfer_thread, &value); - if( result != 0 ) { + request_exit(); + + if( device->transfer_thread_started != false ) + { + value = NULL; + result = pthread_join(device->transfer_thread, &value); + if( result != 0 ) + { return HACKRF_ERROR_THREAD; } device->transfer_thread_started = false; + + /* Cancel all transfers */ + cancel_transfers(device); } return HACKRF_SUCCESS; } -static int create_transfer_thread( - hackrf_device* device, - const uint8_t endpoint_address, - hackrf_sample_block_cb_fn callback -) { - if( device->transfer_thread_started == false ) { - int result = prepare_transfers( +static int create_transfer_thread(hackrf_device* device, + const uint8_t endpoint_address, + hackrf_sample_block_cb_fn callback) +{ + int result; + + if( device->transfer_thread_started == false ) + { + device->streaming = false; + + result = prepare_transfers( device, endpoint_address, (libusb_transfer_cb_fn)hackrf_libusb_transfer_callback ); - if( result != HACKRF_SUCCESS ) { + + if( result != HACKRF_SUCCESS ) + { return result; } - - device->callback = callback; - device->streaming = true; - device->transfer_thread_started = true; + device->streaming = true; + device->callback = callback; result = pthread_create(&device->transfer_thread, 0, transfer_threadproc, device); - if( result != 0 ) { - device->transfer_thread_started = false; + if( result == 0 ) + { + device->transfer_thread_started = true; + }else { return HACKRF_ERROR_THREAD; } } else { return HACKRF_ERROR_BUSY; } - + return HACKRF_SUCCESS; } -bool ADDCALL hackrf_is_streaming(hackrf_device* device) { - return device->streaming; +int ADDCALL hackrf_is_streaming(hackrf_device* device) +{ + /* return hackrf is streaming only when streaming, transfer_thread_started are true and do_exit equal false */ + + if( (device->transfer_thread_started == true) && + (device->streaming == true) && + (do_exit == false) ) + { + return HACKRF_TRUE; + } else { + + if(device->transfer_thread_started == false) + { + return HACKRF_ERROR_STREAMING_THREAD_ERR; + } + + if(device->streaming == false) + { + return HACKRF_ERROR_STREAMING_STOPPED; + } + + return HACKRF_ERROR_STREAMING_EXIT_CALLED; + } } -int ADDCALL hackrf_start_rx(hackrf_device* device, hackrf_sample_block_cb_fn callback, void* rx_ctx) { +int ADDCALL hackrf_start_rx(hackrf_device* device, hackrf_sample_block_cb_fn callback, void* rx_ctx) +{ + int result; const uint8_t endpoint_address = LIBUSB_ENDPOINT_IN | 1; - int result = hackrf_set_transceiver_mode(device, HACKRF_TRANSCEIVER_MODE_RECEIVE); - if( result == HACKRF_SUCCESS ) { + result = hackrf_set_transceiver_mode(device, HACKRF_TRANSCEIVER_MODE_RECEIVE); + if( result == HACKRF_SUCCESS ) + { device->rx_ctx = rx_ctx; - create_transfer_thread(device, endpoint_address, callback); + result = create_transfer_thread(device, endpoint_address, callback); } return result; } -int ADDCALL hackrf_stop_rx(hackrf_device* device) { +int ADDCALL hackrf_stop_rx(hackrf_device* device) +{ int result1, result2; result1 = kill_transfer_thread(device); result2 = hackrf_set_transceiver_mode(device, HACKRF_TRANSCEIVER_MODE_OFF); if (result2 != HACKRF_SUCCESS) + { return result2; + } return result1; } -int ADDCALL hackrf_start_tx(hackrf_device* device, hackrf_sample_block_cb_fn callback, void* tx_ctx) { +int ADDCALL hackrf_start_tx(hackrf_device* device, hackrf_sample_block_cb_fn callback, void* tx_ctx) +{ + int result; const uint8_t endpoint_address = LIBUSB_ENDPOINT_OUT | 2; - int result = hackrf_set_transceiver_mode(device, HACKRF_TRANSCEIVER_MODE_TRANSMIT); - if( result == HACKRF_SUCCESS ) { + result = hackrf_set_transceiver_mode(device, HACKRF_TRANSCEIVER_MODE_TRANSMIT); + if( result == HACKRF_SUCCESS ) + { device->tx_ctx = tx_ctx; result = create_transfer_thread(device, endpoint_address, callback); } return result; } -int ADDCALL hackrf_stop_tx(hackrf_device* device) { +int ADDCALL hackrf_stop_tx(hackrf_device* device) +{ int result1, result2; result1 = kill_transfer_thread(device); result2 = hackrf_set_transceiver_mode(device, HACKRF_TRANSCEIVER_MODE_OFF); if (result2 != HACKRF_SUCCESS) + { return result2; + } return result1; } -int ADDCALL hackrf_close(hackrf_device* device) { - if( device != NULL ) { - int result = hackrf_stop_rx(device); - if( result ) { - return result; - } +int ADDCALL hackrf_close(hackrf_device* device) +{ + int result1, result2; + + result1 = HACKRF_SUCCESS; + result2 = HACKRF_SUCCESS; - result = hackrf_stop_tx(device); - if( result ) { - return result; - } - - if( device->usb_device != NULL ) { - result = libusb_release_interface(device->usb_device, 0); - if( result ) { - return HACKRF_ERROR_LIBUSB; - } - - libusb_close(device->usb_device); - + if( device != NULL ) + { + result1 = hackrf_stop_rx(device); + result2 = hackrf_stop_tx(device); + if( device->usb_device != NULL ) + { + libusb_release_interface(device->usb_device, 0); + libusb_close(device->usb_device); device->usb_device = NULL; } - + free_transfers(device); - + free(device); } - - return HACKRF_SUCCESS; + + if (result2 != HACKRF_SUCCESS) + { + return result2; + } + return result1; } -const char* ADDCALL hackrf_error_name(enum hackrf_error errcode) { - switch(errcode) { +const char* ADDCALL hackrf_error_name(enum hackrf_error errcode) +{ + switch(errcode) + { case HACKRF_SUCCESS: return "HACKRF_SUCCESS"; - + + case HACKRF_TRUE: + return "HACKRF_TRUE"; + case HACKRF_ERROR_INVALID_PARAM: return "HACKRF_ERROR_INVALID_PARAM"; - + case HACKRF_ERROR_NOT_FOUND: return "HACKRF_ERROR_NOT_FOUND"; - + case HACKRF_ERROR_BUSY: return "HACKRF_ERROR_BUSY"; - + case HACKRF_ERROR_NO_MEM: return "HACKRF_ERROR_NO_MEM"; - + case HACKRF_ERROR_LIBUSB: return "HACKRF_ERROR_LIBUSB"; - + case HACKRF_ERROR_THREAD: return "HACKRF_ERROR_THREAD"; - + + case HACKRF_ERROR_STREAMING_THREAD_ERR: + return "HACKRF_ERROR_STREAMING_THREAD_ERR"; + + case HACKRF_ERROR_STREAMING_STOPPED: + return "HACKRF_ERROR_STREAMING_STOPPED"; + + case HACKRF_ERROR_STREAMING_EXIT_CALLED: + return "HACKRF_ERROR_STREAMING_EXIT_CALLED"; + case HACKRF_ERROR_OTHER: return "HACKRF_ERROR_OTHER"; - + default: return "HACKRF unknown error"; } } -const char* ADDCALL hackrf_board_id_name(enum hackrf_board_id board_id) { - switch(board_id) { +const char* ADDCALL hackrf_board_id_name(enum hackrf_board_id board_id) +{ + switch(board_id) + { case BOARD_ID_JELLYBEAN: return "Jellybean"; @@ -919,7 +1078,8 @@ uint32_t ADDCALL hackrf_compute_baseband_filter_bw_round_down_lt(const uint32_t const max2837_ft_t* p = max2837_ft; while( p->bandwidth_hz != 0 ) { - if( p->bandwidth_hz >= bandwidth_hz ) { + if( p->bandwidth_hz >= bandwidth_hz ) + { break; } p++; @@ -938,19 +1098,20 @@ uint32_t ADDCALL hackrf_compute_baseband_filter_bw(const uint32_t bandwidth_hz) const max2837_ft_t* p = max2837_ft; while( p->bandwidth_hz != 0 ) { - if( p->bandwidth_hz >= bandwidth_hz ) { + if( p->bandwidth_hz >= bandwidth_hz ) + { break; } p++; } - + /* Round down (if no equal to first entry) and if > bandwidth_hz */ if(p != max2837_ft) { if(p->bandwidth_hz > bandwidth_hz) p--; } - + return p->bandwidth_hz; } diff --git a/host/libhackrf/src/hackrf.h b/host/libhackrf/src/hackrf.h index 7d5f8f22..a5ae7ea7 100644 --- a/host/libhackrf/src/hackrf.h +++ b/host/libhackrf/src/hackrf.h @@ -49,12 +49,16 @@ enum hackrf_error { HACKRF_SUCCESS = 0, + HACKRF_TRUE = 1, HACKRF_ERROR_INVALID_PARAM = -2, HACKRF_ERROR_NOT_FOUND = -5, HACKRF_ERROR_BUSY = -6, HACKRF_ERROR_NO_MEM = -11, HACKRF_ERROR_LIBUSB = -1000, HACKRF_ERROR_THREAD = -1001, + HACKRF_ERROR_STREAMING_THREAD_ERR = -1002, + HACKRF_ERROR_STREAMING_STOPPED = -1003, + HACKRF_ERROR_STREAMING_EXIT_CALLED = -1004, HACKRF_ERROR_OTHER = -9999, }; @@ -98,8 +102,9 @@ extern ADDAPI int ADDCALL hackrf_stop_rx(hackrf_device* device); extern ADDAPI int ADDCALL hackrf_start_tx(hackrf_device* device, hackrf_sample_block_cb_fn callback, void* tx_ctx); extern ADDAPI int ADDCALL hackrf_stop_tx(hackrf_device* device); - -extern ADDAPI bool ADDCALL hackrf_is_streaming(hackrf_device* device); + +/* return HACKRF_TRUE if success */ +extern ADDAPI int ADDCALL hackrf_is_streaming(hackrf_device* device); extern ADDAPI int ADDCALL hackrf_max2837_read(hackrf_device* device, uint8_t register_number, uint16_t* value); extern ADDAPI int ADDCALL hackrf_max2837_write(hackrf_device* device, uint8_t register_number, uint16_t value);