diff --git a/device.c b/device.c index 413ba5c8a..6a2ae40e8 100644 --- a/device.c +++ b/device.c @@ -355,12 +355,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)); @@ -377,8 +377,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; @@ -391,7 +398,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; @@ -408,6 +415,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/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..3fbb23906 100644 --- a/local.c +++ b/local.c @@ -1235,11 +1235,12 @@ static int add_events(struct iio_device *dev, const char *devpath) static int create_device(void *d, const char *path) { - unsigned int i; + unsigned int i, j; int ret; struct iio_context *ctx = d; struct iio_device *dev; const char *id, *name_ptr = NULL, *label_ptr = NULL; + const char *chn_label_ptr = NULL; char name[512], label[512]; id = strrchr(path, '/') + 1; @@ -1281,6 +1282,16 @@ static int create_device(void *d, const char *path) if (ret < 0) goto err_free_scan_elements; + for (j = 0; j < chn->attrlist.num; ++j) { + if (!strcmp(chn->attrlist.attrs[j].name, "label")) { + ret = local_do_read_dev_attr(id, 0, chn->attrlist.attrs[j].filename, label, sizeof(label), IIO_ATTR_TYPE_CHANNEL); + if (ret > 0) { + chn_label_ptr = label; + chn->label = iio_strdup(chn_label_ptr); + } + } + } + ret = handle_scan_elements(chn); free_protected_attrs(chn); if (ret < 0) 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; }