From 7945d5bf5a597d04c7820c8d779932713c55e9ff Mon Sep 17 00:00:00 2001 From: mikecarper Date: Sat, 3 Oct 2026 20:23:50 -0700 Subject: [PATCH] Retain Companion ACK notifications during host backpressure --- .github/workflows/run-unit-tests.yml | 1 + examples/companion_radio/MyMesh.cpp | 93 ++-- examples/companion_radio/MyMesh.h | 9 +- test/fixtures/companion_ack_delivery/test.cpp | 496 ++++++++++++++++++ test/test_companion_ack_delivery.py | 87 +++ test/test_nrf52_usb_logging_contract.py | 8 +- 6 files changed, 651 insertions(+), 43 deletions(-) create mode 100644 test/fixtures/companion_ack_delivery/test.cpp create mode 100644 test/test_companion_ack_delivery.py diff --git a/.github/workflows/run-unit-tests.yml b/.github/workflows/run-unit-tests.yml index aa792e763..f6c0df8eb 100644 --- a/.github/workflows/run-unit-tests.yml +++ b/.github/workflows/run-unit-tests.yml @@ -130,6 +130,7 @@ jobs: python3 -B test/test_companion_prefs_transactions.py -v python3 -B test/test_companion_primary_radio_persistence.py -v python3 -B test/test_companion_response_bounds.py -v + python3 -B test/test_companion_ack_delivery.py -v python3 -B test/test_companion_identity_cli.py -v python3 -B test/test_companion_uncached_storage.py -v python3 -B test/test_companion_ota_config.py -v diff --git a/examples/companion_radio/MyMesh.cpp b/examples/companion_radio/MyMesh.cpp index 30c998834..f490e63a6 100644 --- a/examples/companion_radio/MyMesh.cpp +++ b/examples/companion_radio/MyMesh.cpp @@ -227,6 +227,8 @@ static constexpr uint8_t DEFAULT_FEM_RX_GAIN = 1; #define LAZY_CONTACTS_WRITE_DELAY 5000 #define CONTACT_PAGE_WRITE_GAP 100 #define EXPECTED_ACK_RETRY_RECHECK_MILLIS 1000 +#define EXPECTED_ACK_HOST_GRACE_MILLIS 10000 +#define EXPECTED_ACK_HOST_RECHECK_MILLIS 10 // The encrypted body of an ANON_REQ starts with its usual four-byte tag. // A distinct marker after that tag prevents a contact introduction from being @@ -968,7 +970,7 @@ void MyMesh::onContactPathUpdated(const ContactInfo &contact) { } void MyMesh::clearExpectedAck(AckTableEntry& entry, bool cancel_retries) { - if (cancel_retries && entry.ack != 0) { + if (cancel_retries && entry.ack != 0 && !entry.confirmed) { cancelActiveRetries(entry.retry_key); } memset(&entry, 0, sizeof(entry)); @@ -976,11 +978,11 @@ void MyMesh::clearExpectedAck(AckTableEntry& entry, bool cancel_retries) { void MyMesh::expireExpectedAcks() { unsigned long now = _ms->getMillis(); - unsigned long nearest_delay = 0; - has_next_ack_expiry = false; + uint32_t nearest_delay = UINT32_MAX; - for (int i = 0; i < EXPECTED_ACK_TABLE_SIZE; i++) { - AckTableEntry& entry = expected_ack_table[i]; + for (AckTableEntry* cursor = expected_ack_table; + cursor != expected_ack_table + EXPECTED_ACK_TABLE_SIZE; ++cursor) { + AckTableEntry& entry = *cursor; if (entry.ack == 0) { continue; } @@ -994,7 +996,27 @@ void MyMesh::expireExpectedAcks() { entry.reply_route = NULL; } - if (entry.expires_at == now || millisHasNowPassed(entry.expires_at)) { + // Use one 32-bit clock snapshot for this pass, including counter wrap. + const bool expired = (int32_t)((uint32_t)now - (uint32_t)entry.expires_at) >= 0; + if (entry.confirmed) { + // The radio delivery is complete. Retain only this captured transport's + // notification, with a fixed deadline independent of duplicate ACKs. + if (entry.reply_route == NULL || expired) { + clearExpectedAck(entry, false); + continue; + } + // Align both 32-bit fields while keeping the nine-byte wire format. + alignas(uint32_t) uint8_t storage[12]; + uint8_t* confirmation = storage + 3; + confirmation[0] = PUSH_CODE_SEND_CONFIRMED; + memcpy(&confirmation[1], &entry.ack, 4); + uint32_t trip_time = entry.msg_sent; + memcpy(&confirmation[5], &trip_time, 4); + if (_serial->writeFrameToRoute(entry.reply_route, confirmation, 9) == 9) { + clearExpectedAck(entry, false); + continue; + } + } else if (expired) { if (!hasActiveRetries(entry.retry_key)) { #if COMPANION_FEATURE_TEXT_TERMINAL if (entry.terminal_origin && hasTerminalOutput()) { @@ -1006,28 +1028,29 @@ void MyMesh::expireExpectedAcks() { } // Keep the semantic match alive while its lower-level retry sequence is // active, so a newer app submission can replace that sequence cleanly. - entry.expires_at = futureMillis(EXPECTED_ACK_RETRY_RECHECK_MILLIS); + entry.expires_at = (uint32_t)(now + EXPECTED_ACK_RETRY_RECHECK_MILLIS); } - unsigned long delay = entry.expires_at - now; - if (!has_next_ack_expiry || delay < nearest_delay) { + uint32_t delay = (uint32_t)(entry.expires_at - now); + if (entry.confirmed && delay > EXPECTED_ACK_HOST_RECHECK_MILLIS) { + delay = EXPECTED_ACK_HOST_RECHECK_MILLIS; + } + if (delay < nearest_delay) { nearest_delay = delay; - next_ack_expiry = entry.expires_at; - has_next_ack_expiry = true; } } - if (!has_next_ack_expiry) { - next_ack_expiry = 0; - } + has_next_ack_expiry = nearest_delay != UINT32_MAX; + next_ack_expiry = has_next_ack_expiry ? (uint32_t)(now + nearest_delay) : 0; } MyMesh::AckTableEntry* MyMesh::findPendingTextMessage( const uint8_t text_fingerprint[MAX_HASH_SIZE], uint32_t message_timestamp) { expireExpectedAcks(); - for (int i = 0; i < EXPECTED_ACK_TABLE_SIZE; i++) { - AckTableEntry& entry = expected_ack_table[i]; - if (entry.ack != 0 + for (AckTableEntry* cursor = expected_ack_table; + cursor != expected_ack_table + EXPECTED_ACK_TABLE_SIZE; ++cursor) { + AckTableEntry& entry = *cursor; + if (entry.ack != 0 && !entry.confirmed && entry.message_timestamp != message_timestamp && memcmp(entry.text_fingerprint, text_fingerprint, MAX_HASH_SIZE) == 0 && hasActiveRetries(entry.retry_key)) { @@ -1039,33 +1062,33 @@ MyMesh::AckTableEntry* MyMesh::findPendingTextMessage( ContactInfo* MyMesh::processAck(const uint8_t *data) { expireExpectedAcks(); + uint32_t incoming; + memcpy(&incoming, data, sizeof(incoming)); // see if matches any in a table - for (int i = 0; i < EXPECTED_ACK_TABLE_SIZE; i++) { - if (expected_ack_table[i].ack != 0 - && memcmp(data, &expected_ack_table[i].ack, 4) == 0) { // got an ACK from recipient - out_frame[0] = PUSH_CODE_SEND_CONFIRMED; - memcpy(&out_frame[1], data, 4); - uint32_t trip_time = _ms->getMillis() - expected_ack_table[i].msg_sent; - memcpy(&out_frame[5], &trip_time, 4); - if (expected_ack_table[i].reply_route != NULL) { - _serial->writeFrameToRoute(expected_ack_table[i].reply_route, - out_frame, 9); - } - + for (AckTableEntry* cursor = expected_ack_table; + cursor != expected_ack_table + EXPECTED_ACK_TABLE_SIZE; ++cursor) { + AckTableEntry& entry = *cursor; + if (entry.ack != 0 + && incoming == entry.ack) { // got an ACK from recipient + // A duplicate must not cancel a newer chat timeout or learn another + // return path merely because its host notification is still queued. + if (entry.confirmed) continue; + ContactInfo* contact = entry.contact; + const unsigned long now = _ms->getMillis(); + entry.msg_sent = (uint32_t)(now - entry.msg_sent); + entry.expires_at = (uint32_t)(now + EXPECTED_ACK_HOST_GRACE_MILLIS); + entry.confirmed = true; + cancelActiveRetries(entry.retry_key); #if COMPANION_FEATURE_TEXT_TERMINAL - if (expected_ack_table[i].terminal_origin && hasTerminalOutput()) { + if (entry.terminal_origin && hasTerminalOutput()) { terminalOutput().printf("\r\n Got ACK! (round trip: %lu ms)\r\n> ", - (unsigned long)trip_time); + entry.msg_sent); } #endif - - // NOTE: the same ACK can be received multiple times! - ContactInfo* contact = expected_ack_table[i].contact; #if MESH_ENABLE_ONE_KEY_DM if (contact != NULL && contact->type == ADV_TYPE_CHAT) rememberOneKeyAck(*contact); #endif - clearExpectedAck(expected_ack_table[i]); expireExpectedAcks(); return contact; } diff --git a/examples/companion_radio/MyMesh.h b/examples/companion_radio/MyMesh.h index 6a522947d..8257e0e82 100644 --- a/examples/companion_radio/MyMesh.h +++ b/examples/companion_radio/MyMesh.h @@ -754,17 +754,18 @@ private: #endif struct AckTableEntry { + uint8_t retry_key[MAX_HASH_SIZE]; + bool confirmed; // msg_sent holds the frozen RTT while host admission is pending +#if COMPANION_FEATURE_TEXT_TERMINAL + bool terminal_origin; +#endif unsigned long msg_sent; unsigned long expires_at; uint32_t ack; uint32_t message_timestamp; ContactInfo* contact; uint8_t text_fingerprint[MAX_HASH_SIZE]; - uint8_t retry_key[MAX_HASH_SIZE]; BaseSerialInterface* reply_route; -#if COMPANION_FEATURE_TEXT_TERMINAL - bool terminal_origin; -#endif }; #define EXPECTED_ACK_TABLE_SIZE 8 AckTableEntry expected_ack_table[EXPECTED_ACK_TABLE_SIZE]; // circular table diff --git a/test/fixtures/companion_ack_delivery/test.cpp b/test/fixtures/companion_ack_delivery/test.cpp new file mode 100644 index 000000000..398f8df83 --- /dev/null +++ b/test/fixtures/companion_ack_delivery/test.cpp @@ -0,0 +1,496 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "production_constants.inc" +#include "production_stream.inc" + +std::deque WiFiServer::incoming; +static bool usb_connected = true; +static bool usbConnected() { return usb_connected; } +static constexpr uint8_t ADV_TYPE_CHAT = 1; +struct ContactInfo { uint8_t type = ADV_TYPE_CHAT; }; +struct Clock { unsigned long getMillis() const { return millis(); } }; +struct Terminal { + std::string text; + void print(const char* value) { text += value; } + void printf(const char* format, ...) { + char buf[160]; + va_list args; + va_start(args, format); + vsnprintf(buf, sizeof(buf), format, args); + va_end(args); + text += buf; + } +}; + +class MyMesh { +public: + BaseSerialInterface* _serial = nullptr; + Clock clock; + Clock* _ms = &clock; + uint8_t out_frame[MAX_FRAME_SIZE] = {}; +#include "production_entry.inc" + AckTableEntry expected_ack_table[EXPECTED_ACK_TABLE_SIZE] = {}; + int next_ack_idx = 0; + bool has_next_ack_expiry = false; + unsigned long next_ack_expiry = 0; + unsigned cancelled = 0, one_key_notices = 0; + mutable unsigned active_queries = 0; + unsigned connection_ack_calls = 0; + uint32_t connection_ack = 0, last_connection_ack = 0; + ContactInfo* connection_ack_contact = nullptr; + const uint8_t* last_connection_ack_input = nullptr; + bool retry_active[256] = {}; + Terminal terminal; + BaseSerialInterface* pending_serial_reply_route = nullptr; + BaseSerialInterface* command_radio_reply_route = nullptr; + BaseSerialInterface* binary_trace_reply_route = nullptr; + BaseSerialInterface* sign_data_reply_route = nullptr; + BaseSerialInterface* private_key_backup_route = nullptr; + unsigned long private_key_backup_deadline = 0; + char private_key_backup_nonce[17] = {}; + uint8_t private_key_backup_sender[6] = {}; + bool command_radio_apply_pending = false, binary_trace_pending = false; + // Radio dispatch, peer-store, and terminal I/O are hardware boundaries. All + // ACK state decisions and transport admission execute production code. + bool cancelActiveRetries(const uint8_t* key) { + ++cancelled; + retry_active[key[0]] = false; + return true; + } + bool hasActiveRetries(const uint8_t* key) const { + ++active_queries; + return retry_active[key[0]]; + } + void rememberOneKeyAck(ContactInfo&) { ++one_key_notices; } + ContactInfo* checkConnectionsAck(const uint8_t* input) { + ++connection_ack_calls; + last_connection_ack_input = input; + memcpy(&last_connection_ack, input, sizeof(last_connection_ack)); + return last_connection_ack == connection_ack ? connection_ack_contact : nullptr; + } + bool hasTerminalOutput() const { return true; } + Terminal& terminalOutput() { return terminal; } + void clearPendingReqs() { pending_serial_reply_route = nullptr; } + void cancelPendingRadioParamApply() { command_radio_reply_route = nullptr; } + void clearBinaryTraceReply() { binary_trace_reply_route = nullptr; } + void cancelSigningSession() { sign_data_reply_route = nullptr; } + bool millisHasNowPassed(unsigned long) const; + unsigned long futureMillis(int) const; + void clearExpectedAck(AckTableEntry&, bool = true); + void expireExpectedAcks(); + AckTableEntry* findPendingTextMessage(const uint8_t*, uint32_t); + ContactInfo* processAck(const uint8_t*); + void cancelSerialOperationsForRoute(BaseSerialInterface*); + bool hasFiniteDelayedReplyForRoute(BaseSerialInterface*) const; + void service(); + void remember(uint32_t, uint32_t, ContactInfo*, const uint8_t*, const uint8_t*, AckTableEntry* = nullptr); +}; + +#include "production_ack.inc" + +using Bytes = std::vector; +using Socket = std::shared_ptr; +struct Fixture { + BufferStream usb_stream, other_stream; + ArduinoSerialInterface usb, other; + SerialWifiInterface tcp; + MultiSerialInterface manager; + MyMesh mesh; + ContactInfo recipient; + uint8_t command[MAX_FRAME_SIZE] = {}; + + Fixture() { + g_mock_millis = 100; + usb_connected = true; + WiFiServer::incoming.clear(); + usb.begin(usb_stream); usb.enableFlowControl(true); + usb.setConnectedCheck(usbConnected); + other.begin(other_stream); other.enableFlowControl(true); + tcp.begin(5000); + assert(manager.addInterface(InterfaceType::USB, &usb)); + assert(manager.addInterface(InterfaceType::HardwareSerial, &other)); + assert(manager.addInterface(InterfaceType::WiFi, &tcp)); + manager.enable(); + mesh._serial = &manager; + select(usb_stream, &usb); + } + ~Fixture() { tcp.end(); } + + void select(BufferStream& stream, BaseSerialInterface* target) { + const uint8_t request[] = {'<', 1, 0, 1}; + stream.push(request, sizeof(request)); + assert(manager.checkRecvFrame(command) == 1); + assert(manager.captureReplyRoute() == target); + } + Socket connect(IPAddress ip, size_t write_limit = 0) { + auto socket = std::make_shared(ip); + socket->write_limit = write_limit; + socket->received = {'<', 1, 0, 1}; + WiFiServer::incoming.push_back(WiFiClient(socket)); + size_t received = 0; + for (int i = 0; i < 8 && received == 0; ++i) received = manager.checkRecvFrame(command); + assert(received == 1); + assert(manager.captureReplyRoute() == &tcp); + return socket; + } + void queue(BaseSerialInterface* target, uint8_t code, unsigned count) { + for (unsigned i = 0; i < count; ++i) { + assert(manager.writeFrameToRoute(target, &code, 1) == 1); + } + } + MyMesh::AckTableEntry& add(BaseSerialInterface* target = nullptr) { + auto& entry = mesh.expected_ack_table[0]; + entry.ack = 0x11223344; + entry.msg_sent = 50; + entry.expires_at = 10000; + entry.reply_route = target == nullptr ? &usb : target; + entry.contact = &recipient; + entry.message_timestamp = 1; + entry.text_fingerprint[0] = 3; + entry.retry_key[0] = 7; + mesh.retry_active[7] = true; + return entry; + } + ContactInfo* ack(uint32_t value = 0x11223344) { + return mesh.processAck(reinterpret_cast(&value)); + } + void tick(uint32_t time) { g_mock_millis = time; mesh.service(); } + void drain() { + usb_stream.write_capacity = 4096; + other_stream.write_capacity = 4096; + for (int i = 0; i < 8; ++i) manager.loop(); + } +}; + +static std::vector frames(const Bytes& bytes) { + std::vector result; + size_t offset = 0; + while (offset < bytes.size()) { + assert(bytes.size() - offset >= 3 && bytes[offset] == '>'); + size_t size = bytes[offset + 1] | size_t(bytes[offset + 2]) << 8; + offset += 3; + assert(size <= bytes.size() - offset); + result.emplace_back(bytes.begin() + offset, bytes.begin() + offset + size); + offset += size; + } + return result; +} +static unsigned ackCount(const Bytes& bytes, uint32_t expected_rtt = 50) { + unsigned count = 0; + for (const auto& frame : frames(bytes)) { + if (frame[0] != PUSH_CODE_SEND_CONFIRMED) continue; + assert(frame.size() == 9); + uint32_t ack, rtt; + memcpy(&ack, frame.data() + 1, 4); + memcpy(&rtt, frame.data() + 5, 4); + assert(ack == 0x11223344 && rtt == expected_rtt); + ++count; + } + return count; +} + +int main() { + unsigned checks = 0; + { + Fixture f; + f.usb_stream.write_capacity = 0; + f.queue(&f.usb, 0, 4); + auto& entry = f.add(); + memset(f.mesh.out_frame, 0x5a, sizeof(f.mesh.out_frame)); + assert(f.ack() == &f.recipient); + for (auto byte : f.mesh.out_frame) assert(byte == 0x5a); + assert(entry.confirmed && entry.msg_sent == 50 && entry.expires_at == 10100); + assert(f.mesh.cancelled == 1 && !f.mesh.retry_active[7]); + assert(f.mesh.hasFiniteDelayedReplyForRoute(&f.usb)); + assert(f.mesh.next_ack_expiry == 110); + f.drain(); + assert(ackCount(f.usb_stream.output) == 0); + f.tick(109); assert(entry.ack != 0); + f.tick(110); assert(entry.ack == 0); + for (auto byte : f.mesh.out_frame) assert(byte == 0x5a); + assert(!f.mesh.hasFiniteDelayedReplyForRoute(&f.usb)); + f.drain(); + assert(ackCount(f.usb_stream.output) == 1 && f.other_stream.output.empty()); + assert(f.mesh.cancelled == 1 && f.ack() == nullptr); + ++checks; + } + { + Fixture f; + f.usb_stream.write_capacity = 0; + f.queue(&f.usb, 0x80, 3); f.queue(&f.usb, 0, 1); + auto& entry = f.add(); + assert(f.ack() == &f.recipient && entry.ack == 0); + f.drain(); + assert(frames(f.usb_stream.output).size() == 4); + assert(ackCount(f.usb_stream.output) == 1); + ++checks; + } + { + Fixture f; + f.usb_stream.write_capacity = 0; f.queue(&f.usb, 0, 4); + auto& entry = f.add(); f.ack(); + const auto deadline = entry.expires_at; + f.tick(2500); assert(f.ack() == nullptr); + f.tick(9000); f.ack(); + assert(entry.expires_at == deadline && entry.msg_sent == 50 && f.mesh.cancelled == 1); + assert(f.mesh.active_queries == 0); + assert(f.mesh.one_key_notices == unsigned(MESH_ENABLE_ONE_KEY_DM)); + f.tick(10099); assert(entry.ack != 0 && f.mesh.next_ack_expiry == 10100); + f.tick(10100); assert(entry.ack == 0 && !f.mesh.has_next_ack_expiry); + assert(!f.mesh.hasFiniteDelayedReplyForRoute(&f.usb)); + assert(f.mesh.terminal.text.find("no ACK") == std::string::npos); + f.drain(); assert(ackCount(f.usb_stream.output) == 0); + ++checks; + } + { + Fixture f; + f.usb_stream.write_capacity = 0; f.queue(&f.usb, 0, 4); + auto& entry = f.add(); + g_mock_millis = UINT32_MAX - 4; + entry.msg_sent = UINT32_MAX - 54; entry.expires_at = UINT32_MAX; + f.ack(); assert(entry.msg_sent == 50 && entry.expires_at == 9995); + assert(f.mesh.next_ack_expiry == 5); + f.tick(4); assert(entry.ack != 0); + f.tick(5); assert(entry.ack != 0); + g_mock_millis = 30; f.ack(); assert(entry.msg_sent == 50); + f.tick(9994); assert(entry.ack != 0); + f.tick(9995); assert(entry.ack == 0); + ++checks; + } + for (unsigned kind = 0; kind < 5; ++kind) { + Fixture f; + f.usb_stream.write_capacity = 0; f.queue(&f.usb, 0, 4); + auto& entry = f.add(); f.ack(); + if (kind == 0) usb_connected = false; + if (kind == 1) f.usb.disable(); + if (kind == 2) assert(f.manager.removeInterface(&f.usb)); + if (kind == 3) f.manager.disable(); + if (kind == 4) { f.mesh.cancelSerialOperationsForRoute(&f.usb); f.usb.resetSessionState(); } + f.tick(110); + assert(entry.ack == 0 && f.mesh.cancelled == 1); + assert(!f.mesh.hasFiniteDelayedReplyForRoute(&f.usb)); + assert(f.other_stream.output.empty()); + usb_connected = true; f.manager.enable(); f.drain(); + assert(ackCount(f.usb_stream.output) == 0); + ++checks; + } + { + Fixture f; + f.usb_stream.write_capacity = 0; f.queue(&f.usb, 0, 4); + auto& entry = f.add(); + g_mock_millis = 25; + entry.msg_sent = UINT32_MAX - 24; + f.ack(); + assert(entry.msg_sent == 50 && entry.expires_at == 10025); + f.drain(); f.tick(35); f.drain(); + assert(entry.ack == 0 && ackCount(f.usb_stream.output) == 1); + ++checks; + } + { + Fixture f; + auto& entry = f.add(); + f.mesh.cancelSerialOperationsForRoute(&f.usb); + assert(entry.ack != 0 && entry.reply_route == nullptr && f.mesh.retry_active[7]); + assert(f.ack() == &f.recipient && entry.ack == 0 && f.mesh.cancelled == 1); + assert(!f.mesh.retry_active[7] && f.usb_stream.output.empty()); + ++checks; + } + { + Fixture f; + f.usb_stream.write_capacity = 0; f.queue(&f.usb, 0, 4); + auto& entry = f.add(); f.ack(); + f.select(f.other_stream, &f.other); + f.drain(); f.tick(110); f.drain(); + assert(entry.ack == 0 && ackCount(f.usb_stream.output) == 1); + assert(f.other_stream.output.empty()); + ++checks; + } + { + Fixture f; + auto socket = f.connect(IPAddress(192, 168, 1, 2)); + f.queue(&f.tcp, 0, 4); + auto& entry = f.add(&f.tcp); f.ack(); + assert(entry.confirmed && f.usb_stream.output.empty()); + socket->write_limit = 4096; + for (int i = 0; i < 4; ++i) f.manager.checkRecvFrame(f.command); + f.tick(110); + for (int i = 0; i < 6; ++i) f.manager.checkRecvFrame(f.command); + assert(entry.ack == 0 && ackCount(socket->sent) == 1 && f.usb_stream.output.empty()); + ++checks; + } + { + Fixture f; + auto socket = f.connect(IPAddress(192, 168, 1, 2)); + f.tcp.setSessionChangedCallback([](void* context) { + auto& fixture = *static_cast(context); + fixture.mesh.cancelSerialOperationsForRoute(&fixture.tcp); + }, &f); + f.queue(&f.tcp, 0, 4); + auto& entry = f.add(&f.tcp); f.ack(); + auto replacement = f.connect(IPAddress(192, 168, 1, 3)); + assert(entry.ack == 0 && f.mesh.cancelled == 1); + replacement->write_limit = 4096; + f.tick(110); + for (int i = 0; i < 8; ++i) f.manager.checkRecvFrame(f.command); + assert(ackCount(replacement->sent) == 0 && socket->sent.empty()); + assert(f.usb_stream.output.empty()); + // Do not let destructor callback outlive MyMesh (member destruction is reverse). + f.tcp.setSessionChangedCallback(nullptr, nullptr); + ++checks; + } + { + Fixture f; + auto socket = f.connect(IPAddress(192, 168, 1, 2)); + f.tcp.setSessionChangedCallback([](void* context) { + auto& fixture = *static_cast(context); + fixture.mesh.cancelSerialOperationsForRoute(&fixture.tcp); + }, &f); + f.queue(&f.tcp, 0, 4); + auto& entry = f.add(&f.tcp); f.ack(); + socket->connected = false; + f.manager.loop(); + assert(entry.ack == 0 && f.mesh.cancelled == 1); + assert(!f.mesh.hasFiniteDelayedReplyForRoute(&f.tcp)); + auto replacement = f.connect(IPAddress(192, 168, 1, 2), 4096); + replacement->write_limit = 4096; + for (int i = 0; i < 8; ++i) f.manager.checkRecvFrame(f.command); + assert(ackCount(replacement->sent) == 0 && socket->sent.empty()); + assert(f.usb_stream.output.empty()); + f.tcp.setSessionChangedCallback(nullptr, nullptr); + ++checks; + } + { + Fixture f; + f.usb_stream.write_capacity = 0; f.queue(&f.usb, 0, 4); + auto& entry = f.add(); + assert(f.mesh.findPendingTextMessage(entry.text_fingerprint, 2) == &entry); + f.ack(); f.mesh.retry_active[7] = true; // a different lower-level retry must not revive this match + const auto queries = f.mesh.active_queries; + assert(f.mesh.findPendingTextMessage(entry.text_fingerprint, 2) == nullptr); + assert(f.mesh.active_queries == queries); + uint8_t fingerprint[MAX_HASH_SIZE] = {4}, key[MAX_HASH_SIZE] = {8}; + f.mesh.remember(0x55667788, 2, &f.recipient, fingerprint, key); + assert(entry.ack == 0x55667788 && !entry.confirmed && entry.msg_sent == 100); + assert(f.mesh.next_ack_idx == 1 && f.mesh.cancelled == 1); + // Successful semantic replacement preserves the selected slot and resets + // confirmed bookkeeping without changing the existing circular bound. + f.mesh.remember(0x88776655, 3, &f.recipient, fingerprint, key, &entry); + assert(entry.ack == 0x88776655 && f.mesh.next_ack_idx == 1); + assert(f.mesh.cancelled == 1); + ++checks; + } + { + Fixture f; + auto& entry = f.add(); entry.reply_route = nullptr; +#if COMPANION_FEATURE_TEXT_TERMINAL + entry.terminal_origin = true; +#endif + assert(f.ack() == &f.recipient && entry.ack == 0); + assert(f.ack() == nullptr && f.mesh.cancelled == 1); + assert(f.mesh.one_key_notices == unsigned(MESH_ENABLE_ONE_KEY_DM)); +#if COMPANION_FEATURE_TEXT_TERMINAL + assert(f.mesh.terminal.text == "\r\n Got ACK! (round trip: 50 ms)\r\n> "); +#endif + assert(f.usb_stream.output.empty()); + ++checks; + } + { + Fixture f; + f.usb_stream.write_capacity = 0; f.queue(&f.usb, 0, 4); + auto& first = f.add(); + auto& second = f.mesh.expected_ack_table[1]; + second = first; + second.reply_route = &f.other; + second.msg_sent = 75; + second.retry_key[0] = 8; + f.mesh.retry_active[8] = true; + assert(f.ack() == &f.recipient && first.confirmed && !second.confirmed); + assert(f.ack() == &f.recipient && first.confirmed && second.ack == 0); + assert(f.mesh.connection_ack_calls == 0); + assert(f.mesh.cancelled == 2 && !f.mesh.retry_active[7] && !f.mesh.retry_active[8]); + f.drain(); + assert(ackCount(f.other_stream.output, 25) == 1); + assert(f.ack() == nullptr && f.mesh.cancelled == 2); + assert(f.mesh.connection_ack_calls == 1); + f.tick(110); f.drain(); + assert(first.ack == 0 && ackCount(f.usb_stream.output) == 1); + ++checks; + } + { + Fixture f; + auto& entry = f.add(); + ContactInfo connection; + f.mesh.connection_ack = 0xdecafbad; + f.mesh.connection_ack_contact = &connection; + const uint32_t unmatched = f.mesh.connection_ack; + const auto* input = reinterpret_cast(&unmatched); + assert(f.mesh.processAck(input) == &connection); + assert(f.mesh.connection_ack_calls == 1); + assert(f.mesh.last_connection_ack_input == input); + assert(f.mesh.last_connection_ack == unmatched && unmatched == 0xdecafbad); + assert(entry.ack == 0x11223344 && !entry.confirmed && f.mesh.retry_active[7]); + assert(f.mesh.cancelled == 0 && f.mesh.one_key_notices == 0); + assert(f.ack(0xaabbccdd) == nullptr && f.mesh.connection_ack_calls == 2); + assert(f.mesh.last_connection_ack == 0xaabbccdd); + assert(f.usb_stream.output.empty() && f.other_stream.output.empty()); + ++checks; + } + { + Fixture f; + f.usb_stream.write_capacity = 0; f.queue(&f.usb, 0, 4); + auto& entry = f.add(); + ContactInfo connection; + f.mesh.connection_ack = entry.ack; + f.mesh.connection_ack_contact = &connection; + // An unconfirmed chat match takes priority over the connection callback. + assert(f.ack() == &f.recipient && entry.confirmed); + assert(f.mesh.connection_ack_calls == 0 && f.mesh.cancelled == 1); + const auto deadline = entry.expires_at; + f.tick(2500); + // A retained, already-confirmed duplicate may still be a connection ACK. + assert(f.ack() == &connection && f.mesh.connection_ack_calls == 1); + assert(entry.confirmed && entry.expires_at == deadline && entry.msg_sent == 50); + assert(f.mesh.cancelled == 1); + assert(f.mesh.one_key_notices == unsigned(MESH_ENABLE_ONE_KEY_DM)); + f.tick(10100); + assert(entry.ack == 0); + assert(f.ack() == &connection && f.mesh.connection_ack_calls == 2); + f.drain(); assert(ackCount(f.usb_stream.output) == 0); + ++checks; + } + { + Fixture f; + auto& entry = f.add(); + ContactInfo connection; + f.mesh.connection_ack = entry.ack; + f.mesh.connection_ack_contact = &connection; + alignas(uint32_t) uint8_t bytes[sizeof(uint32_t) + 2] = {0xa5}; + uint8_t* input = bytes + 1; + memcpy(input, &entry.ack, sizeof(entry.ack)); + bytes[sizeof(bytes) - 1] = 0x5a; + uint8_t before[sizeof(bytes)]; + memcpy(before, bytes, sizeof(bytes)); + assert(reinterpret_cast(input) % alignof(uint32_t) != 0); + assert(f.mesh.processAck(input) == &f.recipient && entry.ack == 0); + assert(f.mesh.connection_ack_calls == 0 && f.mesh.cancelled == 1); + assert(f.mesh.processAck(input) == &connection && f.mesh.connection_ack_calls == 1); + assert(f.mesh.last_connection_ack_input == input && f.mesh.last_connection_ack == 0x11223344); + assert(memcmp(bytes, before, sizeof(bytes)) == 0); + assert(f.mesh.cancelled == 1); + assert(f.mesh.one_key_notices == unsigned(MESH_ENABLE_ONE_KEY_DM)); + f.drain(); assert(ackCount(f.usb_stream.output) == 1); + ++checks; + } + assert(checks == 21); + printf("PASS: %u production ACK delivery checks\n", checks); +} diff --git a/test/test_companion_ack_delivery.py b/test/test_companion_ack_delivery.py new file mode 100644 index 000000000..1a3cdec21 --- /dev/null +++ b/test/test_companion_ack_delivery.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python3 +"""Run production ACK bookkeeping against actual UART/TCP bounded queues.""" + +from pathlib import Path +import os +import re +import shutil +import subprocess +import sys +import tempfile +import unittest + +from test_replay_reset_integration import extract_braced + + +ROOT = Path(__file__).resolve().parents[1] +FIXTURE = ROOT / "test/fixtures/companion_ack_delivery/test.cpp" +SANITIZERS = (["-fsanitize=address,undefined", "-fno-sanitize-recover=all", + "-fno-pie", "-no-pie"] if sys.platform.startswith("linux") else []) + + +class CompanionAckDeliveryTests(unittest.TestCase): + def test_actual_ack_routes_and_backpressure(self): + compiler = os.environ.get("CXX") or shutil.which("g++") or shutil.which("clang++") + if compiler is None: + self.skipTest("a host C++17 compiler is required") + source = (ROOT / "examples/companion_radio/MyMesh.cpp").read_text() + header = (ROOT / "examples/companion_radio/MyMesh.h").read_text() + dispatcher = (ROOT / "src/Dispatcher.cpp").read_text() + native = (ROOT / "test/test_serial_mode_switch/test_serial_mode_switch.cpp").read_text() + functions = "\n".join(extract_braced(source, signature) for signature in ( + "void MyMesh::clearExpectedAck(", "void MyMesh::expireExpectedAcks(", + "MyMesh::AckTableEntry* MyMesh::findPendingTextMessage(", + "ContactInfo* MyMesh::processAck(", + "void MyMesh::cancelSerialOperationsForRoute(", + "bool MyMesh::hasFiniteDelayedReplyForRoute(", + )) + functions += "\n" + "\n".join( + extract_braced(dispatcher, signature).replace("Dispatcher::", "MyMesh::") + for signature in ("bool Dispatcher::millisHasNowPassed(", + "unsigned long Dispatcher::futureMillis(") + ) + loop = extract_braced(source, "if (has_next_ack_expiry\n") + functions += "\nvoid MyMesh::service() {\n" + loop + "\n}\n" + # Execute the original successful-send slot initializer, not a copy of + # its predicates. Radio composition itself is outside this queue test. + sent = source[source.index("// The newest successfully-queued submission wins."):] + initialize = extract_braced(sent, "if (expected_ack) {") + functions += "\nvoid MyMesh::remember(uint32_t expected_ack, uint32_t msg_timestamp,\n" + functions += " ContactInfo* recipient, const uint8_t* text_fingerprint,\n" + functions += " const uint8_t* packet_retry_key, AckTableEntry* replacement_entry) {\n" + functions += " uint32_t est_timeout = 5000, one_key_delay = 0;\n" + initialize + functions += "\n expireExpectedAcks();\n}\n" + constants = "\n".join(re.findall( + r"^#define (?:EXPECTED_ACK_[A-Z_]+|PUSH_CODE_SEND_CONFIRMED)\s+.+$", + source, re.MULTILINE)) + constants += "\n#define EXPECTED_ACK_TABLE_SIZE 8\n#define MAX_HASH_SIZE 8\n" + with tempfile.TemporaryDirectory(prefix="meshcore-ack-delivery-") as directory: + work = Path(directory) + (work / "production_constants.inc").write_text(constants, encoding="ascii") + (work / "production_stream.inc").write_text( + extract_braced(native, "class BufferStream") + ";\n", encoding="ascii") + (work / "production_entry.inc").write_text( + extract_braced(header, "struct AckTableEntry") + ";\n", encoding="ascii") + (work / "production_ack.inc").write_text(functions, encoding="ascii") + for terminal, one_key in ((0, 0), (1, 0), (0, 1), (1, 1)): + with self.subTest(terminal=terminal, one_key=one_key): + binary = work / f"ack-{terminal}-{one_key}.exe" + compiled = subprocess.run([ + compiler, "-std=c++17", "-Werror", *SANITIZERS, + f"-DCOMPANION_FEATURE_TEXT_TERMINAL={terminal}", + f"-DMESH_ENABLE_ONE_KEY_DM={one_key}", + f"-I{work}", f"-I{ROOT / 'test/mocks'}", + f"-I{ROOT / 'test/fixtures/serial_wifi_sessions/mocks'}", + f"-I{ROOT / 'src'}", str(FIXTURE), + str(ROOT / "src/helpers/ArduinoSerialInterface.cpp"), + str(ROOT / "src/helpers/wifi/SerialWifiInterface.cpp"), + "-o", str(binary), + ], capture_output=True, text=True, timeout=60) + self.assertEqual(compiled.returncode, 0, compiled.stdout + compiled.stderr) + checked = subprocess.run([str(binary)], capture_output=True, text=True, timeout=10) + self.assertEqual(checked.returncode, 0, checked.stdout + checked.stderr) + self.assertIn("PASS: 21 production ACK delivery checks", checked.stdout) + + +if __name__ == "__main__": + unittest.main() diff --git a/test/test_nrf52_usb_logging_contract.py b/test/test_nrf52_usb_logging_contract.py index 4170b8b09..579efcf0e 100644 --- a/test/test_nrf52_usb_logging_contract.py +++ b/test/test_nrf52_usb_logging_contract.py @@ -627,15 +627,15 @@ class Nrf52UsbLoggingContractTest(unittest.TestCase): self.assertIn( "entry.reply_route = _serial->captureReplyRoute();", source ) - self.assertIn( - "_serial->writeFrameToRoute(expected_ack_table[i].reply_route,", - source, - ) self.assertIn("expected_ack_table[i].reply_route == route", source) ack_expiry = source[ source.index("void MyMesh::expireExpectedAcks()") : source.index("MyMesh::AckTableEntry* MyMesh::findPendingTextMessage(") ] + self.assertIn( + "_serial->writeFrameToRoute(entry.reply_route, confirmation, 9) == 9", + ack_expiry, + ) self.assertIn("entry.reply_route = NULL;", ack_expiry) self.assertNotIn("clearExpectedAck(entry);", ack_expiry) route_cancel = source[