{
check(sr_init(&_structure));
- struct sr_dev_driver **driver_list = sr_driver_list();
+ struct sr_dev_driver **driver_list = sr_driver_list(_structure);
if (driver_list)
for (int i = 0; driver_list[i]; i++)
_drivers[driver_list[i]->name] =
/*--- hwdriver.c ------------------------------------------------------------*/
-SR_API struct sr_dev_driver **sr_driver_list(void);
+SR_API struct sr_dev_driver **sr_driver_list(const struct sr_context *ctx);
SR_API int sr_driver_init(struct sr_context *ctx,
struct sr_dev_driver *driver);
SR_API GSList *sr_driver_scan(struct sr_dev_driver *driver, GSList *options);
/**
* Sanity-check all libsigrok drivers.
*
+ * @param[in] ctx Pointer to a libsigrok context struct. Must not be NULL.
+ *
* @retval SR_OK All drivers are OK
* @retval SR_ERR One or more drivers have issues.
+ * @retval SR_ERR_ARG Invalid argument.
*/
-static int sanity_check_all_drivers(void)
+static int sanity_check_all_drivers(const struct sr_context *ctx)
{
int i, errors, ret = SR_OK;
struct sr_dev_driver **drivers;
const char *d;
+ if (!ctx)
+ return SR_ERR_ARG;
+
sr_spew("Sanity-checking all drivers.");
- drivers = sr_driver_list();
+ drivers = sr_driver_list(ctx);
for (i = 0; drivers[i]; i++) {
errors = 0;
{
int ret = SR_ERR;
struct sr_context *context;
+ struct sr_dev_driver ***lists, **drivers;
+ GArray *array;
if (!ctx) {
sr_err("%s(): libsigrok context was NULL.", __func__);
return SR_ERR;
}
- if (sanity_check_all_drivers() < 0) {
+ context = g_malloc0(sizeof(struct sr_context));
+
+ /* Generate ctx->driver_list at runtime. */
+ array = g_array_new(TRUE, FALSE, sizeof(struct sr_dev_driver *));
+ for (lists = drivers_lists; *lists; lists++)
+ for (drivers = *lists; *drivers; drivers++)
+ g_array_append_val(array, *drivers);
+ context->driver_list = (struct sr_dev_driver **)array->data;
+ g_array_free(array, FALSE);
+
+ if (sanity_check_all_drivers(context) < 0) {
sr_err("Internal driver error(s), aborting.");
return ret;
}
return ret;
}
- /* + 1 to handle when struct sr_context has no members. */
- context = g_malloc0(sizeof(struct sr_context) + 1);
-
#ifdef HAVE_LIBUSB_1_0
ret = libusb_init(&context->libusb_ctx);
if (LIBUSB_SUCCESS != ret) {
return SR_ERR;
}
- sr_hw_cleanup_all();
+ sr_hw_cleanup_all(ctx);
#ifdef HAVE_LIBUSB_1_0
libusb_exit(ctx->libusb_ctx);
#endif
- g_free(sr_driver_list());
+ g_free(sr_driver_list(ctx));
g_free(ctx);
return SR_OK;
#define LOG_PREFIX "hwdriver"
/** @endcond */
-extern SR_PRIV struct sr_dev_driver **drivers_lists[];
-
/**
* @file
*
/**
* Return the list of supported hardware drivers.
*
- * @return Pointer to the NULL-terminated list of hardware driver pointers.
+ * @param[in] ctx Pointer to a libsigrok context struct. Must not be NULL.
+ *
+ * @retval NULL The ctx argument was NULL, or there are no supported drivers.
+ * @retval Other Pointer to the NULL-terminated list of hardware drivers.
+ * The user should NOT g_free() this list, sr_exit() will do that.
*
- * @since 0.1.0
+ * @since 0.4.0
*/
-SR_API struct sr_dev_driver **sr_driver_list(void)
+SR_API struct sr_dev_driver **sr_driver_list(const struct sr_context *ctx)
{
- static struct sr_dev_driver **combined_list = NULL;
- struct sr_dev_driver ***lists, **drivers;
- GArray *array;
-
- if (combined_list)
- return combined_list;
-
- array = g_array_new(TRUE, FALSE, sizeof(struct sr_dev_driver *));
- for (lists = drivers_lists; *lists; lists++)
- for (drivers = *lists; *drivers; drivers++)
- g_array_append_val(array, *drivers);
- combined_list = (struct sr_dev_driver **)array->data;
- g_array_free(array, FALSE);
+ if (!ctx)
+ return NULL;
- return combined_list;
+ return ctx->driver_list;
}
/**
return l;
}
-/** Call driver cleanup function for all drivers.
- * @private */
-SR_PRIV void sr_hw_cleanup_all(void)
+/**
+ * Call driver cleanup function for all drivers.
+ *
+ * @param[in] ctx Pointer to a libsigrok context struct. Must not be NULL.
+ *
+ * @private
+ */
+SR_PRIV void sr_hw_cleanup_all(const struct sr_context *ctx)
{
int i;
struct sr_dev_driver **drivers;
- drivers = sr_driver_list();
+ if (!ctx)
+ return;
+
+ drivers = sr_driver_list(ctx);
for (i = 0; drivers[i]; i++) {
if (drivers[i]->cleanup)
drivers[i]->cleanup(drivers[i]);
#endif
struct sr_context {
+ struct sr_dev_driver **driver_list;
#ifdef HAVE_LIBUSB_1_0
libusb_context *libusb_ctx;
gboolean usb_source_present;
/*--- hwdriver.c ------------------------------------------------------------*/
+extern SR_PRIV struct sr_dev_driver **drivers_lists[];
+
SR_PRIV const GVariantType *sr_variant_type_get(int datatype);
SR_PRIV int sr_variant_type_check(uint32_t key, GVariant *data);
-SR_PRIV void sr_hw_cleanup_all(void);
+SR_PRIV void sr_hw_cleanup_all(const struct sr_context *ctx);
SR_PRIV struct sr_config *sr_config_new(uint32_t key, GVariant *data);
SR_PRIV void sr_config_free(struct sr_config *src);
SR_PRIV int sr_source_remove(int fd);
{
struct sr_dev_driver **drivers;
- drivers = sr_driver_list();
+ drivers = sr_driver_list(srtest_ctx);
fail_unless(drivers != NULL, "No drivers found.");
}
END_TEST
struct sr_dev_driver **drivers, *driver = NULL;
int i;
- drivers = sr_driver_list();
+ drivers = sr_driver_list(srtest_ctx);
fail_unless(drivers != NULL, "No drivers found.");
for (i = 0; drivers[i]; i++) {
struct sr_dev_driver **drivers, *driver;
int i, ret;
- drivers = sr_driver_list();
+ drivers = sr_driver_list(srtest_ctx);
fail_unless(drivers != NULL, "No drivers found.");
for (i = 0; drivers[i]; i++) {