Skip to content

Commit faa97e0

Browse files
authored
Merge pull request #74 from arduino-libraries/unowifirev2_ota
Add helpers for Filesystem operations
2 parents b50b320 + 1de666a commit faa97e0

File tree

9 files changed

+370
-0
lines changed

9 files changed

+370
-0
lines changed

examples/WiFiStorage/WiFiStorage.ino

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
This example shows how to interact with NiNa internal memory partition
3+
APIs are modeled on SerialFlash library (not on SD) to speedup operations and avoid buffers.
4+
*/
5+
6+
#include <WiFiNINA.h>
7+
8+
void setup() {
9+
10+
Serial.begin(115200);
11+
while (!Serial);
12+
13+
// check for the presence of the shield:
14+
if (WiFi.status() == WL_NO_SHIELD) {
15+
Serial.println("WiFi shield not present");
16+
// don't continue:
17+
while (true);
18+
}
19+
20+
WiFiStorageFile file = WiFiStorage.open("/fs/testfile");
21+
22+
if (file) {
23+
file.erase();
24+
}
25+
26+
String test = "Cantami o Diva del pelide Achille";
27+
file.write(test.c_str(), test.length());
28+
29+
if (file) {
30+
file.seek(0);
31+
while (file.available()) {
32+
uint8_t buf[128];
33+
int ret = file.read(buf, 128);
34+
Serial.write(buf, ret);
35+
}
36+
}
37+
}
38+
39+
void loop() {
40+
// put your main code here, to run repeatedly:
41+
42+
}

src/WiFi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ extern "C" {
3434
#include "WiFiClient.h"
3535
#include "WiFiSSLClient.h"
3636
#include "WiFiServer.h"
37+
#include "WiFiStorage.h"
3738

3839
class WiFiClass
3940
{

src/WiFiStorage.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
WiFiStorage.cpp - Library for Arduino boards based on NINA wifi module.
3+
Copyright (c) 2018 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include "WiFiStorage.h"
21+
22+
WiFiStorageFile WiFiStorageClass::open(const char *filename) {
23+
WiFiStorageFile file(filename);
24+
file.size();
25+
return file;
26+
}
27+
28+
WiFiStorageFile WiFiStorageClass::open(String filename) {
29+
return open(filename.c_str());
30+
}

src/WiFiStorage.h

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
WiFiStorage.h - Library for Arduino boards based on NINA wifi module.
3+
Copyright (c) 2018 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef wifistorage_h
21+
#define wifistorage_h
22+
23+
#include "utility/wifi_drv.h"
24+
25+
class WiFiStorageFile;
26+
27+
class WiFiStorageClass
28+
{
29+
public:
30+
static bool begin();
31+
32+
static WiFiStorageFile open(const char *filename);
33+
static WiFiStorageFile open(String filename);
34+
35+
static bool exists(const char *filename) {
36+
uint32_t len;
37+
return (WiFiDrv::existsFile(filename, strlen(filename), &len) > 0);
38+
}
39+
static bool exists(const char *filename, uint32_t* len) {
40+
return (WiFiDrv::existsFile(filename, strlen(filename), len) > 0);
41+
}
42+
static bool remove(const char *filename) {
43+
WiFiDrv::deleteFile(filename, strlen(filename));
44+
return true;
45+
}
46+
static bool rename(const char * old_file_name, const char * new_file_name) {
47+
return (WiFiDrv::renameFile(old_file_name, strlen(old_file_name), new_file_name, strlen(new_file_name)) == 0);
48+
}
49+
static bool read(const char *filename, uint32_t offset, uint8_t* buffer, uint32_t buffer_len) {
50+
WiFiDrv::readFile(filename, strlen(filename), offset, buffer, buffer_len);
51+
return true;
52+
}
53+
static bool write(const char *filename, uint32_t offset, uint8_t* buffer, uint32_t buffer_len) {
54+
WiFiDrv::writeFile(filename, strlen(filename), offset, buffer, buffer_len);
55+
return true;
56+
}
57+
static bool download(const char* url, const char *filename) {
58+
WiFiDrv::downloadFile(url, strlen(url), filename, strlen(filename));
59+
return true;
60+
}
61+
62+
static bool remove(String filename) {
63+
return remove(filename.c_str());
64+
}
65+
static bool rename(String old_file_name, String new_file_name) {
66+
return rename(old_file_name.c_str(), new_file_name.c_str());
67+
}
68+
static bool read(String filename, uint32_t offset, uint8_t* buffer, uint32_t buffer_len) {
69+
return read(filename.c_str(), offset, buffer, buffer_len);
70+
}
71+
static bool write(String filename, uint32_t offset, uint8_t* buffer, uint32_t buffer_len) {
72+
return write(filename.c_str(), offset, buffer, buffer_len);
73+
}
74+
static bool download(String url, String filename) {
75+
return download(url.c_str(), filename.c_str());
76+
}
77+
};
78+
79+
extern WiFiStorageClass WiFiStorage;
80+
81+
82+
class WiFiStorageFile
83+
{
84+
public:
85+
constexpr WiFiStorageFile(const char* _filename) : filename(_filename) { }
86+
87+
operator bool() {
88+
return WiFiStorage.exists(filename, &length);
89+
}
90+
uint32_t read(void *buf, uint32_t rdlen) {
91+
if (offset + rdlen > length) {
92+
if (offset >= length) return 0;
93+
rdlen = length - offset;
94+
}
95+
WiFiStorage.read(filename, offset, (uint8_t*)buf, rdlen);
96+
offset += rdlen;
97+
return rdlen;
98+
}
99+
uint32_t write(const void *buf, uint32_t wrlen) {
100+
WiFiStorage.write(filename, offset, (uint8_t*)buf, wrlen);
101+
offset += wrlen;
102+
return wrlen;
103+
}
104+
void seek(uint32_t n) {
105+
offset = n;
106+
}
107+
uint32_t position() {
108+
return offset;
109+
}
110+
uint32_t size() {
111+
WiFiStorage.exists(filename, &length);
112+
return length;
113+
}
114+
uint32_t available() {
115+
WiFiStorage.exists(filename, &length);
116+
return length - offset;
117+
}
118+
void erase() {
119+
offset = 0;
120+
WiFiStorage.remove(filename);
121+
}
122+
void flush();
123+
void close() {
124+
offset = 0;
125+
}
126+
protected:
127+
friend class WiFiStorageClass;
128+
uint32_t offset = 0;
129+
uint32_t length = 0;
130+
const char* filename;
131+
};
132+
133+
#endif

src/utility/spi_drv.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,22 @@ int SpiDrv::waitResponse(uint8_t cmd, uint8_t* numParamRead, uint8_t** params, u
449449
return 1;
450450
}
451451

452+
void SpiDrv::sendParamNoLen(uint8_t* param, size_t param_len, uint8_t lastParam)
453+
{
454+
int i = 0;
455+
// Send Spi paramLen
456+
sendParamLen8(0);
457+
458+
// Send Spi param data
459+
for (i=0; i<param_len; ++i)
460+
{
461+
spiTransfer(param[i]);
462+
}
463+
464+
// if lastParam==1 Send Spi END CMD
465+
if (lastParam == 1)
466+
spiTransfer(END_CMD);
467+
}
452468

453469
void SpiDrv::sendParam(uint8_t* param, uint8_t param_len, uint8_t lastParam)
454470
{

src/utility/spi_drv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ class SpiDrv
8585

8686
static void sendParam(uint8_t* param, uint8_t param_len, uint8_t lastParam = NO_LAST_PARAM);
8787

88+
static void sendParamNoLen(uint8_t* param, size_t param_len, uint8_t lastParam = NO_LAST_PARAM);
89+
8890
static void sendParamLen8(uint8_t param_len);
8991

9092
static void sendParamLen16(uint16_t param_len);

src/utility/wifi_drv.cpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,4 +1114,118 @@ void WiFiDrv::analogWrite(uint8_t pin, uint8_t value)
11141114
SpiDrv::spiSlaveDeselect();
11151115
}
11161116

1117+
int8_t WiFiDrv::downloadFile(const char* url, uint8_t url_len, const char *filename, uint8_t filename_len)
1118+
{
1119+
WAIT_FOR_SLAVE_SELECT();
1120+
// Send Command
1121+
SpiDrv::sendCmd(DOWNLOAD_FILE, PARAM_NUMS_2);
1122+
SpiDrv::sendParam((uint8_t*)url, url_len, NO_LAST_PARAM);
1123+
SpiDrv::sendParam((uint8_t*)filename, filename_len, LAST_PARAM);
1124+
1125+
// pad to multiple of 4
1126+
int commandSize = 6 + url_len + filename_len;
1127+
while (commandSize % 4) {
1128+
SpiDrv::readChar();
1129+
commandSize++;
1130+
}
1131+
1132+
SpiDrv::spiSlaveDeselect();
1133+
//Wait the reply elaboration
1134+
SpiDrv::waitForSlaveReady();
1135+
SpiDrv::spiSlaveSelect();
1136+
1137+
// Wait for reply
1138+
uint8_t _data = 0;
1139+
uint8_t _dataLen = 0;
1140+
if (!SpiDrv::waitResponseCmd(DOWNLOAD_FILE, PARAM_NUMS_1, &_data, &_dataLen))
1141+
{
1142+
WARN("error waitResponse");
1143+
_data = WL_FAILURE;
1144+
}
1145+
SpiDrv::spiSlaveDeselect();
1146+
return _data;
1147+
}
1148+
1149+
int8_t WiFiDrv::renameFile(const char * old_file_name, uint8_t const old_file_name_len, const char * new_file_name, uint8_t const new_file_name_len)
1150+
{
1151+
WAIT_FOR_SLAVE_SELECT();
1152+
/* Send Command */
1153+
SpiDrv::sendCmd(RENAME_FILE, PARAM_NUMS_2);
1154+
SpiDrv::sendParam((uint8_t*)old_file_name, old_file_name_len, NO_LAST_PARAM);
1155+
SpiDrv::sendParam((uint8_t*)new_file_name, new_file_name_len, LAST_PARAM);
1156+
1157+
/* pad to multiple of 4 */
1158+
int commandSize = 6 + old_file_name_len + new_file_name_len;
1159+
while (commandSize % 4) {
1160+
SpiDrv::readChar();
1161+
commandSize++;
1162+
}
1163+
1164+
SpiDrv::spiSlaveDeselect();
1165+
/* Wait the reply elaboration */
1166+
SpiDrv::waitForSlaveReady();
1167+
SpiDrv::spiSlaveSelect();
1168+
1169+
/* Wait for reply */
1170+
uint8_t data = 0;
1171+
uint8_t dataLen = 0;
1172+
if (!SpiDrv::waitResponseCmd(RENAME_FILE, PARAM_NUMS_1, &data, &dataLen))
1173+
{
1174+
WARN("error waitResponse");
1175+
data = WL_FAILURE;
1176+
}
1177+
SpiDrv::spiSlaveDeselect();
1178+
return data;
1179+
}
1180+
1181+
int8_t WiFiDrv::fileOperation(uint8_t operation, const char *filename, uint8_t filename_len, uint32_t offset, uint8_t* buffer, uint32_t len)
1182+
{
1183+
WAIT_FOR_SLAVE_SELECT();
1184+
// Send Command
1185+
uint8_t numParams = PARAM_NUMS_3;
1186+
if (operation == WRITE_FILE) {
1187+
numParams = PARAM_NUMS_4;
1188+
}
1189+
1190+
SpiDrv::sendCmd(operation, numParams);
1191+
SpiDrv::sendParam((uint8_t*)&offset, sizeof(offset), NO_LAST_PARAM);
1192+
SpiDrv::sendParam((uint8_t*)&len, sizeof(len), NO_LAST_PARAM);
1193+
SpiDrv::sendParam((uint8_t*)filename, filename_len, (operation == WRITE_FILE) ? NO_LAST_PARAM : LAST_PARAM);
1194+
if (operation == WRITE_FILE) {
1195+
SpiDrv::sendParamNoLen((uint8_t*)buffer, len, LAST_PARAM);
1196+
}
1197+
1198+
// pad to multiple of 4
1199+
int commandSize = 4 + numParams + sizeof(offset) + sizeof(len) + filename_len;
1200+
while (commandSize % 4) {
1201+
SpiDrv::readChar();
1202+
commandSize++;
1203+
}
1204+
1205+
SpiDrv::spiSlaveDeselect();
1206+
//Wait the reply elaboration
1207+
SpiDrv::waitForSlaveReady();
1208+
SpiDrv::spiSlaveSelect();
1209+
1210+
// Wait for reply
1211+
uint8_t _data = 0;
1212+
uint8_t _dataLen = 0;
1213+
SpiDrv::waitResponseCmd(operation, PARAM_NUMS_1, (operation == WRITE_FILE) ? &_data : buffer, &_dataLen);
1214+
1215+
SpiDrv::spiSlaveDeselect();
1216+
return _dataLen;
1217+
}
1218+
1219+
void WiFiDrv::applyOTA() {
1220+
WAIT_FOR_SLAVE_SELECT();
1221+
1222+
// Send Command
1223+
SpiDrv::sendCmd(APPLY_OTA_COMMAND, PARAM_NUMS_0);
1224+
1225+
SpiDrv::spiSlaveDeselect();
1226+
1227+
// don't wait for return; OTA operation should be fire and forget :)
1228+
}
1229+
1230+
11171231
WiFiDrv wiFiDrv;

src/utility/wifi_drv.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,29 @@ class WiFiDrv
293293
static void digitalWrite(uint8_t pin, uint8_t value);
294294
static void analogWrite(uint8_t pin, uint8_t value);
295295

296+
static int8_t downloadFile(const char* url, uint8_t url_len, const char *filename, uint8_t filename_len);
297+
static int8_t renameFile(const char * old_file_name, uint8_t const old_file_name_len, const char * new_file_name, uint8_t const new_file_name_len);
298+
299+
static int8_t fileOperation(uint8_t operation, const char *filename, uint8_t filename_len, uint32_t offset, uint8_t* buffer, uint32_t len);
300+
301+
static int8_t readFile(const char *filename, uint8_t filename_len, uint32_t offset, uint8_t* buffer, uint32_t buffer_len) {
302+
return fileOperation(READ_FILE, filename, filename_len, offset, buffer, buffer_len);
303+
};
304+
static int8_t writeFile(const char *filename, uint8_t filename_len, uint32_t offset, uint8_t* buffer, uint32_t buffer_len) {
305+
return fileOperation(WRITE_FILE, filename, filename_len, offset, buffer, buffer_len);
306+
};
307+
static int8_t deleteFile(const char *filename, uint8_t filename_len) {
308+
return fileOperation(DELETE_FILE, filename, filename_len, 0, NULL, 0);
309+
};
310+
static int8_t existsFile(const char *filename, uint8_t filename_len, uint32_t* len) {
311+
int32_t length = 0;
312+
fileOperation(EXISTS_FILE, filename, filename_len, 0, (uint8_t*)&length, sizeof(length));
313+
*len = length;
314+
return length >= 0;
315+
};
316+
317+
static void applyOTA();
318+
296319
friend class WiFiUDP;
297320
friend class WiFiClient;
298321
};

0 commit comments

Comments
 (0)