Skip to content

Commit 44acce9

Browse files
committed
Handle a dedicated 'rename' step in which the file used for downloading (UPDATE.BIN.TMP) is renamed to the one expected by the SFU (UPDATE.BIN). This step is only performed when the complete file with valid checksum has been received
1 parent 664ca5e commit 44acce9

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

src/utility/ota/OTALogic.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ OTAError OTALogic::update()
7676
case OTAState::WaitForBinary: _ota_state = handle_WaitForBinary (); break;
7777
case OTAState::BinaryReceived: _ota_state = handle_BinaryReceived(); break;
7878
case OTAState::Verify: _ota_state = handle_Verify (); break;
79+
case OTAState::Rename: _ota_state = handle_Rename (); break;
7980
case OTAState::Reset: _ota_state = handle_Reset (); break;
8081
case OTAState::Error: break;
8182
}
@@ -123,7 +124,7 @@ OTAState OTALogic::handle_Idle()
123124

124125
OTAState OTALogic::handle_StartDownload()
125126
{
126-
if(_ota_storage->open()) {
127+
if(_ota_storage->open("UPDATE.BIN.TMP")) {
127128
return OTAState::WaitForHeader;
128129
} else {
129130
_ota_error = OTAError::StorageOpenFailed;
@@ -212,15 +213,26 @@ OTAState OTALogic::handle_BinaryReceived()
212213
OTAState OTALogic::handle_Verify()
213214
{
214215
if(_ota_bin_data.crc32 == _ota_bin_data.hdr_crc32) {
215-
_ota_storage->deinit();
216-
return OTAState::Reset;
216+
return OTAState::Rename;
217217
} else {
218-
_ota_storage->remove();
218+
_ota_storage->remove("UPDATE.BIN.TMP");
219219
_ota_error = OTAError::ChecksumMismatch;
220220
return OTAState::Error;
221221
}
222222
}
223223

224+
OTAState OTALogic::handle_Rename()
225+
{
226+
if(_ota_storage->rename("UPDATE.BIN.TMP", "UPDATE.BIN")) {
227+
_ota_storage->deinit();
228+
return OTAState::Reset;
229+
}
230+
else {
231+
_ota_error = OTAError::RenameOfTempFileFailed;
232+
return OTAState::Error;
233+
}
234+
}
235+
224236
OTAState OTALogic::handle_Reset()
225237
{
226238
#if !defined(HOST) && !defined(ESP8266)

src/utility/ota/OTALogic.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,18 @@ static size_t const MQTT_OTA_BUF_SIZE = 256;
4545

4646
enum class OTAState
4747
{
48-
Init, Idle, StartDownload, WaitForHeader, HeaderReceived, WaitForBinary, BinaryReceived, Verify, Reset, Error
48+
Init, Idle, StartDownload, WaitForHeader, HeaderReceived, WaitForBinary, BinaryReceived, Verify, Rename, Reset, Error
4949
};
5050

5151
enum class OTAError : int
5252
{
53-
None = 0,
54-
StorageInitFailed = 1,
55-
StorageOpenFailed = 2,
56-
StorageWriteFailed = 3,
57-
ChecksumMismatch = 4,
58-
ReceivedDataOverrun = 5
53+
None = 0,
54+
StorageInitFailed = 1,
55+
StorageOpenFailed = 2,
56+
StorageWriteFailed = 3,
57+
ChecksumMismatch = 4,
58+
ReceivedDataOverrun = 5,
59+
RenameOfTempFileFailed = 6
5960
};
6061

6162
/******************************************************************************
@@ -114,6 +115,7 @@ class OTALogic
114115
OTAState handle_WaitForBinary();
115116
OTAState handle_BinaryReceived();
116117
OTAState handle_Verify();
118+
OTAState handle_Rename();
117119
OTAState handle_Reset();
118120

119121
void init_mqtt_ota_buffer();

0 commit comments

Comments
 (0)