Skip to content

Commit 91e5f97

Browse files
committed
channel: Support attribute names that start with the channel's label
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]>
1 parent c61f450 commit 91e5f97

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

channel.c

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,21 +344,53 @@ const char * iio_channel_get_attr(const struct iio_channel *chn,
344344
return chn->attrs[index].name;
345345
}
346346

347-
const char * iio_channel_find_attr(const struct iio_channel *chn,
348-
const char *name)
347+
static const char *
348+
iio_channel_do_find_attr(const struct iio_channel *chn, const char *name)
349349
{
350350
unsigned int i;
351+
351352
for (i = 0; i < chn->nb_attrs; i++) {
352353
const char *attr = chn->attrs[i].name;
353354
if (!strcmp(attr, name))
354355
return attr;
355356
}
357+
358+
return NULL;
359+
}
360+
361+
const char * iio_channel_find_attr(const struct iio_channel *chn,
362+
const char *name)
363+
{
364+
const char *attr;
365+
size_t len;
366+
367+
attr = iio_channel_do_find_attr(chn, name);
368+
if (attr)
369+
return attr;
370+
371+
/* Support attribute names that start with the channel's label to avoid
372+
* breaking compatibility with old kernels, which did not offer a
373+
* 'label' attribute, and caused Libiio to sometimes misdetect the
374+
* channel's extended name as being part of the attribute name. */
375+
if (chn->name) {
376+
len = strlen(chn->name);
377+
378+
if (!strncmp(chn->name, name, len) && name[len] == '_') {
379+
name += len + 1;
380+
return iio_channel_do_find_attr(chn, name);
381+
}
382+
}
383+
356384
return NULL;
357385
}
358386

359387
ssize_t iio_channel_attr_read(const struct iio_channel *chn,
360388
const char *attr, char *dst, size_t len)
361389
{
390+
attr = iio_channel_find_attr(chn, attr);
391+
if (!attr)
392+
return -ENOENT;
393+
362394
if (chn->dev->ctx->ops->read_channel_attr)
363395
return chn->dev->ctx->ops->read_channel_attr(chn,
364396
attr, dst, len);
@@ -369,6 +401,10 @@ ssize_t iio_channel_attr_read(const struct iio_channel *chn,
369401
ssize_t iio_channel_attr_write_raw(const struct iio_channel *chn,
370402
const char *attr, const void *src, size_t len)
371403
{
404+
attr = iio_channel_find_attr(chn, attr);
405+
if (!attr)
406+
return -ENOENT;
407+
372408
if (chn->dev->ctx->ops->write_channel_attr)
373409
return chn->dev->ctx->ops->write_channel_attr(chn,
374410
attr, src, len);

0 commit comments

Comments
 (0)