@@ -41,28 +41,47 @@ OTAStorage_Nina::OTAStorage_Nina()
4141bool OTAStorage_Nina::init ()
4242{
4343 /* Nothing to do */
44+ return true ;
4445}
4546
4647bool OTAStorage_Nina::open (char const * file_name)
4748{
4849 /* It is necessary to prepend "/fs/" when opening a file on the nina
4950 * for the rename operation "/fs/"" does not need to be prepended.
5051 */
52+
5153 char nina_file_name[32 ] = {0 };
5254 strcpy (nina_file_name, " /fs/" );
5355 strcat (nina_file_name, file_name);
54-
55- WiFiStorageFile file = WiFiStorage.open (nina_file_name);
56- if (!file)
56+
57+ WiFiStorage.remove (nina_file_name);
58+ WiFiStorageFile f = WiFiStorage.open (nina_file_name);
59+
60+ if (!f)
5761 return false ;
58-
59- _file = new WiFiStorageFile (file);
62+
63+ _file = new WiFiStorageFile (f);
64+
6065 return true ;
6166}
6267
6368size_t OTAStorage_Nina::write (uint8_t const * const buf, size_t const num_bytes)
6469{
65- return _file->write (buf, num_bytes);
70+ /* We have to write in chunks because otherwise we exceed the size of
71+ * the SPI buffer within the nina module.
72+ */
73+ size_t bytes_written = 0 ;
74+ size_t const WRITE_CHUNK_SIZE = 32 ;
75+
76+ for (; bytes_written < (num_bytes - WRITE_CHUNK_SIZE); bytes_written += WRITE_CHUNK_SIZE)
77+ {
78+ if (_file->write (buf + bytes_written, WRITE_CHUNK_SIZE) != WRITE_CHUNK_SIZE)
79+ return bytes_written;
80+ }
81+
82+ bytes_written += _file->write (buf + bytes_written, num_bytes - bytes_written);
83+
84+ return bytes_written;
6685}
6786
6887void OTAStorage_Nina::close ()
@@ -73,12 +92,18 @@ void OTAStorage_Nina::close()
7392
7493void OTAStorage_Nina::remove (char const * file_name)
7594{
76- WiFiStorage.remove (file_name);
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);
77102}
78103
79104bool OTAStorage_Nina::rename (char const * old_file_name, char const * new_file_name)
80105{
81- return WiFiStorage.rename (old_file_name, new_file_name);
106+ return ( WiFiStorage.rename (old_file_name, new_file_name) == 0 );
82107}
83108
84109void OTAStorage_Nina::deinit ()
0 commit comments