From 55774906939e1c7f58a5227565760b625a4b6da1 Mon Sep 17 00:00:00 2001 From: Martin Bauer Date: Sun, 8 Sep 2019 20:28:27 +0200 Subject: [PATCH] Bugfix in firmware partial sending --- commands.readme | 7 +++++++ lib/session/MeasurementSession.h | 3 +++ lib/session/StreamingMsgPackEncoder.h | 9 ++++++++- platformio.ini | 8 ++++---- src/ConfigHardware.h | 2 +- src/firmware_main.cpp | 12 +++++++++--- test/test_common/sessiontest.cpp | 10 ++++++---- 7 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 commands.readme diff --git a/commands.readme b/commands.readme new file mode 100644 index 0000000..4e387d3 --- /dev/null +++ b/commands.readme @@ -0,0 +1,7 @@ +Test locally + platformio test -v -e native + + +Device: + platformio device monitor + platformio run -t upload diff --git a/lib/session/MeasurementSession.h b/lib/session/MeasurementSession.h index 300a899..73e831f 100644 --- a/lib/session/MeasurementSession.h +++ b/lib/session/MeasurementSession.h @@ -52,6 +52,9 @@ public: assert(startIdx == lastIdx, "Not all data was sent"); } + uint32_t getStartTime() const { + return currentChunk->getStartTime(); + } private: void rotate() { if( otherChunkFilled() ) diff --git a/lib/session/StreamingMsgPackEncoder.h b/lib/session/StreamingMsgPackEncoder.h index a9964e6..59a7c85 100644 --- a/lib/session/StreamingMsgPackEncoder.h +++ b/lib/session/StreamingMsgPackEncoder.h @@ -200,12 +200,19 @@ public: sentBytes_ += sizeof(T) * length; return; } else { - const uint32_t elementsToSend = length - elementsToSkip; + sentBytes_ += sizeof(T) * elementsToSkip; + const uint32_t elementsRemaining = length - elementsToSkip; + const uint32_t maxElementsToSend = (maxBytes_ - sentBytes_) / sizeof(T); + const uint32_t elementsToSend = min(elementsRemaining, maxElementsToSend); if( elementsToSend == 0 ) { sendingFinished_ = true; return; } else { encoder_.sendArrayPartialContents(data + elementsToSkip, elementsToSend); + sentBytes_ += sizeof(T) * elementsToSend; + if( elementsRemaining > elementsToSend ) { + sendingFinished_ = true; + } } } } diff --git a/platformio.ini b/platformio.ini index a668b4b..a1f3aba 100644 --- a/platformio.ini +++ b/platformio.ini @@ -20,7 +20,7 @@ lib_deps = AsyncTCP NTPClient -;[env:native] -;platform = native -;test_ignore = test_embedded -;build_flags = -g \ No newline at end of file +[env:native] +platform = native +test_ignore = test_embedded +build_flags = -g \ No newline at end of file diff --git a/src/ConfigHardware.h b/src/ConfigHardware.h index f9818fb..008e9d7 100644 --- a/src/ConfigHardware.h +++ b/src/ConfigHardware.h @@ -8,4 +8,4 @@ const int CONFIG_VALUE_DIVIDER = 128; // uint32 measurements are divid const int CONFIG_MEASURE_DELAY = 100; // interval in ms between measurements -const uint32_t CONFIG_SESSION_CHUNK_SIZE = 1024*8 - 3 * sizeof(uint32_t); +const uint32_t CONFIG_SESSION_CHUNK_SIZE = 1024*8 - 16 * sizeof(uint32_t); diff --git a/src/firmware_main.cpp b/src/firmware_main.cpp index f8d0532..9769187 100644 --- a/src/firmware_main.cpp +++ b/src/firmware_main.cpp @@ -113,16 +113,22 @@ void httpSetup(SessionManager * sessionManager) auto totalSize = encoderToDetermineSize.getContentLength(); Serial.print("Sending started of total size "); Serial.println(totalSize); - req->send("application/x-msgpack", totalSize, [=](uint8_t *buffer, size_t maxLen, size_t index) -> size_t { + auto callback = [=](uint8_t *buffer, size_t maxLen, size_t index) -> size_t { Serial.print("Partial send maxLen "); Serial.print(maxLen); Serial.print(" index "); - Serial.println("index"); + Serial.println(index); CopyWriter copyWriter(buffer); ChunkedStreamingMsgPackEncoder encoder(©Writer, index, index + maxLen); sessionManager->getSession().serialize(encoder, startIdx); + Serial.print("Bytes sent "); + Serial.println(encoder.sentBytes() - index); return encoder.sentBytes() - index; - }); + }; + AsyncWebServerResponse *response = req->beginResponse("application/x-msgpack", totalSize, callback); + auto sessionId = sessionManager->getSession().getStartTime(); + response->addHeader("content-disposition", "attachment; filename=\"" + String(sessionId) + ".st\""); + req->send(response); }); server.onNotFound(onNotFound); diff --git a/test/test_common/sessiontest.cpp b/test/test_common/sessiontest.cpp index c64d27b..86f1359 100644 --- a/test/test_common/sessiontest.cpp +++ b/test/test_common/sessiontest.cpp @@ -147,11 +147,11 @@ void testSession() { void testPartialSessionSerialization() { - const uint32_t SESSION_SIZE = 128; + const uint32_t SESSION_SIZE = 1024*8 - 16 * sizeof(uint32_t); typedef MeasurementSession MockSession; const uint32_t startTime = 194842; - const uint_t fillSize = SESSION_SIZE * 4 + 7; + const uint_t fillSize = 4937 + 81; MockSession session; session.init(startTime); @@ -167,7 +167,8 @@ void testPartialSessionSerialization() { session.serialize(encoder, 0); auto totalSize = encoder.getContentLength(); - std::vector splits = {16, 32, 128, 256, 512, 721, 1024, totalSize}; + std::vector splits = {953, totalSize}; + //std::vector splits = {totalSize}; uint32_t written = 0; data.clear(); for(auto & split : splits) { @@ -175,6 +176,7 @@ void testPartialSessionSerialization() { session.serialize(encoder, 0); written = encoder.sentBytes(); } + TEST_ASSERT(written == totalSize); uint32_t readStartTime=0; uint32_t readStartIndex=0; @@ -189,11 +191,11 @@ void testPartialSessionSerialization() { void allTests() { UNITY_BEGIN(); + RUN_TEST(testPartialSessionSerialization); RUN_TEST(testSessionChunkAdd); RUN_TEST(testSessionChunkGetterSetter); RUN_TEST(testSessionChunkSerialization); RUN_TEST(testSession); - RUN_TEST(testPartialSessionSerialization); UNITY_END(); }