Skip to content

Commit 6a8b597

Browse files
committed
When a data overrun occurs because the MQTT OTA data buffer is full before its content can be processed by update() then we transition to OTAState::Error and set OTAError::ReceivedDataOverrun
1 parent 276122b commit 6a8b597

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

extras/test/src/test_OTALogic.cpp

+39-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void simulateOTABinaryReception(OTALogic & ota_logic, OTAData const & ota_test_d
4949
TEST CODE
5050
**************************************************************************************/
5151

52-
TEST_CASE("OTAStorage initialisation fails ", "[OTAStorage::init() -> returns false]")
52+
TEST_CASE("OTAStorage initialisation fails", "[OTAStorage::init() -> returns false]")
5353
{
5454
Mock<OTAStorage> ota_storage;
5555

@@ -81,7 +81,7 @@ TEST_CASE("OTAStorage initialisation fails ", "[OTAStorage::init() -> returns fa
8181

8282
/**************************************************************************************/
8383

84-
TEST_CASE("OTAStorage opening of storage file fails ", "[OTAStorage::open() -> returns false]")
84+
TEST_CASE("OTAStorage opening of storage file fails", "[OTAStorage::open() -> returns false]")
8585
{
8686
Mock<OTAStorage> ota_storage;
8787

@@ -117,7 +117,7 @@ TEST_CASE("OTAStorage opening of storage file fails ", "[OTAStorage::open() -> r
117117

118118
/**************************************************************************************/
119119

120-
TEST_CASE("OTAStorage writing to storage file fails ", "[OTAStorage::write() -> fails]")
120+
TEST_CASE("OTAStorage writing to storage file fails", "[OTAStorage::write() -> fails]")
121121
{
122122
Mock<OTAStorage> ota_storage;
123123

@@ -152,6 +152,42 @@ TEST_CASE("OTAStorage writing to storage file fails ", "[OTAStorage::write() ->
152152

153153
/**************************************************************************************/
154154

155+
TEST_CASE("Data overrun due to receiving too much data", "[OTALogic - Data Overrun]")
156+
{
157+
Mock<OTAStorage> ota_storage;
158+
159+
/* Configure mock object */
160+
When(Method(ota_storage, init)).Return(true);
161+
When(Method(ota_storage, open)).Return(true);
162+
When(Method(ota_storage, write)).AlwaysDo([](uint8_t const * const /* buf */, size_t const num_bytes) -> size_t { return num_bytes; });
163+
Fake(Method(ota_storage, close));
164+
Fake(Method(ota_storage, remove));
165+
Fake(Method(ota_storage, deinit));
166+
167+
168+
/* Perform test */
169+
OTALogic ota_logic(ota_storage.get());
170+
171+
WHEN("Too much data is received before OTALogic::update() is called again to process the incoming data")
172+
{
173+
uint8_t const SOME_FAKE_DATA[MQTT_OTA_BUF_SIZE] = {0};
174+
ota_logic.onOTADataReceived(SOME_FAKE_DATA, MQTT_OTA_BUF_SIZE);
175+
ota_logic.onOTADataReceived(SOME_FAKE_DATA, MQTT_OTA_BUF_SIZE);
176+
ota_logic.update();
177+
178+
THEN("The OTA logic should be in the 'Error' state")
179+
{
180+
REQUIRE(ota_logic.state() == OTAState::Error);
181+
}
182+
THEN("The OTA error should be set to OTAError::ReceivedDataOverrun")
183+
{
184+
REQUIRE(ota_logic.error() == OTAError::ReceivedDataOverrun);
185+
}
186+
}
187+
}
188+
189+
/**************************************************************************************/
190+
155191
TEST_CASE("Valid OTA data is received ", "[OTALogic]")
156192
{
157193
Mock<OTAStorage> ota_storage;

src/utility/ota/OTALogic.cpp

+10-3
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,16 @@ OTAError OTALogic::update()
8585
void OTALogic::onOTADataReceived(uint8_t const * const data, size_t const length)
8686
{
8787
size_t const bytes_available = (MQTT_OTA_BUF_SIZE - _mqtt_ota_buf.num_bytes);
88-
size_t const bytes_to_copy = min(bytes_available, length);
89-
memcpy(_mqtt_ota_buf.buf + _mqtt_ota_buf.num_bytes, data, bytes_to_copy);
90-
_mqtt_ota_buf.num_bytes += bytes_to_copy;
88+
if(length <= bytes_available)
89+
{
90+
memcpy(_mqtt_ota_buf.buf + _mqtt_ota_buf.num_bytes, data, length);
91+
_mqtt_ota_buf.num_bytes += length;
92+
}
93+
else
94+
{
95+
_ota_state = OTAState::Error;
96+
_ota_error = OTAError::ReceivedDataOverrun;
97+
}
9198
}
9299

93100
/******************************************************************************

src/utility/ota/OTALogic.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ enum class OTAState
4343

4444
enum class OTAError : int
4545
{
46-
None = 0,
47-
StorageInitFailed = 1,
48-
StorageOpenFailed = 2,
49-
StorageWriteFailed = 3,
50-
ChecksumMismatch = 4
46+
None = 0,
47+
StorageInitFailed = 1,
48+
StorageOpenFailed = 2,
49+
StorageWriteFailed = 3,
50+
ChecksumMismatch = 4,
51+
ReceivedDataOverrun = 5
5152
};
5253

5354
/******************************************************************************

0 commit comments

Comments
 (0)