From 5e6addefffa04fb8c3a7536bde45eb72fed6a30d Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Tue, 7 Jul 2020 11:59:19 -0400 Subject: [PATCH] tests: Support context timeout in common manner and add it to the utils that did not support it before. Signed-off-by: Robin Getz --- tests/iio_adi_xflow_check.c | 1 + tests/iio_attr.c | 1 + tests/iio_common.c | 23 +++++++++++++++++++++++ tests/iio_common.h | 2 +- tests/iio_genxml.c | 1 + tests/iio_info.c | 1 + tests/iio_readdev.c | 23 ++--------------------- tests/iio_reg.c | 1 + tests/iio_stresstest.c | 8 ++++---- tests/iio_writedev.c | 23 ++--------------------- 10 files changed, 37 insertions(+), 47 deletions(-) diff --git a/tests/iio_adi_xflow_check.c b/tests/iio_adi_xflow_check.c index 813f8b17a..0b05cb9b1 100644 --- a/tests/iio_adi_xflow_check.c +++ b/tests/iio_adi_xflow_check.c @@ -185,6 +185,7 @@ int main(int argc, char **argv) case 'n': case 'x': case 'u': + case 'T': break; case 'S': case 'a': diff --git a/tests/iio_attr.c b/tests/iio_attr.c index 2aa30353e..bf0396010 100644 --- a/tests/iio_attr.c +++ b/tests/iio_attr.c @@ -385,6 +385,7 @@ int main(int argc, char **argv) case 'n': case 'x': case 'u': + case 'T': break; case 'S': context_scan = true; diff --git a/tests/iio_common.c b/tests/iio_common.c index 7b59aae7b..0b789c6f8 100644 --- a/tests/iio_common.c +++ b/tests/iio_common.c @@ -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}, }; @@ -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. filters backend(s)" "\n\t\t\t 'ip', 'usb' or 'ip:usb:'", + "Context timeout in milliseconds." + "\n\t\t\t0 = no timeout (wait forever)", }; @@ -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; @@ -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; } @@ -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; } diff --git a/tests/iio_common.h b/tests/iio_common.h index a06e30488..58a98e168 100644 --- a/tests/iio_common.h +++ b/tests/iio_common.h @@ -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, diff --git a/tests/iio_genxml.c b/tests/iio_genxml.c index 2701110d8..48ee5b984 100644 --- a/tests/iio_genxml.c +++ b/tests/iio_genxml.c @@ -63,6 +63,7 @@ int main(int argc, char **argv) case 'n': case 'x': case 'u': + case 'T': break; case 'S': case 'a': diff --git a/tests/iio_info.c b/tests/iio_info.c index 02a2b5be6..47ee2523c 100644 --- a/tests/iio_info.c +++ b/tests/iio_info.c @@ -95,6 +95,7 @@ int main(int argc, char **argv) case 'n': case 'x': case 'u': + case 'T': break; case 'S': case 'a': diff --git a/tests/iio_readdev.c b/tests/iio_readdev.c index 34d811aed..94d052f3f 100644 --- a/tests/iio_readdev.c +++ b/tests/iio_readdev.c @@ -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 ] [-T ] [-b ]" + "[-t ] [-b ]" "[-s ] [ ...]", "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.", }; @@ -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; @@ -219,6 +216,7 @@ int main(int argc, char **argv) case 'n': case 'x': case 'u': + case 'T': break; case 'S': case 'a': @@ -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; @@ -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]); diff --git a/tests/iio_reg.c b/tests/iio_reg.c index a082a560c..04bb27e4a 100644 --- a/tests/iio_reg.c +++ b/tests/iio_reg.c @@ -99,6 +99,7 @@ int main(int argc, char **argv) case 'n': case 'x': case 'u': + case 'T': break; case 'S': case 'a': diff --git a/tests/iio_stresstest.c b/tests/iio_stresstest.c index eb046d9c6..4217dcc38 100644 --- a/tests/iio_stresstest.c +++ b/tests/iio_stresstest.c @@ -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}, }; @@ -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], diff --git a/tests/iio_writedev.c b/tests/iio_writedev.c index a0db8425f..1e3103360 100644 --- a/tests/iio_writedev.c +++ b/tests/iio_writedev.c @@ -47,7 +47,6 @@ 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}, @@ -55,12 +54,11 @@ static const struct option options[] = { static const char *options_descriptions[] = { "[-t ] " - "[-T ] [-b ] [-s ] " + "[-b ] [-s ] " " [ ...]", "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.", }; @@ -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; @@ -230,6 +227,7 @@ int main(int argc, char **argv) case 'n': case 'x': case 'u': + case 'T': break; case 'S': case 'a': @@ -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; @@ -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]);