From 5c362194280a39aff5e57d061054ddfb6dadbf98 Mon Sep 17 00:00:00 2001 From: Rob Dobson Date: Sun, 15 Nov 2026 16:24:09 +0000 Subject: [PATCH] File downloads: sent at the rate the channel takes blocks, with a real fallback to smaller blocks - Blocks are sent when the last has left the channel's outbound queue (CommsCoreIF::outboundQueuedCount) and its transport is ready (outboundCanAccept), within the ack window, instead of one every 100 ms - The first block waits for the client's first dfAck (clients ack as soon as they are ready to receive), or 200 ms: a block sent straight after the reply to dfStart can reach a client before it has handled the reply - When nothing is acknowledged for 3 s the block size is halved (down to 64 bytes) before resending, in case the channel offers blocks its transport can't deliver - Blocks are marked as acknowledged by the protocol (CommsChannelMsg::setAckedByProtocol), so a transport needn't confirm each packet (BLE sends them as notifications) - test_file_download_okto Measured on an ESP32-S3 (Axiom): USB serial 320 kB/s (downloads of 5000-byte blocks failed before), WiFi 125 kB/s (48), BLE 50 kB/s (5), with the matching RaftSysMods and RaftWebServer changes. Co-Authored-By: Claude Fable 5.2 --- .../comms/CommsChannelMsg/CommsChannelMsg.h | 14 + .../CommsChannels/CommsChannelManager.cpp | 11 + .../comms/CommsChannels/CommsChannelManager.h | 3 + components/comms/CommsCoreIF/CommsCoreIF.h | 8 + .../FileDownloadOKTOProtocol.cpp | 49 ++- .../FileDownloadOKTOProtocol.h | 13 +- .../ProtocolRICSerial/ProtocolRICSerial.cpp | 1 + linux_unit_tests/Makefile | 3 +- linux_unit_tests/test_file_download_okto.cpp | 302 ++++++++++++++++++ 9 files changed, 395 insertions(+), 9 deletions(-) create mode 100644 linux_unit_tests/test_file_download_okto.cpp diff --git a/components/comms/CommsChannelMsg/CommsChannelMsg.h b/components/comms/CommsChannelMsg/CommsChannelMsg.h index 7bdf3145..a56b07cd 100644 --- a/components/comms/CommsChannelMsg/CommsChannelMsg.h +++ b/components/comms/CommsChannelMsg/CommsChannelMsg.h @@ -202,12 +202,26 @@ class CommsChannelMsg return _bufTakeable; } + // Whether the message is part of an exchange with its own acknowledgements and resends (e.g. a file block + // of a download, which the client acks and the sender sends again if it is lost). A transport that can send + // either with or without a confirmation of each packet (e.g. BLE: indications or notifications) needn't + // have these confirmed. Set by the sender; a protocol codec copies it to the message it encodes + void setAckedByProtocol(bool ackedByProtocol) + { + _ackedByProtocol = ackedByProtocol; + } + bool isAckedByProtocol() const + { + return _ackedByProtocol; + } + private: uint32_t _channelID; CommsMsgProtocol _msgProtocol; uint32_t _msgNum; CommsMsgTypeCode _msgTypeCode; bool _bufTakeable = false; + bool _ackedByProtocol = false; #ifdef IMPLEMENT_NO_PSRAM_FOR_COMMS_CHANNEL_MSG std::vector _cmdVector; #else diff --git a/components/comms/CommsChannels/CommsChannelManager.cpp b/components/comms/CommsChannels/CommsChannelManager.cpp index dde89002..38144fb6 100644 --- a/components/comms/CommsChannels/CommsChannelManager.cpp +++ b/components/comms/CommsChannels/CommsChannelManager.cpp @@ -539,6 +539,17 @@ uint32_t CommsChannelManager::outboundMsgBlockMax(uint32_t channelID, uint32_t d return blockMax; } +///////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Number of outbound messages waiting in a channel's queue +///////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +uint32_t CommsChannelManager::outboundQueuedCount(uint32_t channelID) +{ + if ((channelID >= _commsChannelVec.size()) || !_commsChannelVec[channelID]) + return 0; + return _commsChannelVec[channelID]->outboundQueuedCount(); +} + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Handle outbound message on a specific channel ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/components/comms/CommsChannels/CommsChannelManager.h b/components/comms/CommsChannels/CommsChannelManager.h index 2f5f4370..aef36853 100644 --- a/components/comms/CommsChannels/CommsChannelManager.h +++ b/components/comms/CommsChannels/CommsChannelManager.h @@ -60,6 +60,9 @@ class CommsChannelManager : public RaftSysMod, public CommsCoreIF // Get the max outbound message size virtual uint32_t outboundMsgBlockMax(uint32_t channelID, uint32_t defaultSize) override final; + // Number of outbound messages waiting in a channel's queue + virtual uint32_t outboundQueuedCount(uint32_t channelID) override final; + // Get info String getInfoJSON(); diff --git a/components/comms/CommsCoreIF/CommsCoreIF.h b/components/comms/CommsCoreIF/CommsCoreIF.h index 42b75a8c..bc937fe2 100644 --- a/components/comms/CommsCoreIF/CommsCoreIF.h +++ b/components/comms/CommsCoreIF/CommsCoreIF.h @@ -68,6 +68,14 @@ class CommsCoreIF // Get the max outbound message size virtual uint32_t outboundMsgBlockMax(uint32_t channelID, uint32_t defaultSize) = 0; + // Number of outbound messages waiting in a channel's queue (not yet passed to its transport). A sender of + // many messages (e.g. a file download) sends the next when this is 0 and outboundCanAccept() is true, so it + // goes at the rate the channel takes them and the queue (which drops messages when full) can't fill + virtual uint32_t outboundQueuedCount(uint32_t channelID) + { + return 0; + } + // Get channel IDs virtual int32_t getChannelIDByName(const String& channelName, const String& protocolName) = 0; diff --git a/components/comms/FileStreamProtocols/FileDownloadOKTOProtocol.cpp b/components/comms/FileStreamProtocols/FileDownloadOKTOProtocol.cpp index 655873ee..f4030e6f 100644 --- a/components/comms/FileStreamProtocols/FileDownloadOKTOProtocol.cpp +++ b/components/comms/FileStreamProtocols/FileDownloadOKTOProtocol.cpp @@ -293,6 +293,15 @@ RaftRetCode FileDownloadOKTOProtocol::handleAckMsg(const RICRESTMsg& ricRESTReqM // Handle ack RaftJson cmdFrame = ricRESTReqMsg.getPayloadJson(); uint32_t oktoFilePos = cmdFrame.getLong("okto", 0); + + // Any ack shows the client is receiving (see START_ACK_WAIT_MS) + if (!_clientReady) + { + _clientReady = true; + _lastBatchAckRxOrRetryMs = millis(); + if (oktoFilePos == 0) + return RaftRetCode::RAFT_OK; + } if ((oktoFilePos != 0) && (oktoFilePos > _oktoFilePos)) { uint32_t newBytesReceived = oktoFilePos - _oktoFilePos; @@ -376,6 +385,7 @@ bool FileDownloadOKTOProtocol::validateFileStreamStart(const String& fileName, _oktoFilePos = 0; _lastSentUptoFilePos = 0; _batchBlockSendRetryCount = 0; + _clientReady = false; // Timing of blocks _lastBatchAckRxOrRetryMs = millis(); @@ -393,11 +403,7 @@ void FileDownloadOKTOProtocol::transferService() if (!_isDownloading || !_fileStreamBlockRead) return; - // Check min time between blocks unsigned long nowMillis = millis(); - if (!Raft::isTimeout(millis(), _betweenBlocksMs, MIN_TIME_BETWEEN_BLOCKS_MS)) - return; - _betweenBlocksMs = millis(); // Check if we're finished - with the CRC at the end the session waits (a while) for dfEnd, whose reply carries it if (_oktoFilePos >= _fileSize) @@ -438,6 +444,15 @@ void FileDownloadOKTOProtocol::transferService() // Go back to last okto position _lastSentUptoFilePos = _oktoFilePos; + // Nothing has been acknowledged since the last ack (or the start): the channel may not be able to + // deliver blocks this big (it may offer more than its transport can send), so try smaller ones. + // Clients place blocks by file position and take any length + if (_blockSize > FILE_BLOCK_SIZE_FALLBACK_MIN) + { + _blockSize = std::max(_blockSize / 2, FILE_BLOCK_SIZE_FALLBACK_MIN); + LOG_W(MODULE_PREFIX, "transferService no ack - block size reduced to %d", (int)_blockSize); + } + // Update timing _lastBatchAckRxOrRetryMs = millis(); } @@ -449,6 +464,14 @@ void FileDownloadOKTOProtocol::transferService() } } + // Wait for the client to be ready for the first block (its first ack, or a short time) + if (!_clientReady) + { + if (!Raft::isTimeout(nowMillis, _startMs, START_ACK_WAIT_MS)) + return; + _clientReady = true; + } + uint32_t blocksOutstanding = (_lastSentUptoFilePos + (_blockSize-1) - _oktoFilePos) / _blockSize; if ((_lastSentUptoFilePos >= _fileSize) || (blocksOutstanding >= _batchAckSize)) { @@ -459,6 +482,18 @@ void FileDownloadOKTOProtocol::transferService() return; } + // Send at the rate the channel takes blocks: the next one goes when the last has left the channel's queue + // and its transport is ready for another (e.g. the serial port has sent the last frame, or BLE's queue has + // room). The ack window above limits how far ahead of the client this gets + if (_pCommsCore) + { + if (_pCommsCore->outboundQueuedCount(_commsChannelID) > 0) + return; + bool noConn = false; + if (!_pCommsCore->outboundCanAccept(_commsChannelID, MSG_TYPE_COMMAND, noConn)) + return; + } + // Check if internal heap is too low to safely send - throttle to let TCP drain #ifdef ESP_PLATFORM uint32_t freeInternalHeap = heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); @@ -477,8 +512,7 @@ void FileDownloadOKTOProtocol::transferService() RaftRetCode retc = _fileStreamBlockRead(block, _lastSentUptoFilePos, _blockSize); if (retc == RaftRetCode::RAFT_BUSY) { - // Try again on the next pass (not after the time between blocks) - _betweenBlocksMs = nowMillis - MIN_TIME_BETWEEN_BLOCKS_MS; + // Try again on the next pass return; } if (retc == RaftRetCode::RAFT_OK) @@ -592,6 +626,9 @@ void FileDownloadOKTOProtocol::sendBlock(FileStreamBlockOwned& block) block.getBlockData(), block.getBlockLen(), endpointMsg); + // The client acks blocks and a lost one is sent again, so the transport needn't confirm each packet + endpointMsg.setAckedByProtocol(true); + // Send message if (_pCommsCore) _pCommsCore->outboundHandleMsg(endpointMsg); diff --git a/components/comms/FileStreamProtocols/FileDownloadOKTOProtocol.h b/components/comms/FileStreamProtocols/FileDownloadOKTOProtocol.h index 8e59e7af..7ac6ed78 100644 --- a/components/comms/FileStreamProtocols/FileDownloadOKTOProtocol.h +++ b/components/comms/FileStreamProtocols/FileDownloadOKTOProtocol.h @@ -15,10 +15,17 @@ class FileDownloadOKTOProtocol : public FileStreamBase { public: // Consts - static const uint32_t MIN_TIME_BETWEEN_BLOCKS_MS = 100; + // Blocks are sent as fast as the channel takes them (see transferService), within the ack window. After + // BLOCK_MSGS_TIMEOUT_MS with nothing acknowledged the block size is halved (down to + // FILE_BLOCK_SIZE_FALLBACK_MIN) before resending, in case the channel can't deliver blocks that big + static constexpr uint32_t FILE_BLOCK_SIZE_FALLBACK_MIN = 64; // With the CRC at the end, how long the session waits for dfEnd (its reply carries the CRC) after the last ack static const uint32_t END_MSG_WAIT_MS = 2000; static const uint32_t BLOCK_MSGS_TIMEOUT_MS = 3000; + // The first block waits for the client's first dfAck (clients send one, with okto 0, as soon as they are ready + // to receive), or this long for a client that doesn't send one. A block sent straight after the reply to + // dfStart can reach a client before it has handled that reply, and it then discards the block + static const uint32_t START_ACK_WAIT_MS = 200; static const uint32_t MAX_BATCH_BLOCK_ACK_RETRIES = 10; static constexpr uint32_t FILE_BLOCK_SIZE_MIN = 20; static const uint32_t FILE_BLOCK_SIZE_DEFAULT = 5000; @@ -134,6 +141,9 @@ class FileDownloadOKTOProtocol : public FileStreamBase uint32_t _lastSentUptoFilePos = 0; uint32_t _batchBlockSendRetryCount = 0; + // The client has sent an ack since the start (it is ready for blocks) + bool _clientReady = false; + // CRC at end support bool _crcAtEnd = false; uint32_t _incrementalCrc = 0; @@ -143,7 +153,6 @@ class FileDownloadOKTOProtocol : public FileStreamBase uint32_t _debugLastStatsMs = millis(); static const uint32_t DEBUG_STATS_MS = 10000; bool _debugFinalMsgToSend = false; - uint32_t _betweenBlocksMs = 0; // Debug static constexpr const char* MODULE_PREFIX = "FileDLOKTO"; diff --git a/components/comms/ProtocolRICSerial/ProtocolRICSerial.cpp b/components/comms/ProtocolRICSerial/ProtocolRICSerial.cpp index 4e24fb89..3c237f09 100644 --- a/components/comms/ProtocolRICSerial/ProtocolRICSerial.cpp +++ b/components/comms/ProtocolRICSerial/ProtocolRICSerial.cpp @@ -165,6 +165,7 @@ void ProtocolRICSerial::encodeTxMsgAndSend(CommsChannelMsg& msg) // Nothing uses the encoded message after it is sent, so the channel may take its buffer encodedMsg.setBufTakeable(true); + encodedMsg.setAckedByProtocol(msg.isAckedByProtocol()); #endif // Debug diff --git a/linux_unit_tests/Makefile b/linux_unit_tests/Makefile index ea701189..566d6c58 100644 --- a/linux_unit_tests/Makefile +++ b/linux_unit_tests/Makefile @@ -113,6 +113,7 @@ SOURCES_test_runtime_decoder = test_runtime_decoder.cpp $(CORE)/DeviceTypes/Devi SOURCES_test_poll_data_decode = test_poll_data_decode.cpp $(CORE)/DeviceTypes/DeviceTypeRecords.cpp $(CORE)/DeviceTypes/DeviceTypeGrammar.cpp $(CORE)/DeviceTypes/DeviceTypeRuntimeDecoder.cpp $(CORE)/DeviceTypes/DeviceTypeCodeInterpreter.cpp $(CORE)/DeviceTypes/DeviceTypeDecodeVerifier.cpp $(CORE)/DeviceTypes/DeviceTypeFileLoader.cpp $(CORE)/MiniHDLC/MiniHDLC.cpp $(COMMON_SOURCES) SOURCES_test_protocol_exchange = test_protocol_exchange.cpp $(COMMS_SOURCES) $(COMMON_SOURCES) SOURCES_test_file_upload_okto = test_file_upload_okto.cpp $(COMMS_SOURCES) $(COMMON_SOURCES) +SOURCES_test_file_download_okto = test_file_download_okto.cpp $(COMMS_SOURCES) $(COMMON_SOURCES) SOURCES_test_file_upload_background = test_file_upload_background.cpp $(COMMS_SOURCES) $(COMMON_SOURCES) SOURCES_test_thread_safe_queue = test_thread_safe_queue.cpp $(COMMON_SOURCES) SOURCES_test_simple_buffer = test_simple_buffer.cpp $(CORE)/MiniHDLC/MiniHDLC.cpp $(COMMON_SOURCES) @@ -127,7 +128,7 @@ SOURCES_test_espnow_config = test_espnow_config.cpp $(ESPNOW_DIR)/ESPNowConfig.c # Tests run by "make check" TESTS = test_raft_json test_raft_utils test_device_names test_device_settings test_device_type_settings test_device_data_subscribers test_device_type_records \ - test_device_type_table test_device_type_grammar test_device_type_policy test_device_type_file test_catalogue_index test_runtime_decoder test_poll_data_decode test_bus_ident test_bus_scan_schedule test_device_poll_timing test_device_poll_scheduler test_poll_plan test_protocol_exchange test_file_upload_okto test_file_upload_background test_mini_hdlc test_mqtt_protocol test_thread_safe_queue test_simple_buffer test_file_system_paths test_led_strip_rmt_plan test_device_action_encoder test_rest_api_params test_comms_framing_vectors + test_device_type_table test_device_type_grammar test_device_type_policy test_device_type_file test_catalogue_index test_runtime_decoder test_poll_data_decode test_bus_ident test_bus_scan_schedule test_device_poll_timing test_device_poll_scheduler test_poll_plan test_protocol_exchange test_file_upload_okto test_file_download_okto test_file_upload_background test_mini_hdlc test_mqtt_protocol test_thread_safe_queue test_simple_buffer test_file_system_paths test_led_strip_rmt_plan test_device_action_encoder test_rest_api_params test_comms_framing_vectors # Extra device type catalogues for test_device_type_records to sweep, e.g. # make check DEV_TYPE_CATALOGUES="../../RaftI2C/linux_unit_tests/TestDevTypeRecs.json" diff --git a/linux_unit_tests/test_file_download_okto.cpp b/linux_unit_tests/test_file_download_okto.cpp new file mode 100644 index 00000000..c49116ca --- /dev/null +++ b/linux_unit_tests/test_file_download_okto.cpp @@ -0,0 +1,302 @@ +///////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// +// FileDownloadOKTOProtocol tests +// +// Drives the download protocol directly (dfStart, loop(), dfAck, dfEnd) and checks how it sends blocks: +// - as fast as the channel takes them (no fixed time between blocks), within the ack window +// - not while the channel's queue holds a message or its transport isn't ready +// - with the block size the channel offers +// - with smaller blocks when nothing is acknowledged (a channel that can't deliver blocks that big) +// +///////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include "testhdr.h" +#include "ArduinoWString.h" +#include "RaftJson.h" +#include "CommsCoreIF.h" +#include "CommsChannelMsg.h" +#include "RICRESTMsg.h" +#include "MiniHDLC.h" +#include "FileStreamBlockOwned.h" +#include "FileDownloadOKTOProtocol.h" + +static const uint32_t TEST_CHANNEL_ID = 3; +static const uint32_t STREAM_ID = 5; + +///////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// CommsCore stand-in: a channel with a block size, a queue and a transport that may not be ready, which records +// the blocks sent on it +///////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +struct SentBlock +{ + uint32_t pos; + uint32_t len; + uint32_t streamID; + std::vector data; + bool ackedByProtocol; +}; + +class TestCommsCore : public CommsCoreIF +{ +public: + std::vector blocks; + std::vector jsonSent; + uint32_t blockMax = 5000; + uint32_t queued = 0; + bool canAccept = true; + // Largest block the transport delivers (bigger ones are lost, as a transport that can't send them) + uint32_t deliverMax = UINT32_MAX; + + virtual uint32_t registerChannel(const char*, const char*, const char*, CommsChannelOutboundHandleMsgFnType, + CommsChannelOutboundCanAcceptFnType, const CommsChannelSettings*) override { return 0; } + virtual void addProtocol(ProtocolCodecFactoryHelper&) override {} + virtual bool inboundCanAccept(uint32_t) override { return true; } + virtual void inboundHandleMsg(uint32_t, const uint8_t*, uint32_t) override {} + virtual void inboundHandleMsgVec(uint32_t, const SpiramAwareUint8Vector&) override {} + virtual uint32_t inboundMsgBlockMax(uint32_t, uint32_t) override { return 1200; } + virtual bool outboundCanAccept(uint32_t, CommsMsgTypeCode, bool& noConn) override { noConn = false; return canAccept; } + virtual CommsCoreRetCode outboundHandleMsg(CommsChannelMsg& msg) override + { + RICRESTMsg ricMsg; + if (!ricMsg.decode(msg.getBuf(), msg.getBufLen())) + return COMMS_CORE_RET_OK; + if (ricMsg.getElemCode() == RICRESTMsg::RICREST_ELEM_CODE_FILEBLOCK) + { + if (ricMsg.getBinLen() <= deliverMax) + blocks.push_back({ ricMsg.getBufferPos(), ricMsg.getBinLen(), ricMsg.getStreamID(), + std::vector(ricMsg.getBinBuf(), ricMsg.getBinBuf() + ricMsg.getBinLen()), + msg.isAckedByProtocol() }); + } + else + { + jsonSent.push_back(ricMsg.getPayloadJson()); + } + return COMMS_CORE_RET_OK; + } + virtual uint32_t outboundMsgBlockMax(uint32_t, uint32_t) override { return blockMax; } + virtual uint32_t outboundQueuedCount(uint32_t) override { return queued; } + virtual int32_t getChannelIDByName(const String&, const String&) override { return -1; } + virtual uint32_t bridgeRegister(const char*, uint32_t, uint32_t, uint32_t) override { return 0; } + virtual void bridgeUnregister(uint32_t, bool) override {} + virtual void bridgeHandleInboundMsg(uint32_t, CommsChannelMsg&) override {} + virtual bool bridgeHandleOutboundMsg(CommsChannelMsg&) override { return false; } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// A download in progress: the protocol, the file it reads, and a client that takes blocks in order and acks +///////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +class Download +{ +public: + TestCommsCore commsCore; + std::vector contents; + std::vector received; + uint32_t blocksTaken = 0; + FileDownloadOKTOProtocol protocol; + String startResp; + + explicit Download(uint32_t fileLen) : + protocol(nullptr, + [this](FileStreamBlockOwned& block, uint32_t filePos, uint32_t maxLen) { + uint32_t len = std::min(maxLen, contents.size() - filePos); + block.set("test.bin", contents.size(), filePos, contents.data() + filePos, len, + filePos + len >= contents.size(), 0, false, contents.size(), true, filePos == 0); + return RAFT_OK; + }, + nullptr, nullptr, &commsCore, + FileStreamBase::FILE_STREAM_CONTENT_TYPE_FILE, FileStreamBase::FILE_STREAM_FLOW_TYPE_RICREST_DOWNLOAD, + STREAM_ID, fileLen, "test.bin") + { + for (uint32_t i = 0; i < fileLen; i++) + contents.push_back((uint8_t)(i * 7 + (i >> 8))); + } + + String command(FileStreamBase::FileStreamMsgType msgType, const String& json) + { + CommsChannelMsg msg(TEST_CHANNEL_ID, MSG_PROTOCOL_RICREST, 1, MSG_TYPE_COMMAND); + RICRESTMsg::encode(json, msg, RICRESTMsg::RICREST_ELEM_CODE_COMMAND_FRAME); + RICRESTMsg req; + req.decode(msg.getBuf(), msg.getBufLen()); + String resp; + protocol.handleCmdFrame(msgType, req, resp, msg); + return resp; + } + + // Start a download. Clients ack (okto 0) as soon as they are ready to receive, which starts the blocks + void start(uint32_t block, uint32_t batch, bool readyAck = true) + { + startResp = command(FileStreamBase::FILE_STREAM_MSG_TYPE_DOWNLOAD_START, + R"({"cmdName":"dfStart","reqStr":"getFile","fileType":"fs","fileName":"test.bin","batchMsgSize":)" + + String(block) + R"(,"batchAckSize":)" + String(batch) + R"(,"crcAt":"end"})"); + if (readyAck) + clientTakesAndAcks(); + } + + // Run the protocol's loop a number of times (no time passes) + void loops(uint32_t count) + { + for (uint32_t i = 0; i < count; i++) + protocol.loop(); + } + + // The client takes the blocks sent since it last looked (those in sequence) and acks what it has + void clientTakesAndAcks() + { + for (; blocksTaken < commsCore.blocks.size(); blocksTaken++) + { + const SentBlock& block = commsCore.blocks[blocksTaken]; + if (block.pos == received.size()) + received.insert(received.end(), block.data.begin(), block.data.end()); + } + command(FileStreamBase::FILE_STREAM_MSG_TYPE_DOWNLOAD_ACK, + R"({"cmdName":"dfAck","okto":)" + String((uint32_t)received.size()) + + R"(,"streamID":)" + String(STREAM_ID) + R"(,"rslt":"ok"})"); + } + + String end() + { + return command(FileStreamBase::FILE_STREAM_MSG_TYPE_DOWNLOAD_END, + R"({"cmdName":"dfEnd","reqStr":"getFile","fileName":"test.bin","streamID":)" + String(STREAM_ID) + "}"); + } + + String crcHex() const + { + char buf[10]; + snprintf(buf, sizeof(buf), "%04x", MiniHDLC::crcUpdateCCITT(MiniHDLC::crcInitCCITT(), contents.data(), contents.size())); + return buf; + } +}; + +///////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Tests +///////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +static void testSendsAsFastAsTheChannelAllows() +{ + printf("Running testSendsAsFastAsTheChannelAllows...\n"); + Download down(10 * 1000 + 300); + down.start(1000, 4); + TEST_ASSERT(RaftJson(down.startResp).getString("rslt", "") == "ok", "dfStart ok"); + TEST_ASSERT(RaftJson(down.startResp).getInt("batchMsgSize", 0) == 1000, "block size as requested"); + TEST_ASSERT(RaftJson(down.startResp).getInt("fileLen", 0) == 10300, "file length"); + // A batch goes at once (one block a loop, no time between them), then it waits for the ack + down.loops(3); + TEST_ASSERT(down.commsCore.blocks.size() == 3, ("a block a loop " + String((int)down.commsCore.blocks.size())).c_str()); + down.loops(20); + TEST_ASSERT(down.commsCore.blocks.size() == 4, ("waits for the ack after a batch " + String((int)down.commsCore.blocks.size())).c_str()); + TEST_ASSERT(down.commsCore.blocks[3].pos == 3000 && down.commsCore.blocks[3].streamID == STREAM_ID, "block header"); + // Blocks are marked as acknowledged by the protocol (a transport such as BLE then needn't confirm each packet) + TEST_ASSERT(down.commsCore.blocks[3].ackedByProtocol, "blocks marked as acked by the protocol"); + // Acked: the next batch + down.clientTakesAndAcks(); + down.loops(20); + TEST_ASSERT(down.commsCore.blocks.size() == 8, "next batch after the ack"); + down.clientTakesAndAcks(); + down.loops(20); + TEST_ASSERT(down.commsCore.blocks.size() == 11, "the rest"); + TEST_ASSERT(down.commsCore.blocks[10].len == 300, "last block is the remainder"); + down.clientTakesAndAcks(); + TEST_ASSERT(down.received == down.contents, "file intact"); + TEST_ASSERT(RaftJson(down.end()).getString("crc16", "") == down.crcHex(), "CRC at the end"); +} + +static void testWaitsForTheChannel() +{ + printf("Running testWaitsForTheChannel...\n"); + Download down(8000); + down.start(1000, 4); + // The transport isn't ready (e.g. the serial port is still sending the last frame) + down.commsCore.canAccept = false; + down.loops(10); + TEST_ASSERT(down.commsCore.blocks.empty(), "nothing sent while the transport isn't ready"); + // A message is still in the channel's queue + down.commsCore.canAccept = true; + down.commsCore.queued = 1; + down.loops(10); + TEST_ASSERT(down.commsCore.blocks.empty(), "nothing sent while the queue holds a message"); + down.commsCore.queued = 0; + down.loops(1); + TEST_ASSERT(down.commsCore.blocks.size() == 1, "sent when the channel is ready"); +} + +static void testBlockSizeFromTheChannel() +{ + printf("Running testBlockSizeFromTheChannel...\n"); + Download down(3000); + down.commsCore.blockMax = 500; + down.start(5000, 4); + TEST_ASSERT(RaftJson(down.startResp).getInt("batchMsgSize", 0) == 500, "the channel's block size"); + down.loops(10); + TEST_ASSERT(down.commsCore.blocks.size() == 4 && down.commsCore.blocks[0].len == 500, "blocks of that size"); +} + +static void testSmallerBlocksWhenNothingIsAcked() +{ + printf("Running testSmallerBlocksWhenNothingIsAcked (waits for two ack timeouts)...\n"); + // The channel offers 1000-byte blocks but its transport only delivers up to 300 + Download down(2500); + down.commsCore.blockMax = 1000; + down.commsCore.deliverMax = 300; + down.start(5000, 4); + down.loops(10); + TEST_ASSERT(down.commsCore.blocks.empty(), "blocks of 1000 are lost"); + // No ack: after the timeout it goes back to the start with blocks of 500 (lost too), then 250 + std::this_thread::sleep_for(std::chrono::milliseconds(FileDownloadOKTOProtocol::BLOCK_MSGS_TIMEOUT_MS + 100)); + down.loops(10); + TEST_ASSERT(down.commsCore.blocks.empty(), "blocks of 500 are lost"); + std::this_thread::sleep_for(std::chrono::milliseconds(FileDownloadOKTOProtocol::BLOCK_MSGS_TIMEOUT_MS + 100)); + down.loops(10); + TEST_ASSERT(down.commsCore.blocks.size() == 4, ("blocks of 250 arrive " + String((int)down.commsCore.blocks.size())).c_str()); + TEST_ASSERT(down.commsCore.blocks.size() == 4 && down.commsCore.blocks[0].pos == 0 && down.commsCore.blocks[0].len == 250, + "from the start, 250 bytes"); + // The client acks as it receives: the rest follows at that size, and the CRC covers the file once + for (int i = 0; i < 5; i++) + { + down.clientTakesAndAcks(); + down.loops(10); + } + TEST_ASSERT(down.received == down.contents, "file intact"); + TEST_ASSERT(RaftJson(down.end()).getString("crc16", "") == down.crcHex(), "CRC at the end"); +} + +static void testFirstBlockWaitsForTheClient() +{ + printf("Running testFirstBlockWaitsForTheClient (waits for the start time)...\n"); + // Nothing is sent until the client's first ack: a block sent straight after the reply to dfStart can reach a + // client before it has handled that reply + Download down(3000); + down.start(1000, 4, false); + down.loops(10); + TEST_ASSERT(down.commsCore.blocks.empty(), "nothing sent before the client's first ack"); + down.clientTakesAndAcks(); + down.loops(10); + TEST_ASSERT(down.commsCore.blocks.size() == 3, "blocks follow the first ack"); + down.clientTakesAndAcks(); + TEST_ASSERT(down.received == down.contents, "file intact"); + + // A client that doesn't ack until it has data: the blocks start after a short wait + Download quiet(3000); + quiet.start(1000, 4, false); + quiet.loops(10); + TEST_ASSERT(quiet.commsCore.blocks.empty(), "nothing sent at once"); + std::this_thread::sleep_for(std::chrono::milliseconds(FileDownloadOKTOProtocol::START_ACK_WAIT_MS + 50)); + quiet.loops(10); + TEST_ASSERT(quiet.commsCore.blocks.size() == 3, "blocks start after the wait"); + quiet.clientTakesAndAcks(); + TEST_ASSERT(quiet.received == quiet.contents, "file intact without a first ack"); +} + +int main() +{ + testFirstBlockWaitsForTheClient(); + testSendsAsFastAsTheChannelAllows(); + testWaitsForTheChannel(); + testBlockSizeFromTheChannel(); + testSmallerBlocksWhenNothingIsAcked(); + return testSummary("test_file_download_okto"); +}