Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backends API rework #1059

Merged
merged 6 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 18 additions & 37 deletions backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,47 @@
* Author: Paul Cercueil <[email protected]>
*/

#include "dynamic.h"
#include "iio-config.h"
#include "iio-private.h"

#include <string.h>

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;
}
16 changes: 8 additions & 8 deletions compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion context.c
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
21 changes: 17 additions & 4 deletions dynamic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
7 changes: 7 additions & 0 deletions dynamic.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@
#ifndef __IIO_DYNAMIC_H
#define __IIO_DYNAMIC_H

#include <stdbool.h>

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 */
2 changes: 1 addition & 1 deletion iio-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
29 changes: 14 additions & 15 deletions include/iio/iio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);


/** @} *//* ------------------------------------------------------------------*/
Expand Down
4 changes: 2 additions & 2 deletions utils/iio_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down