Skip to content

Commit f025588

Browse files
committed
Implement FS operations (read/write/delete)
1 parent 7eec2e1 commit f025588

File tree

6 files changed

+196
-0
lines changed

6 files changed

+196
-0
lines changed

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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "WiFiStorage.h"
2+
3+
WiFiStorageFile WiFiStorageClass::open(const char *filename) {
4+
WiFiStorageFile file(filename);
5+
file.size();
6+
return file;
7+
}

src/WiFiStorage.h

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include "utility/wifi_drv.h"
2+
3+
class WiFiStorageFile;
4+
5+
class WiFiStorageClass
6+
{
7+
public:
8+
static bool begin();
9+
10+
static WiFiStorageFile open(const char *filename);
11+
static bool exists(const char *filename, size_t* len) {
12+
return (WiFiDrv::existsFile(filename, strlen(filename), len) > 0);
13+
}
14+
static bool remove(const char *filename) {
15+
WiFiDrv::deleteFile(filename, strlen(filename));
16+
return true;
17+
}
18+
static bool read(const char *filename, size_t offset, uint8_t* buffer, size_t buffer_len) {
19+
WiFiDrv::readFile(filename, strlen(filename), offset, buffer, buffer_len);
20+
return true;
21+
}
22+
static bool write(const char *filename, size_t offset, uint8_t* buffer, size_t buffer_len) {
23+
WiFiDrv::writeFile(filename, strlen(filename), offset, buffer, buffer_len);
24+
return true;
25+
}
26+
static bool download(const char* url, const char *filename) {
27+
WiFiDrv::downloadFile(url, strlen(url), filename, strlen(filename));
28+
return true;
29+
}
30+
31+
static bool remove(String filename) {
32+
return remove(filename.c_str());
33+
}
34+
static bool read(String filename, size_t offset, uint8_t* buffer, size_t buffer_len) {
35+
return read(filename.c_str(), offset, buffer, buffer_len);
36+
}
37+
static bool write(String filename, size_t offset, uint8_t* buffer, size_t buffer_len) {
38+
return write(filename.c_str(), offset, buffer, buffer_len);
39+
}
40+
static bool download(String url, String filename) {
41+
return download(url.c_str(), filename.c_str());
42+
}
43+
};
44+
45+
extern WiFiStorageClass WiFiStorage;
46+
47+
48+
class WiFiStorageFile
49+
{
50+
public:
51+
constexpr WiFiStorageFile(const char* _filename) : filename(_filename) { }
52+
operator bool() {
53+
return WiFiStorage.exists(filename, &length);
54+
}
55+
uint32_t read(void *buf, uint32_t rdlen) {
56+
if (offset + rdlen > length) {
57+
if (offset >= length) return 0;
58+
rdlen = length - offset;
59+
}
60+
WiFiStorage.read(filename, offset, (uint8_t*)buf, rdlen);
61+
offset += rdlen;
62+
return rdlen;
63+
}
64+
uint32_t write(const void *buf, uint32_t wrlen) {
65+
WiFiStorage.write(filename, offset, (uint8_t*)buf, wrlen);
66+
offset += wrlen;
67+
return wrlen;
68+
}
69+
void seek(uint32_t n) {
70+
offset = n;
71+
}
72+
uint32_t position() {
73+
return offset;
74+
}
75+
uint32_t size() {
76+
WiFiStorage.exists(filename, &length);
77+
return length;
78+
}
79+
uint32_t available() {
80+
WiFiStorage.exists(filename, &length);
81+
return length - offset;
82+
}
83+
void erase() {
84+
offset = 0;
85+
WiFiStorage.remove(filename);
86+
}
87+
void flush();
88+
void close() {
89+
offset = 0;
90+
}
91+
protected:
92+
friend class WiFiStorageClass;
93+
size_t offset = 0;
94+
size_t length = 0;
95+
const char* filename;
96+
};

src/utility/wifi_drv.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,4 +1078,69 @@ void WiFiDrv::analogWrite(uint8_t pin, uint8_t value)
10781078
SpiDrv::spiSlaveDeselect();
10791079
}
10801080

1081+
int8_t WiFiDrv::downloadFile(const char* url, uint8_t url_len, const char *filename, uint8_t filename_len)
1082+
{
1083+
WAIT_FOR_SLAVE_SELECT();
1084+
// Send Command
1085+
SpiDrv::sendCmd(DOWNLOAD_FILE, PARAM_NUMS_2);
1086+
SpiDrv::sendParam((uint8_t*)url, url_len, NO_LAST_PARAM);
1087+
SpiDrv::sendParam((uint8_t*)filename, filename_len, LAST_PARAM);
1088+
1089+
// pad to multiple of 4
1090+
int commandSize = 6 + url_len + filename_len;
1091+
while (commandSize % 4) {
1092+
SpiDrv::readChar();
1093+
commandSize++;
1094+
}
1095+
1096+
SpiDrv::spiSlaveDeselect();
1097+
//Wait the reply elaboration
1098+
SpiDrv::waitForSlaveReady();
1099+
SpiDrv::spiSlaveSelect();
1100+
1101+
// Wait for reply
1102+
uint8_t _data = 0;
1103+
uint8_t _dataLen = 0;
1104+
if (!SpiDrv::waitResponseCmd(DOWNLOAD_FILE, PARAM_NUMS_1, &_data, &_dataLen))
1105+
{
1106+
WARN("error waitResponse");
1107+
_data = WL_FAILURE;
1108+
}
1109+
SpiDrv::spiSlaveDeselect();
1110+
return _data;
1111+
}
1112+
1113+
int8_t WiFiDrv::fileOperation(uint8_t operation, const char *filename, uint8_t filename_len, size_t offset, uint8_t* buffer, uint8_t len)
1114+
{
1115+
WAIT_FOR_SLAVE_SELECT();
1116+
// Send Command
1117+
SpiDrv::sendCmd(operation, PARAM_NUMS_3);
1118+
SpiDrv::sendParam((uint8_t*)&offset, sizeof(offset), NO_LAST_PARAM);
1119+
SpiDrv::sendParam((uint8_t*)&len, sizeof(len), NO_LAST_PARAM);
1120+
SpiDrv::sendParam((uint8_t*)filename, filename_len, (operation == WRITE_FILE) ? NO_LAST_PARAM : LAST_PARAM);
1121+
if (operation == WRITE_FILE) {
1122+
SpiDrv::sendParam((uint8_t*)buffer, len, LAST_PARAM);
1123+
}
1124+
1125+
// pad to multiple of 4
1126+
int commandSize = 7 + sizeof(offset) + sizeof(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+
SpiDrv::waitResponseCmd(operation, PARAM_NUMS_1, (operation == WRITE_FILE) ? &_data : buffer, &_dataLen);
1141+
1142+
SpiDrv::spiSlaveDeselect();
1143+
return _dataLen;
1144+
}
1145+
10811146
WiFiDrv wiFiDrv;

src/utility/wifi_drv.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,26 @@ class WiFiDrv
286286
static void digitalWrite(uint8_t pin, uint8_t value);
287287
static void analogWrite(uint8_t pin, uint8_t value);
288288

289+
static int8_t downloadFile(const char* url, uint8_t url_len, const char *filename, uint8_t filename_len);
290+
291+
static int8_t fileOperation(uint8_t operation, const char *filename, uint8_t filename_len, size_t offset, uint8_t* buffer, uint8_t len);
292+
293+
static int8_t readFile(const char *filename, uint8_t filename_len, size_t offset, uint8_t* buffer, size_t buffer_len) {
294+
return fileOperation(READ_FILE, filename, filename_len, offset, buffer, buffer_len);
295+
};
296+
static int8_t writeFile(const char *filename, uint8_t filename_len, size_t offset, uint8_t* buffer, size_t buffer_len) {
297+
return fileOperation(WRITE_FILE, filename, filename_len, offset, buffer, buffer_len);
298+
};
299+
static int8_t deleteFile(const char *filename, uint8_t filename_len) {
300+
return fileOperation(DELETE_FILE, filename, filename_len, 0, NULL, 0);
301+
};
302+
static int8_t existsFile(const char *filename, uint8_t filename_len, size_t* len) {
303+
int32_t length = 0;
304+
fileOperation(EXISTS_FILE, filename, filename_len, 0, (uint8_t*)&length, sizeof(length));
305+
*len = length;
306+
return length >= 0;
307+
};
308+
289309
friend class WiFiUDP;
290310
friend class WiFiClient;
291311
};

src/utility/wifi_spi.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ enum {
101101
SET_PIN_MODE = 0x50,
102102
SET_DIGITAL_WRITE = 0x51,
103103
SET_ANALOG_WRITE = 0x52,
104+
105+
// regular format commands
106+
WRITE_FILE = 0x53,
107+
READ_FILE = 0x54,
108+
DELETE_FILE = 0x55,
109+
EXISTS_FILE = 0x56,
110+
DOWNLOAD_FILE = 0x57,
104111
};
105112

106113

0 commit comments

Comments
 (0)