Skip to content

Refactor OTA Storage API #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 16, 2020
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
6 changes: 3 additions & 3 deletions src/utility/ota/OTALogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ OTAState OTALogic::handle_Idle()

OTAState OTALogic::handle_StartDownload()
{
if(_ota_storage->open("UPDATE.BIN.TMP")) {
if(_ota_storage->open()) {
return OTAState::WaitForHeader;
} else {
_ota_error = OTAError::StorageOpenFailed;
Expand Down Expand Up @@ -232,15 +232,15 @@ OTAState OTALogic::handle_Verify()
if(_ota_bin_data.crc32 == _ota_bin_data.hdr_crc32) {
return OTAState::Rename;
} else {
_ota_storage->remove("UPDATE.BIN.TMP");
_ota_storage->remove();
_ota_error = OTAError::ChecksumMismatch;
return OTAState::Error;
}
}

OTAState OTALogic::handle_Rename()
{
if(_ota_storage->rename("UPDATE.BIN.TMP", "UPDATE.BIN")) {
if(_ota_storage->rename()) {
_ota_storage->deinit();
return OTAState::Reset;
}
Expand Down
6 changes: 3 additions & 3 deletions src/utility/ota/OTAStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ class OTAStorage


virtual bool init () = 0;
virtual bool open (char const * file_name) = 0;
virtual bool open () = 0;
virtual size_t write (uint8_t const * const buf, size_t const num_bytes) = 0;
virtual void close () = 0;
virtual void remove(char const * file_name) = 0;
virtual bool rename(char const * old_file_name, char const * new_file_name) = 0;
virtual void remove() = 0;
virtual bool rename() = 0;
virtual void deinit() = 0;

};
Expand Down
19 changes: 13 additions & 6 deletions src/utility/ota/OTAStorage_SFU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@

#include <Arduino_DebugUtils.h>

/******************************************************************************
* CONSTANTS
******************************************************************************/

static char const SFU_UPDATE_FILENAME[] = "UPDATE.BIN";
static char const SFU_TEMP_UPDATE_FILENAME[] = "UPDATE.BIN.TMP";

/******************************************************************************
* CTOR/DTOR
******************************************************************************/
Expand Down Expand Up @@ -56,10 +63,10 @@ bool OTAStorage_SFU::init()
return true;
}

bool OTAStorage_SFU::open(char const * file_name)
bool OTAStorage_SFU::open()
{
filesystem.clearerr();
_file = new File(filesystem.open(file_name, CREATE | WRITE_ONLY| TRUNCATE));
_file = new File(filesystem.open(SFU_TEMP_UPDATE_FILENAME, CREATE | WRITE_ONLY| TRUNCATE));
if(SPIFFS_OK != filesystem.err()) {
DBG_ERROR("OTAStorage_SFU::open - open() failed with error code %d", filesystem.err());
delete _file;
Expand All @@ -79,14 +86,14 @@ void OTAStorage_SFU::close()
delete _file;
}

void OTAStorage_SFU::remove(char const * file_name)
void OTAStorage_SFU::remove()
{
filesystem.remove(file_name);
filesystem.remove(SFU_TEMP_UPDATE_FILENAME);
}

bool OTAStorage_SFU::rename(char const * old_file_name, char const * new_file_name)
bool OTAStorage_SFU::rename()
{
return (SPIFFS_OK == filesystem.rename(old_file_name, new_file_name));
return (SPIFFS_OK == filesystem.rename(SFU_TEMP_UPDATE_FILENAME, SFU_UPDATE_FILENAME));
}

void OTAStorage_SFU::deinit()
Expand Down
6 changes: 3 additions & 3 deletions src/utility/ota/OTAStorage_SFU.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ class OTAStorage_SFU : public OTAStorage


virtual bool init () override;
virtual bool open (char const * file_name) override;
virtual bool open () override;
virtual size_t write (uint8_t const * const buf, size_t const num_bytes) override;
virtual void close () override;
virtual void remove(char const * file_name) override;
virtual bool rename(char const * old_file_name, char const * new_file_name) override;
virtual void remove() override;
virtual bool rename() override;
virtual void deinit() override;


Expand Down
6 changes: 3 additions & 3 deletions src/utility/ota/OTAStorage_SNU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ bool OTAStorage_SNU::init()
return true;
}

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

void OTAStorage_SNU::remove(char const * /* file_name */)
void OTAStorage_SNU::remove()
{
WiFiStorage.remove(SNU_TEMP_UPDATE_FILENAME);
}

bool OTAStorage_SNU::rename(char const * /* old_file_name */, char const * /* new_file_name */)
bool OTAStorage_SNU::rename()
{
return WiFiStorage.rename(SNU_TEMP_UPDATE_FILENAME, SNU_UPDATE_FILENAME);
}
Expand Down
6 changes: 3 additions & 3 deletions src/utility/ota/OTAStorage_SNU.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ class OTAStorage_SNU : public OTAStorage


virtual bool init () override;
virtual bool open (char const * file_name) override;
virtual bool open () override;
virtual size_t write (uint8_t const * const buf, size_t const num_bytes) override;
virtual void close () override;
virtual void remove(char const * file_name) override;
virtual bool rename(char const * old_file_name, char const * new_file_name) override;
virtual void remove() override;
virtual bool rename() override;
virtual void deinit() override;

};
Expand Down
6 changes: 3 additions & 3 deletions src/utility/ota/OTAStorage_SSU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ bool OTAStorage_SSU::init()
return false;
}

bool OTAStorage_SSU::open(char const * /* file_name */)
bool OTAStorage_SSU::open()
{
return true;
}
Expand All @@ -65,12 +65,12 @@ void OTAStorage_SSU::close()
/* Nothing to do */
}

void OTAStorage_SSU::remove(char const * /* file_name */)
void OTAStorage_SSU::remove()
{
_fileUtils.deleteFile(SSU_UPDATE_FILENAME);
}

bool OTAStorage_SSU::rename(char const * /* old_file_name */, char const * /* new_file_name */)
bool OTAStorage_SSU::rename()
{
/* Create a file 'UPDATE.OK' which is used by the SSU
* 2nd stage bootloader to recognise that the update
Expand Down
6 changes: 3 additions & 3 deletions src/utility/ota/OTAStorage_SSU.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ class OTAStorage_SSU : public OTAStorage


virtual bool init () override;
virtual bool open (char const * file_name) override;
virtual bool open () override;
virtual size_t write (uint8_t const * const buf, size_t const num_bytes) override;
virtual void close () override;
virtual void remove(char const * file_name) override;
virtual bool rename(char const * old_file_name, char const * new_file_name) override;
virtual void remove() override;
virtual bool rename() override;
virtual void deinit() override;

private:
Expand Down