From 5a8480f3ef59d78dcc4650726e304a8550507e41 Mon Sep 17 00:00:00 2001 From: Philip Molloy Date: Tue, 2 Jul 2024 15:28:17 +0200 Subject: [PATCH] DRAFT: utils: iio_common: Set timeout specified at CLI immediately When a timeout is specified as a command-line argument then it is only applied after the final context is created. If the creation of the context requires transfering data then a default timeout is used. That default timeout might be too short for slow transfers, preventing the context from being created. For example, a serial backend without any devices needs to send a context XML string of roughly 1170 bytes, which when communicating at 9600 baud will take more than the default timeout of 1000 ms. Signed-off-by: Philip Molloy --- utils/iio_common.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/utils/iio_common.c b/utils/iio_common.c index b3b4e82b3..2e4f7086e 100644 --- a/utils/iio_common.c +++ b/utils/iio_common.c @@ -257,12 +257,12 @@ struct iio_context * handle_common_opts(char * name, int argc, int *err_code) { struct iio_context *ctx = NULL; + struct iio_context_params params = { .timeout_ms = -1 }; enum backend backend = IIO_LOCAL; const char *arg = NULL, *prefix = NULL; bool do_scan = false, detect_context = false; char buf[1024]; struct option *opts; - int timeout = -1; int err, c; /* Setting opterr to zero disables error messages from getopt_long */ @@ -331,7 +331,7 @@ struct iio_context * handle_common_opts(char * name, int argc, fprintf(stderr, "Timeout requires an argument\n"); goto err_fail; } - timeout = sanitize_clamp("timeout", optarg, 0, INT_MAX); + params.timeout_ms = sanitize_clamp("timeout", optarg, 0, INT_MAX); break; case '?': break; @@ -359,7 +359,7 @@ struct iio_context * handle_common_opts(char * name, int argc, } else if (!arg && backend != IIO_LOCAL) fprintf(stderr, "argument parsing error\n"); else if (backend == IIO_URI) - ctx = iio_create_context(NULL, arg); + ctx = iio_create_context(¶ms, arg); else ctx = iio_create_context(NULL, NULL); @@ -373,14 +373,6 @@ struct iio_context * handle_common_opts(char * name, int argc, goto err_fail; } - if (ctx && timeout >= 0) { - err = iio_context_set_timeout(ctx, timeout); - if (err < 0) { - ctx_perror(ctx, err, "IIO context set timeout failed"); - iio_context_destroy(ctx); - goto err_fail; - } - } return ctx;