Skip to content

Commit

Permalink
local: 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 16, 2025
1 parent a6c019b commit 57c0f58
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
15 changes: 12 additions & 3 deletions device.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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:
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
13 changes: 12 additions & 1 deletion local.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
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 57c0f58

Please sign in to comment.