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
0 commit comments