24
24
25
25
#include " OTAStorage_SNU.h"
26
26
27
+ #include < WiFiStorage.h>
28
+
27
29
/* *****************************************************************************
28
- * CTOR/DTOR
30
+ * CONSTANTS
29
31
******************************************************************************/
30
32
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" ;
36
35
37
36
/* *****************************************************************************
38
37
* PUBLIC MEMBER FUNCTIONS
39
38
******************************************************************************/
40
39
41
40
bool OTAStorage_SNU::init ()
42
41
{
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);
44
47
return true ;
45
48
}
46
49
47
- bool OTAStorage_SNU::open (char const * file_name)
50
+ bool OTAStorage_SNU::open (char const * /* file_name */ )
48
51
{
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'.
51
56
*/
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
-
65
57
return true ;
66
58
}
67
59
68
60
size_t OTAStorage_SNU::write (uint8_t const * const buf, size_t const num_bytes)
69
61
{
62
+ WiFiStorageFile file (SNU_TEMP_UPDATE_FILENAME);
63
+
70
64
/* We have to write in chunks because otherwise we exceed the size of
71
65
* the SPI buffer within the nina module.
72
66
*/
@@ -75,35 +69,28 @@ size_t OTAStorage_SNU::write(uint8_t const * const buf, size_t const num_bytes)
75
69
76
70
for (; bytes_written < (num_bytes - WRITE_CHUNK_SIZE); bytes_written += WRITE_CHUNK_SIZE)
77
71
{
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)
79
73
return bytes_written;
80
74
}
81
75
82
- bytes_written += _file-> write (buf + bytes_written, num_bytes - bytes_written);
76
+ bytes_written += file. write (buf + bytes_written, num_bytes - bytes_written);
83
77
84
78
return bytes_written;
85
79
}
86
80
87
81
void OTAStorage_SNU::close ()
88
82
{
89
- /* There is no close API within WiFiNiNa */
90
- delete _file;
83
+ /* Files are closed after each file operation on the nina side. */
91
84
}
92
85
93
- void OTAStorage_SNU::remove (char const * file_name)
86
+ void OTAStorage_SNU::remove (char const * /* file_name */ )
94
87
{
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);
102
89
}
103
90
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 */ )
105
92
{
106
- return ( WiFiStorage.rename (old_file_name, new_file_name) == 0 );
93
+ return WiFiStorage.rename (SNU_TEMP_UPDATE_FILENAME, SNU_UPDATE_FILENAME );
107
94
}
108
95
109
96
void OTAStorage_SNU::deinit ()
0 commit comments