Skip to content

Commit

Permalink
DRAFT: utils: iio_common: Set timeout specified at CLI immediately
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
pamolloy committed Jul 2, 2024
1 parent 82d1266 commit 5a8480f
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions utils/iio_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(&params, arg);
else
ctx = iio_create_context(NULL, NULL);

Expand All @@ -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;

Expand Down

0 comments on commit 5a8480f

Please sign in to comment.