diff --git a/examples/htool_console.c b/examples/htool_console.c index be687f5..8f7e9f5 100644 --- a/examples/htool_console.c +++ b/examples/htool_console.c @@ -105,26 +105,32 @@ int htool_console_run(struct libhoth_device* dev, bool quit = false; while (!quit) { + // Any previous failure should reset all of the USB state before retrying + while (status != LIBHOTH_OK) { + status = libhoth_device_reconnect(dev); + // If USB is down we might fail reconnect, just retry + } + // Give an opportunity for other clients to use the interface. + libhoth_release_device(dev); + usleep(1000 * opts->yield_ms); + status = libhoth_claim_device(dev, 1000 * 1000 * opts->claim_timeout_secs); + if (status != LIBHOTH_OK) { + // If USB is down we might fail claim, just go back and retry + continue; + } + status = libhoth_read_console(dev, STDOUT_FILENO, false, opts->channel_id, &offset); if (status != LIBHOTH_OK) { - break; + // Device resets cause failures, just loop and allow reconnection + continue; } status = libhoth_write_console(dev, opts->channel_id, opts->force_drive_tx, &quit); if (status != LIBHOTH_OK) { - break; - } - - libhoth_release_device(dev); - - // Give an opportunity for other clients to use the interface. - usleep(1000 * opts->yield_ms); - - status = libhoth_claim_device(dev, 1000 * 1000 * opts->claim_timeout_secs); - if (status != LIBHOTH_OK) { - break; + // Device resets cause failures, just loop and allow reconnection + continue; } } diff --git a/protocol/console.c b/protocol/console.c index 141d9ce..16a7f04 100644 --- a/protocol/console.c +++ b/protocol/console.c @@ -106,6 +106,10 @@ int libhoth_read_console(struct libhoth_device* dev, int fd, if (status != 0) { return status; } + if (req.offset != resp.resp.offset) { + fprintf(stderr, "Console jumped forward %u bytes\n", + resp.resp.offset - req.offset); + } int len = response_size - sizeof(resp.resp); if (len > 0) {