From 19306f46bc02abf85f0d5bd1cf3b3d4f92e7a364 Mon Sep 17 00:00:00 2001 From: Dan Date: Tue, 10 Dec 2024 12:05:38 +0200 Subject: [PATCH] utils: Add more helpers for reading/writing iio attributes Signed-off-by: Dan --- iio_utils.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ iio_utils.h | 7 ++++++ 2 files changed, 70 insertions(+) diff --git a/iio_utils.c b/iio_utils.c index 1d71452e..e81be32a 100644 --- a/iio_utils.c +++ b/iio_utils.c @@ -196,6 +196,36 @@ int dev_attr_read_longlong(struct iio_device *dev, const char *attr_name, long l return -ENOENT; } +int dev_debug_attr_read_raw(struct iio_device *dev, const char *attr_name, char *dst, size_t len) +{ + const struct iio_attr *attr = iio_device_find_debug_attr(dev, attr_name); + + if (attr) + return iio_attr_read_raw(attr, dst, len); + else + return -ENOENT; +} + +int dev_debug_attr_read_longlong(struct iio_device *dev, const char *attr_name, long long *value) +{ + const struct iio_attr *attr = iio_device_find_debug_attr(dev, attr_name); + + if (attr) + return iio_attr_read_longlong(attr, value); + else + return -ENOENT; +} + +int dev_attr_write_raw(struct iio_device *dev, const char *attr_name, const char *src, size_t len) +{ + const struct iio_attr *attr = iio_device_find_attr(dev, attr_name); + + if (attr) + return iio_attr_write_raw(attr, src, len); + else + return -ENOENT; +} + int dev_attr_write_double(struct iio_device *dev, const char *attr_name, double value) { const struct iio_attr *attr = iio_device_find_attr(dev, attr_name); @@ -216,6 +246,16 @@ int dev_attr_write_longlong(struct iio_device *dev, const char *attr_name, long return -ENOENT; } +int dev_debug_attr_write_string(struct iio_device *dev, const char *attr_name, const char *value) +{ + const struct iio_attr *attr = iio_device_find_debug_attr(dev, attr_name); + + if (attr) + return iio_attr_write_string(attr, value); + else + return -ENOENT; +} + int dev_debug_attr_write_longlong(struct iio_device *dev, const char *attr_name, long long value) { const struct iio_attr *attr = iio_device_find_debug_attr(dev, attr_name); @@ -327,6 +367,29 @@ void dev_attr_read_all(struct iio_device *dev, } } +int dev_debug_attr_read_all(struct iio_device *dev, + int (*cb)(struct iio_device *dev, const char *attr, const char *value, size_t len, void *d), + void *data) +{ + unsigned int i, attr_cnt = iio_device_get_debug_attrs_count(dev); + const struct iio_attr *attr; + char local_value[8192]; + int ret; + + for (i = 0; i < attr_cnt; ++i) { + attr = iio_device_get_debug_attr(dev, i); + ret = iio_attr_read_raw(attr, local_value, ARRAY_SIZE(local_value)); + if (ret < 0) { + fprintf(stderr, "Failed to read attribute: %s\n", iio_attr_get_name(attr)); + return ret; + } else { + cb(dev, iio_attr_get_name(attr), local_value, strlen(local_value), data); + } + } + + return 0; +} + void chn_attr_read_all(struct iio_channel *chn, int (*cb)(struct iio_channel *chn, const char *attr, const char *value, size_t len, void *d), void *data) diff --git a/iio_utils.h b/iio_utils.h index 308c3049..1c324dcf 100644 --- a/iio_utils.h +++ b/iio_utils.h @@ -30,10 +30,14 @@ bool iio_attr_not_found(struct iio_device *dev, struct iio_channel *chn, const c inline int dev_attr_read_raw(struct iio_device *dev, const char *attr_name, char *dst, size_t len); inline int dev_attr_read_double(struct iio_device *dev, const char *attr_name, double *value); inline int dev_attr_read_longlong(struct iio_device *dev, const char *attr_name, long long *value); +inline int dev_debug_attr_read_raw(struct iio_device *dev, const char *attr_name, char *dst, size_t len); +inline int dev_debug_attr_read_longlong(struct iio_device *dev, const char *attr_name, long long *value); /* Helpers to write to iio attributes of devices */ +inline int dev_attr_write_raw(struct iio_device *dev, const char *attr_name, const char *src, size_t len); inline int dev_attr_write_double(struct iio_device *dev, const char *attr_name, double value); inline int dev_attr_write_longlong(struct iio_device *dev, const char *attr_name, long long value); +inline int dev_debug_attr_write_string(struct iio_device *dev, const char *attr_name, const char *value); inline int dev_debug_attr_write_longlong(struct iio_device *dev, const char *attr_name, long long value); /* Helpers to read from iio attributes of channels */ @@ -52,6 +56,9 @@ inline int chn_attr_write_longlong(struct iio_channel *chn, const char *attr_nam inline void dev_attr_read_all(struct iio_device *dev, int (*cb)(struct iio_device *dev, const char *attr, const char *value, size_t len, void *d), void *data); +inline int dev_debug_attr_read_all(struct iio_device *dev, + int (*cb)(struct iio_device *dev, const char *attr, const char *value, size_t len, void *d), + void *data); inline void chn_attr_read_all(struct iio_channel *chn, int (*cb)(struct iio_channel *chn, const char *attr, const char *value, size_t len, void *d), void *data);