Skip to content

Commit 593a5f5

Browse files
authored
Merge pull request #171 from arduino-libraries/refactor-ota-storage-api
Refactor OTA Storage API
2 parents 858cf33 + f350152 commit 593a5f5

8 files changed

+34
-27
lines changed

src/utility/ota/OTALogic.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ OTAState OTALogic::handle_Idle()
141141

142142
OTAState OTALogic::handle_StartDownload()
143143
{
144-
if(_ota_storage->open("UPDATE.BIN.TMP")) {
144+
if(_ota_storage->open()) {
145145
return OTAState::WaitForHeader;
146146
} else {
147147
_ota_error = OTAError::StorageOpenFailed;
@@ -232,15 +232,15 @@ OTAState OTALogic::handle_Verify()
232232
if(_ota_bin_data.crc32 == _ota_bin_data.hdr_crc32) {
233233
return OTAState::Rename;
234234
} else {
235-
_ota_storage->remove("UPDATE.BIN.TMP");
235+
_ota_storage->remove();
236236
_ota_error = OTAError::ChecksumMismatch;
237237
return OTAState::Error;
238238
}
239239
}
240240

241241
OTAState OTALogic::handle_Rename()
242242
{
243-
if(_ota_storage->rename("UPDATE.BIN.TMP", "UPDATE.BIN")) {
243+
if(_ota_storage->rename()) {
244244
_ota_storage->deinit();
245245
return OTAState::Reset;
246246
}

src/utility/ota/OTAStorage.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ class OTAStorage
3838

3939

4040
virtual bool init () = 0;
41-
virtual bool open (char const * file_name) = 0;
41+
virtual bool open () = 0;
4242
virtual size_t write (uint8_t const * const buf, size_t const num_bytes) = 0;
4343
virtual void close () = 0;
44-
virtual void remove(char const * file_name) = 0;
45-
virtual bool rename(char const * old_file_name, char const * new_file_name) = 0;
44+
virtual void remove() = 0;
45+
virtual bool rename() = 0;
4646
virtual void deinit() = 0;
4747

4848
};

src/utility/ota/OTAStorage_SFU.cpp

+13-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626

2727
#include <Arduino_DebugUtils.h>
2828

29+
/******************************************************************************
30+
* CONSTANTS
31+
******************************************************************************/
32+
33+
static char const SFU_UPDATE_FILENAME[] = "UPDATE.BIN";
34+
static char const SFU_TEMP_UPDATE_FILENAME[] = "UPDATE.BIN.TMP";
35+
2936
/******************************************************************************
3037
* CTOR/DTOR
3138
******************************************************************************/
@@ -56,10 +63,10 @@ bool OTAStorage_SFU::init()
5663
return true;
5764
}
5865

59-
bool OTAStorage_SFU::open(char const * file_name)
66+
bool OTAStorage_SFU::open()
6067
{
6168
filesystem.clearerr();
62-
_file = new File(filesystem.open(file_name, CREATE | WRITE_ONLY| TRUNCATE));
69+
_file = new File(filesystem.open(SFU_TEMP_UPDATE_FILENAME, CREATE | WRITE_ONLY| TRUNCATE));
6370
if(SPIFFS_OK != filesystem.err()) {
6471
DBG_ERROR("OTAStorage_SFU::open - open() failed with error code %d", filesystem.err());
6572
delete _file;
@@ -79,14 +86,14 @@ void OTAStorage_SFU::close()
7986
delete _file;
8087
}
8188

82-
void OTAStorage_SFU::remove(char const * file_name)
89+
void OTAStorage_SFU::remove()
8390
{
84-
filesystem.remove(file_name);
91+
filesystem.remove(SFU_TEMP_UPDATE_FILENAME);
8592
}
8693

87-
bool OTAStorage_SFU::rename(char const * old_file_name, char const * new_file_name)
94+
bool OTAStorage_SFU::rename()
8895
{
89-
return (SPIFFS_OK == filesystem.rename(old_file_name, new_file_name));
96+
return (SPIFFS_OK == filesystem.rename(SFU_TEMP_UPDATE_FILENAME, SFU_UPDATE_FILENAME));
9097
}
9198

9299
void OTAStorage_SFU::deinit()

src/utility/ota/OTAStorage_SFU.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ class OTAStorage_SFU : public OTAStorage
4444

4545

4646
virtual bool init () override;
47-
virtual bool open (char const * file_name) override;
47+
virtual bool open () override;
4848
virtual size_t write (uint8_t const * const buf, size_t const num_bytes) override;
4949
virtual void close () override;
50-
virtual void remove(char const * file_name) override;
51-
virtual bool rename(char const * old_file_name, char const * new_file_name) override;
50+
virtual void remove() override;
51+
virtual bool rename() override;
5252
virtual void deinit() override;
5353

5454

src/utility/ota/OTAStorage_SNU.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ bool OTAStorage_SNU::init()
4747
return true;
4848
}
4949

50-
bool OTAStorage_SNU::open(char const * /* file_name */)
50+
bool OTAStorage_SNU::open()
5151
{
5252
/* There's no need to explicitly open the file
5353
* because when writing to it the file will always
@@ -83,12 +83,12 @@ void OTAStorage_SNU::close()
8383
/* Files are closed after each file operation on the nina side. */
8484
}
8585

86-
void OTAStorage_SNU::remove(char const * /* file_name */)
86+
void OTAStorage_SNU::remove()
8787
{
8888
WiFiStorage.remove(SNU_TEMP_UPDATE_FILENAME);
8989
}
9090

91-
bool OTAStorage_SNU::rename(char const * /* old_file_name */, char const * /* new_file_name */)
91+
bool OTAStorage_SNU::rename()
9292
{
9393
return WiFiStorage.rename(SNU_TEMP_UPDATE_FILENAME, SNU_UPDATE_FILENAME);
9494
}

src/utility/ota/OTAStorage_SNU.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ class OTAStorage_SNU : public OTAStorage
4141

4242

4343
virtual bool init () override;
44-
virtual bool open (char const * file_name) override;
44+
virtual bool open () override;
4545
virtual size_t write (uint8_t const * const buf, size_t const num_bytes) override;
4646
virtual void close () override;
47-
virtual void remove(char const * file_name) override;
48-
virtual bool rename(char const * old_file_name, char const * new_file_name) override;
47+
virtual void remove() override;
48+
virtual bool rename() override;
4949
virtual void deinit() override;
5050

5151
};

src/utility/ota/OTAStorage_SSU.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ bool OTAStorage_SSU::init()
4949
return false;
5050
}
5151

52-
bool OTAStorage_SSU::open(char const * /* file_name */)
52+
bool OTAStorage_SSU::open()
5353
{
5454
return true;
5555
}
@@ -65,12 +65,12 @@ void OTAStorage_SSU::close()
6565
/* Nothing to do */
6666
}
6767

68-
void OTAStorage_SSU::remove(char const * /* file_name */)
68+
void OTAStorage_SSU::remove()
6969
{
7070
_fileUtils.deleteFile(SSU_UPDATE_FILENAME);
7171
}
7272

73-
bool OTAStorage_SSU::rename(char const * /* old_file_name */, char const * /* new_file_name */)
73+
bool OTAStorage_SSU::rename()
7474
{
7575
/* Create a file 'UPDATE.OK' which is used by the SSU
7676
* 2nd stage bootloader to recognise that the update

src/utility/ota/OTAStorage_SSU.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ class OTAStorage_SSU : public OTAStorage
4343

4444

4545
virtual bool init () override;
46-
virtual bool open (char const * file_name) override;
46+
virtual bool open () override;
4747
virtual size_t write (uint8_t const * const buf, size_t const num_bytes) override;
4848
virtual void close () override;
49-
virtual void remove(char const * file_name) override;
50-
virtual bool rename(char const * old_file_name, char const * new_file_name) override;
49+
virtual void remove() override;
50+
virtual bool rename() override;
5151
virtual void deinit() override;
5252

5353
private:

0 commit comments

Comments
 (0)