Skip to content

Commit bb3b072

Browse files
committed
iio_readdev: Skip output channels
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]>
1 parent 4ce82fa commit bb3b072

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

tests/iio_readdev.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ static struct iio_context *scan(void)
253253
int main(int argc, char **argv)
254254
{
255255
unsigned int i, nb_channels;
256+
unsigned int nb_active_channels = 0;
256257
unsigned int buffer_size = SAMPLES_PER_READ;
257258
const char *arg_uri = NULL;
258259
const char *arg_ip = NULL;
@@ -378,21 +379,34 @@ int main(int argc, char **argv)
378379

379380
if (argc == optind + 1) {
380381
/* Enable all channels */
381-
for (i = 0; i < nb_channels; i++)
382-
iio_channel_enable(iio_device_get_channel(dev, i));
382+
for (i = 0; i < nb_channels; i++) {
383+
struct iio_channel *ch = iio_device_get_channel(dev, i);
384+
if (!iio_channel_is_output(ch)) {
385+
iio_channel_enable(ch);
386+
nb_active_channels++;
387+
}
388+
}
383389
} else {
384390
for (i = 0; i < nb_channels; i++) {
385391
unsigned int j;
386392
struct iio_channel *ch = iio_device_get_channel(dev, i);
387393
for (j = optind + 1; j < (unsigned int) argc; j++) {
388394
const char *n = iio_channel_get_name(ch);
389-
if (!strcmp(argv[j], iio_channel_get_id(ch)) ||
390-
(n && !strcmp(n, argv[j])))
395+
if ((!strcmp(argv[j], iio_channel_get_id(ch)) ||
396+
(n && !strcmp(n, argv[j]))) &&
397+
!iio_channel_is_output(ch)) {
391398
iio_channel_enable(ch);
399+
nb_active_channels++;
400+
}
392401
}
393402
}
394403
}
395404

405+
if (!nb_active_channels) {
406+
fprintf(stderr, "No input channels found.\n");
407+
return EXIT_FAILURE;
408+
}
409+
396410
sample_size = iio_device_get_sample_size(dev);
397411

398412
buffer = iio_device_create_buffer(dev, buffer_size, false);

0 commit comments

Comments
 (0)