Fix streaming hangs in consumers

* Update device->streaming to reflect whether we're streaming data,
  rather than just whether the streaming thread is active.
  The streaming thread is now always active!
This commit is contained in:
Adrian Chadd
2020-11-09 09:37:49 -08:00
parent 0961a76f26
commit 9a278d267a

View File

@ -322,6 +322,7 @@ static int detach_kernel_drivers(libusb_device_handle* usb_device_handle)
static int set_hackrf_configuration(libusb_device_handle* usb_device, int config) static int set_hackrf_configuration(libusb_device_handle* usb_device, int config)
{ {
int result, curr_config; int result, curr_config;
result = libusb_get_configuration(usb_device, &curr_config); result = libusb_get_configuration(usb_device, &curr_config);
if( result != 0 ) if( result != 0 )
{ {
@ -1496,7 +1497,7 @@ static void* transfer_threadproc(void* arg)
int error; int error;
struct timeval timeout = { 0, 500000 }; struct timeval timeout = { 0, 500000 };
while( (device->streaming) && (device->do_exit == false) ) while(device->do_exit == false )
{ {
error = libusb_handle_events_timeout(g_libusb_context, &timeout); error = libusb_handle_events_timeout(g_libusb_context, &timeout);
if( (error != 0) && (error != LIBUSB_ERROR_INTERRUPTED) ) if( (error != 0) && (error != LIBUSB_ERROR_INTERRUPTED) )
@ -1542,6 +1543,7 @@ static void LIBUSB_CALL hackrf_libusb_transfer_callback(struct libusb_transfer*
LIBUSB_TRANSFER_STALL, LIBUSB_TRANSFER_OVERFLOW .... LIBUSB_TRANSFER_STALL, LIBUSB_TRANSFER_OVERFLOW ....
*/ */
request_exit(device); /* Fatal error stop transfer */ request_exit(device); /* Fatal error stop transfer */
device->streaming = false;
} }
} }
@ -1615,13 +1617,11 @@ static int prepare_setup_transfers(hackrf_device* device,
static int create_transfer_thread(hackrf_device* device) static int create_transfer_thread(hackrf_device* device)
{ {
int result; int result;
if( device->transfer_thread_started == false ) if( device->transfer_thread_started == false )
{ {
device->streaming = false; device->streaming = false;
device->do_exit = false; device->do_exit = false;
device->streaming = true;
result = pthread_create(&device->transfer_thread, 0, transfer_threadproc, device); result = pthread_create(&device->transfer_thread, 0, transfer_threadproc, device);
if( result == 0 ) if( result == 0 )
{ {
@ -1639,7 +1639,7 @@ static int create_transfer_thread(hackrf_device* device)
int ADDCALL hackrf_is_streaming(hackrf_device* device) 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 */ /* return hackrf is streaming only when streaming, transfer_thread_started are true and do_exit equal false */
if( (device->transfer_thread_started == true) && if( (device->transfer_thread_started == true) &&
(device->streaming == true) && (device->streaming == true) &&
(device->do_exit == false) ) (device->do_exit == false) )
@ -1665,24 +1665,30 @@ int ADDCALL hackrf_start_rx(hackrf_device* device, hackrf_sample_block_cb_fn cal
{ {
int result; int result;
const uint8_t endpoint_address = LIBUSB_ENDPOINT_IN | 1; const uint8_t endpoint_address = LIBUSB_ENDPOINT_IN | 1;
device->rx_ctx = rx_ctx;
result = hackrf_set_transceiver_mode(device, HACKRF_TRANSCEIVER_MODE_RECEIVE); result = hackrf_set_transceiver_mode(device, HACKRF_TRANSCEIVER_MODE_RECEIVE);
if( result == HACKRF_SUCCESS ) if( result == HACKRF_SUCCESS )
{ {
device->rx_ctx = rx_ctx;
result = prepare_setup_transfers(device, endpoint_address, callback); result = prepare_setup_transfers(device, endpoint_address, callback);
} }
if (result == HACKRF_SUCCESS) {
device->streaming = true;
}
return result; return result;
} }
int ADDCALL hackrf_stop_rx(hackrf_device* device) int ADDCALL hackrf_stop_rx(hackrf_device* device)
{ {
int result; int result;
device->streaming = false;
result = cancel_transfers(device); result = cancel_transfers(device);
if (result != HACKRF_SUCCESS) if (result != HACKRF_SUCCESS)
{ {
return result; return result;
} }
return hackrf_set_transceiver_mode(device, HACKRF_TRANSCEIVER_MODE_OFF); result = hackrf_set_transceiver_mode(device, HACKRF_TRANSCEIVER_MODE_OFF);
return result;
} }
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)
@ -1695,19 +1701,23 @@ int ADDCALL hackrf_start_tx(hackrf_device* device, hackrf_sample_block_cb_fn cal
device->tx_ctx = tx_ctx; device->tx_ctx = tx_ctx;
result = prepare_setup_transfers(device, endpoint_address, callback); result = prepare_setup_transfers(device, endpoint_address, callback);
} }
if (result == HACKRF_SUCCESS) {
device->streaming = true;
}
return result; return result;
} }
int ADDCALL hackrf_stop_tx(hackrf_device* device) int ADDCALL hackrf_stop_tx(hackrf_device* device)
{ {
int result; int result;
device->streaming = false;
result = cancel_transfers(device); result = cancel_transfers(device);
if (result != HACKRF_SUCCESS) if (result != HACKRF_SUCCESS)
{ {
return result; return result;
} }
result = hackrf_set_transceiver_mode(device, HACKRF_TRANSCEIVER_MODE_OFF);
return hackrf_set_transceiver_mode(device, HACKRF_TRANSCEIVER_MODE_OFF); return result;
} }
int ADDCALL hackrf_close(hackrf_device* device) int ADDCALL hackrf_close(hackrf_device* device)
@ -2226,6 +2236,9 @@ int ADDCALL hackrf_start_rx_sweep(hackrf_device* device, hackrf_sample_block_cb_
device->rx_ctx = rx_ctx; device->rx_ctx = rx_ctx;
result = prepare_setup_transfers(device, endpoint_address, callback); result = prepare_setup_transfers(device, endpoint_address, callback);
} }
if (result == HACKRF_SUCCESS) {
device->streaming = true;
}
return result; return result;
} }