From 626e910d353e9a88dde6862213ab1cf93cc1c8d6 Mon Sep 17 00:00:00 2001 From: Paul Cercueil Date: Fri, 7 Jan 2022 11:30:58 +0000 Subject: [PATCH] iio.h: Add function iio_device_is_hwmon() This function can be used to detect if a given device is a hardware monitoring device, or a IIO device. Signed-off-by: Paul Cercueil --- channel.c | 2 +- iio-private.h | 5 ----- iio.h | 12 ++++++++++++ local.c | 12 ++++++------ 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/channel.c b/channel.c index 7a60f6572..b89413481 100644 --- a/channel.c +++ b/channel.c @@ -177,7 +177,7 @@ void iio_channel_init_finalize(struct iio_channel *chn) char *mod; int type; - if (iio_device_is_hwmon(chn->dev)) { + if (WITH_HWMON && iio_device_is_hwmon(chn->dev)) { type = iio_channel_find_type(chn->id, hwmon_chan_type_name_spec, ARRAY_SIZE(hwmon_chan_type_name_spec)); } else { diff --git a/iio-private.h b/iio-private.h index e0a992d2f..41872d154 100644 --- a/iio-private.h +++ b/iio-private.h @@ -291,9 +291,4 @@ static inline void iio_update_xml_indexes(ssize_t ret, char **ptr, ssize_t *len, bool iio_channel_is_hwmon(const char *id); -static inline bool iio_device_is_hwmon(const struct iio_device *dev) -{ - return WITH_HWMON && dev->id[0] == 'h'; -} - #endif /* __IIO_PRIVATE_H__ */ diff --git a/iio.h b/iio.h index 7bd795c29..2f6a44321 100644 --- a/iio.h +++ b/iio.h @@ -1620,6 +1620,18 @@ hwmon_channel_get_type(const struct iio_channel *chn) return (enum hwmon_chan_type) iio_channel_get_type(chn); } +/** + * @brief Get whether or not the device is a hardware monitoring device + * @param dev A pointer to an iio_device structure + * @return True if the device is a hardware monitoring device, + * false if it is a IIO device */ +static inline bool iio_device_is_hwmon(const struct iio_device *dev) +{ + const char *id = iio_device_get_id(dev); + + return id[0] == 'h'; +} + /** @} *//* ------------------------------------------------------------------*/ /* ------------------------- Low-level functions -----------------------------*/ diff --git a/local.c b/local.c index edf2c9c32..02088c629 100644 --- a/local.c +++ b/local.c @@ -676,7 +676,7 @@ static ssize_t local_read_dev_attr(const struct iio_device *dev, switch (type) { case IIO_ATTR_TYPE_DEVICE: - if (iio_device_is_hwmon(dev)) { + if (WITH_HWMON && iio_device_is_hwmon(dev)) { iio_snprintf(buf, sizeof(buf), "/sys/class/hwmon/%s/%s", dev->id, attr); } else { @@ -730,7 +730,7 @@ static ssize_t local_write_dev_attr(const struct iio_device *dev, switch (type) { case IIO_ATTR_TYPE_DEVICE: - if (iio_device_is_hwmon(dev)) { + if (WITH_HWMON && iio_device_is_hwmon(dev)) { iio_snprintf(buf, sizeof(buf), "/sys/class/hwmon/%s/%s", dev->id, attr); } else { @@ -1144,7 +1144,7 @@ static bool is_channel(const struct iio_device *dev, const char *attr, bool stri { char *ptr = NULL; - if (iio_device_is_hwmon(dev)) + if (WITH_HWMON && iio_device_is_hwmon(dev)) return iio_channel_is_hwmon(attr); if (!strncmp(attr, "in_timestamp_", sizeof("in_timestamp_") - 1)) return true; @@ -1169,7 +1169,7 @@ static char * get_channel_id(struct iio_device *dev, const char *attr) char *res, *ptr = strchr(attr, '_'); size_t len; - if (!iio_device_is_hwmon(dev)) { + if (!WITH_HWMON || !iio_device_is_hwmon(dev)) { attr = ptr + 1; ptr = strchr(attr, '_'); if (find_channel_modifier(ptr + 1, &len) != IIO_NO_MOD) @@ -1196,7 +1196,7 @@ static char * get_short_attr_name(struct iio_channel *chn, const char *attr) char *ptr = strchr(attr, '_'); size_t len; - if (iio_device_is_hwmon(chn->dev)) { + if (WITH_HWMON && iio_device_is_hwmon(chn->dev)) { /* * PWM hwmon devices can have an attribute named directly after * the channel's ID; in that particular case we don't need to @@ -1457,7 +1457,7 @@ static struct iio_channel *create_channel(struct iio_device *dev, if (!chn->pdata) goto err_free_chn; - if (!iio_device_is_hwmon(dev)) { + if (!WITH_HWMON || !iio_device_is_hwmon(dev)) { if (!strncmp(attr, "out_", 4)) { chn->is_output = true; } else if (strncmp(attr, "in_", 3)) {