Skip to content

Commit

Permalink
utils: Add more helpers for reading/writing iio attributes
Browse files Browse the repository at this point in the history
Signed-off-by: Dan <[email protected]>
  • Loading branch information
dNechita committed Dec 10, 2024
1 parent bf5e9f3 commit 985d938
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
63 changes: 63 additions & 0 deletions iio_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions iio_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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);
Expand Down

0 comments on commit 985d938

Please sign in to comment.