Skip to content

Commit

Permalink
scan: Rework scanning
Browse files Browse the repository at this point in the history
Instead of going through the list of enabled backends and checking for
each one whether or not we were asked to scan from it, we now go through
the user-supplied backends list, find the corresponding backend, and
call its scan function.

This change will make it possible to support scanning from the same
backend multiple times, for instance with different USB devices.

Signed-off-by: Paul Cercueil <[email protected]>
  • Loading branch information
pcercuei committed Feb 17, 2022
1 parent 84ad761 commit b058c69
Showing 1 changed file with 30 additions and 36 deletions.
66 changes: 30 additions & 36 deletions scan.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
#include <string.h>

struct iio_scan_context {
bool scan_usb;
bool scan_network;
bool scan_local;
char *backendopts;
};

const char * iio_context_info_get_description(
Expand All @@ -35,37 +33,34 @@ ssize_t iio_scan_context_get_info_list(struct iio_scan_context *ctx,
struct iio_context_info ***info)
{
struct iio_scan_result scan_result = { 0, NULL };

if (WITH_LOCAL_BACKEND && ctx->scan_local) {
int ret = local_context_scan(&scan_result);
if (ret < 0) {
if (scan_result.info)
iio_context_info_list_free(scan_result.info);
return ret;
}
}

if (WITH_USB_BACKEND && ctx->scan_usb) {
int ret = usb_context_scan(&scan_result);
if (ret < 0) {
if (scan_result.info)
iio_context_info_list_free(scan_result.info);
return ret;
}
}

if (HAVE_DNS_SD && ctx->scan_network) {
int ret = dnssd_context_scan(&scan_result);
if (ret < 0) {
if (scan_result.info)
iio_context_info_list_free(scan_result.info);
return ret;
char *token, *rest=NULL;
ssize_t ret;

for (token = iio_strtok_r(ctx->backendopts, ":", &rest);
token; token = iio_strtok_r(NULL, ":", &rest)) {

/* Since tokens are all null terminated, it's safe to use strcmp on them */
if (WITH_LOCAL_BACKEND && !strcmp(token, "local")) {
ret = local_context_scan(&scan_result);
} else if (WITH_USB_BACKEND && !strcmp(token, "usb")) {
ret = usb_context_scan(&scan_result);
} else if (HAVE_DNS_SD && !strcmp(token, "ip")) {
ret = dnssd_context_scan(&scan_result);
} else {
ret = -ENODEV;
}
if (ret < 0)
goto err_free_scan_result_info;
}

*info = scan_result.info;

return (ssize_t) scan_result.size;

err_free_scan_result_info:
if (scan_result.info)
iio_context_info_list_free(scan_result.info);
return ret;
}

void iio_context_info_list_free(struct iio_context_info **list)
Expand Down Expand Up @@ -126,20 +121,19 @@ struct iio_scan_context * iio_create_scan_context(
return NULL;
}

if (!backend || strstr(backend, "local"))
ctx->scan_local = true;

if (!backend || strstr(backend, "usb"))
ctx->scan_usb = true;

if (!backend || strstr(backend, "ip"))
ctx->scan_network = true;
ctx->backendopts = iio_strndup(backend ? backend : "local:usb:ip", PATH_MAX);
if (!ctx->backendopts) {
free(ctx);
errno = ENOMEM;
return NULL;
}

return ctx;
}

void iio_scan_context_destroy(struct iio_scan_context *ctx)
{
free(ctx->backendopts);
free(ctx);
}

Expand Down

0 comments on commit b058c69

Please sign in to comment.