Skip to content

Commit

Permalink
treewide: Init channel label when found
Browse files Browse the repository at this point in the history
When the "label" channel attribute is found, initialize the channel's
label to its value.

Signed-off-by: Dan Nechita <[email protected]>
  • Loading branch information
dNechita committed Jan 27, 2025
1 parent 3e76e90 commit f7af85f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 9 deletions.
9 changes: 9 additions & 0 deletions channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
15 changes: 12 additions & 3 deletions device.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion iio-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions include/iio/iio-backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions local.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
12 changes: 9 additions & 3 deletions xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down

0 comments on commit f7af85f

Please sign in to comment.