Skip to content

Commit

Permalink
iio_readdev: Skip output channels
Browse files Browse the repository at this point in the history
Calling iio_readdev without any channels specified will enable all channels
of the device. Regardless whether the channel is an input or output
channel. Change this so that output channels are skipped.

Similarly ignore explicitly specified channels if they are an output
channel.

The main advantage of this change is that iio_readdev will now exit with an
error when it is accidentally called on an IIO output device, since no
input channels will be found.

But it also future-proofs the application for a time when there is support
for devices which have both input and output channels.

Signed-off-by: Lars-Peter Clausen <[email protected]>
  • Loading branch information
larsclausen committed Mar 25, 2020
1 parent 4ce82fa commit bb3b072
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions tests/iio_readdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ static struct iio_context *scan(void)
int main(int argc, char **argv)
{
unsigned int i, nb_channels;
unsigned int nb_active_channels = 0;
unsigned int buffer_size = SAMPLES_PER_READ;
const char *arg_uri = NULL;
const char *arg_ip = NULL;
Expand Down Expand Up @@ -378,21 +379,34 @@ int main(int argc, char **argv)

if (argc == optind + 1) {
/* Enable all channels */
for (i = 0; i < nb_channels; i++)
iio_channel_enable(iio_device_get_channel(dev, i));
for (i = 0; i < nb_channels; i++) {
struct iio_channel *ch = iio_device_get_channel(dev, i);
if (!iio_channel_is_output(ch)) {
iio_channel_enable(ch);
nb_active_channels++;
}
}
} else {
for (i = 0; i < nb_channels; i++) {
unsigned int j;
struct iio_channel *ch = iio_device_get_channel(dev, i);
for (j = optind + 1; j < (unsigned int) argc; j++) {
const char *n = iio_channel_get_name(ch);
if (!strcmp(argv[j], iio_channel_get_id(ch)) ||
(n && !strcmp(n, argv[j])))
if ((!strcmp(argv[j], iio_channel_get_id(ch)) ||
(n && !strcmp(n, argv[j]))) &&
!iio_channel_is_output(ch)) {
iio_channel_enable(ch);
nb_active_channels++;
}
}
}
}

if (!nb_active_channels) {
fprintf(stderr, "No input channels found.\n");
return EXIT_FAILURE;
}

sample_size = iio_device_get_sample_size(dev);

buffer = iio_device_create_buffer(dev, buffer_size, false);
Expand Down

0 comments on commit bb3b072

Please sign in to comment.