Skip to content

Commit

Permalink
local: use API accessors for accessing devices from an IIO context
Browse files Browse the repository at this point in the history
The aim is to make all libiio backends plugins, so they need to decouple a
bit from accessing internal IIO context objects, and channels.

For all the backends in libiio, this change replaces the access of IIO
devices count and objects with IIO API accessors to get the number of
devices and a reference for each device when iterating.

Signed-off-by: Alexandru Ardelean <[email protected]>
  • Loading branch information
commodo committed Oct 9, 2020
1 parent c7f4555 commit 10d9775
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
10 changes: 6 additions & 4 deletions dns_sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,17 @@ static int dnssd_fill_context_info(struct iio_context_info *info,
iio_snprintf(description, sizeof(description), "%s %s", addr_str, hw_model);
} else if (serial) {
iio_snprintf(description, sizeof(description), "%s %s", addr_str, serial);
} else if (ctx->nb_devices == 0) {
} else if (iio_context_get_devices_count(ctx) == 0) {
iio_snprintf(description, sizeof(description), "%s", ctx->description);
} else {
iio_snprintf(description, sizeof(description), "%s (", addr_str);
p = description + strlen(description);
for (i = 0; i < ctx->nb_devices - 1; i++) {
if (ctx->devices[i]->name) {
for (i = 0; i < iio_context_get_devices_count(ctx) - 1; i++) {
const struct iio_device *dev = iio_context_get_device(ctx, i);
const char *name = iio_device_get_name(dev);
if (name) {
iio_snprintf(p, sizeof(description) - strlen(description) -1,
"%s,", ctx->devices[i]->name);
"%s,", name);
p += strlen(p);
}
}
Expand Down
12 changes: 6 additions & 6 deletions local.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ static void local_shutdown(struct iio_context *ctx)
/* Free the backend data stored in every device structure */
unsigned int i;

for (i = 0; i < ctx->nb_devices; i++) {
struct iio_device *dev = ctx->devices[i];
for (i = 0; i < iio_context_get_devices_count(ctx); i++) {
struct iio_device *dev = iio_context_get_device(ctx, i);

iio_device_close(dev);
local_free_pdata(dev);
Expand Down Expand Up @@ -1125,9 +1125,9 @@ static int local_get_trigger(const struct iio_device *dev,
return 0;
}

nb = dev->ctx->nb_devices;
nb = iio_context_get_devices_count(dev->ctx);
for (i = 0; i < (size_t) nb; i++) {
const struct iio_device *cur = dev->ctx->devices[i];
const struct iio_device *cur = iio_context_get_device(dev->ctx, i);
if (cur->name && !strcmp(cur->name, buf)) {
*trigger = cur;
return 0;
Expand Down Expand Up @@ -1986,8 +1986,8 @@ static void init_scan_elements(struct iio_context *ctx)
{
unsigned int i, j;

for (i = 0; i < ctx->nb_devices; i++) {
struct iio_device *dev = ctx->devices[i];
for (i = 0; i < iio_context_get_devices_count(ctx); i++) {
struct iio_device *dev = iio_context_get_device(ctx, i);

for (j = 0; j < dev->nb_channels; j++)
init_data_scale(dev->channels[j]);
Expand Down
8 changes: 4 additions & 4 deletions network.c
Original file line number Diff line number Diff line change
Expand Up @@ -1085,8 +1085,8 @@ static void network_shutdown(struct iio_context *ctx)
close(pdata->io_ctx.fd);
iio_mutex_unlock(pdata->lock);

for (i = 0; i < ctx->nb_devices; i++) {
struct iio_device *dev = ctx->devices[i];
for (i = 0; i < iio_context_get_devices_count(ctx); i++) {
struct iio_device *dev = iio_context_get_device(ctx, i);
struct iio_device_pdata *dpdata = dev->pdata;

if (dpdata) {
Expand Down Expand Up @@ -1462,8 +1462,8 @@ struct iio_context * network_create_context(const char *host)
if (ret < 0)
goto err_free_description;

for (i = 0; i < ctx->nb_devices; i++) {
struct iio_device *dev = ctx->devices[i];
for (i = 0; i < iio_context_get_devices_count(ctx); i++) {
struct iio_device *dev = iio_context_get_device(ctx, i);

dev->pdata = zalloc(sizeof(*dev->pdata));
if (!dev->pdata) {
Expand Down
13 changes: 7 additions & 6 deletions usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,13 @@ static int usb_set_trigger(const struct iio_device *dev,

static void usb_shutdown(struct iio_context *ctx)
{
unsigned int nb_devices = iio_context_get_devices_count(ctx);
unsigned int i;

usb_io_context_exit(&ctx->pdata->io_ctx);

for (i = 0; i < ctx->nb_devices; i++)
usb_close(ctx->devices[i]);
for (i = 0; i < nb_devices; i++)
usb_close(iio_context_get_device(ctx, i));

iio_mutex_destroy(ctx->pdata->lock);
iio_mutex_destroy(ctx->pdata->ep_lock);
Expand All @@ -465,8 +466,8 @@ static void usb_shutdown(struct iio_context *ctx)
if (ctx->pdata->io_endpoints)
free(ctx->pdata->io_endpoints);

for (i = 0; i < ctx->nb_devices; i++) {
struct iio_device *dev = ctx->devices[i];
for (i = 0; i < nb_devices; i++) {
struct iio_device *dev = iio_context_get_device(ctx, i);

usb_io_context_exit(&dev->pdata->io_ctx);
free(dev->pdata);
Expand Down Expand Up @@ -1015,8 +1016,8 @@ struct iio_context * usb_create_context(unsigned int bus,
ctx->ops = &usb_ops;
ctx->pdata = pdata;

for (i = 0; i < ctx->nb_devices; i++) {
struct iio_device *dev = ctx->devices[i];
for (i = 0; i < iio_context_get_devices_count(ctx); i++) {
struct iio_device *dev = iio_context_get_device(ctx, i);

dev->pdata = zalloc(sizeof(*dev->pdata));
if (!dev->pdata) {
Expand Down

0 comments on commit 10d9775

Please sign in to comment.