Skip to content

Commit

Permalink
tests: Support context timeout in common manner
Browse files Browse the repository at this point in the history
and add it to the utils that did not support it before.

Signed-off-by: Robin Getz <[email protected]>
  • Loading branch information
rgetz committed Jul 7, 2020
1 parent 0150c31 commit 5e6adde
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 47 deletions.
1 change: 1 addition & 0 deletions tests/iio_adi_xflow_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ int main(int argc, char **argv)
case 'n':
case 'x':
case 'u':
case 'T':
break;
case 'S':
case 'a':
Expand Down
1 change: 1 addition & 0 deletions tests/iio_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ int main(int argc, char **argv)
case 'n':
case 'x':
case 'u':
case 'T':
break;
case 'S':
context_scan = true;
Expand Down
23 changes: 23 additions & 0 deletions tests/iio_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ static const struct option common_options[] = {
{"uri", required_argument, 0, 'u'},
{"scan", optional_argument, 0, 'S'},
{"auto", optional_argument, 0, 'a'},
{"timeout", required_argument, 0, 'T'},
{0, 0, 0, 0},
};

Expand Down Expand Up @@ -247,6 +248,8 @@ static const char *common_options_descriptions[] = {
"Scan for available contexts and if a single context is"
"\n\t\t\tavailable use it. <arg> filters backend(s)"
"\n\t\t\t 'ip', 'usb' or 'ip:usb:'",
"Context timeout in milliseconds."
"\n\t\t\t0 = no timeout (wait forever)",
};


Expand All @@ -261,6 +264,7 @@ struct iio_context * handle_common_opts(char * name, int argc,
bool do_scan = false, detect_context = false;
char buf[128];
struct option *opts;
int timeout = -1;

/* Setting opterr to zero disables error messages from getopt_long */
opterr = 0;
Expand Down Expand Up @@ -339,6 +343,13 @@ struct iio_context * handle_common_opts(char * name, int argc,
arg = argv[optind++];
}
break;
case 'T':
if (!optarg) {
fprintf(stderr, "Timeout requires an argument\n");
return NULL;
}
timeout = sanitize_clamp("timeout", optarg, 0, INT_MAX);
break;
case '?':
break;
}
Expand Down Expand Up @@ -372,6 +383,18 @@ struct iio_context * handle_common_opts(char * name, int argc,
fprintf(stderr, "Unable to create Local IIO context : %s\n", buf);
}

if (ctx && timeout >= 0) {
ssize_t ret = iio_context_set_timeout(ctx, timeout);
if (ret < 0) {
char err_str[1024];
iio_strerror(-(int)ret, err_str, sizeof(err_str));
fprintf(stderr, "IIO contexts set timeout failed : %s (%zd)\n",
err_str, ret);
iio_context_destroy(ctx);
return NULL;
}
}

return ctx;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/iio_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ unsigned long int sanitize_clamp(const char *name, const char *argv,
* If such a character is followed by a colon, the option requires an argument.
* Two colons mean an option takes an optional argument.
*/
#define COMMON_OPTIONS "hn:x:u:a::S::"
#define COMMON_OPTIONS "hn:x:u:a::S::T:"

struct iio_context * handle_common_opts(char * name, int argc,
char * const argv[], const char *optstring,
Expand Down
1 change: 1 addition & 0 deletions tests/iio_genxml.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ int main(int argc, char **argv)
case 'n':
case 'x':
case 'u':
case 'T':
break;
case 'S':
case 'a':
Expand Down
1 change: 1 addition & 0 deletions tests/iio_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ int main(int argc, char **argv)
case 'n':
case 'x':
case 'u':
case 'T':
break;
case 'S':
case 'a':
Expand Down
23 changes: 2 additions & 21 deletions tests/iio_readdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,16 @@ static const struct option options[] = {
{"trigger", required_argument, 0, 't'},
{"buffer-size", required_argument, 0, 'b'},
{"samples", required_argument, 0, 's' },
{"timeout", required_argument, 0, 'T'},
{"auto", no_argument, 0, 'a'},
{0, 0, 0, 0},
};

static const char *options_descriptions[] = {
"[-t <trigger>] [-T <timeout-ms>] [-b <buffer-size>]"
"[-t <trigger>] [-b <buffer-size>]"
"[-s <samples>] <iio_device> [<channel> ...]",
"Use the specified trigger.",
"Size of the capture buffer. Default is 256.",
"Number of samples to capture, 0 = infinite. Default is 0.",
"Buffer timeout in milliseconds. 0 = no timeout",
"Scan for available contexts and if only one is available use it.",
};

Expand Down Expand Up @@ -198,7 +196,6 @@ int main(int argc, char **argv)
int c;
struct iio_device *dev;
ssize_t sample_size;
int timeout = -1;
ssize_t ret;
struct option *opts;

Expand All @@ -219,6 +216,7 @@ int main(int argc, char **argv)
case 'n':
case 'x':
case 'u':
case 'T':
break;
case 'S':
case 'a':
Expand Down Expand Up @@ -247,13 +245,6 @@ int main(int argc, char **argv)
}
num_samples = sanitize_clamp("number of samples", optarg, 0, SIZE_MAX);
break;
case 'T':
if (!optarg) {
fprintf(stderr, "Timeout requires an argument\n");
return EXIT_FAILURE;
}
timeout = sanitize_clamp("timeout", optarg, 0, INT_MAX);
break;
case '?':
printf("Unknown argument '%c'\n", c);
return EXIT_FAILURE;
Expand All @@ -272,16 +263,6 @@ int main(int argc, char **argv)

setup_sig_handler();

if (timeout >= 0) {
ret = iio_context_set_timeout(ctx, timeout);
if (ret < 0) {
char err_str[1024];
iio_strerror(-(int)ret, err_str, sizeof(err_str));
fprintf(stderr, "IIO contexts set timeout failed : %s (%zd)\n",
err_str, ret);
}
}

dev = iio_context_find_device(ctx, argw[optind]);
if (!dev) {
fprintf(stderr, "Device %s not found\n", argw[optind]);
Expand Down
1 change: 1 addition & 0 deletions tests/iio_reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ int main(int argc, char **argv)
case 'n':
case 'x':
case 'u':
case 'T':
break;
case 'S':
case 'a':
Expand Down
8 changes: 4 additions & 4 deletions tests/iio_stresstest.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ static const struct option options[] = {
{"uri", required_argument, 0, 'u'},
{"buffer-size", required_argument, 0, 'b'},
{"samples", required_argument, 0, 's' },
{"timeout", required_argument, 0, 't'},
{"Threads", required_argument, 0, 'T'},
{"Timeout", required_argument, 0, 'T'},
{"threads", required_argument, 0, 't'},
{"verbose", no_argument, 0, 'v'},
{0, 0, 0, 0},
};
Expand Down Expand Up @@ -430,13 +430,13 @@ int main(int argc, char **argv)
info.buffer_size = sanitize_clamp("buffersize", info.argv[info.arg_index],
min_samples, 1024 * 1024 * 4);
break;
case 't':
case 'T':
info.arg_index +=2;
/* ensure between least once a day and never (0) */
info.timeout = 1000 * sanitize_clamp("timeout", info.argv[info.arg_index],
0, 60 * 60 * 24);
break;
case 'T':
case 't':
info.arg_index +=2;
/* Max number threads 1024, min 1 */
info.num_threads = sanitize_clamp("threads", info.argv[info.arg_index],
Expand Down
23 changes: 2 additions & 21 deletions tests/iio_writedev.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,18 @@ static const struct option options[] = {
{"trigger", required_argument, 0, 't'},
{"buffer-size", required_argument, 0, 'b'},
{"samples", required_argument, 0, 's' },
{"timeout", required_argument, 0, 'T'},
{"auto", no_argument, 0, 'a'},
{"cyclic", no_argument, 0, 'c'},
{0, 0, 0, 0},
};

static const char *options_descriptions[] = {
"[-t <trigger>] "
"[-T <timeout-ms>] [-b <buffer-size>] [-s <samples>] "
"[-b <buffer-size>] [-s <samples>] "
"<iio_device> [<channel> ...]",
"Use the specified trigger.",
"Size of the transmit buffer. Default is 256.",
"Number of samples to write, 0 = infinite. Default is 0.",
"Buffer timeout in milliseconds. 0 = no timeout",
"Scan for available contexts and if only one is available use it.",
"Use cyclic buffer mode.",
};
Expand Down Expand Up @@ -209,7 +207,6 @@ int main(int argc, char **argv)
int c;
struct iio_device *dev;
ssize_t sample_size;
int timeout = -1;
bool cyclic_buffer = false;
ssize_t ret;
struct option *opts;
Expand All @@ -230,6 +227,7 @@ int main(int argc, char **argv)
case 'n':
case 'x':
case 'u':
case 'T':
break;
case 'S':
case 'a':
Expand Down Expand Up @@ -258,13 +256,6 @@ int main(int argc, char **argv)
}
num_samples = sanitize_clamp("number of samples", optarg, 0, SIZE_MAX);
break;
case 'T':
if (!optarg) {
fprintf(stderr, "Timeout requires argument\n");
return EXIT_FAILURE;
}
timeout = sanitize_clamp("timeout", optarg, 0, INT_MAX);
break;
case 'c':
cyclic_buffer = true;
break;
Expand All @@ -286,16 +277,6 @@ int main(int argc, char **argv)

setup_sig_handler();

if (timeout >= 0) {
ret = iio_context_set_timeout(ctx, timeout);
if (ret < 0) {
char err_str[1024];
iio_strerror(-(int)ret, err_str, sizeof(err_str));
fprintf(stderr, "IIO contexts set timeout failed : %s (%zd)\n",
err_str, ret);
}
}

dev = iio_context_find_device(ctx, argw[optind]);
if (!dev) {
fprintf(stderr, "Device %s not found\n", argw[optind]);
Expand Down

0 comments on commit 5e6adde

Please sign in to comment.