Skip to content

Commit

Permalink
local: add some more info to local scan contexts
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rgetz committed May 22, 2020
1 parent ab9a0e7 commit 9bfe89c
Showing 1 changed file with 64 additions and 2 deletions.
66 changes: 64 additions & 2 deletions local.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down

0 comments on commit 9bfe89c

Please sign in to comment.