From 3f69133167215b2da00054fd2440d9b8490f9ac6 Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Mon, 4 May 2020 13:05:31 -0400 Subject: [PATCH 1/2] iio_attr: make dealing with devices with no name easier. ---------- I've been through libiio on devices with no name It felt good to be out of the rain In the libiio you don't need remember your name 'Cause there ain't no one for to give you no pain apologies Dewey Bunnell ----------- Some properly formed iio devices have no name attribute, so don't force people to use a name when refering to them. Now when you find devices like this (no name), you can refer to things by the ID. $ ./tests/iio_attr -u ip:192.168.1.120 -d IIO context has 7 devices: iio:device1, ad9361-phy: found 18 device attributes iio:device3, cf-ad9361-dds-core-lpc: found 0 device attributes iio:device4, cf-ad9361-lpc: found 0 device attributes iio_sysfs_trigger: found 2 device attributes trigger0, sysfstrig1: found 1 device attributes iio ID and name can be used interchangeably $ ./tests/iio_attr -u ip:192.168.1.120 -d iio:device2 dev 'xadc', attr 'sampling_frequency', value :'961538' $ ./tests/iio_attr -u ip:192.168.1.120 -d xadc dev 'xadc', attr 'sampling_frequency', value :'961538' And when there is no name at all, you can just use the ID. $ ./tests/iio_attr -u ip:192.168.1.120 -d iio_sysfs_trigger dev 'iio_sysfs_trigger', attr 'add_trigger', value :ERROR: Unknown error 13 (-13) dev 'iio_sysfs_trigger', attr 'remove_trigger', value :ERROR: Unknown error 13 (-13) devices which are triggers will show up as triggers: ./tests/iio_attr -u ip:192.168.1.120 -d sysfstrig1 trig 'sysfstrig1', attr 'trigger_now', value :ERROR: Unknown error 13 (-13) Signed-off-by: Robin Getz --- tests/iio_attr.c | 84 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 60 insertions(+), 24 deletions(-) diff --git a/tests/iio_attr.c b/tests/iio_attr.c index d054efee7..9df19ebf5 100644 --- a/tests/iio_attr.c +++ b/tests/iio_attr.c @@ -98,11 +98,18 @@ static void dump_device_attributes(const struct iio_device *dev, { ssize_t ret; char *buf = xmalloc(BUF_SIZE, MY_NAME); + const char *name = iio_device_get_name(dev); if (!wbuf || !quiet) { - if (!quiet) - printf("dev '%s', attr '%s', value :", - iio_device_get_name(dev), attr); + if (!quiet) { + printf("%s ", iio_device_is_trigger(dev) ? "trig" : "dev"); + if(name) + printf("'%s'", name); + else + printf("'%s'", iio_device_get_id(dev)); + + printf(", attr '%s', value :", attr); + } gen_function("device", "dev", attr, NULL); ret = iio_device_attr_read(dev, attr, buf, BUF_SIZE); if (ret > 0) { @@ -136,14 +143,21 @@ static void dump_buffer_attributes(const struct iio_device *dev, { ssize_t ret; char *buf = xmalloc(BUF_SIZE, MY_NAME); + const char *name = iio_device_get_name(dev); if (!wbuf || !quiet) { gen_function("device_buffer", "dev", attr, NULL); ret = iio_device_buffer_attr_read(dev, attr, buf, BUF_SIZE); - if (!quiet) - printf("dev '%s', buffer attr '%s', value :", - iio_device_get_name(dev), attr); + if (!quiet) { + printf("%s ", iio_device_is_trigger(dev) ? "trig" : "dev"); + if (name) + printf("'%s'", name); + else + printf("'%s'", iio_device_get_id(dev)); + + printf(", buffer attr '%s', value :", attr); + } if (ret > 0) { if (quiet) @@ -178,14 +192,22 @@ static void dump_debug_attributes(const struct iio_device *dev, { ssize_t ret; char *buf = xmalloc(BUF_SIZE, MY_NAME); + const char *name = iio_device_get_name(dev); if (!wbuf || !quiet) { gen_function("device_debug", "dev", attr, NULL); ret = iio_device_debug_attr_read(dev, attr, buf, BUF_SIZE); - if (!quiet) - printf("dev '%s', debug attr '%s', value :", - iio_device_get_name(dev), attr); + if (!quiet) { + printf("%s ", iio_device_is_trigger(dev) ? "trig" : "dev"); + if (name) + printf("'%s'", name); + else + printf("'%s'", iio_device_get_id(dev)); + + printf(", debug attr '%s', value :", attr); + + } if (ret > 0) { if (quiet) @@ -221,6 +243,7 @@ static void dump_channel_attributes(const struct iio_device *dev, ssize_t ret; char *buf = xmalloc(BUF_SIZE, MY_NAME); const char *type_name; + const char *name = iio_device_get_name(dev); if (!wbuf || !quiet) { if (iio_channel_is_output(ch)) @@ -230,11 +253,17 @@ static void dump_channel_attributes(const struct iio_device *dev, gen_function("channel", "ch", attr, NULL); ret = iio_channel_attr_read(ch, attr, buf, BUF_SIZE); - if (!quiet) - printf("dev '%s', channel '%s' (%s), ", - iio_device_get_name(dev), + if (!quiet) { + printf("%s ", iio_device_is_trigger(dev) ? "trig" : "dev"); + if (name) + printf("'%s'", name); + else + printf("'%s'", iio_device_get_id(dev)); + + printf(", channel '%s' (%s), ", iio_channel_get_id(ch), type_name); + } if (iio_channel_get_name(ch) && !quiet) printf("id '%s', ", iio_channel_get_name(ch)); @@ -576,18 +605,21 @@ int main(int argc, char **argv) for (i = 0; i < nb_devices; i++) { const struct iio_device *dev = iio_context_get_device(ctx, i); const char *name = iio_device_get_name(dev); + const char *ch_name; + const char *dev_id = iio_device_get_id(dev); unsigned int nb_attrs, nb_channels, j; - - if (device_index && !str_match(name, argv[device_index], ignore_case)) + if (device_index && !str_match(dev_id, argv[device_index], ignore_case) + && !str_match(name, argv[device_index], ignore_case)) { continue; + } if ((search_device && !device_index) || (search_channel && !device_index) || (search_buffer && !device_index) || (search_debug && !device_index)) { - printf("\t%s:", iio_device_get_id(dev)); + printf("\t%s", dev_id); if (name) - printf(" %s", name); - printf(", "); + printf(", %s", name); + printf(": "); } if (search_channel && !device_index) @@ -616,22 +648,26 @@ int main(int argc, char **argv) else type_name = "input"; - name = iio_channel_get_name(ch); + ch_name = iio_channel_get_name(ch); if (channel_index && !str_match(iio_channel_get_id(ch), argv[channel_index], ignore_case) && - (!name || (name && - !str_match( name,argv[channel_index], ignore_case)))) + (!ch_name || !str_match(ch_name,argv[channel_index], ignore_case))) continue; if ((!scan_only && !channel_index) || ( scan_only && iio_channel_is_scan_element(ch))) { - printf("dev '%s', channel '%s'", - iio_device_get_name(dev), + printf("%s ", iio_device_is_trigger(dev) ? "trig" : "dev"); + if (name) + printf("'%s', ", name); + else + printf("'%s', ", dev_id); + + printf("channel '%s'", iio_channel_get_id(ch)); - if (name) - printf(", id '%s'", name); + if (ch_name) + printf(", id '%s'", ch_name); printf(" (%s", type_name); From 886b643fb707d5c7e22acdbb034cd1e47acd4228 Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Mon, 4 May 2020 13:17:35 -0400 Subject: [PATCH 2/2] iio_common: fix an off by one that was introduced during 9a007668 found this while debugging trigger issues. Signed-off-by: Robin Getz --- tests/iio_common.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/iio_common.c b/tests/iio_common.c index b5cc9b76e..220d40310 100644 --- a/tests/iio_common.c +++ b/tests/iio_common.c @@ -56,11 +56,10 @@ char *cmn_strndup(const char *str, size_t n) #else size_t len = strnlen(str, n + 1); char *buf = malloc(len + 1); - if (buf) { /* len = size of buf, so memcpy is OK */ memcpy(buf, str, len); /* Flawfinder: ignore */ - buf[len + 1] = 0; + buf[len] = 0; } return buf; #endif