A subsequent commit will add floating-point support to the ABI; enhance
the iio_generic_buffer tool to be able to parse this new data format.
Signed-off-by: Francesco Lavra <flavra@baylibre.com>
---
tools/iio/iio_generic_buffer.c | 59 ++++++++++++++++++++++++++++++----
tools/iio/iio_utils.c | 8 ++---
tools/iio/iio_utils.h | 4 +--
3 files changed, 58 insertions(+), 13 deletions(-)
diff --git a/tools/iio/iio_generic_buffer.c b/tools/iio/iio_generic_buffer.c
index bc82bb6a7a2a..a21552c2b190 100644
--- a/tools/iio/iio_generic_buffer.c
+++ b/tools/iio/iio_generic_buffer.c
@@ -26,6 +26,7 @@
#include <string.h>
#include <poll.h>
#include <endian.h>
+#include <float.h>
#include <getopt.h>
#include <inttypes.h>
#include <stdbool.h>
@@ -89,7 +90,7 @@ static void print1byte(uint8_t input, struct iio_channel_info *info)
*/
input >>= info->shift;
input &= info->mask;
- if (info->is_signed) {
+ if (info->format == 's') {
int8_t val = (int8_t)(input << (8 - info->bits_used)) >>
(8 - info->bits_used);
printf("%05f ", ((float)val + info->offset) * info->scale);
@@ -112,12 +113,28 @@ static void print2byte(uint16_t input, struct iio_channel_info *info)
*/
input >>= info->shift;
input &= info->mask;
- if (info->is_signed) {
+ switch (info->format) {
+ case 's': {
int16_t val = (int16_t)(input << (16 - info->bits_used)) >>
(16 - info->bits_used);
printf("%05f ", ((float)val + info->offset) * info->scale);
- } else {
+ break;
+ }
+ case 'u':
printf("%05f ", ((float)input + info->offset) * info->scale);
+ break;
+#if defined(__FLT16_MAX__)
+ case 'f': {
+ union {
+ uint16_t u;
+ _Float16 f;
+ } converter;
+
+ converter.u = input;
+ printf("%05f ", ((float)converter.f + info->offset) * info->scale);
+ break;
+ }
+#endif
}
}
@@ -135,12 +152,26 @@ static void print4byte(uint32_t input, struct iio_channel_info *info)
*/
input >>= info->shift;
input &= info->mask;
- if (info->is_signed) {
+ switch (info->format) {
+ case 's': {
int32_t val = (int32_t)(input << (32 - info->bits_used)) >>
(32 - info->bits_used);
printf("%05f ", ((float)val + info->offset) * info->scale);
- } else {
+ break;
+ }
+ case 'u':
printf("%05f ", ((float)input + info->offset) * info->scale);
+ break;
+ case 'f': {
+ union {
+ uint32_t u;
+ float f;
+ } converter;
+
+ converter.u = input;
+ printf("%05f ", (converter.f + info->offset) * info->scale);
+ break;
+ }
}
}
@@ -158,7 +189,8 @@ static void print8byte(uint64_t input, struct iio_channel_info *info)
*/
input >>= info->shift;
input &= info->mask;
- if (info->is_signed) {
+ switch (info->format) {
+ case 's': {
int64_t val = (int64_t)(input << (64 - info->bits_used)) >>
(64 - info->bits_used);
/* special case for timestamp */
@@ -167,8 +199,21 @@ static void print8byte(uint64_t input, struct iio_channel_info *info)
else
printf("%05f ",
((float)val + info->offset) * info->scale);
- } else {
+ break;
+ }
+ case 'u':
printf("%05f ", ((float)input + info->offset) * info->scale);
+ break;
+ case 'f': {
+ union {
+ uint64_t u;
+ double f;
+ } converter;
+
+ converter.u = input;
+ printf("%05f ", (converter.f + info->offset) * info->scale);
+ break;
+ }
}
}
diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c
index c5c5082cb24e..f1d58db69942 100644
--- a/tools/iio/iio_utils.c
+++ b/tools/iio/iio_utils.c
@@ -70,7 +70,7 @@ int iioutils_break_up_name(const char *full_name, char **generic_name)
/**
* iioutils_get_type() - find and process _type attribute data
- * @is_signed: output whether channel is signed
+ * @format: output channel format
* @bytes: output how many bytes the channel storage occupies
* @bits_used: output number of valid bits of data
* @shift: output amount of bits to shift right data before applying bit mask
@@ -83,7 +83,7 @@ int iioutils_break_up_name(const char *full_name, char **generic_name)
*
* Returns a value >= 0 on success, otherwise a negative error code.
**/
-static int iioutils_get_type(unsigned int *is_signed, unsigned int *bytes,
+static int iioutils_get_type(char *format, unsigned int *bytes,
unsigned int *bits_used, unsigned int *shift,
uint64_t *mask, unsigned int *be,
const char *device_dir, int buffer_idx,
@@ -162,7 +162,7 @@ static int iioutils_get_type(unsigned int *is_signed, unsigned int *bytes,
else
*mask = (1ULL << *bits_used) - 1ULL;
- *is_signed = (signchar == 's');
+ *format = signchar;
if (fclose(sysfsfp)) {
ret = -errno;
fprintf(stderr, "Failed to close %s\n",
@@ -487,7 +487,7 @@ int build_channel_array(const char *device_dir, int buffer_idx,
if ((ret < 0) && (ret != -ENOENT))
goto error_cleanup_array;
- ret = iioutils_get_type(¤t->is_signed,
+ ret = iioutils_get_type(¤t->format,
¤t->bytes,
¤t->bits_used,
¤t->shift,
diff --git a/tools/iio/iio_utils.h b/tools/iio/iio_utils.h
index 663c94a6c705..8c72f002d050 100644
--- a/tools/iio/iio_utils.h
+++ b/tools/iio/iio_utils.h
@@ -32,7 +32,7 @@ extern const char *iio_dir;
* @shift: amount of bits to shift right data before applying bit mask
* @mask: a bit mask for the raw output
* @be: flag if data is big endian
- * @is_signed: is the raw value stored signed
+ * @format: format of the raw value
* @location: data offset for this channel inside the buffer (in bytes)
**/
struct iio_channel_info {
@@ -46,7 +46,7 @@ struct iio_channel_info {
unsigned shift;
uint64_t mask;
unsigned be;
- unsigned is_signed;
+ char format;
unsigned location;
};
--
2.39.5
On 3/17/26 10:04 AM, Francesco Lavra wrote:
> A subsequent commit will add floating-point support to the ABI; enhance
> the iio_generic_buffer tool to be able to parse this new data format.
>
> Signed-off-by: Francesco Lavra <flavra@baylibre.com>
> ---
> tools/iio/iio_generic_buffer.c | 59 ++++++++++++++++++++++++++++++----
> tools/iio/iio_utils.c | 8 ++---
> tools/iio/iio_utils.h | 4 +--
> 3 files changed, 58 insertions(+), 13 deletions(-)
>
> diff --git a/tools/iio/iio_generic_buffer.c b/tools/iio/iio_generic_buffer.c
> index bc82bb6a7a2a..a21552c2b190 100644
> --- a/tools/iio/iio_generic_buffer.c
> +++ b/tools/iio/iio_generic_buffer.c
> @@ -26,6 +26,7 @@
> #include <string.h>
> #include <poll.h>
> #include <endian.h>
> +#include <float.h>
> #include <getopt.h>
> #include <inttypes.h>
> #include <stdbool.h>
> @@ -89,7 +90,7 @@ static void print1byte(uint8_t input, struct iio_channel_info *info)
> */
> input >>= info->shift;
> input &= info->mask;
> - if (info->is_signed) {
> + if (info->format == 's') {
To be consistent, maybe change this one to a switch case too and print
an error if we accidentally have a 1-byte float format.
> int8_t val = (int8_t)(input << (8 - info->bits_used)) >>
> (8 - info->bits_used);
> printf("%05f ", ((float)val + info->offset) * info->scale);
> @@ -112,12 +113,28 @@ static void print2byte(uint16_t input, struct iio_channel_info *info)
> */
> input >>= info->shift;
> input &= info->mask;
> - if (info->is_signed) {
> + switch (info->format) {
> + case 's': {
> int16_t val = (int16_t)(input << (16 - info->bits_used)) >>
> (16 - info->bits_used);
> printf("%05f ", ((float)val + info->offset) * info->scale);
> - } else {
> + break;
> + }
> + case 'u':
> printf("%05f ", ((float)input + info->offset) * info->scale);
> + break;
> +#if defined(__FLT16_MAX__)
> + case 'f': {
> + union {
> + uint16_t u;
> + _Float16 f;
> + } converter;
> +
> + converter.u = input;
> + printf("%05f ", ((float)converter.f + info->offset) * info->scale);
> + break;
> + }
Should we have #else here and print an error if it isn't supported?
> +#endif
> }
> }
>
> @@ -135,12 +152,26 @@ static void print4byte(uint32_t input, struct iio_channel_info *info)
> */
> input >>= info->shift;
> input &= info->mask;
> - if (info->is_signed) {
> + switch (info->format) {
> + case 's': {
> int32_t val = (int32_t)(input << (32 - info->bits_used)) >>
> (32 - info->bits_used);
> printf("%05f ", ((float)val + info->offset) * info->scale);
> - } else {
> + break;
> + }
> + case 'u':
> printf("%05f ", ((float)input + info->offset) * info->scale);
> + break;
> + case 'f': {
> + union {
> + uint32_t u;
> + float f;
> + } converter;
> +
> + converter.u = input;
> + printf("%05f ", (converter.f + info->offset) * info->scale);
> + break;
> + }
> }
> }
>
> @@ -158,7 +189,8 @@ static void print8byte(uint64_t input, struct iio_channel_info *info)
> */
> input >>= info->shift;
> input &= info->mask;
> - if (info->is_signed) {
> + switch (info->format) {
> + case 's': {
> int64_t val = (int64_t)(input << (64 - info->bits_used)) >>
> (64 - info->bits_used);
> /* special case for timestamp */
> @@ -167,8 +199,21 @@ static void print8byte(uint64_t input, struct iio_channel_info *info)
> else
> printf("%05f ",
> ((float)val + info->offset) * info->scale);
> - } else {
> + break;
> + }
> + case 'u':
> printf("%05f ", ((float)input + info->offset) * info->scale);
> + break;
> + case 'f': {
> + union {
> + uint64_t u;
> + double f;
> + } converter;
> +
> + converter.u = input;
> + printf("%05f ", (converter.f + info->offset) * info->scale);
> + break;
> + }
> }
> }
>
> diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c
> index c5c5082cb24e..f1d58db69942 100644
> --- a/tools/iio/iio_utils.c
> +++ b/tools/iio/iio_utils.c
> @@ -70,7 +70,7 @@ int iioutils_break_up_name(const char *full_name, char **generic_name)
>
> /**
> * iioutils_get_type() - find and process _type attribute data
> - * @is_signed: output whether channel is signed
> + * @format: output channel format
> * @bytes: output how many bytes the channel storage occupies
> * @bits_used: output number of valid bits of data
> * @shift: output amount of bits to shift right data before applying bit mask
> @@ -83,7 +83,7 @@ int iioutils_break_up_name(const char *full_name, char **generic_name)
> *
> * Returns a value >= 0 on success, otherwise a negative error code.
> **/
> -static int iioutils_get_type(unsigned int *is_signed, unsigned int *bytes,
> +static int iioutils_get_type(char *format, unsigned int *bytes,
> unsigned int *bits_used, unsigned int *shift,
> uint64_t *mask, unsigned int *be,
> const char *device_dir, int buffer_idx,
> @@ -162,7 +162,7 @@ static int iioutils_get_type(unsigned int *is_signed, unsigned int *bytes,
> else
> *mask = (1ULL << *bits_used) - 1ULL;
>
> - *is_signed = (signchar == 's');
> + *format = signchar;
Should we also rename signchar to formatchar?
> if (fclose(sysfsfp)) {
> ret = -errno;
> fprintf(stderr, "Failed to close %s\n",
> @@ -487,7 +487,7 @@ int build_channel_array(const char *device_dir, int buffer_idx,
> if ((ret < 0) && (ret != -ENOENT))
> goto error_cleanup_array;
>
> - ret = iioutils_get_type(¤t->is_signed,
> + ret = iioutils_get_type(¤t->format,
> ¤t->bytes,
> ¤t->bits_used,
> ¤t->shift,
> diff --git a/tools/iio/iio_utils.h b/tools/iio/iio_utils.h
> index 663c94a6c705..8c72f002d050 100644
> --- a/tools/iio/iio_utils.h
> +++ b/tools/iio/iio_utils.h
> @@ -32,7 +32,7 @@ extern const char *iio_dir;
> * @shift: amount of bits to shift right data before applying bit mask
> * @mask: a bit mask for the raw output
> * @be: flag if data is big endian
> - * @is_signed: is the raw value stored signed
> + * @format: format of the raw value
> * @location: data offset for this channel inside the buffer (in bytes)
> **/
> struct iio_channel_info {
> @@ -46,7 +46,7 @@ struct iio_channel_info {
> unsigned shift;
> uint64_t mask;
> unsigned be;
> - unsigned is_signed;
> + char format;
> unsigned location;
> };
>
© 2016 - 2026 Red Hat, Inc.