Skip to content

Commit 3e8864c

Browse files
committed
Doing various adjustments in order to deal with the nina storage pecularities
1 parent 1cbae74 commit 3e8864c

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

src/utility/ota/OTAStorage_Nina.cpp

+27-22
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,7 @@
2424

2525
#include "OTAStorage_Nina.h"
2626

27-
/******************************************************************************
28-
* CTOR/DTOR
29-
******************************************************************************/
30-
31-
OTAStorage_Nina::OTAStorage_Nina()
32-
: _file{nullptr}
33-
{
34-
35-
}
27+
#include <WiFiStorage.h>
3628

3729
/******************************************************************************
3830
* PUBLIC MEMBER FUNCTIONS
@@ -48,37 +40,50 @@ bool OTAStorage_Nina::open(char const * file_name)
4840
/* It is necessary to prepend "/fs/" when opening a file on the nina
4941
* for the rename operation "/fs/"" does not need to be prepended.
5042
*/
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);
6044
return true;
6145
}
6246

6347
size_t OTAStorage_Nina::write(uint8_t const * const buf, size_t const num_bytes)
6448
{
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;
6666
}
6767

6868
void OTAStorage_Nina::close()
6969
{
7070
/* There is no close API within WiFiNiNa */
71-
delete _file;
7271
}
7372

7473
void OTAStorage_Nina::remove(char const * file_name)
7574
{
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);
7782
}
7883

7984
bool OTAStorage_Nina::rename(char const * old_file_name, char const * new_file_name)
8085
{
81-
return WiFiStorage.rename(old_file_name, new_file_name);
86+
return (WiFiStorage.rename(old_file_name, new_file_name) == 0);
8287
}
8388

8489
void OTAStorage_Nina::deinit()

src/utility/ota/OTAStorage_Nina.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
#include "OTAStorage.h"
2929

30-
#include <WiFiNINA.h>
30+
#include <Arduino.h>
3131

3232
/******************************************************************************
3333
* CLASS DECLARATION
@@ -37,7 +37,6 @@ class OTAStorage_Nina : public OTAStorage
3737
{
3838
public:
3939

40-
OTAStorage_Nina();
4140
virtual ~OTAStorage_Nina() { }
4241

4342

@@ -53,7 +52,7 @@ class OTAStorage_Nina : public OTAStorage
5352

5453
private:
5554

56-
WiFiStorageFile * _file;
55+
String _nina_update_file_name;
5756

5857
};
5958

0 commit comments

Comments
 (0)