Skip to content

Commit

Permalink
iio_writedev: Skip input channels
Browse files Browse the repository at this point in the history
Calling iio_writedev 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 input channels are skipped.

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

The main advantage of this change is that iio_writedev will now exit with an
error when it is accidentally called on an IIO input device, since no
output 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 bb3b072 commit f27f04f
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions tests/iio_writedev.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,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 @@ -391,21 +392,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 output channels found\n");
return EXIT_FAILURE;
}

sample_size = iio_device_get_sample_size(dev);

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

0 comments on commit f27f04f

Please sign in to comment.