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