From ecc23a5a76e5150109ed28000074fbc1260c6e85 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Tue, 9 Feb 2021 20:56:51 +0100 Subject: [PATCH] Simplify the loop for reading the packet header The for loop construct without an increment statement is a bit unusual and thus easy to miss. With an equivalent while loop, the intent becomes a bit more obvious. --- src/uwatec_aladin.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/uwatec_aladin.c b/src/uwatec_aladin.c index 3ebd094..15bcbfd 100644 --- a/src/uwatec_aladin.c +++ b/src/uwatec_aladin.c @@ -164,7 +164,8 @@ uwatec_aladin_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) unsigned char answer[SZ_MEMORY + 2] = {0}; // Receive the header of the package. - for (unsigned int i = 0; i < 4;) { + unsigned int i = 0; + while (i < 4) { if (device_is_cancelled (abstract)) return DC_STATUS_CANCELLED; @@ -173,11 +174,13 @@ uwatec_aladin_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) ERROR (abstract->context, "Failed to receive the answer."); return status; } - if (answer[i] == (i < 3 ? 0x55 : 0x00)) { - i++; // Continue. - } else { - i = 0; // Reset. + + const unsigned char expected = i < 3 ? 0x55 : 0x00; + if (answer[i] != expected) { device_event_emit (abstract, DC_EVENT_WAITING, NULL); + i = 0; + } else { + i++; } }