From 8a94f8d828f18f310aa1f17523f4ebb21b510013 Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Mon, 15 Nov 2021 18:24:25 -0500 Subject: [PATCH] iio_readdev/iio_writedev: print out examples based on context If you are given a context, but no device/channel, walk through things and print out examples which you might be able to use, as a hint for people. (so I can stop telling them to do it with iio_attr manually). Signed-off-by: Robin Getz --- tests/iio_readdev.c | 47 +++++++++++++++++++++++++++++++++++++++++--- tests/iio_writedev.c | 47 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 88 insertions(+), 6 deletions(-) diff --git a/tests/iio_readdev.c b/tests/iio_readdev.c index b200fd761..1d250b16f 100644 --- a/tests/iio_readdev.c +++ b/tests/iio_readdev.c @@ -195,7 +195,7 @@ static ssize_t print_sample(const struct iio_channel *chn, int main(int argc, char **argv) { char **argw; - unsigned int i, nb_channels; + unsigned int i, j, nb_channels; unsigned int nb_active_channels = 0; unsigned int buffer_size = SAMPLES_PER_READ; unsigned int refill_per_benchmark = REFILL_PER_BENCHMARK; @@ -263,7 +263,7 @@ int main(int argc, char **argv) } free(opts); - if (argc == optind) { + if (argc < optind || argc > optind + 2) { fprintf(stderr, "Incorrect number of arguments.\n\n"); usage(MY_NAME, options, options_descriptions); return EXIT_FAILURE; @@ -272,6 +272,48 @@ int main(int argc, char **argv) if (!ctx) return EXIT_FAILURE; + if (!argw[optind]) { + unsigned int nb_devices = iio_context_get_devices_count(ctx); + + for (i = 0; i < nb_devices; i++) { + const char *dev_id, *label, *name; + bool hit; + + dev = iio_context_get_device(ctx, i); + nb_channels = iio_device_get_channels_count(dev); + + if (!nb_channels) + continue; + + hit = false; + for (j = 0; j < nb_channels; j++) { + struct iio_channel *ch = iio_device_get_channel(dev, j); + + if (!iio_channel_is_scan_element(ch) || + iio_channel_is_output(ch)) + continue; + + hit = true; + + dev_id = iio_device_get_id(dev); + label = iio_device_get_label(dev); + name = iio_device_get_name(dev); + + printf("Example : " MY_NAME " -u %s -b 256 -s 1024 %s %s\n", + iio_context_get_attr_value(ctx, "uri"), + label ? label : name ? name : dev_id, + iio_channel_get_id(ch)); + } + if (hit) + printf("Example : " MY_NAME " -u %s -b 256 -s 1024 %s\n", + iio_context_get_attr_value(ctx, "uri"), + label ? label : name ? name : dev_id); + } + iio_context_destroy(ctx); + usage(MY_NAME, options, options_descriptions); + return EXIT_FAILURE; + } + setup_sig_handler(); dev = iio_context_find_device(ctx, argw[optind]); @@ -332,7 +374,6 @@ int main(int argc, char **argv) } } 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); diff --git a/tests/iio_writedev.c b/tests/iio_writedev.c index ea74c2ec7..2491e45b1 100644 --- a/tests/iio_writedev.c +++ b/tests/iio_writedev.c @@ -206,7 +206,7 @@ static ssize_t read_sample(const struct iio_channel *chn, int main(int argc, char **argv) { char **argw; - unsigned int i, nb_channels; + unsigned int i, j, nb_channels; unsigned int nb_active_channels = 0; unsigned int buffer_size = SAMPLES_PER_READ; unsigned int refill_per_benchmark = REFILL_PER_BENCHMARK; @@ -276,7 +276,7 @@ int main(int argc, char **argv) } free(opts); - if (argc == optind) { + if (argc < optind || argc > optind + 2) { fprintf(stderr, "Incorrect number of arguments.\n\n"); usage(MY_NAME, options, options_descriptions); return EXIT_FAILURE; @@ -285,6 +285,48 @@ int main(int argc, char **argv) if (!ctx) return EXIT_FAILURE; + if (!argw[optind]) { + unsigned int nb_devices = iio_context_get_devices_count(ctx); + + for (i = 0; i < nb_devices; i++) { + const char *dev_id, *label, *name; + bool hit; + + dev = iio_context_get_device(ctx, i); + nb_channels = iio_device_get_channels_count(dev); + + if (!nb_channels) + continue; + + hit = false; + for (j = 0; j < nb_channels; j++) { + struct iio_channel *ch = iio_device_get_channel(dev, j); + + if (!iio_channel_is_scan_element(ch) || + !iio_channel_is_output(ch)) + continue; + + hit = true; + + dev_id = iio_device_get_id(dev); + label = iio_device_get_label(dev); + name = iio_device_get_name(dev); + + printf("Example : " MY_NAME " -u %s -b 256 -s 1024 %s %s\n", + iio_context_get_attr_value(ctx, "uri"), + label ? label : name ? name : dev_id, + iio_channel_get_id(ch)); + } + if (hit) + printf("Example : " MY_NAME " -u %s -b 256 -s 1024 %s\n", + iio_context_get_attr_value(ctx, "uri"), + label ? label : name ? name : dev_id); + } + iio_context_destroy(ctx); + usage(MY_NAME, options, options_descriptions); + return EXIT_FAILURE; + } + if (benchmark && cyclic_buffer) { fprintf(stderr, "Cannot benchmark in cyclic mode.\n"); iio_context_destroy(ctx); @@ -351,7 +393,6 @@ int main(int argc, char **argv) } } 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);