Skip to content

Commit

Permalink
iio: re-use add_iio_dev_attr() helper in xml creation
Browse files Browse the repository at this point in the history
The code is functionally the same.
So, we're re-using it.
But we need to move it to the device.c file to make sure it exists on all 3
platforms.
The local backend is only built on Linux.

Signed-off-by: Alexandru Ardelean <[email protected]>
  • Loading branch information
commodo committed Feb 16, 2021
1 parent ea04aaa commit 4221354
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 59 deletions.
21 changes: 21 additions & 0 deletions device.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,27 @@ char * iio_device_get_xml(const struct iio_device *dev, size_t *length)
return NULL;
}

int add_iio_dev_attr(struct iio_dev_attrs *attrs, const char *attr,
const char *type, const char *dev_id)
{
char **names, *name;

name = iio_strdup(attr);
if (!name)
return -ENOMEM;

names = realloc(attrs->names, (1 + attrs->num) * sizeof(char *));
if (!names) {
free(name);
return -ENOMEM;
}

names[attrs->num++] = name;
attrs->names = names;
IIO_DEBUG("Added%s attr \'%s\' to device \'%s\'\n", type, attr, dev_id);
return 0;
}

const char * iio_device_get_id(const struct iio_device *dev)
{
return dev->id;
Expand Down
3 changes: 3 additions & 0 deletions iio-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ int iio_context_add_attr(struct iio_context *ctx,

struct iio_context_pdata * iio_context_get_pdata(const struct iio_context *ctx);

int add_iio_dev_attr(struct iio_dev_attrs *attrs, const char *attr,
const char *type, const char *dev_id);

#undef __api

#endif /* __IIO_PRIVATE_H__ */
21 changes: 0 additions & 21 deletions local.c
Original file line number Diff line number Diff line change
Expand Up @@ -1221,27 +1221,6 @@ static int read_device_name(struct iio_device *dev)
return 0;
}

static int add_iio_dev_attr(struct iio_dev_attrs *attrs, const char *attr,
const char *type, const char *dev_id)
{
char **names, *name;

name = iio_strdup(attr);
if (!name)
return -ENOMEM;

names = realloc(attrs->names, (1 + attrs->num) * sizeof(char *));
if (!names) {
free(name);
return -ENOMEM;
}

names[attrs->num++] = name;
attrs->names = names;
IIO_DEBUG("Added%s attr \'%s\' to device \'%s\'\n", type, attr, dev_id);
return 0;
}

static int add_attr_to_device(struct iio_device *dev, const char *attr)
{
unsigned int i;
Expand Down
45 changes: 7 additions & 38 deletions xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ static int add_attr_to_channel(struct iio_channel *chn, xmlNode *n)
static int add_attr_to_device(struct iio_device *dev, xmlNode *n, enum iio_attr_type type)
{
xmlAttr *attr;
char **attrs, *name = NULL;
char *name = NULL;

for (attr = n->properties; attr; attr = attr->next) {
if (!strcmp((char *) attr->name, "name")) {
name = iio_strdup((char *) attr->children->content);
name = (char *) attr->children->content;
} else {
IIO_WARNING("Unknown field \'%s\' in device %s\n",
attr->name, dev->id);
Expand All @@ -95,50 +95,19 @@ static int add_attr_to_device(struct iio_device *dev, xmlNode *n, enum iio_attr_

if (!name) {
IIO_ERROR("Incomplete attribute in device %s\n", dev->id);
goto err_free;
return -1;
}

switch(type) {
case IIO_ATTR_TYPE_DEBUG:
attrs = realloc(dev->debug_attrs.names,
(1 + dev->debug_attrs.num) * sizeof(char *));
break;
return add_iio_dev_attr(&dev->debug_attrs, name, " debug", dev->id);
case IIO_ATTR_TYPE_DEVICE:
attrs = realloc(dev->attrs.names,
(1 + dev->attrs.num) * sizeof(char *));
break;
return add_iio_dev_attr(&dev->attrs, name, " ", dev->id);
case IIO_ATTR_TYPE_BUFFER:
attrs = realloc(dev->buffer_attrs.names,
(1 + dev->buffer_attrs.num) * sizeof(char *));
break;
return add_iio_dev_attr(&dev->buffer_attrs, name, " buffer", dev->id);
default:
attrs = NULL;
break;
return -1;
}
if (!attrs)
goto err_free;

switch(type) {
case IIO_ATTR_TYPE_DEBUG:
attrs[dev->debug_attrs.num++] = name;
dev->debug_attrs.names = attrs;
break;
case IIO_ATTR_TYPE_DEVICE:
attrs[dev->attrs.num++] = name;
dev->attrs.names = attrs;
break;
case IIO_ATTR_TYPE_BUFFER:
attrs[dev->buffer_attrs.num++] = name;
dev->buffer_attrs.names = attrs;
break;
}

return 0;

err_free:
if (name)
free(name);
return -1;
}

static void setup_scan_element(struct iio_channel *chn, xmlNode *n)
Expand Down

0 comments on commit 4221354

Please sign in to comment.