From 9bfe89c5ffb82d9aaddd6e3689c0fbb9b426fef2 Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Fri, 22 May 2020 04:43:28 +0000 Subject: [PATCH] local: add some more info to local scan contexts In the past, when doing an 'iio_info -s' on machines with local contexts, you would get something like: "Local devices" which isn't exactly descriptive... look in sysfs for the machine (DT model for embedded ARM and DRI for x86), and combine that with a device list so now you get something that looks like: Available contexts: 0: (spi1.0, ad9361-phy, cf-ad9361-lpc, spi1.1, xadc, ad7291, cf-ad9361-dds-core-lpc on Xilinx Zynq ZED) [local:] or Available contexts: 0: (ad7124-4 on Raspberry Pi 4 Model B Rev 1.1) [local:] which makes it easier to identify for end users. Signed-off-by: Robin Getz --- local.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/local.c b/local.c index dd7a8d108..74a34a10e 100644 --- a/local.c +++ b/local.c @@ -2080,6 +2080,48 @@ struct iio_context * local_create_context(void) return NULL; } +#define BUF_SIZE 128 + +static char * cat_file(const char *path) +{ + char buf[BUF_SIZE]; + ssize_t ret; + + FILE *f; + + f = fopen(path, "re"); + if (!f) + return NULL; + + ret = fread(buf, 1, sizeof(buf)-1, f); + fclose(f); + if (ret > 0) + buf[ret - 1] = '\0'; + else + return NULL; + + return strndup(buf, sizeof(buf) - 1); +} + +static int build_names(void *d, const char *path) +{ + char buf[BUF_SIZE], *dst; + char *names = (char *)d; + size_t len; + + if (!strstr(path, "iio:device")) + return 0; + + iio_snprintf(buf, sizeof(buf), "%s/name", path); + dst = cat_file(buf); + if (dst) { + len = strnlen(names, sizeof(buf)); + iio_snprintf(&names[len], BUF_SIZE - len - 1, "%s, ", dst); + free(dst); + } + return 0; +} + static int check_device(void *d, const char *path) { *(bool *)d = true; @@ -2090,14 +2132,34 @@ int local_context_scan(struct iio_scan_result *scan_result) { struct iio_context_info **info; bool exists = false; - char *desc, *uri; + char *desc, *uri, *machine, buf[2 * BUF_SIZE], names[BUF_SIZE]; int ret; ret = foreach_in_dir(&exists, "/sys/bus/iio", true, check_device); if (ret < 0 || !exists) return 0; - desc = iio_strdup("Local devices"); + names[0] = '\0'; + ret = foreach_in_dir(&names, "/sys/bus/iio/devices", true, build_names); + if (ret < 0) + return 0; + + machine = cat_file("/sys/firmware/devicetree/base/model"); + if (!machine) + machine = cat_file("/sys/class/dmi/id/board_vendor"); + + if (machine) { + if (names[0]) { + ret = strnlen(names, sizeof(names)); + names[ret - 2] = '\0'; + iio_snprintf(buf, sizeof(buf), "(%s on %s)", names, machine); + } else + iio_snprintf(buf, sizeof(buf), "(Local IIO devices on %s)", machine); + free(machine); + desc = iio_strdup(buf); + } else { + desc = iio_strdup("(Local IIO devices)"); + } if (!desc) return -ENOMEM;