Skip to content

Commit

Permalink
channel: Support attribute names that start with the channel's label
Browse files Browse the repository at this point in the history
Support attribute names that start with the channel's label to avoid
breaking compatibility with old kernels, which did not offer a 'label'
attribute, and caused Libiio to sometimes misdetect the channel's
extended name as being part of the attribute name.

Signed-off-by: Paul Cercueil <[email protected]>
  • Loading branch information
pcercuei committed Apr 28, 2023
1 parent c61f450 commit 91e5f97
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,21 +344,53 @@ const char * iio_channel_get_attr(const struct iio_channel *chn,
return chn->attrs[index].name;
}

const char * iio_channel_find_attr(const struct iio_channel *chn,
const char *name)
static const char *
iio_channel_do_find_attr(const struct iio_channel *chn, const char *name)
{
unsigned int i;

for (i = 0; i < chn->nb_attrs; i++) {
const char *attr = chn->attrs[i].name;
if (!strcmp(attr, name))
return attr;
}

return NULL;
}

const char * iio_channel_find_attr(const struct iio_channel *chn,
const char *name)
{
const char *attr;
size_t len;

attr = iio_channel_do_find_attr(chn, name);
if (attr)
return attr;

/* Support attribute names that start with the channel's label to avoid
* breaking compatibility with old kernels, which did not offer a
* 'label' attribute, and caused Libiio to sometimes misdetect the
* channel's extended name as being part of the attribute name. */
if (chn->name) {
len = strlen(chn->name);

if (!strncmp(chn->name, name, len) && name[len] == '_') {
name += len + 1;
return iio_channel_do_find_attr(chn, name);
}
}

return NULL;
}

ssize_t iio_channel_attr_read(const struct iio_channel *chn,
const char *attr, char *dst, size_t len)
{
attr = iio_channel_find_attr(chn, attr);
if (!attr)
return -ENOENT;

if (chn->dev->ctx->ops->read_channel_attr)
return chn->dev->ctx->ops->read_channel_attr(chn,
attr, dst, len);
Expand All @@ -369,6 +401,10 @@ ssize_t iio_channel_attr_read(const struct iio_channel *chn,
ssize_t iio_channel_attr_write_raw(const struct iio_channel *chn,
const char *attr, const void *src, size_t len)
{
attr = iio_channel_find_attr(chn, attr);
if (!attr)
return -ENOENT;

if (chn->dev->ctx->ops->write_channel_attr)
return chn->dev->ctx->ops->write_channel_attr(chn,
attr, src, len);
Expand Down

0 comments on commit 91e5f97

Please sign in to comment.