From 59f05a657c02ffdf25d1872eec73237101e909c2 Mon Sep 17 00:00:00 2001 From: lawther Date: Thu, 25 Jun 2026 22:03:46 +1000 Subject: [PATCH] Add default case for resync counter and document decode_message invariant Addresses PR #46 review comments: the resync type switch now has a default branch (resyncs_unexpected) so total stays consistent with the breakdown if a new resync type is added, and decode_message documents that it relies on the framing layer for validation since it is also called directly from the TX-echo path and test harness. --- main/main.c | 1 + main/message_decoder.c | 5 +++++ main/pool_state.h | 1 + main/web_handlers.c | 1 + 4 files changed, 8 insertions(+) diff --git a/main/main.c b/main/main.c index c5976d2..f411a46 100644 --- a/main/main.c +++ b/main/main.c @@ -68,6 +68,7 @@ static void resync_wrapper(tcp_bridge_resync_type_t type, const uint8_t *data, i case TCP_BRIDGE_RESYNC_BAD_END: s_pool_state.resyncs_bad_end++; break; case TCP_BRIDGE_RESYNC_BAD_DATA_CHECKSUM: s_pool_state.resyncs_bad_data_checksum++; break; case TCP_BRIDGE_RESYNC_BUFFER_OVERFLOW: s_pool_state.resyncs_buffer_overflow++; break; + default: s_pool_state.resyncs_unexpected++; break; } xSemaphoreGive(s_pool_state_mutex); diff --git a/main/message_decoder.c b/main/message_decoder.c index 6eae2fd..3b9c22b 100644 --- a/main/message_decoder.c +++ b/main/message_decoder.c @@ -2899,6 +2899,11 @@ static bool handle_pump_buttons( static bool dispatch_message(const uint8_t *data, int len, const uint8_t *payload, int payload_len, const char *addr_info, message_decoder_context_t *ctx); +// This function assumes the framing layer has validated that the message buffer +// has valid checksums, start and stop bytes etc. It does not repeat the validation. +// Note also that this function can be called directly by the test harness and from +// the TX-echo path - in these cases it is incumbent on the caller to ensure that +// messages are valid. bool decode_message(const uint8_t *data, int len, message_decoder_context_t *ctx) { if (!ctx || !ctx->pool_state) { diff --git a/main/pool_state.h b/main/pool_state.h index 2944a6a..4531d63 100644 --- a/main/pool_state.h +++ b/main/pool_state.h @@ -259,6 +259,7 @@ typedef struct { uint32_t resyncs_bad_end; // End byte != 0x03 at declared length uint32_t resyncs_bad_data_checksum; // Data checksum mismatch uint32_t resyncs_buffer_overflow; // Buffer filled without a complete frame + uint32_t resyncs_unexpected; // Resync for unexpected reason - this should be investigated // Timers (up to MAX_TIMERS) timer_state_t timers[MAX_TIMERS]; diff --git a/main/web_handlers.c b/main/web_handlers.c index ed387d6..356137e 100644 --- a/main/web_handlers.c +++ b/main/web_handlers.c @@ -639,6 +639,7 @@ static esp_err_t status_get_handler(httpd_req_t *req) cJSON_AddNumberToObject(resyncs, "bad_end", state.resyncs_bad_end); cJSON_AddNumberToObject(resyncs, "data_checksum", state.resyncs_bad_data_checksum); cJSON_AddNumberToObject(resyncs, "buffer_overflow", state.resyncs_buffer_overflow); + cJSON_AddNumberToObject(resyncs, "unexpected", state.resyncs_unexpected); cJSON_AddItemToObject(root, "resyncs", resyncs); // Devices observed on the bus