diff --git a/backend.c b/backend.c index bf6f1f37b..835a80650 100644 --- a/backend.c +++ b/backend.c @@ -6,66 +6,47 @@ * Author: Paul Cercueil */ +#include "dynamic.h" #include "iio-config.h" #include "iio-private.h" #include -unsigned int iio_get_backends_count(void) +unsigned int iio_get_builtin_backends_count(void) { - unsigned int count = 0; + unsigned int i, count = 0; - count += WITH_LOCAL_BACKEND; - count += WITH_XML_BACKEND; - count += WITH_NETWORK_BACKEND; - count += WITH_USB_BACKEND; - count += WITH_SERIAL_BACKEND; + for (i = 0; i < iio_backends_size; i++) + count += !!iio_backends[i]; return count; } -const char * iio_get_backend(unsigned int index) +const char * iio_get_builtin_backend(unsigned int index) { - if (WITH_LOCAL_BACKEND) { - if (index == 0) - return "local"; - index--; - } - - if (WITH_XML_BACKEND) { - if (index == 0) - return "xml"; - index--; - } - - if (WITH_NETWORK_BACKEND) { - if (index == 0) - return "ip"; - index--; - } + unsigned int i; - if (WITH_USB_BACKEND) { - if (index == 0) - return "usb"; - index--; - } + for (i = 0; i < iio_backends_size; i++) { + if (index == 0 && iio_backends[i]) + return iio_backends[i]->name; - if (WITH_SERIAL_BACKEND) { - if (index == 0) - return "serial"; - index --; + index -= !!iio_backends[i]; } return NULL; } -bool iio_has_backend(const char *backend) +bool +iio_has_backend(const struct iio_context_params *params, const char *backend) { unsigned int i; - for (i = 0; i < iio_get_backends_count(); i++) - if (strcmp(backend, iio_get_backend(i)) == 0) + for (i = 0; i < iio_get_builtin_backends_count(); i++) + if (strcmp(backend, iio_get_builtin_backend(i)) == 0) return true; + if (WITH_MODULES) + return iio_has_backend_dynamic(params, backend); + return false; } diff --git a/compat.c b/compat.c index e1f7a21be..b8777645a 100644 --- a/compat.c +++ b/compat.c @@ -53,9 +53,9 @@ struct compat { const char * (*iio_scan_get_uri)(const struct iio_scan *, size_t ); /* Backends */ - bool (*iio_has_backend)(const char *); - unsigned int (*iio_get_backends_count)(void); - const char * (*iio_get_backend)(unsigned int); + bool (*iio_has_backend)(const struct iio_context_params *, const char *); + unsigned int (*iio_get_builtin_backends_count)(void); + const char * (*iio_get_builtin_backend)(unsigned int); /* Context */ struct iio_context * (*iio_create_context)(const struct iio_context_params *, @@ -1657,17 +1657,17 @@ void iio_library_get_version(unsigned int *major, unsigned int *minor, bool iio_has_backend(const char *backend) { - return IIO_CALL(iio_has_backend)(backend); + return IIO_CALL(iio_has_backend)(NULL, backend); } unsigned int iio_get_backends_count(void) { - return IIO_CALL(iio_get_backends_count)(); + return IIO_CALL(iio_get_builtin_backends_count)(); } const char * iio_get_backend(unsigned int index) { - return IIO_CALL(iio_get_backend)(index); + return IIO_CALL(iio_get_builtin_backend)(index); } void iio_strerror(int err, char *dst, size_t len) @@ -1964,8 +1964,8 @@ static void compat_lib_init(void) FIND_SYMBOL(ctx->lib, iio_scan_get_uri); FIND_SYMBOL(ctx->lib, iio_has_backend); - FIND_SYMBOL(ctx->lib, iio_get_backends_count); - FIND_SYMBOL(ctx->lib, iio_get_backend); + FIND_SYMBOL(ctx->lib, iio_get_builtin_backends_count); + FIND_SYMBOL(ctx->lib, iio_get_builtin_backend); FIND_SYMBOL(ctx->lib, iio_create_context); FIND_SYMBOL(ctx->lib, iio_context_destroy); diff --git a/context.c b/context.c index 6bec7a6c7..f9edc3c61 100644 --- a/context.c +++ b/context.c @@ -406,7 +406,7 @@ int iio_context_set_timeout(struct iio_context *ctx, unsigned int timeout) return 0; } -const struct iio_backend *iio_backends[] = { +const struct iio_backend * const iio_backends[] = { IF_ENABLED(WITH_LOCAL_BACKEND, &iio_local_backend), IF_ENABLED(WITH_NETWORK_BACKEND && !WITH_NETWORK_BACKEND_DYNAMIC, &iio_ip_backend), diff --git a/dynamic.c b/dynamic.c index 20f53a3a5..17160a794 100644 --- a/dynamic.c +++ b/dynamic.c @@ -48,7 +48,7 @@ struct iio_module * iio_open_module(const struct iio_context_params *params, module->lib = iio_dlopen(buf); if (!module->lib) { - prm_err(params, "Unable to open plug-in\n"); + prm_dbg(params, "Unable to open plug-in\n"); err = -ENOSYS; goto err_free_name; } @@ -95,10 +95,8 @@ get_iio_backend(const struct iio_context_params *params, lib = iio_open_module(params, name); ret = iio_err(lib); - if (ret) { - prm_dbg(params, "Unable to open plug-in\n"); + if (ret) return iio_err_cast(lib); - } backend = iio_module_get_backend(lib); ret = iio_err(backend); @@ -163,3 +161,18 @@ iio_create_dynamic_context(const struct iio_context_params *params, iio_release_module(lib); return iio_ptr(ret); } + +bool iio_has_backend_dynamic(const struct iio_context_params *params, + const char *name) +{ + const struct iio_backend *backend; + struct iio_module *lib; + bool found; + + backend = get_iio_backend(params, name, &lib); + found = !iio_err(backend); + if (found) + iio_release_module(lib); + + return found; +} diff --git a/dynamic.h b/dynamic.h index 74c4df989..e0e00199e 100644 --- a/dynamic.h +++ b/dynamic.h @@ -9,8 +9,15 @@ #ifndef __IIO_DYNAMIC_H #define __IIO_DYNAMIC_H +#include + +struct iio_context_params; + void * iio_dlopen(const char *path); void iio_dlclose(void *lib); void * iio_dlsym(void *lib, const char *symbol); +bool iio_has_backend_dynamic(const struct iio_context_params *params, + const char *name); + #endif /* __IIO_DYNAMIC_H */ diff --git a/iio-private.h b/iio-private.h index 9ddae57fe..c212fff3e 100644 --- a/iio-private.h +++ b/iio-private.h @@ -246,7 +246,7 @@ extern const struct iio_backend iio_serial_backend; extern const struct iio_backend iio_usb_backend; extern const struct iio_backend iio_xml_backend; -extern const struct iio_backend *iio_backends[]; +extern const struct iio_backend * const iio_backends[]; extern const unsigned int iio_backends_size; ssize_t iio_xml_print_and_sanitized_param(char *ptr, ssize_t len, diff --git a/include/iio/iio.h b/include/iio/iio.h index db4672647..c225e46a6 100644 --- a/include/iio/iio.h +++ b/include/iio/iio.h @@ -396,27 +396,26 @@ __api void iio_strerror(int err, char *dst, size_t len); /** @brief Check if the specified backend is available + * @param params A pointer to a iio_context_params structure that contains + * context creation information; can be NULL * @param backend The name of the backend to query - * @return True if the backend is available, false otherwise - * - * Introduced in version 0.9. */ -__api __check_ret __cnst bool iio_has_backend(const char *backend); + * @return True if the backend is available, false otherwise */ +__api __check_ret bool +iio_has_backend(const struct iio_context_params *params, const char *backend); -/** @brief Get the number of available backends - * @return The number of available backends - * - * Introduced in version 0.9. */ -__api __check_ret __cnst unsigned int iio_get_backends_count(void); +/** @brief Get the number of available built-in backends + * @return The number of available built-in backends */ +__api __check_ret __cnst unsigned int +iio_get_builtin_backends_count(void); -/** @brief Retrieve the name of a given backend - * @param index The index corresponding to the attribute +/** @brief Retrieve the name of a given built-in backend + * @param index The index corresponding to the backend * @return On success, a pointer to a static NULL-terminated string - * @return If the index is invalid, NULL is returned - * - * Introduced in version 0.9. */ -__api __check_ret __cnst const char * iio_get_backend(unsigned int index); + * @return If the index is invalid, NULL is returned */ +__api __check_ret __cnst const char * +iio_get_builtin_backend(unsigned int index); /** @} *//* ------------------------------------------------------------------*/ diff --git a/utils/iio_common.c b/utils/iio_common.c index 1b37d743b..dc10800e1 100644 --- a/utils/iio_common.c +++ b/utils/iio_common.c @@ -432,8 +432,8 @@ void version(char *name) iio_context_get_version_major(NULL), iio_context_get_version_minor(NULL), iio_context_get_version_tag(NULL)); - for (i = 0; i < iio_get_backends_count(); i++) - printf(" %s", iio_get_backend(i)); + for (i = 0; i < iio_get_builtin_backends_count(); i++) + printf(" %s", iio_get_builtin_backend(i)); printf("\n"); }