Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow multiple scan backends #528

Merged
merged 2 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions iio.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,10 @@ enum iio_modifier {


/** @brief Create a scan context
* @param backend A NULL-terminated string containing the backend to use for
* scanning. If NULL, all the available backends are used.
* @param backend A NULL-terminated string containing the backend(s) to use for
* scanning (example: pre version 0.20 : "local", "ip", or "usb"; post version
* 0.20 can handle multiple, including "local:usb:", "ip:usb:", "local:usb:ip:").
* If NULL, all the available backends are used.
* @param flags Unused for now. Set to 0.
* @return on success, a pointer to a iio_scan_context structure
* @return On failure, NULL is returned and errno is set appropriately */
Expand Down
6 changes: 3 additions & 3 deletions scan.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,15 @@ struct iio_scan_context * iio_create_scan_context(
return NULL;
}

if (!backend || !strcmp(backend, "local"))
if (!backend || strstr(backend, "local"))
ctx->scan_local = true;

#ifdef WITH_USB_BACKEND
if (!backend || !strcmp(backend, "usb"))
if (!backend || strstr(backend, "usb"))
ctx->usb_ctx = usb_context_scan_init();
#endif
#ifdef HAVE_DNS_SD
if (!backend || !strcmp(backend, "ip"))
if (!backend || strstr(backend, "ip"))
ctx->dnssd_ctx = dnssd_context_scan_init();
#endif

Expand Down
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