From 99cd3acd80a6406e960ea0f5a557375da895b4d6 Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Sun, 16 Jul 2023 23:15:09 -0600 Subject: [PATCH] scanning: add hwmon only systems to scanning. previously scanning on devices like default Raspberry Pi, would show things like: pi@raspberrypi:~ $ iio_info -s No IIO context found. However, this is not correct, since it has HWMON devices (which libiio now understands). So make sure we add this to the scan context. After the patch is applied, we get things like: pi@raspberrypi:~/libiio $ ./build/tests/iio_info -s Available contexts: 0: (cpu_thermal, rpi_volt on Raspberry Pi 4 Model B Rev 1.1) [local:] which is correct. Signed-off-by: Robin Getz --- local.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/local.c b/local.c index b8c4ecbdc..78f2486dc 100644 --- a/local.c +++ b/local.c @@ -2170,7 +2170,7 @@ static int build_names(void *d, const char *path) char *names = (char *)d; size_t len; - if (!strstr(path, "iio:device")) + if (!strstr(path, "iio:device") && !(WITH_HWMON && strstr(path, "class/hwmon"))) return 0; iio_snprintf(buf, sizeof(buf), "%s/name", path); @@ -2195,15 +2195,32 @@ int local_context_scan(struct iio_scan_result *scan_result) bool exists = false; char *desc, *uri, *machine, buf[2 * BUF_SIZE], names[BUF_SIZE]; int ret; + bool no_iio; ret = foreach_in_dir(&exists, "/sys/bus/iio", true, check_device); - if (ret < 0 || !exists) + no_iio = ret == -ENOENT; + if (WITH_HWMON && no_iio) + ret = 0; /* Not an error, unless we also have no hwmon devices */ + if (ret < 0) return 0; names[0] = '\0'; - ret = foreach_in_dir(&names, "/sys/bus/iio/devices", true, build_names); - if (ret < 0) - return 0; + if (!no_iio) { + ret = foreach_in_dir(&names, "/sys/bus/iio/devices", true, build_names); + if (ret < 0) + return 0; + } + + if (WITH_HWMON) { + ret = foreach_in_dir(&exists, "/sys/class/hwmon", true, check_device); + if (ret == -ENOENT && !no_iio) + ret = 0; /* IIO devices but no hwmon devices - not an error */ + if (ret < 0) + return 0; + ret = foreach_in_dir(&names, "/sys/class/hwmon", true, build_names); + if (ret < 0) + return 0; + } machine = cat_file("/sys/firmware/devicetree/base/model"); if (!machine)