diff --git a/host/libraries/libbladeRF/include/libbladeRF.h b/host/libraries/libbladeRF/include/libbladeRF.h
index 8e046a66..887dd43e 100644
--- a/host/libraries/libbladeRF/include/libbladeRF.h
+++ b/host/libraries/libbladeRF/include/libbladeRF.h
@@ -81,6 +81,10 @@ extern "C" {
/**
* @defgroup FN_INIT Initialization/deinitialization
*
+ * @bug These functions are not currently thread-safe. This will be addressed
+ * in future revisions of the library. The caller is responsible for ensuring
+ * these functions are called in a thread-safe manner.
+ *
* @{
*/
@@ -240,6 +244,10 @@ void CALL_CONV bladerf_init_devinfo(struct bladerf_devinfo *info);
*
* @pre dev must be a valid device handle.
*
+ * @bug This function is not currently thread-safe. This will be addressed
+ * in future revisions of the library. The caller is responsible for ensuring
+ * this function are called in a thread-safe manner.
+ *
* @param[in] dev Device handle previously obtained with bladerf_open()
* @param[out] info Device information populated by this function
*
@@ -294,6 +302,11 @@ bool CALL_CONV bladerf_devstr_matches(const char *dev_str,
/**
* @defgroup FN_CTRL Device control and configuration
*
+ * @bug These functions are not currently thread-safe. This will be addressed
+ * in future revisions of the library. The caller is responsible for ensuring
+ * these functions are called in a thread-safe manner. Generally, this implies
+ * locking access to a device handle when making control/configuration calls.
+ *
* @{
*/
@@ -1028,7 +1041,7 @@ struct bladerf_metadata {
* performance.
*
* When using this interface, one must be aware of thread-safety implications.
- * Internally, this library does not enforce thread-safe access to device
+ * Internally, this interface does not enforce thread-safe access to device
* handles; API users are responsible for this.
*
* However, it easy to avoid thread-safety issues if the following guidelines are
@@ -1051,25 +1064,47 @@ struct bladerf_metadata {
* @{
*/
+/**
+ * Use this as a return value in callbacks or as the buffer parameter to
+ * bladerf_submit_stream_buffer() to shutdown a stream.
+ */
+#define BLADERF_STREAM_SHUTDOWN (NULL)
+
+/**
+ * Use this value in a stream callback to indicate that no buffer is being
+ * provided. In this case, buffers are expected to be provided via
+ * bladerf_submit_stream_buffer().
+ */
+#define BLADERF_STREAM_NO_DATA ((void*)(-1))
+
/** This opaque structure is used to keep track of stream information */
struct bladerf_stream;
/**
- * Stream callback
+ * This typedef represents a callback function that is executed in response to
+ * this interface's asynchronous events.
*
* Stream callbacks must not block or perform long-running operations.
- * Otherwise, timeouts may occur and the callbacks for another module may be
- * starved.
- *
- * Callbacks should be handled quickly, with work offloaded to other
- * threads.
+ * Otherwise, timeouts may occur. If this cannot be guaranteed, consider
+ * returning BLADERF_STREAM_NO_DATA in callbacks and later submit a buffer using
+ * bladerf_submit_stream_buffer(). However, callbacks should always take a
+ * single approach of returning buffers or returning
+ * BLADERF_STREAM_NO_DATA and submitting buffers later -- but not both.
*
* In most use-cases, stream callbacks will be executing in a thread that is
- * separate from the thread used to configure device parameters.
+ * separate from the thread used to configure device parameters. Because this
+ * interface does not currently ensure thread safe accesses to devices,
+ * callbacks being handled in different threads must not make make API
+ * calls.
*
- * Because this library does not currently ensure thread safe accesses to
- * devices, callbacks being handled in different threads must not make make
- * API calls.
+ * When running in a full-duplex mode of operation with simultaneous TX and RX
+ * stream threads, be aware that one module's callback may occur in the context
+ * of another module's thread. The API user is responsible for ensuring their
+ * callbacks are thread safe.
+ *
+ * As of libbladeRF v0.15.0, is guaranteed that only one callback from a module
+ * will occur at a time. (i.e., a second TX callback will not fire while one is
+ * currently being handled.)
*
* The API user is responsible for managing access to sample buffers, and
* ensuring that if another thread is using a buffer, it is not returned via the
@@ -1088,13 +1123,16 @@ struct bladerf_stream;
* For TX callbacks:
* - samples: Pointer to buffer of samples that was sent
* - num_samples: Number of sent in last transfer and to send in next transfer
- * - Return value: The user specifies the address of the next buffer to send
+ * - Return value: The user specifies the address of the next buffer to send,
+ * BLADERF_STREAM_SHUTDOWN, or BLADERF_STREAM_NO_DATA.
*
* For RX callbacks:
* - samples: Buffer filled with received data
* - num_samples: Number of samples received and size of next buffers
* - Return value: The user specifies the next buffer to fill with RX data,
- * which should be `num_samples` in size.
+ * which should be `num_samples` in size,
+ * BLADERF_STREAM_SHUTDOWN, or BLADERF_STREAM_NO_DATA.
+ *
*
*/
typedef void *(*bladerf_stream_cb)(struct bladerf *dev,
@@ -1140,6 +1178,9 @@ typedef void *(*bladerf_stream_cb)(struct bladerf *dev,
* While increasing the number of buffers available provides additional
* elasticity, be aware that it also increases latency.
*
+ * @bug This function is not currently thread-safe. Callers should ensure other
+ * threads are not accessing the `dev` handle when this call is made.
+ *
* @param[out] stream Upon success, this will be updated to contain
* a stream handle (i.e., address)
*
@@ -1186,23 +1227,13 @@ int CALL_CONV bladerf_init_stream(struct bladerf_stream **stream,
void *user_data);
/**
- * Begin running a stream. This call will block until the steam completes.
+ * Begin running a stream. This call will block until the steam completes.
*
* Only 1 RX stream and 1 TX stream may be running at a time. Attempting to
* call bladerf_stream() with more than one stream per module will yield
* unexpected (and most likely undesirable) results. See the ::bladerf_stream_cb
* description for additional thread-safety caveats.
*
- * When running a full-duplex configuration with two threads (e.g,
- * one thread calling bladerf_stream() for TX, and another for RX), stream
- * callbacks may be executed in the context of either thread. Therefore, the
- * caller is responsible for ensuring that his or her callbacks are thread-safe.
- *
- * When starting a TX stream, an initial set of callbacks will be immediately
- * invoked. The caller must ensure that there are at *more than* T buffers
- * filled before calling bladerf_stream(..., BLADERF_MODULE_TX), where T is the
- * num_transfers value provided to bladerf_init_stream(), to avoid an underrun.
- *
* @pre This function should be preceded by a call to bladerf_enable_module()
* to enable the associated RX or TX module before attempting to use
* it to stream data.
@@ -1218,9 +1249,36 @@ API_EXPORT
int CALL_CONV bladerf_stream(struct bladerf_stream *stream,
bladerf_module module);
+/**
+ * Submit a buffer to a stream from outside of a stream callback function.
+ * Use this only when returning BLADERF_STREAM_NO_DATA from callbacks. Do
+ * not mix usage of this function with callbacks that do return buffers
+ * for submission.
+ *
+ * This call may block if the device is not ready to submit a buffer for
+ * transfer. Use the `timeout_ms` to place an upper limit on the time this
+ * function can block.
+ *
+ * @param stream Stream to submit buffer to
+ * @param buffer Buffer to fill (RX) or containing data (TX). This buffer
+ * is assumed to be the size specified in the associated
+ * bladerf_init_stream() call.
+ * @param timeout_ms Milliseconds to timeout in, if this call blocks. 0
+ * implies an "infinite" wait.
+ *
+ * @return 0 on success, BLADERF_ERR_TIMEOUT upon a timeout, or a value from
+ * \ref RETCODES list on other failures
+ */
+API_EXPORT
+int CALL_CONV bladerf_submit_stream_buffer(struct bladerf_stream *stream,
+ void *buffer,
+ unsigned int timeout_ms);
+
/**
* Deinitialize and deallocate stream resources.
*
+ * @pre Stream is no longer being used (via bladerf_submit_stream_buffer() or
+ * bladerf_stream() calls.)
* @post Stream is deallocated and may no longer be used.
*
* @param stream Stream to deinitialize. This function does nothing
@@ -1232,6 +1290,10 @@ void CALL_CONV bladerf_deinit_stream(struct bladerf_stream *stream);
/**
* Set stream transfer timeout in milliseconds
*
+ * @bug This call is not threadsafe; this will be addressed in future
+ * versions of the library. Callers should ensure no other threads
+ * are accessing the specified device handle with this call is made.
+ *
* @param dev Device handle
* @param module Module to adjust
* @param timeout Timeout in milliseconds
@@ -1247,6 +1309,10 @@ int CALL_CONV bladerf_set_stream_timeout(struct bladerf *dev,
/**
* Get transfer timeout in milliseconds
*
+ * @bug This call is not threadsafe; this will be addressed in future
+ * versions of the library. Callers should ensure no other threads
+ * are accessing the specified device handle with this call is made.
+ *
* @param[in] dev Device handle
* @param[in] module Module to adjust
* @param[out] timeout On success, updated with current transfer
@@ -1366,6 +1432,9 @@ int CALL_CONV bladerf_get_stream_timeout(struct bladerf *dev,
* The `num_buffers` parameter should generally be increased as the amount of
* work done between bladerf_sync_rx() or bladerf_sync_tx() calls increases.
*
+ * @bug This call is not threadsafe; this will be addressed in future
+ * versions of the library. Callers should ensure no other threads
+ * are accessing the specified device handle with this call is made.
*
* @param dev Device to configure
*
@@ -1486,10 +1555,13 @@ int CALL_CONV bladerf_sync_rx(struct bladerf *dev,
/**
* @defgroup FN_INFO Device info
*
+ * @bug These functions are not currently thread-safe. This will be addressed
+ * in future revisions of the library. The caller is responsible for ensuring
+ * these functions are called in a thread-safe manner.
+ *
* @{
*/
-
/**
* Version structure for FPGA, firmware, libbladeRF, and associated utilities
*/
@@ -1603,6 +1675,10 @@ bladerf_dev_speed CALL_CONV bladerf_device_speed(struct bladerf *dev);
/**
* @defgroup FN_PROG Device loading and programming
*
+ * @bug These functions are not currently thread-safe. This will be addressed
+ * in future revisions of the library. The caller is responsible for ensuring
+ * these functions are called in a thread-safe manner.
+ *
* @{
*/
@@ -1931,6 +2007,9 @@ int CALL_CONV bladerf_image_read(struct bladerf_image *image, const char *file);
* Be careful when mixing these calls with higher-level routines that manipulate
* the same registers/settings.
*
+ * @bug These functions are not currently thread-safe. This will be addressed
+ * in future revisions of the library. The caller is responsible for ensuring
+ * these functions are called in a thread-safe manner.
*
* @{
*/
@@ -2126,6 +2205,11 @@ int CALL_CONV bladerf_calibrate_dc(struct bladerf *dev,
*
* https://github.com/nuand/bladeRF/wiki/FX3-Firmware#spi-flash-layout
*
+ * @bug These functions are not currently thread-safe. This will be addressed
+ * in future revisions of the library. The caller is responsible for ensuring
+ * these functions are called in a thread-safe manner.
+ *
+ *
* @{
*/
#define BLADERF_FLASH_PAGE_BITS (8) /**< 256bytes == 2^8 bytes */
diff --git a/host/libraries/libbladeRF/src/async.c b/host/libraries/libbladeRF/src/async.c
index 776fdfcc..508bc3ca 100644
--- a/host/libraries/libbladeRF/src/async.c
+++ b/host/libraries/libbladeRF/src/async.c
@@ -60,12 +60,16 @@ int async_init_stream(struct bladerf_stream **stream,
return BLADERF_ERR_UNEXPECTED;
}
+ if (pthread_cond_init(&lstream->can_submit_buffer, NULL) != 0) {
+ free(lstream);
+ return BLADERF_ERR_UNEXPECTED;
+ }
+
lstream->dev = dev;
lstream->error_code = 0;
lstream->state = STREAM_IDLE;
lstream->samples_per_buffer = samples_per_buffer;
lstream->num_buffers = num_buffers;
- lstream->num_transfers = num_transfers;
lstream->format = format;
lstream->cb = callback;
lstream->user_data = user_data;
@@ -109,7 +113,7 @@ int async_init_stream(struct bladerf_stream **stream,
free(lstream);
} else {
/* Perform any backend-specific stream initialization */
- status = dev->fn->init_stream(lstream);
+ status = dev->fn->init_stream(lstream, num_transfers);
if (status < 0) {
bladerf_deinit_stream(lstream);
@@ -143,6 +147,19 @@ int async_run_stream(struct bladerf_stream *stream, bladerf_module module)
return status == 0 ? stream->error_code : status;
}
+int async_submit_stream_buffer(struct bladerf_stream *stream,
+ void *buffer,
+ unsigned int timeout_ms)
+{
+ int status;
+
+ pthread_mutex_lock(&stream->lock);
+ status = stream->dev->fn->submit_stream_buffer(stream, buffer, timeout_ms);
+ pthread_mutex_unlock(&stream->lock);
+
+ return status;
+}
+
void async_deinit_stream(struct bladerf_stream *stream)
{
size_t i;
diff --git a/host/libraries/libbladeRF/src/async.h b/host/libraries/libbladeRF/src/async.h
index 2527608b..785ad8ae 100644
--- a/host/libraries/libbladeRF/src/async.h
+++ b/host/libraries/libbladeRF/src/async.h
@@ -46,7 +46,6 @@ struct bladerf_stream {
void *user_data;
size_t samples_per_buffer;
size_t num_buffers;
- size_t num_transfers;
void **buffers;
pthread_mutex_t lock;
@@ -54,6 +53,7 @@ struct bladerf_stream {
/* The following items must be accessed atomically */
int error_code;
bladerf_stream_state state;
+ pthread_cond_t can_submit_buffer;
void *backend_data;
};
@@ -68,8 +68,16 @@ int async_init_stream(struct bladerf_stream **stream,
size_t num_transfers,
void *user_data);
+/* Backend code is responsible for acquiring stream->lock in thier callbacks */
int async_run_stream(struct bladerf_stream *stream, bladerf_module module);
+
+/* This function WILL acquire stream->lock before calling backend code */
+int async_submit_stream_buffer(struct bladerf_stream *stream,
+ void *buffer,
+ unsigned int timeout_ms);
+
+
void async_deinit_stream(struct bladerf_stream *stream);
#endif
diff --git a/host/libraries/libbladeRF/src/backend/dummy.c b/host/libraries/libbladeRF/src/backend/dummy.c
index 2538ef35..01b1dd75 100644
--- a/host/libraries/libbladeRF/src/backend/dummy.c
+++ b/host/libraries/libbladeRF/src/backend/dummy.c
@@ -155,6 +155,13 @@ static int dummy_stream(struct bladerf_stream *stream, bladerf_module module)
return 0;
}
+static int dummy_submit_stream_buffer(struct bladerf_stream *stream,
+ void *buffer,
+ unsigned int timeout_ms)
+{
+ return 0;
+}
+
void dummy_deinit_stream(struct bladerf_stream *stream)
{
return;
@@ -205,5 +212,6 @@ const struct bladerf_fn bladerf_dummy_fn = {
FIELD_INIT(.init_stream, dummy_stream_init),
FIELD_INIT(.stream, dummy_stream),
+ FIELD_INIT(.submit_stream_buffer, dummy_submit_stream_buffer),
FIELD_INIT(.deinit_stream, dummy_deinit_stream),
};
diff --git a/host/libraries/libbladeRF/src/backend/libusb.c b/host/libraries/libbladeRF/src/backend/libusb.c
index eacaf9f5..7345189c 100644
--- a/host/libraries/libbladeRF/src/backend/libusb.c
+++ b/host/libraries/libbladeRF/src/backend/libusb.c
@@ -62,18 +62,19 @@ typedef enum {
const struct bladerf_fn bladerf_lusb_fn;
-struct lusb_stream_data {
- struct libusb_transfer **transfers;
- size_t active_transfers;
+typedef enum {
+ TRANSFER_UNINITIALIZED = 0,
+ TRANSFER_AVAIL,
+ TRANSFER_IN_FLIGHT,
+ TRANSFER_CANCEL_PENDING
+} transfer_status;
- /* libusb recommends use of libusb_handle_events_timeout_completed() for
- * multithreaded applications.
- * http://libusb.sourceforge.net/api-1.0/mtasync.html
- *
- * In our case, "completion" is associated with a transistion to the done
- * state.
- */
- int libusb_completed;
+struct lusb_stream_data {
+ size_t num_transfers; /**< Total number of allocated transfers */
+ size_t num_avail_transfers;
+ size_t i; /**< Index to next transfer */
+ struct libusb_transfer **transfers;
+ transfer_status *transfer_status; /**< Status of each transfer */
};
static int error_libusb2bladerf(int error)
@@ -2020,31 +2021,112 @@ static inline void cancel_all_transfers(struct bladerf_stream *stream)
int status;
struct lusb_stream_data *stream_data = stream->backend_data;
- for (i = 0; i < stream->num_transfers; i++ ) {
- status = libusb_cancel_transfer(stream_data->transfers[i]);
- if (status < 0 && status != LIBUSB_ERROR_NOT_FOUND) {
- log_error("Error canceling transfer (%d): %s\n",
- status, libusb_error_name(status));
+ for (i = 0; i < stream_data->num_transfers; i++) {
+ if (stream_data->transfer_status[i] == TRANSFER_IN_FLIGHT) {
+
+ status = libusb_cancel_transfer(stream_data->transfers[i]);
+ if (status < 0 && status != LIBUSB_ERROR_NOT_FOUND) {
+ log_error("Error canceling transfer (%d): %s\r\n",
+ status, libusb_error_name(status));
+ } else {
+ stream_data->transfer_status[i] = TRANSFER_CANCEL_PENDING;
+ }
}
}
}
+static inline size_t transfer_idx(struct lusb_stream_data *stream_data,
+ struct libusb_transfer *transfer)
+{
+ size_t i;
+ for (i = 0; i < stream_data->num_transfers; i++) {
+ if (stream_data->transfers[i] == transfer) {
+ return i;
+ }
+ }
+
+ return UINT_MAX;
+}
+
+static void LIBUSB_CALL lusb_stream_cb(struct libusb_transfer *transfer);
+
+/* Precondition: A transfer is available. */
+static int submit_transfer(struct bladerf_stream *stream, void *buffer)
+{
+ int status;
+ struct bladerf_lusb *lusb = stream->dev->backend;
+ struct lusb_stream_data *stream_data = stream->backend_data;
+ struct libusb_transfer *transfer;
+ size_t bytes_per_buffer;
+ const unsigned char ep =
+ stream->module == BLADERF_MODULE_TX ? EP_OUT(0x01) : EP_IN(0x01);
+
+ assert(stream_data->transfer_status[stream_data->i] == TRANSFER_AVAIL);
+ transfer = stream_data->transfers[stream_data->i];
+
+ switch (stream->format) {
+ case BLADERF_FORMAT_SC16_Q11:
+ bytes_per_buffer = c16_samples_to_bytes(stream->samples_per_buffer);
+ break;
+
+ default:
+ assert(!"Unexpected format");
+ return BLADERF_ERR_INVAL;
+ }
+
+ assert(bytes_per_buffer <= INT_MAX);
+ libusb_fill_bulk_transfer(transfer,
+ lusb->handle,
+ ep,
+ buffer,
+ bytes_per_buffer,
+ lusb_stream_cb,
+ stream,
+ stream->dev->transfer_timeout[stream->module]);
+
+ status = libusb_submit_transfer(transfer);
+
+ if (status == 0) {
+ stream_data->transfer_status[stream_data->i] = TRANSFER_IN_FLIGHT;
+ stream_data->i = (stream_data->i + 1) % stream_data->num_transfers;
+ assert(stream_data->num_avail_transfers != 0);
+ stream_data->num_avail_transfers--;
+ } else {
+ log_error("Failed to submit transfer in %s: %s\n",
+ __FUNCTION__, libusb_error_name(status));
+ }
+
+ return error_libusb2bladerf(status);
+}
+
static void LIBUSB_CALL lusb_stream_cb(struct libusb_transfer *transfer)
{
struct bladerf_stream *stream = transfer->user_data;
- struct bladerf_lusb *lusb = stream->dev->backend;
void *next_buffer = NULL;
struct bladerf_metadata metadata;
struct lusb_stream_data *stream_data = stream->backend_data;
- size_t bytes_per_buffer;
+ size_t transfer_i;
/* Currently unused - zero out for out own debugging sanity... */
memset(&metadata, 0, sizeof(metadata));
pthread_mutex_lock(&stream->lock);
+ transfer_i = transfer_idx(stream_data, transfer);
+ assert(stream_data->transfer_status[transfer_i] == TRANSFER_IN_FLIGHT ||
+ stream_data->transfer_status[transfer_i] == TRANSFER_CANCEL_PENDING);
+
+ if (transfer_i >= stream_data->num_transfers) {
+ log_error("Unable to find transfer");
+ stream->state = STREAM_SHUTTING_DOWN;
+ } else {
+ stream_data->transfer_status[transfer_i] = TRANSFER_AVAIL;
+ stream_data->num_avail_transfers++;
+ pthread_cond_signal(&stream->can_submit_buffer);
+ }
+
/* Check to see if the transfer has been cancelled or errored */
- if( transfer->status != LIBUSB_TRANSFER_COMPLETED ) {
+ if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
/* Errored out for some reason .. */
stream->state = STREAM_SHUTTING_DOWN;
@@ -2061,7 +2143,7 @@ static void LIBUSB_CALL lusb_stream_cb(struct libusb_transfer *transfer)
case LIBUSB_TRANSFER_ERROR:
log_error("Got transfer error for buffer %p\n",
- transfer->buffer);
+ transfer->buffer);
stream->error_code = BLADERF_ERR_IO;
break;
@@ -2085,72 +2167,59 @@ static void LIBUSB_CALL lusb_stream_cb(struct libusb_transfer *transfer)
break;
}
- /* This transfer is no longer active */
- stream_data->active_transfers--;
- cancel_all_transfers(stream);
}
- /* Check to see if the stream is still valid */
- else if( stream->state == STREAM_RUNNING ) {
+ if (stream->state == STREAM_RUNNING) {
- /* Call user callback requesting more data to transmit */
+ /* Sanity check for debugging purposes */
if (transfer->length != transfer->actual_length) {
log_warning( "Received short transfer\n" );
+
+ /* For the SC16Q11 format, we're 4 bytes per samples... */
if ((transfer->actual_length & 3) != 0) {
log_warning( "Fractional samples received - stream likely corrupt\n" );
}
}
+
+ /* Call user callback requesting more data to transmit */
next_buffer = stream->cb(
stream->dev,
stream,
&metadata,
transfer->buffer,
bytes_to_c16_samples(transfer->actual_length),
- stream->user_data
- );
- if( next_buffer == NULL ) {
+ stream->user_data);
+
+ if (next_buffer == BLADERF_STREAM_SHUTDOWN) {
stream->state = STREAM_SHUTTING_DOWN;
- stream_data->active_transfers--;
+ } else if (next_buffer != BLADERF_STREAM_NO_DATA) {
+ int status = submit_transfer(stream, next_buffer);
+ if (status != 0) {
+ /* If this fails, we probably have a serious problem...so just
+ * shut it down. */
+ stream->state = STREAM_SHUTTING_DOWN;
+ }
}
- } else {
- stream_data->active_transfers--;
- cancel_all_transfers(stream);
}
- bytes_per_buffer = c16_samples_to_bytes(stream->samples_per_buffer);
- assert(bytes_per_buffer <= INT_MAX);
-
- /* Check the stream to make sure we're still valid and submit transfer,
- * as the user may want to stop the stream by returning NULL for the next buffer */
- if( next_buffer != NULL ) {
- /* Fill up the bulk transfer request */
- libusb_fill_bulk_transfer(
- transfer,
- lusb->handle,
- stream->module == BLADERF_MODULE_TX ? EP_OUT(0x01) : EP_IN(0x01),
- next_buffer,
- (int)bytes_per_buffer,
- lusb_stream_cb,
- stream,
- stream->dev->transfer_timeout[stream->module]
- );
- /* Submit the tranfer */
- libusb_submit_transfer(transfer);
- }
/* Check to see if all the transfers have been cancelled,
* and if so, clean up the stream */
- if( stream->state == STREAM_SHUTTING_DOWN ) {
- if( stream_data->active_transfers == 0 ) {
+ if (stream->state == STREAM_SHUTTING_DOWN) {
+
+ /* We know we're done when all of our transfers have returned to their
+ * "available" states */
+ if (stream_data->num_avail_transfers == stream_data->num_transfers) {
stream->state = STREAM_DONE;
- stream_data->libusb_completed = 1;
+ } else {
+ cancel_all_transfers(stream);
}
}
pthread_mutex_unlock(&stream->lock);
}
-static int lusb_stream_init(struct bladerf_stream *stream)
+static int lusb_stream_init(struct bladerf_stream *stream, size_t num_transfers)
{
size_t i;
struct lusb_stream_data *stream_data;
@@ -2165,28 +2234,31 @@ static int lusb_stream_init(struct bladerf_stream *stream)
/* Backend stream information */
stream->backend_data = stream_data;
- stream_data->libusb_completed = 0;
+ stream_data->num_transfers = num_transfers;
+ stream_data->num_avail_transfers = 0;
+ stream_data->i = 0;
- /* Pointers for libusb transfers */
- stream_data->transfers =
- calloc(stream->num_transfers, sizeof(struct libusb_transfer *));
-
- if (!stream_data->transfers) {
+ stream_data->transfers = malloc(num_transfers * sizeof(struct libusb_transfer *));
+ if (stream_data->transfers == NULL) {
log_error("Failed to allocate libusb tranfers\n");
- free(stream_data);
- stream->backend_data = NULL;
- return BLADERF_ERR_MEM;
+ status = BLADERF_ERR_MEM;
+ goto error;
}
- stream_data->active_transfers = 0;
+ stream_data->transfer_status = calloc(num_transfers, sizeof(transfer_status));
+ if (stream_data->transfer_status == NULL) {
+ log_error("Failed to allocated libusb transfer status array\n");
+ status = BLADERF_ERR_MEM;
+ goto error;
+ }
/* Create the libusb transfers */
- for( i = 0; i < stream->num_transfers; i++ ) {
+ for (i = 0; i < stream_data->num_transfers; i++) {
stream_data->transfers[i] = libusb_alloc_transfer(0);
/* Upon error, start tearing down anything we've started allocating
* and report that the stream is in a bad state */
- if (!stream_data->transfers[i]) {
+ if (stream_data->transfers[i] == NULL) {
/* Note: <= 0 not appropriate as we're dealing
* with an unsigned index */
@@ -2194,17 +2266,34 @@ static int lusb_stream_init(struct bladerf_stream *stream)
if (--i) {
libusb_free_transfer(stream_data->transfers[i]);
stream_data->transfers[i] = NULL;
+ stream_data->transfer_status[i] = TRANSFER_UNINITIALIZED;
+ stream_data->num_avail_transfers--;
}
}
status = BLADERF_ERR_MEM;
break;
+ } else {
+ stream_data->transfer_status[i] = TRANSFER_AVAIL;
+ stream_data->num_avail_transfers++;
}
}
+error:
+ if (status != 0) {
+ free(stream_data->transfer_status);
+ free(stream_data->transfers);
+ free(stream_data);
+ stream->backend_data = NULL;
+ }
+
return status;
}
+#ifndef LIBUSB_HANDLE_EVENTS_TIMEOUT_NSEC
+# define LIBUSB_HANDLE_EVENTS_TIMEOUT_NSEC (15 * 1000)
+#endif
+
static int lusb_stream(struct bladerf_stream *stream, bladerf_module module)
{
size_t i;
@@ -2214,10 +2303,7 @@ static int lusb_stream(struct bladerf_stream *stream, bladerf_module module)
struct bladerf *dev = stream->dev;
struct bladerf_lusb *lusb = dev->backend;
struct lusb_stream_data *stream_data = stream->backend_data;
- struct timeval tv = { 5, 0 };
- const size_t buffer_size = c16_samples_to_bytes(stream->samples_per_buffer);
-
- assert(buffer_size <= INT_MAX);
+ struct timeval tv = { 0, LIBUSB_HANDLE_EVENTS_TIMEOUT_NSEC };
/* Currently unused, so zero it out for a sanity check when debugging */
memset(&metadata, 0, sizeof(metadata));
@@ -2225,21 +2311,19 @@ static int lusb_stream(struct bladerf_stream *stream, bladerf_module module)
pthread_mutex_lock(&stream->lock);
/* Set up initial set of buffers */
- for( i = 0; i < stream->num_transfers; i++ ) {
- if( module == BLADERF_MODULE_TX ) {
- buffer = stream->cb(
- dev,
- stream,
- &metadata,
- NULL,
- stream->samples_per_buffer,
- stream->user_data
- );
+ for (i = 0; i < stream_data->num_transfers; i++) {
+ if (module == BLADERF_MODULE_TX) {
+ buffer = stream->cb(dev,
+ stream,
+ &metadata,
+ NULL,
+ stream->samples_per_buffer,
+ stream->user_data);
- if (!buffer) {
+ if (buffer == BLADERF_STREAM_SHUTDOWN) {
/* If we have transfers in flight and the user prematurely
* cancels the stream, we'll start shutting down */
- if (stream_data->active_transfers > 0) {
+ if (stream_data->num_avail_transfers != stream_data->num_transfers) {
stream->state = STREAM_SHUTTING_DOWN;
} else {
/* No transfers have been shipped out yet so we can
@@ -2255,75 +2339,90 @@ static int lusb_stream(struct bladerf_stream *stream, bladerf_module module)
buffer = stream->buffers[i];
}
- /* Fill and submit the bulk transfer request */
- libusb_fill_bulk_transfer(
- stream_data->transfers[i],
- lusb->handle,
- module == BLADERF_MODULE_TX ? EP_OUT(0x01) : EP_IN(0x01),
- buffer,
- (int)buffer_size,
- lusb_stream_cb,
- stream,
- dev->transfer_timeout[module]
- );
+ if (buffer != BLADERF_STREAM_NO_DATA) {
+ status = submit_transfer(stream, buffer);
- log_verbose("Initial transfer with buffer: %p (i=%" PRIu64 ")\n",
- buffer, (uint64_t)i);
-
- stream_data->active_transfers++;
- status = libusb_submit_transfer(stream_data->transfers[i]);
-
- /* If we failed to submit any transfers, cancel everything in flight.
- * We'll leave the stream in the running state so we can have
- * libusb fire off callbacks with the cancelled status*/
- if (status < 0) {
- stream->error_code = error_libusb2bladerf(status);
-
- /* Note: <= 0 not appropriate as we're dealing
- * with an unsigned index */
- while (i > 0) {
- if (--i) {
- status = libusb_cancel_transfer(stream_data->transfers[i]);
- if (status < 0) {
- log_error("Failed to cancel transfer " PRIu64 ": %s\n",
- (uint64_t)i, libusb_error_name(status));
- }
- }
+ /* If we failed to submit any transfers, cancel everything in
+ * flight. We'll leave the stream in the running state so we can
+ * have libusb fire off callbacks with the cancelled status*/
+ if (status < 0) {
+ stream->error_code = status;
+ cancel_all_transfers(stream);
}
}
}
+ pthread_mutex_unlock(&stream->lock);
/* This loop is required so libusb can do callbacks and whatnot */
- while( stream->state != STREAM_DONE ) {
- status = libusb_handle_events_timeout_completed(lusb->context, &tv,
- &stream_data->libusb_completed);
+ while (stream->state != STREAM_DONE) {
+ status = libusb_handle_events_timeout(lusb->context, &tv);
if (status < 0 && status != LIBUSB_ERROR_INTERRUPTED) {
log_warning("unexpected value from events processing: "
- "%d: %s\n", status, libusb_error_name(status));
+ "%d: %s\n", status, libusb_error_name(status));
}
}
- pthread_mutex_unlock(&stream->lock);
- return 0;
+ return error_libusb2bladerf(status);
}
+/* The top-level code will have aquired the stream->lock for us */
+int lusb_submit_stream_buffer(struct bladerf_stream *stream,
+ void *buffer,
+ unsigned int timeout_ms)
+{
+ int status;
+ struct lusb_stream_data *stream_data = stream->backend_data;
+ struct timespec timeout_abs;
+
+ if (buffer == BLADERF_STREAM_SHUTDOWN) {
+ if (stream_data->num_avail_transfers == stream_data->num_transfers) {
+ stream->state = STREAM_DONE;
+ } else {
+ stream->state = STREAM_SHUTTING_DOWN;
+ }
+
+ return 0;
+ }
+
+
+ status = populate_abs_timeout(&timeout_abs, timeout_ms);
+ if (status != 0) {
+ return BLADERF_ERR_UNEXPECTED;
+ }
+
+ while (stream_data->num_avail_transfers == 0 && status == 0) {
+ status = pthread_cond_timedwait(&stream->can_submit_buffer,
+ &stream->lock,
+ &timeout_abs);
+ }
+
+ if (status == ETIMEDOUT) {
+ return BLADERF_ERR_TIMEOUT;
+ } else if (status != 0) {
+ return BLADERF_ERR_UNEXPECTED;
+ } else {
+ return submit_transfer(stream, buffer);
+ }
+}
+
+
void lusb_deinit_stream(struct bladerf_stream *stream)
{
size_t i;
struct lusb_stream_data *stream_data = stream->backend_data;
- for (i = 0; i < stream->num_transfers; i++ ) {
+ for (i = 0; i < stream_data->num_transfers; i++) {
libusb_free_transfer(stream_data->transfers[i]);
stream_data->transfers[i] = NULL;
+ stream_data->transfer_status[i] = TRANSFER_UNINITIALIZED;
}
free(stream_data->transfers);
+ free(stream_data->transfer_status);
free(stream->backend_data);
stream->backend_data = NULL;
-
- return;
}
int lusb_probe(struct bladerf_devinfo_list *info_list)
@@ -2426,5 +2525,6 @@ const struct bladerf_fn bladerf_lusb_fn = {
FIELD_INIT(.init_stream, lusb_stream_init),
FIELD_INIT(.stream, lusb_stream),
+ FIELD_INIT(.submit_stream_buffer, lusb_submit_stream_buffer),
FIELD_INIT(.deinit_stream, lusb_deinit_stream),
};
diff --git a/host/libraries/libbladeRF/src/bladerf.c b/host/libraries/libbladeRF/src/bladerf.c
index 4680b869..e3393d14 100644
--- a/host/libraries/libbladeRF/src/bladerf.c
+++ b/host/libraries/libbladeRF/src/bladerf.c
@@ -575,6 +575,13 @@ int bladerf_stream(struct bladerf_stream *stream, bladerf_module module)
return async_run_stream(stream, module);
}
+int bladerf_submit_stream_buffer(struct bladerf_stream *stream,
+ void *buffer,
+ unsigned int timeout_ms)
+{
+ return async_submit_stream_buffer(stream, buffer, timeout_ms);
+}
+
void bladerf_deinit_stream(struct bladerf_stream *stream)
{
return async_deinit_stream(stream);
diff --git a/host/libraries/libbladeRF/src/bladerf_priv.h b/host/libraries/libbladeRF/src/bladerf_priv.h
index 9c971997..ab330f1f 100644
--- a/host/libraries/libbladeRF/src/bladerf_priv.h
+++ b/host/libraries/libbladeRF/src/bladerf_priv.h
@@ -128,8 +128,10 @@ struct bladerf_fn {
/* Sample stream */
int (*enable_module)(struct bladerf *dev, bladerf_module m, bool enable);
- int (*init_stream)(struct bladerf_stream *stream);
+ int (*init_stream)(struct bladerf_stream *stream, size_t num_transfers);
int (*stream)(struct bladerf_stream *stream, bladerf_module module);
+ int (*submit_stream_buffer)(struct bladerf_stream *stream, void *buffer,
+ unsigned int timeout_ms);
void (*deinit_stream)(struct bladerf_stream *stream);
};
diff --git a/host/libraries/libbladeRF/src/sync.c b/host/libraries/libbladeRF/src/sync.c
index 81d7b29f..953e4f1a 100644
--- a/host/libraries/libbladeRF/src/sync.c
+++ b/host/libraries/libbladeRF/src/sync.c
@@ -95,6 +95,9 @@ int sync_init(struct bladerf *dev,
}
sync->dev = dev;
+ sync->state = SYNC_STATE_CHECK_AND_START_WORKER;
+
+ sync->buf_mgmt.num_buffers = num_buffers;
sync->stream_config.module = module;
sync->stream_config.format = format;
@@ -103,11 +106,9 @@ int sync_init(struct bladerf *dev,
sync->stream_config.timeout_ms = stream_timeout;
sync->stream_config.bytes_per_sample = bytes_per_sample;
- sync->buf_mgmt.num_buffers = num_buffers;
pthread_mutex_init(&sync->buf_mgmt.lock, NULL);
- pthread_cond_init(&sync->buf_mgmt.buf_consumed, NULL);
- pthread_cond_init(&sync->buf_mgmt.buf_produced, NULL);
+ pthread_cond_init(&sync->buf_mgmt.buf_ready, NULL);
sync->buf_mgmt.status = (sync_buffer_status*) malloc(num_buffers * sizeof(sync_buffer_status));
if (sync->buf_mgmt.status == NULL) {
@@ -161,13 +162,16 @@ void sync_deinit(struct bladerf_sync *sync)
case BLADERF_MODULE_RX:
sync_worker_deinit(sync->worker,
&sync->buf_mgmt.lock,
- &sync->buf_mgmt.buf_consumed);
+ &sync->buf_mgmt.buf_ready);
break;
case BLADERF_MODULE_TX:
+ bladerf_submit_stream_buffer(sync->worker->stream,
+ BLADERF_STREAM_SHUTDOWN, 0);
+
sync_worker_deinit(sync->worker,
&sync->buf_mgmt.lock,
- &sync->buf_mgmt.buf_produced);
+ &sync->buf_mgmt.buf_ready);
break;
default:
@@ -181,16 +185,15 @@ void sync_deinit(struct bladerf_sync *sync)
}
}
-static int check_worker(struct bladerf_sync *s)
+static int check_and_start_worker(struct bladerf_sync *s)
{
sync_worker_state state;
int status = 0;
- const int max_retries = 8;
+ const int max_retries = 4;
int retry = 0;
do {
state = sync_worker_get_state(s->worker);
-
if (state != SYNC_WORKER_STATE_RUNNING) {
/* We need to start up the worker */
@@ -222,90 +225,239 @@ static int check_worker(struct bladerf_sync *s)
return status;
}
+static int wait_for_buffer(struct buffer_mgmt *b, unsigned int timeout_ms,
+ const char *dbg_name, unsigned int dbg_idx)
+{
+ int status;
+ struct timespec timeout;
+
+ if (timeout_ms == 0) {
+ log_verbose("%s: Infinite wait for [%d] to fill.\n", dbg_name, dbg_idx);
+ status = pthread_cond_wait(&b->buf_ready, &b->lock);
+ } else {
+ log_verbose("%s: Timed wait for [%d] to fill.\n", dbg_name, dbg_idx);
+ status = populate_abs_timeout(&timeout, timeout_ms);
+ if (status == 0) {
+ status = pthread_cond_timedwait(&b->buf_ready, &b->lock, &timeout);
+ }
+ }
+
+ if (status == -ETIMEDOUT) {
+ status = BLADERF_ERR_TIMEOUT;
+ } else if (status != 0) {
+ status = BLADERF_ERR_UNEXPECTED;
+ }
+
+ return status;
+}
+
int sync_rx(struct bladerf *dev, void *samples, unsigned num_samples,
struct bladerf_metadata *metadata, unsigned int timeout_ms)
{
- int status;
struct bladerf_sync *s = dev->sync[BLADERF_MODULE_RX];
struct buffer_mgmt *b = &s->buf_mgmt;
- unsigned int samples_returned = 0;
- struct timespec timeout_abs;
- if (s == NULL) {
+ int status = 0;
+ unsigned int samples_returned = 0;
+ uint8_t *samples_dest = (uint8_t*)samples;
+ uint8_t *buf_src;
+ unsigned int samples_to_copy;
+ const unsigned int samples_per_buffer = s->stream_config.samples_per_buffer;
+
+
+ if (s == NULL || samples == NULL) {
return BLADERF_ERR_INVAL;
}
- /* Check if our worker thread is running, and start it if it's not */
- status = check_worker(s);
+ while (samples_returned < num_samples && status == 0) {
- while(status == 0 && samples_returned < num_samples) {
- pthread_mutex_lock(&b->lock);
+ switch (s->state) {
+ case SYNC_STATE_CHECK_AND_START_WORKER:
+ status = check_and_start_worker(s);
+ if (status == 0) {
+ s->state = SYNC_STATE_WAIT_FOR_BUFFER;
+ }
+ break;
- /* If we have a fresh buffer, mark that we're consuming it */
- if (b->status[b->cons_i] == SYNC_BUFFER_FULL) {
- b->status[b->cons_i] = SYNC_BUFFER_PARTIAL;
- b->partial_off = 0;
- }
+ case SYNC_STATE_WAIT_FOR_BUFFER:
+ pthread_mutex_lock(&b->lock);
- if (b->status[b->cons_i] == SYNC_BUFFER_PARTIAL) {
- /* We currently have a buffer to remove samples from */
- uint8_t *samples_dest = (uint8_t*)samples;
- uint8_t *buf_src = (uint8_t*)b->buffers[b->cons_i];
- const unsigned int samples_to_copy =
- uint_min(num_samples - samples_returned,
- s->stream_config.samples_per_buffer - b->partial_off);
+ /* Check the buffer state, as the worker may have produced one
+ * since we last queried the status */
+ if (b->status[b->cons_i] == SYNC_BUFFER_FULL) {
+ s->state = SYNC_STATE_BUFFER_READY;
+ } else {
+ status = wait_for_buffer(b, timeout_ms,
+ __FUNCTION__, b->cons_i);
+ }
- memcpy(samples_dest + samples2bytes(s, samples_returned),
- buf_src + samples2bytes(s, b->partial_off),
- samples2bytes(s, samples_to_copy));
+ pthread_mutex_unlock(&b->lock);
+ break;
- b->partial_off += samples_to_copy;
- samples_returned += samples_to_copy;
+ case SYNC_STATE_BUFFER_READY:
+ pthread_mutex_lock(&b->lock);
+ b->status[b->cons_i] = SYNC_BUFFER_PARTIAL;
+ b->partial_off = 0;
+ pthread_mutex_unlock(&b->lock);
- log_verbose("%s: Provided %u samples to caller\n",
- __FUNCTION__, samples_to_copy);
+ s->state= SYNC_STATE_USING_BUFFER;
+ break;
- /* We've finished consuming this buffer and can start looking
- * for available samples in the next buffer */
- if (b->partial_off >= s->stream_config.samples_per_buffer) {
- log_verbose("%s: Marking buf[%u] empty.\n",
- __FUNCTION__, b->cons_i);
+ case SYNC_STATE_USING_BUFFER:
+ pthread_mutex_lock(&b->lock);
- b->status[b->cons_i] = SYNC_BUFFER_EMPTY;
- b->cons_i = (b->cons_i + 1) % b->num_buffers;
- pthread_cond_signal(&b->buf_consumed);
- }
+ buf_src = (uint8_t*)b->buffers[b->cons_i];
- } else {
- /* Need to wait for a buffer to become available */
- if (timeout_ms == 0) {
- status = pthread_cond_wait(&b->buf_produced, &b->lock);
- log_verbose("%s: Infinite wait for [%d] to fill.\n",
- __FUNCTION__, b->cons_i);
- } else {
- log_verbose("%s: Timed wait for [%d] to fill.\n",
- __FUNCTION__, b->cons_i);
- status = populate_abs_timeout(&timeout_abs, timeout_ms);
- if (status == 0) {
- status = pthread_cond_timedwait(&b->buf_produced, &b->lock,
- &timeout_abs);
- }
- }
- }
- pthread_mutex_unlock(&b->lock);
+ samples_to_copy = uint_min(num_samples - samples_returned,
+ samples_per_buffer - b->partial_off);
- if (status == -ETIMEDOUT) {
- status = BLADERF_ERR_TIMEOUT;
- } else if (status != 0) {
- status = BLADERF_ERR_UNEXPECTED;
- } else {
- status = check_worker(s);
+ memcpy(samples_dest + samples2bytes(s, samples_returned),
+ buf_src + samples2bytes(s, b->partial_off),
+ samples2bytes(s, samples_to_copy));
+
+ b->partial_off += samples_to_copy;
+ samples_returned += samples_to_copy;
+
+ log_verbose("%s: Provided %u samples to caller\n",
+ __FUNCTION__, samples_to_copy);
+
+ /* We've finished consuming this buffer and can start looking
+ * for available samples in the next buffer */
+ if (b->partial_off >= samples_per_buffer) {
+
+ /* Check for symptom of out-of-bounds accesses */
+ assert(b->partial_off == samples_per_buffer);
+
+ log_verbose("%s: Marking buf[%u] empty.\n",
+ __FUNCTION__, b->cons_i);
+
+ b->status[b->cons_i] = SYNC_BUFFER_EMPTY;
+ b->cons_i = (b->cons_i + 1) % b->num_buffers;
+
+ /* Go handle the next buffer, if we have one available.
+ * Otherwise, check up on the worker's state and restart
+ * it if needed. */
+ if (b->status[b->cons_i] == SYNC_BUFFER_FULL) {
+ s->state = SYNC_STATE_BUFFER_READY;
+ } else {
+ s->state = SYNC_STATE_CHECK_AND_START_WORKER;
+ }
+ }
+
+ pthread_mutex_unlock(&b->lock);
+ break;
}
}
return status;
}
+int sync_tx(struct bladerf *dev, void *samples, unsigned int num_samples,
+ struct bladerf_metadata *metadata, unsigned int timeout_ms)
+{
+ struct bladerf_sync *s = dev->sync[BLADERF_MODULE_TX];
+ struct buffer_mgmt *b = &s->buf_mgmt;
+
+ int status = 0;
+ unsigned int samples_written = 0;
+ unsigned int samples_to_copy;
+ const unsigned int samples_per_buffer = s->stream_config.samples_per_buffer;
+ uint8_t *samples_src = (uint8_t*)samples;
+ uint8_t *buf_dest;
+
+ if (s == NULL || samples == NULL) {
+ return BLADERF_ERR_INVAL;
+ }
+
+ while (status == 0 && samples_written < num_samples) {
+
+ switch (s->state) {
+
+ case SYNC_STATE_CHECK_AND_START_WORKER:
+ status = check_and_start_worker(s);
+ if (status == 0) {
+ s->state = SYNC_STATE_WAIT_FOR_BUFFER;
+ }
+ break;
+
+ case SYNC_STATE_WAIT_FOR_BUFFER:
+ pthread_mutex_lock(&b->lock);
+
+ /* Check the buffer state, as the worker may have consumed one
+ * since we last queried the status */
+ if (b->status[b->prod_i] == SYNC_BUFFER_EMPTY) {
+ s->state = SYNC_STATE_BUFFER_READY;
+ } else {
+ status = wait_for_buffer(b, timeout_ms,
+ __FUNCTION__, b->prod_i);
+ }
+
+ pthread_mutex_unlock(&b->lock);
+ break;
+
+ case SYNC_STATE_BUFFER_READY:
+ pthread_mutex_lock(&b->lock);
+ b->status[b->prod_i] = SYNC_BUFFER_PARTIAL;
+ b->partial_off = 0;
+ pthread_mutex_unlock(&b->lock);
+
+ s->state= SYNC_STATE_USING_BUFFER;
+ break;
+
+
+ case SYNC_STATE_USING_BUFFER:
+ pthread_mutex_lock(&b->lock);
+
+ buf_dest = (uint8_t*)b->buffers[b->prod_i];
+ samples_to_copy = uint_min(num_samples - samples_written,
+ samples_per_buffer - b->partial_off);
+
+ memcpy(buf_dest + samples2bytes(s, b->partial_off),
+ samples_src + samples2bytes(s, samples_written),
+ samples2bytes(s, samples_to_copy));
+
+ b->partial_off += samples_to_copy;
+ samples_written += samples_to_copy;
+
+ log_verbose("%s: Buffered %u samples from caller\n",
+ __FUNCTION__, samples_to_copy);
+
+ if (b->partial_off >= samples_per_buffer) {
+ /* Check for symptom of out-of-bounds accesses */
+ assert(b->partial_off == samples_per_buffer);
+
+ log_verbose("%s: Marking buf[%u] full\n",
+ __FUNCTION__, b->prod_i);
+
+ b->status[b->prod_i] = SYNC_BUFFER_IN_FLIGHT;
+ status = bladerf_submit_stream_buffer(
+ s->worker->stream,
+ buf_dest,
+ s->stream_config.timeout_ms);
+
+ if (status == 0) {
+ b->prod_i = (b->prod_i + 1) % b->num_buffers;
+
+ /* Go handle the next buffer, if we have one available.
+ * Otherwise, check up on the worker's state and restart
+ * it if needed. */
+ if (b->status[b->prod_i] == SYNC_BUFFER_EMPTY) {
+ s->state = SYNC_STATE_BUFFER_READY;
+ } else {
+ s->state = SYNC_STATE_CHECK_AND_START_WORKER;
+ }
+ }
+ }
+
+ pthread_mutex_unlock(&b->lock);
+ break;
+ }
+ }
+
+ return status;
+}
+
+#if 0
int sync_tx(struct bladerf *dev, void *samples, unsigned int num_samples,
struct bladerf_metadata *metadata, unsigned int timeout_ms)
{
@@ -320,7 +472,7 @@ int sync_tx(struct bladerf *dev, void *samples, unsigned int num_samples,
}
/* Check that our work thread is running, and start it if it's not */
- status = check_worker(s);
+ status = check_and_start_worker(s);
while (status == 0 && samples_written < num_samples) {
pthread_mutex_lock(&b->lock);
@@ -353,9 +505,15 @@ int sync_tx(struct bladerf *dev, void *samples, unsigned int num_samples,
log_verbose("%s: Marking buf[%u] full\n",
__FUNCTION__, b->prod_i);
- b->status[b->prod_i] = SYNC_BUFFER_FULL;
- b->prod_i = (b->prod_i + 1) % b->num_buffers;
- pthread_cond_signal(&b->buf_produced);
+ b->status[b->prod_i] = SYNC_BUFFER_IN_FLIGHT;;
+ status = bladerf_submit_stream_buffer(
+ s->worker->stream,
+ buf_dest,
+ s->stream_config.timeout_ms);
+
+ if (status != 0) {
+ b->prod_i = (b->prod_i + 1) % b->num_buffers;
+ }
}
} else {
@@ -363,31 +521,34 @@ int sync_tx(struct bladerf *dev, void *samples, unsigned int num_samples,
if (timeout_ms == 0) {
log_verbose("%s: Infinite wait for [%d] to empty.\n",
__FUNCTION__, b->prod_i);
- status = pthread_cond_wait(&b->buf_consumed, &b->lock);
+ status = pthread_cond_wait(&b->buf_ready, &b->lock);
} else {
status = populate_abs_timeout(&timeout_abs, timeout_ms);
if (status == 0) {
log_verbose("%s: Timed wait for [%d] to empty.\n",
__FUNCTION__, b->prod_i);
- status = pthread_cond_timedwait(&b->buf_consumed, &b->lock,
+ status = pthread_cond_timedwait(&b->buf_ready, &b->lock,
&timeout_abs);
}
}
+
+ if (status == ETIMEDOUT) {
+ status = BLADERF_ERR_TIMEOUT;
+ } else if (status != 0) {
+ status = BLADERF_ERR_UNEXPECTED;
+ }
}
pthread_mutex_unlock(&b->lock);
- if (status == ETIMEDOUT) {
- status = BLADERF_ERR_TIMEOUT;
- } else if (status != 0) {
- status = BLADERF_ERR_UNEXPECTED;
- } else {
- status = check_worker(s);
+ if (status == 0) {
+ status = check_and_start_worker(s);
}
}
return status;
}
+#endif
unsigned int sync_buf2idx(struct buffer_mgmt *b, void *addr)
{
diff --git a/host/libraries/libbladeRF/src/sync.h b/host/libraries/libbladeRF/src/sync.h
index 9ec11be1..2e8283e6 100644
--- a/host/libraries/libbladeRF/src/sync.h
+++ b/host/libraries/libbladeRF/src/sync.h
@@ -58,15 +58,24 @@ struct buffer_mgmt {
unsigned int partial_off; /**< Current index into partial buffer */
pthread_mutex_t lock;
- pthread_cond_t buf_consumed;
- pthread_cond_t buf_produced;
+ pthread_cond_t buf_ready; /** Buffer produced by RX callback, or
+ * buffer emptied by TX callback */
};
+/* State of API-side sync inferface */
+typedef enum {
+ SYNC_STATE_CHECK_AND_START_WORKER,
+ SYNC_STATE_WAIT_FOR_BUFFER,
+ SYNC_STATE_BUFFER_READY,
+ SYNC_STATE_USING_BUFFER
+} sync_state;
+
struct bladerf_sync {
struct bladerf *dev;
+ sync_state state;
struct buffer_mgmt buf_mgmt;
- struct sync_worker *worker;
struct stream_config stream_config;
+ struct sync_worker *worker;
};
/**
diff --git a/host/libraries/libbladeRF/src/sync_worker.c b/host/libraries/libbladeRF/src/sync_worker.c
index b2fe42b9..152c1755 100644
--- a/host/libraries/libbladeRF/src/sync_worker.c
+++ b/host/libraries/libbladeRF/src/sync_worker.c
@@ -49,7 +49,6 @@ static void *rx_callback(struct bladerf *dev,
size_t num_samples,
void *user_data)
{
- int status = 0;
unsigned int requests; /* Pending requests */
unsigned int next_idx;
unsigned int samples_idx;
@@ -74,82 +73,31 @@ static void *rx_callback(struct bladerf *dev,
pthread_mutex_lock(&b->lock);
- /* Mark this buffer as full */
+ /* Get the index of the buffer we've been notified about having been
+ * completed */
samples_idx = sync_buf2idx(b, samples);
- assert(b->status[samples_idx] == SYNC_BUFFER_IN_FLIGHT);
- b->status[samples_idx] = SYNC_BUFFER_FULL;
- /* Update to our next buffer */
- next_idx = b->prod_i;
- next_buf = b->buffers[next_idx];
-
- /* Wait for a buffer to free up, if needed */
- if (s->stream_config.timeout_ms == 0) {
- while (status == 0 &&
- b->status[next_idx] != SYNC_BUFFER_EMPTY &&
- (requests & SYNC_WORKER_STOP) == 0) {
-
- log_verbose("%s worker: Infinite wait for buf[%u] to empty\n",
- MODULE_STR(s), next_idx);
-
- status = pthread_cond_wait(&b->buf_consumed, &b->lock);
-
- /* Fetch requests that we might have gotten while sleeping */
- pthread_mutex_lock(&w->request_lock);
- requests = w->requests;
- pthread_mutex_unlock(&w->request_lock);
- }
- } else {
- struct timespec timeout_abs;
- status = populate_abs_timeout(&timeout_abs,
- s->stream_config.timeout_ms);
-
- while (status == 0 &&
- b->status[next_idx] != SYNC_BUFFER_EMPTY &&
- (requests & SYNC_WORKER_STOP) == 0) {
-
- log_verbose("%s worker: Timed wait for buf[%u] to empty\n",
- MODULE_STR(s), next_idx);
-
- status = pthread_cond_timedwait(&b->buf_consumed, &b->lock,
- &timeout_abs);
-
- pthread_mutex_lock(&w->request_lock);
- requests = w->requests;
- pthread_mutex_unlock(&w->request_lock);
- }
- }
-
- if (requests & SYNC_WORKER_STOP) {
- next_buf = NULL;
- log_verbose("%s worker: Got STOP request while waiting in callback. "
- "Ending stream.\n", MODULE_STR(s));
- } else if (status == 0) {
- b->status[next_idx] = SYNC_BUFFER_IN_FLIGHT;
+ if (b->status[b->prod_i] == SYNC_BUFFER_EMPTY) {
+ next_idx = b->prod_i;
b->prod_i = (next_idx + 1) % b->num_buffers;
- pthread_cond_signal(&b->buf_produced);
- }
-
-
- if (status == 0) {
- log_verbose("%s worker: buf[%u] = full, buf[%u] = in_flight\n",
- MODULE_STR(s), samples_idx, next_idx);
+ b->status[samples_idx] = SYNC_BUFFER_FULL;
+ b->status[next_idx] = SYNC_BUFFER_IN_FLIGHT;
+ pthread_cond_signal(&b->buf_ready);
} else {
- next_buf = NULL;
- if (status == ETIMEDOUT) {
- log_debug("%s worker: Timed out. Shutting down.\n",
- MODULE_STR(s), status);
- } else {
- log_debug("%s worker: Unexpected error (%d), shutting down.\n",
- MODULE_STR(s), status);
- }
- /* The API caller may be blocked waiting on us to produce a buffer,
- * so we need to wake them and let them figure out what to do */
- pthread_cond_signal(&b->buf_produced);
+ /* TODO propgate back the RX Overrun to the sync_rx() caller */
+ log_warning("RX overrun @ buffer %u\r\n", samples_idx);
+
+ next_idx = samples_idx;
+ b->status[samples_idx] = SYNC_BUFFER_IN_FLIGHT;
}
+ next_buf = b->buffers[next_idx];
+
+ log_verbose("%s worker: buf[%u] = full, buf[%u] = in_flight\n",
+ MODULE_STR(s), samples_idx, next_idx);
+
pthread_mutex_unlock(&b->lock);
return next_buf;
}
@@ -161,11 +109,8 @@ static void *tx_callback(struct bladerf *dev,
size_t num_samples,
void *user_data)
{
- int status = 0;
unsigned int requests; /* Pending requests */
unsigned int completed_idx; /* Index of completed buffer */
- unsigned int next_idx; /* Index of next buffer to submit */
- void *next_buf = NULL; /* Next buffer to submit for transmission */
struct bladerf_sync *s = (struct bladerf_sync *)user_data;
struct sync_worker *w = s->worker;
@@ -180,98 +125,29 @@ static void *tx_callback(struct bladerf *dev,
if (requests & SYNC_WORKER_STOP) {
log_verbose("%s worker: Got STOP request upon entering callback. "
- "Ending stream.\n", MODULE_STR(s));
+ "Ending stream.\r\n", MODULE_STR(s));
return NULL;
}
- pthread_mutex_lock(&b->lock);
/* Mark the last transfer as being completed. Note that the first
* callbacks we get have samples=NULL */
if (samples != NULL) {
+
+ pthread_mutex_lock(&b->lock);
+
completed_idx = sync_buf2idx(b, samples);
assert(b->status[completed_idx] == SYNC_BUFFER_IN_FLIGHT);
b->status[completed_idx] = SYNC_BUFFER_EMPTY;
- pthread_cond_signal(&b->buf_consumed);
- } else {
- completed_idx = UINT_MAX; /* Just for debug purposes */
+
+ pthread_cond_signal(&b->buf_ready);
+ pthread_mutex_unlock(&b->lock);
+
+ log_verbose("%s worker: Buffer %u emptied.\r\n",
+ MODULE_STR(s), completed_idx);
}
- /* Wait for a full buffer to become available for consumption */
- if (s->stream_config.timeout_ms == 0) {
- while (status == 0 &&
- b->status[b->cons_i] != SYNC_BUFFER_FULL &&
- (requests & SYNC_WORKER_STOP) == 0) {
-
- log_verbose("%s worker: Infinite wait for buf[%u] to fill\n",
- MODULE_STR(s), b->cons_i);
-
- status = pthread_cond_wait(&b->buf_produced, &b->lock);
-
- /* Fetch requests that we might have gotten while sleeping */
- pthread_mutex_lock(&w->request_lock);
- requests = w->requests;
- pthread_mutex_unlock(&w->request_lock);
-
- log_verbose("%s worker: Woke, requests are now: 0x%08x\n",
- MODULE_STR(s), requests);
- }
- } else {
- struct timespec timeout_abs;
- status = populate_abs_timeout(&timeout_abs,
- s->stream_config.timeout_ms);
-
- while (status == 0 &&
- b->status[b->cons_i] != SYNC_BUFFER_FULL &&
- (requests & SYNC_WORKER_STOP) == 0) {
-
- log_verbose("%s worker: Timed wait for buf[%u] to fill\n",
- MODULE_STR(s), b->cons_i);
-
- status = pthread_cond_timedwait(&b->buf_produced, &b->lock,
- &timeout_abs);
-
- pthread_mutex_lock(&w->request_lock);
- requests = w->requests;
- pthread_mutex_unlock(&w->request_lock);
- }
- }
-
- /* Mark this buffer as being sent and update our reference to the
- * next buffer we'll use */
- if (status == 0) {
- next_idx = b->cons_i;
- b->status[next_idx] = SYNC_BUFFER_IN_FLIGHT;
- next_buf = b->buffers[next_idx];
- b->cons_i = (b->cons_i + 1) % b->num_buffers;
- }
-
-
- if (requests & SYNC_WORKER_STOP) {
- next_buf = NULL;
- log_verbose("%s worker: Got STOP request while waiting in callback. "
- "Ending stream.\n", MODULE_STR(s));
- } else if (status == 0) {
- log_verbose("%s worker: buf[%u] = in_flight, buf[%u] = empty\n",
- MODULE_STR(s), next_idx, completed_idx);
- } else {
- next_buf = NULL;
-
- if (status == ETIMEDOUT) {
- log_debug("%s worker: Timed out. Shutting down.\n",
- MODULE_STR(s), status);
- } else {
- log_debug("%s worker: Unexpected error (%d), shutting down.\n",
- MODULE_STR(s), status);
- }
-
- /* The API caller may be blocked waiting on us to consume a buffer,
- * so we need to wake them and let them figure out what to do */
- pthread_cond_signal(&b->buf_consumed);
- }
-
- pthread_mutex_unlock(&b->lock);
- return next_buf;
+ return BLADERF_STREAM_NO_DATA;
}
int sync_worker_init(struct bladerf_sync *s)
@@ -520,7 +396,7 @@ static void exec_running_state(struct bladerf_sync *s)
}
}
- pthread_cond_signal(&s->buf_mgmt.buf_consumed);
+ pthread_cond_signal(&s->buf_mgmt.buf_ready);
pthread_mutex_unlock(&s->buf_mgmt.lock);
} /* TODO similar handling for RX */