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
4140bool 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
6860size_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
8781void 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
10996void OTAStorage_SNU::deinit ()
0 commit comments