Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions components/comms/CommsChannelMsg/CommsChannelMsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> _cmdVector;
#else
Expand Down
11 changes: 11 additions & 0 deletions components/comms/CommsChannels/CommsChannelManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
3 changes: 3 additions & 0 deletions components/comms/CommsChannels/CommsChannelManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions components/comms/CommsCoreIF/CommsCoreIF.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
49 changes: 43 additions & 6 deletions components/comms/FileStreamProtocols/FileDownloadOKTOProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -376,6 +385,7 @@ bool FileDownloadOKTOProtocol::validateFileStreamStart(const String& fileName,
_oktoFilePos = 0;
_lastSentUptoFilePos = 0;
_batchBlockSendRetryCount = 0;
_clientReady = false;

// Timing of blocks
_lastBatchAckRxOrRetryMs = millis();
Expand All @@ -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)
Expand Down Expand Up @@ -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();
}
Expand All @@ -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))
{
Expand All @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 11 additions & 2 deletions components/comms/FileStreamProtocols/FileDownloadOKTOProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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";
Expand Down
1 change: 1 addition & 0 deletions components/comms/ProtocolRICSerial/ProtocolRICSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion linux_unit_tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
Expand Down
Loading
Loading