Skip to content

Commit

Permalink
iio_attr: make dealing with devices with no name easier.
Browse files Browse the repository at this point in the history
----------
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 <[email protected]>
  • Loading branch information
rgetz committed May 4, 2020
1 parent 3a202a5 commit 3f69133
Showing 1 changed file with 60 additions and 24 deletions.
84 changes: 60 additions & 24 deletions tests/iio_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand All @@ -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));

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 3f69133

Please sign in to comment.