From f7af85f1bf69030f6e9b8b5c5ed2545233f91513 Mon Sep 17 00:00:00 2001 From: Dan Nechita Date: Wed, 15 Jan 2025 11:30:50 +0200 Subject: [PATCH] treewide: Init channel label when found When the "label" channel attribute is found, initialize the channel's label to its value. Signed-off-by: Dan Nechita --- channel.c | 9 +++++++++ device.c | 15 ++++++++++++--- iio-private.h | 2 +- include/iio/iio-backend.h | 5 +++-- local.c | 16 ++++++++++++++++ xml.c | 12 +++++++++--- 6 files changed, 50 insertions(+), 9 deletions(-) diff --git a/channel.c b/channel.c index 5c8756017..4c04997ad 100644 --- a/channel.c +++ b/channel.c @@ -282,6 +282,14 @@ ssize_t iio_snprintf_channel_xml(char *ptr, ssize_t len, iio_update_xml_indexes(ret, &ptr, &len, &alen); } + if (chn->label) { + ret = iio_snprintf(ptr, len, " label=\"%s\"", chn->label); + if (ret < 0) + return ret; + + iio_update_xml_indexes(ret, &ptr, &len, &alen); + } + ret = iio_snprintf(ptr, len, " type=\"%s\" >", chn->is_output ? "output" : "input"); if (ret < 0) return ret; @@ -420,6 +428,7 @@ void free_channel(struct iio_channel *chn) { iio_free_attrs(&chn->attrlist); free(chn->name); + free(chn->label); free(chn->id); free(chn); } diff --git a/device.c b/device.c index 0a0d90d55..7092a1ebc 100644 --- a/device.c +++ b/device.c @@ -354,12 +354,12 @@ void iio_device_set_pdata(struct iio_device *dev, struct iio_device_pdata *d) } struct iio_channel * iio_device_add_channel(struct iio_device *dev, long index, - const char *id, const char *name, + const char *id, const char *name, const char *label, bool output, bool scan_element, const struct iio_data_format *fmt) { struct iio_channel *chn, **chns; - char *new_id, *new_name = NULL; + char *new_id, *new_name = NULL, *new_label = NULL; unsigned int i; chn = zalloc(sizeof(*chn)); @@ -376,8 +376,15 @@ struct iio_channel * iio_device_add_channel(struct iio_device *dev, long index, goto err_free_id; } + if (label) { + new_label = iio_strdup(label); + if (!new_label) + goto err_free_name; + } + chn->id = new_id; chn->name = new_name; + chn->label = new_label; chn->dev = dev; chn->is_output = output; chn->is_scan_element = scan_element; @@ -390,7 +397,7 @@ struct iio_channel * iio_device_add_channel(struct iio_device *dev, long index, chns = realloc(dev->channels, (dev->nb_channels + 1) * sizeof(*chn)); if (!chns) - goto err_free_name; + goto err_free_label; chns[dev->nb_channels++] = chn; dev->channels = chns; @@ -407,6 +414,8 @@ struct iio_channel * iio_device_add_channel(struct iio_device *dev, long index, return chn; +err_free_label: + free(new_label); err_free_name: free(new_name); err_free_id: diff --git a/iio-private.h b/iio-private.h index 669cbe72d..fe9a621f0 100644 --- a/iio-private.h +++ b/iio-private.h @@ -109,7 +109,7 @@ struct iio_channel { bool is_scan_element; struct iio_data_format format; char __format_padding[128 - sizeof(struct iio_data_format)]; - char *name, *id; + char *name, *id, *label; long index; enum iio_modifier modifier; enum iio_chan_type type; diff --git a/include/iio/iio-backend.h b/include/iio/iio-backend.h index 823da8a9b..2fd2640b7 100644 --- a/include/iio/iio-backend.h +++ b/include/iio/iio-backend.h @@ -168,8 +168,9 @@ iio_context_add_device(struct iio_context *ctx, __api struct iio_channel * iio_device_add_channel(struct iio_device *dev, long index, - const char *id, const char *name, bool output, - bool scan_element, const struct iio_data_format *fmt); + const char *id, const char *name, const char *label, + bool output, bool scan_element, + const struct iio_data_format *fmt); __api int iio_context_add_attr(struct iio_context *ctx, diff --git a/local.c b/local.c index 9322c4b67..d662d5afc 100644 --- a/local.c +++ b/local.c @@ -835,6 +835,22 @@ static int add_attr_to_channel(struct iio_channel *chn, union iio_pointer p = { .chn = chn, }; struct iio_attr_list *attrs; int ret; + size_t lbl_len = strlen("_label"); + char label[512]; + const char *dev_id; + + if (strlen(name) >= lbl_len && + !strcmp(name + strlen(name) - lbl_len, "_label")) { + dev_id = iio_device_get_id(iio_channel_get_device(chn)); + ret = local_do_read_dev_attr(dev_id, 0, name, + label, sizeof(label), IIO_ATTR_TYPE_CHANNEL); + if (ret > 0) + chn->label = iio_strdup(label); + else + chn_perror(chn, ret, "Unable to read channel label"); + + return 0; + } name = get_short_attr_name(chn, name); diff --git a/xml.c b/xml.c index 729363d77..e1fb049e2 100644 --- a/xml.c +++ b/xml.c @@ -144,7 +144,7 @@ static int create_channel(struct iio_device *dev, xmlNode *node) xmlAttr *attr; struct iio_channel *chn; int err = -ENOMEM; - char *name_ptr = NULL, *id_ptr = NULL; + char *name_ptr = NULL, *label_ptr = NULL, *id_ptr = NULL; bool output = false; bool scan_element = false; long index = -ENOENT; @@ -158,6 +158,10 @@ static int create_channel(struct iio_device *dev, xmlNode *node) name_ptr = iio_strdup(content); if (!name_ptr) goto err_free_name_id; + } else if (!strcmp(name, "label")) { + label_ptr = iio_strdup(content); + if (!label_ptr) + goto err_free_name_id; } else if (!strcmp(name, "id")) { id_ptr = iio_strdup(content); if (!id_ptr) @@ -190,14 +194,15 @@ static int create_channel(struct iio_device *dev, xmlNode *node) } } - chn = iio_device_add_channel(dev, index, id_ptr, name_ptr, output, - scan_element, &format); + chn = iio_device_add_channel(dev, index, id_ptr, name_ptr, label_ptr, + output, scan_element, &format); if (!chn) { err = -ENOMEM; goto err_free_name_id; } free(name_ptr); + free(label_ptr); free(id_ptr); for (n = node->children; n; n = n->next) { @@ -217,6 +222,7 @@ static int create_channel(struct iio_device *dev, xmlNode *node) err_free_name_id: free(name_ptr); + free(label_ptr); free(id_ptr); return err; }