Skip to content

Commit

Permalink
iio_readdev/iio_writedev: print out examples based on context
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rgetz authored and pcercuei committed Feb 17, 2022
1 parent 8268715 commit 8a94f8d
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 6 deletions.
47 changes: 44 additions & 3 deletions tests/iio_readdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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]);
Expand Down Expand Up @@ -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);
Expand Down
47 changes: 44 additions & 3 deletions tests/iio_writedev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 8a94f8d

Please sign in to comment.