Skip to content

Commit

Permalink
iio_utils: now that we support backend filtering, do so.
Browse files Browse the repository at this point in the history
On the -S (scan) and -a (connect automatically).

This enables things like any iio_utility to do something like:
(only scan USB context) iio_info -S usb
(only attach to USB context) iio_info -a usb:
(only scan USB or IP context) iio_info -S usb:ip:

Signed-off-by: Robin Getz <[email protected]>
  • Loading branch information
rgetz committed Jun 1, 2020
1 parent d532845 commit 53c4add
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 18 deletions.
5 changes: 4 additions & 1 deletion tests/iio_adi_xflow_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,12 @@ int main(int argc, char **argv)
case 'h':
case 'n':
case 'x':
case 'S':
case 'u':
break;
case 'S':
case 'a':
if (!optarg && argv[optind] != NULL && argv[optind][0] != '-')
optind++;
break;

case 's':
Expand Down
5 changes: 4 additions & 1 deletion tests/iio_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,12 @@ int main(int argc, char **argv)
case 'h':
case 'n':
case 'x':
case 'S':
case 'u':
break;
case 'S':
case 'a':
if (!optarg && argv[optind] != NULL && argv[optind][0] != '-')
optind++;
break;
/* Attribute type
* 'd'evice, 'c'hannel, 'C'ontext, 'B'uffer or 'D'ebug
Expand Down
31 changes: 23 additions & 8 deletions tests/iio_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ char *cmn_strndup(const char *str, size_t n)



struct iio_context * autodetect_context(bool rtn, bool gen_code, const char * name)
struct iio_context * autodetect_context(bool rtn, bool gen_code, const char * name, const char * scan)
{
struct iio_scan_context *scan_ctx;
struct iio_context_info **info;
Expand All @@ -81,7 +81,7 @@ struct iio_context * autodetect_context(bool rtn, bool gen_code, const char * na
ssize_t ret;
FILE *out;

scan_ctx = iio_create_scan_context(NULL, 0);
scan_ctx = iio_create_scan_context(scan, 0);
if (!scan_ctx) {
fprintf(stderr, "Unable to create scan context\n");
return NULL;
Expand Down Expand Up @@ -195,17 +195,19 @@ static const struct option common_options[] = {
{"help", no_argument, 0, 'h'},
{"xml", required_argument, 0, 'x'},
{"uri", required_argument, 0, 'u'},
{"scan", no_argument, 0, "S"},
{"auto", no_argument, 0, "a"},
{"scan", optional_argument, 0, "S"},
{"auto", optional_argument, 0, "a"},
{0, 0, 0, 0},
};

static const char *common_options_descriptions[] = {
"Show this help and quit.",
"Use the XML backend with the provided XML file.",
"Use the context at the provided URI.",
"Scan for available backends.",
"Scan for available contexts and if only one is available use it.",
"Scan for available backends."
"\n\t\t\toptional arg of specific backend(s)",
"Scan for available contexts and if only one is available use it."
"\n\t\t\toptional arg of specific backend(s)",
};


Expand All @@ -222,6 +224,7 @@ struct iio_context * handle_common_opts(char * name, int argc, char * const argv
opterr = 0;
/* start over at first index */
optind = 1;

while ((c = getopt_long(argc, argv, COMMON_OPTIONS, /* Flawfinder: ignore */
common_options, &option_index)) != -1) {
switch (c) {
Expand Down Expand Up @@ -258,9 +261,21 @@ struct iio_context * handle_common_opts(char * name, int argc, char * const argv
return NULL;
}
detect_context = true;
if (optarg) {
arg = optarg;
} else {
if (argv[optind] && argv[optind][0] != '-')
arg = argv[optind++];
}
break;
case 'S':
do_scan = true;
if (optarg) {
arg = optarg;
} else {
if (argv[optind] && argv[optind][0] != '-')
arg = argv[optind++];
}
break;
case '?':
break;
Expand All @@ -270,10 +285,10 @@ struct iio_context * handle_common_opts(char * name, int argc, char * const argv
opterr = 1;

if (do_scan) {
autodetect_context(false, false, name);
autodetect_context(false, false, name, arg);
exit(0);
} else if (detect_context)
ctx = autodetect_context(true, false, name);
ctx = autodetect_context(true, false, name, arg);
else if (!arg && backend != IIO_LOCAL)
fprintf(stderr, "argument parsing error\n");
else if (backend == IIO_XML)
Expand Down
4 changes: 2 additions & 2 deletions tests/iio_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ enum backend {
void * xmalloc(size_t n, const char *name);
char *cmn_strndup(const char *str, size_t n);

struct iio_context * autodetect_context(bool rtn, bool gen_code, const char *name);
struct iio_context * autodetect_context(bool rtn, bool gen_code, const char *name, const char *scan);
unsigned long int sanitize_clamp(const char *name, const char *argv,
uint64_t min, uint64_t max);

#define COMMON_OPTIONS "hn:x:u:aS"
#define COMMON_OPTIONS "hn:x:u:a::S::"
struct iio_context * handle_common_opts(char * name, int argc, char * const argv[],
const struct option *options, const char *options_descriptions[]);
void usage(char *name, const struct option *options, const char *options_descriptions[]);
Expand Down
5 changes: 4 additions & 1 deletion tests/iio_genxml.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ int main(int argc, char **argv)
case 'h':
case 'n':
case 'x':
case 'S':
case 'u':
break;
case 'S':
case 'a':
if (!optarg && argv[optind] != NULL && argv[optind][0] != '-')
optind++;
break;
case '?':
printf("Unknown argument '%c'\n", c);
Expand Down
7 changes: 5 additions & 2 deletions tests/iio_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,15 @@ int main(int argc, char **argv)
case 'h':
case 'n':
case 'x':
case 'S':
case 'u':
break;
case 'S':
case 'a':
if (!optarg && argv[optind] != NULL && argv[optind][0] != '-')
optind++;
break;
case 's':
autodetect_context(false, false, MY_NAME);
autodetect_context(false, false, MY_NAME, NULL);
return EXIT_SUCCESS;
case '?':
printf("Unknown argument '%c'\n", c);
Expand Down
5 changes: 4 additions & 1 deletion tests/iio_readdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,12 @@ int main(int argc, char **argv)
case 'h':
case 'n':
case 'x':
case 'S':
case 'u':
break;
case 'S':
case 'a':
if (!optarg && argv[optind] != NULL && argv[optind][0] != '-')
optind++;
break;
case 't':
trigger_name = optarg;
Expand Down
5 changes: 4 additions & 1 deletion tests/iio_reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,12 @@ int main(int argc, char **argv)
case 'h':
case 'n':
case 'x':
case 'S':
case 'u':
break;
case 'S':
case 'a':
if (!optarg && argv[optind] != NULL && argv[optind][0] != '-')
optind++;
break;
case '?':
printf("Unknown argument '%c'\n", c);
Expand Down
5 changes: 4 additions & 1 deletion tests/iio_writedev.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,12 @@ int main(int argc, char **argv)
case 'h':
case 'n':
case 'x':
case 'S':
case 'u':
break;
case 'S':
case 'a':
if (!optarg && argv[optind] != NULL && argv[optind][0] != '-')
optind++;
break;
case 't':
trigger_name = optarg;
Expand Down

0 comments on commit 53c4add

Please sign in to comment.