Skip to content

Commit 8ed7e93

Browse files
committed
Restructuring NINA OTA storage code in order to be the most time-efficient
1 parent aaba4d3 commit 8ed7e93

File tree

2 files changed

+24
-45
lines changed

2 files changed

+24
-45
lines changed

Diff for: src/utility/ota/OTAStorage_SNU.cpp

+24-37
Original file line numberDiff line numberDiff line change
@@ -24,49 +24,43 @@
2424

2525
#include "OTAStorage_SNU.h"
2626

27+
#include <WiFiStorage.h>
28+
2729
/******************************************************************************
28-
* CTOR/DTOR
30+
* CONSTANTS
2931
******************************************************************************/
3032

31-
OTAStorage_SNU::OTAStorage_SNU()
32-
: _file{nullptr}
33-
{
34-
35-
}
33+
static char const SNU_UPDATE_FILENAME[] = "/fs/UPDATE.BIN";
34+
static char const SNU_TEMP_UPDATE_FILENAME[] = "/fs/UPDATE.BIN.TMP";
3635

3736
/******************************************************************************
3837
* PUBLIC MEMBER FUNCTIONS
3938
******************************************************************************/
4039

4140
bool OTAStorage_SNU::init()
4241
{
43-
/* Nothing to do */
42+
/* Ensure that there are no remains of previous
43+
* aborted downloads still existing in the memory
44+
* of the nina module.
45+
*/
46+
WiFiStorage.remove(SNU_TEMP_UPDATE_FILENAME);
4447
return true;
4548
}
4649

47-
bool OTAStorage_SNU::open(char const * file_name)
50+
bool OTAStorage_SNU::open(char const * /* file_name */)
4851
{
49-
/* It is necessary to prepend "/fs/" when opening a file on the nina
50-
* for the rename operation "/fs/"" does not need to be prepended.
52+
/* There's no need to explicitly open the file
53+
* because when writing to it the file will always
54+
* be opened with "ab+" mode and closed after each
55+
* call to 'write'.
5156
*/
52-
53-
char nina_file_name[32] = {0};
54-
strcpy(nina_file_name, "/fs/");
55-
strcat(nina_file_name, file_name);
56-
57-
WiFiStorage.remove(nina_file_name);
58-
WiFiStorageFile f = WiFiStorage.open(nina_file_name);
59-
60-
if (!f)
61-
return false;
62-
63-
_file = new WiFiStorageFile(f);
64-
6557
return true;
6658
}
6759

6860
size_t OTAStorage_SNU::write(uint8_t const * const buf, size_t const num_bytes)
6961
{
62+
WiFiStorageFile file(SNU_TEMP_UPDATE_FILENAME);
63+
7064
/* We have to write in chunks because otherwise we exceed the size of
7165
* the SPI buffer within the nina module.
7266
*/
@@ -75,35 +69,28 @@ size_t OTAStorage_SNU::write(uint8_t const * const buf, size_t const num_bytes)
7569

7670
for(; bytes_written < (num_bytes - WRITE_CHUNK_SIZE); bytes_written += WRITE_CHUNK_SIZE)
7771
{
78-
if (_file->write(buf + bytes_written, WRITE_CHUNK_SIZE) != WRITE_CHUNK_SIZE)
72+
if (file.write(buf + bytes_written, WRITE_CHUNK_SIZE) != WRITE_CHUNK_SIZE)
7973
return bytes_written;
8074
}
8175

82-
bytes_written += _file->write(buf + bytes_written, num_bytes - bytes_written);
76+
bytes_written += file.write(buf + bytes_written, num_bytes - bytes_written);
8377

8478
return bytes_written;
8579
}
8680

8781
void OTAStorage_SNU::close()
8882
{
89-
/* There is no close API within WiFiNiNa */
90-
delete _file;
83+
/* Files are closed after each file operation on the nina side. */
9184
}
9285

93-
void OTAStorage_SNU::remove(char const * file_name)
86+
void OTAStorage_SNU::remove(char const * /* file_name */)
9487
{
95-
/* Prepend "/fs/" */
96-
char nina_file_name[32] = {0};
97-
strcpy(nina_file_name, "/fs/");
98-
strcat(nina_file_name, file_name);
99-
100-
/* Remove file */
101-
WiFiStorage.remove(nina_file_name);
88+
WiFiStorage.remove(SNU_TEMP_UPDATE_FILENAME);
10289
}
10390

104-
bool OTAStorage_SNU::rename(char const * old_file_name, char const * new_file_name)
91+
bool OTAStorage_SNU::rename(char const * /* old_file_name */, char const * /* new_file_name */)
10592
{
106-
return (WiFiStorage.rename(old_file_name, new_file_name) == 0);
93+
return WiFiStorage.rename(SNU_TEMP_UPDATE_FILENAME, SNU_UPDATE_FILENAME);
10794
}
10895

10996
void OTAStorage_SNU::deinit()

Diff for: src/utility/ota/OTAStorage_SNU.h

-8
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929

3030
#include "OTAStorage.h"
3131

32-
#include <WiFiStorage.h>
33-
3432
/******************************************************************************
3533
* CLASS DECLARATION
3634
******************************************************************************/
@@ -39,7 +37,6 @@ class OTAStorage_SNU : public OTAStorage
3937
{
4038
public:
4139

42-
OTAStorage_SNU();
4340
virtual ~OTAStorage_SNU() { }
4441

4542

@@ -51,11 +48,6 @@ class OTAStorage_SNU : public OTAStorage
5148
virtual bool rename(char const * old_file_name, char const * new_file_name) override;
5249
virtual void deinit() override;
5350

54-
55-
private:
56-
57-
WiFiStorageFile * _file;
58-
5951
};
6052

6153
#endif /* OTA_STORAGE_SNU */

0 commit comments

Comments
 (0)