24
24
25
25
#include " OTAStorage_Nina.h"
26
26
27
- /* *****************************************************************************
28
- * CTOR/DTOR
29
- ******************************************************************************/
30
-
31
- OTAStorage_Nina::OTAStorage_Nina ()
32
- : _file{nullptr }
33
- {
34
-
35
- }
27
+ #include < WiFiStorage.h>
36
28
37
29
/* *****************************************************************************
38
30
* PUBLIC MEMBER FUNCTIONS
@@ -48,37 +40,50 @@ bool OTAStorage_Nina::open(char const * file_name)
48
40
/* It is necessary to prepend "/fs/" when opening a file on the nina
49
41
* for the rename operation "/fs/"" does not need to be prepended.
50
42
*/
51
- char nina_file_name[32 ] = {0 };
52
- strcpy (nina_file_name, " /fs/" );
53
- strcat (nina_file_name, file_name);
54
-
55
- WiFiStorageFile file = WiFiStorage.open (nina_file_name);
56
- if (!file)
57
- return false ;
58
-
59
- _file = new WiFiStorageFile (file);
43
+ _nina_update_file_name = String (" /fs/" ) + String (file_name);
60
44
return true ;
61
45
}
62
46
63
47
size_t OTAStorage_Nina::write (uint8_t const * const buf, size_t const num_bytes)
64
48
{
65
- return _file->write (buf, num_bytes);
49
+ WiFiStorageFile file = WiFiStorage.open (_nina_update_file_name);
50
+
51
+ /* We have to write in chunks because otherwise we exceed the size of
52
+ * the SPI buffer within the nina module.
53
+ */
54
+ size_t bytes_written = 0 ;
55
+ size_t const WRITE_CHUNK_SIZE = 32 ;
56
+
57
+ for (; bytes_written < (num_bytes - WRITE_CHUNK_SIZE); bytes_written += WRITE_CHUNK_SIZE)
58
+ {
59
+ if (file.write (buf + bytes_written, WRITE_CHUNK_SIZE) != WRITE_CHUNK_SIZE)
60
+ return bytes_written;
61
+ }
62
+
63
+ bytes_written += file.write (buf + bytes_written, num_bytes - bytes_written);
64
+
65
+ return bytes_written;
66
66
}
67
67
68
68
void OTAStorage_Nina::close ()
69
69
{
70
70
/* There is no close API within WiFiNiNa */
71
- delete _file;
72
71
}
73
72
74
73
void OTAStorage_Nina::remove (char const * file_name)
75
74
{
76
- WiFiStorage.remove (file_name);
75
+ /* Prepend "/fs/" */
76
+ char nina_file_name[32 ] = {0 };
77
+ strcpy (nina_file_name, " /fs/" );
78
+ strcat (nina_file_name, file_name);
79
+
80
+ /* Remove file */
81
+ WiFiStorage.remove (nina_file_name);
77
82
}
78
83
79
84
bool OTAStorage_Nina::rename (char const * old_file_name, char const * new_file_name)
80
85
{
81
- return WiFiStorage.rename (old_file_name, new_file_name);
86
+ return ( WiFiStorage.rename (old_file_name, new_file_name) == 0 );
82
87
}
83
88
84
89
void OTAStorage_Nina::deinit ()
0 commit comments