Skip to content

Commit

Permalink
local: Implement and use new .scan callback
Browse files Browse the repository at this point in the history
Drop the old local_context_scan() function and implement a proper
.scan() callback instead. The use of iio_scan_add_result() allows to
significantly reduce the code size.

Signed-off-by: Paul Cercueil <[email protected]>
  • Loading branch information
pcercuei committed Jun 15, 2021
1 parent 40447e2 commit 40f1961
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 35 deletions.
2 changes: 0 additions & 2 deletions iio-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@ struct iio_context *
iio_create_dynamic_context(const struct iio_context_params *params,
const char *uri);

int local_context_scan(struct iio_scan_result *scan_result);

int dnssd_context_scan(struct iio_scan_result *scan_result);

ssize_t iio_device_get_sample_size_mask(const struct iio_device *dev,
Expand Down
35 changes: 10 additions & 25 deletions local.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ static ssize_t local_write_chn_attr(const struct iio_channel *chn,
const char *attr, const char *src, size_t len);
static struct iio_context *
local_create_context(const struct iio_context_params *params, const char *args);
static int local_context_scan(const struct iio_context_params *params,
struct iio_scan *ctx);

struct block_alloc_req {
uint32_t type,
Expand Down Expand Up @@ -1897,6 +1899,7 @@ static char * local_get_description(const struct iio_context *ctx)
}

static const struct iio_backend_ops local_ops = {
.scan = local_context_scan,
.create = local_create_context,
.clone = local_clone,
.open = local_open,
Expand Down Expand Up @@ -2118,11 +2121,12 @@ static int check_device(void *d, const char *path)
return 0;
}

int local_context_scan(struct iio_scan_result *scan_result)
static int local_context_scan(const struct iio_context_params *params,
struct iio_scan *ctx)
{
struct iio_context_info *info;
char *machine, buf[2 * BUF_SIZE], names[BUF_SIZE];
bool exists = false;
char *desc, *uri, *machine, buf[2 * BUF_SIZE], names[BUF_SIZE];
const char *desc;
int ret;

ret = foreach_in_dir(NULL, &exists, "/sys/bus/iio", true, check_device);
Expand All @@ -2147,29 +2151,10 @@ int local_context_scan(struct iio_scan_result *scan_result)
} else
iio_snprintf(buf, sizeof(buf), "(Local IIO devices on %s)", machine);
free(machine);
desc = iio_strdup(buf);
desc = buf;
} else {
desc = iio_strdup("(Local IIO devices)");
desc = "(Local IIO devices)";
}
if (!desc)
return -ENOMEM;

uri = iio_strdup("local:");
if (!uri)
goto err_free_desc;

info = iio_scan_result_add(scan_result);
if (!info)
goto err_free_uri;

info->description = desc;
info->uri = uri;

return 0;

err_free_uri:
free(uri);
err_free_desc:
free(desc);
return -ENOMEM;
return iio_scan_add_result(ctx, desc, "local:");
}
8 changes: 0 additions & 8 deletions scan.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,6 @@ struct iio_scan * iio_scan(const struct iio_context_params *params,
}
}

if (WITH_LOCAL_BACKEND && has_backend(backends, "local")) {
ret = local_context_scan(&ctx->scan_result);
if (ret < 0) {
prm_perror(params, -ret,
"Unable to scan local context(s)");
}
}

if (HAVE_DNS_SD && has_backend(backends, "ip")) {
ret = dnssd_context_scan(&ctx->scan_result);
if (ret < 0) {
Expand Down

0 comments on commit 40f1961

Please sign in to comment.