Skip to content

Commit

Permalink
iio.h: Add function iio_device_is_hwmon()
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
pcercuei committed Jan 11, 2022
1 parent 145b322 commit 626e910
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 0 additions & 5 deletions iio-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__ */
12 changes: 12 additions & 0 deletions iio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 -----------------------------*/
Expand Down
12 changes: 6 additions & 6 deletions local.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 626e910

Please sign in to comment.