Skip to content

Commit

Permalink
local: Track buffer attributes
Browse files Browse the repository at this point in the history
Track attributes within the buffer/ sysfs directory. These attributes
include watermarks, fifo controls, etc.

Signed-off-by: Matt Fornero <matt.fornero@mathworks.com>
  • Loading branch information
Matt Fornero committed Nov 30, 2017
1 parent 50c4a0c commit e69e131
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions device.c
Expand Up @@ -405,6 +405,10 @@ void free_device(struct iio_device *dev)
free(dev->attrs[i]);
if (dev->nb_attrs)
free(dev->attrs);
for (i = 0; i < dev->nb_buffer_attrs; i++)
free(dev->buffer_attrs[i]);
if (dev->nb_buffer_attrs)
free(dev->buffer_attrs);
for (i = 0; i < dev->nb_debug_attrs; i++)
free(dev->debug_attrs[i]);
if (dev->nb_debug_attrs)
Expand Down
3 changes: 3 additions & 0 deletions iio-private.h
Expand Up @@ -187,6 +187,9 @@ struct iio_device {
char **attrs;
unsigned int nb_attrs;

char **buffer_attrs;
unsigned int nb_buffer_attrs;

char **debug_attrs;
unsigned int nb_debug_attrs;

Expand Down
52 changes: 52 additions & 0 deletions local.c
Expand Up @@ -109,6 +109,11 @@ static const char * const device_attrs_blacklist[] = {
"uevent",
};

static const char * const buffer_attrs_reserved[] = {
"length",
"enable",
};

static int ioctl_nointr(int fd, unsigned long request, void *data)
{
int ret;
Expand Down Expand Up @@ -1488,6 +1493,33 @@ static int detect_and_move_global_attrs(struct iio_device *dev)
return 0;
}

static int add_buffer_attr(void *d, const char *path)
{
struct iio_device *dev = (struct iio_device *) d;
const char *name = strrchr(path, '/') + 1;
char **attrs, *attr;
int i;

for (i = 0; i < ARRAY_SIZE(buffer_attrs_reserved); i++)
if (!strcmp(buffer_attrs_reserved[i], name))
return 0;

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

attrs = realloc(dev->buffer_attrs, (1 + dev->nb_buffer_attrs) * sizeof(char *));
if (!attrs) {
free(attr);
return -ENOMEM;
}

attrs[dev->nb_buffer_attrs++] = attr;
dev->buffer_attrs = attrs;
DEBUG("Added buffer attr \'%s\' to device \'%s\'\n", attr, dev->id);
return 0;
}

static int add_attr_or_channel_helper(struct iio_device *dev,
const char *path, bool dir_is_scan_elements)
{
Expand Down Expand Up @@ -1583,6 +1615,22 @@ static int add_scan_elements(struct iio_device *dev, const char *devpath)
return 0;
}

static int add_buffer_attributes(struct iio_device *dev, const char *devpath)
{
struct stat st;
char buf[1024];

iio_snprintf(buf, sizeof(buf), "%s/buffer", devpath);

if (!stat(buf, &st) && S_ISDIR(st.st_mode)) {
int ret = foreach_in_dir(dev, buf, false, add_buffer_attr);
if (ret < 0)
return ret;
}

return 0;
}

static int create_device(void *d, const char *path)
{
uint32_t *mask = NULL;
Expand Down Expand Up @@ -1615,6 +1663,10 @@ static int create_device(void *d, const char *path)
if (ret < 0)
goto err_free_device;

ret = add_buffer_attributes(dev, path);
if (ret < 0)
goto err_free_device;

ret = add_scan_elements(dev, path);
if (ret < 0)
goto err_free_scan_elements;
Expand Down

0 comments on commit e69e131

Please sign in to comment.