From e69e1310dd808963ab2a91bb1779f56952c1e588 Mon Sep 17 00:00:00 2001 From: Matt Fornero Date: Thu, 30 Nov 2017 14:07:52 -0500 Subject: [PATCH] local: Track buffer attributes Track attributes within the buffer/ sysfs directory. These attributes include watermarks, fifo controls, etc. Signed-off-by: Matt Fornero --- device.c | 4 ++++ iio-private.h | 3 +++ local.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/device.c b/device.c index 8ce3c924d..0fc88bcfd 100644 --- a/device.c +++ b/device.c @@ -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) diff --git a/iio-private.h b/iio-private.h index 07c6a0e28..295bbd891 100644 --- a/iio-private.h +++ b/iio-private.h @@ -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; diff --git a/local.c b/local.c index 8cefd2777..48646cfe7 100644 --- a/local.c +++ b/local.c @@ -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; @@ -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) { @@ -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; @@ -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;