diff --git a/examples/WiFiStorage/WiFiStorage.ino b/examples/WiFiStorage/WiFiStorage.ino new file mode 100644 index 00000000..71ef56ef --- /dev/null +++ b/examples/WiFiStorage/WiFiStorage.ino @@ -0,0 +1,42 @@ +/* + This example shows how to interact with NiNa internal memory partition + APIs are modeled on SerialFlash library (not on SD) to speedup operations and avoid buffers. +*/ + +#include + +void setup() { + + Serial.begin(115200); + while (!Serial); + + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while (true); + } + + WiFiStorageFile file = WiFiStorage.open("/fs/testfile"); + + if (file) { + file.erase(); + } + + String test = "Cantami o Diva del pelide Achille"; + file.write(test.c_str(), test.length()); + + if (file) { + file.seek(0); + while (file.available()) { + uint8_t buf[128]; + int ret = file.read(buf, 128); + Serial.write(buf, ret); + } + } +} + +void loop() { + // put your main code here, to run repeatedly: + +} diff --git a/src/WiFi.h b/src/WiFi.h index 70844fdf..da436731 100644 --- a/src/WiFi.h +++ b/src/WiFi.h @@ -34,6 +34,7 @@ extern "C" { #include "WiFiClient.h" #include "WiFiSSLClient.h" #include "WiFiServer.h" +#include "WiFiStorage.h" class WiFiClass { diff --git a/src/WiFiStorage.cpp b/src/WiFiStorage.cpp new file mode 100644 index 00000000..9d3d13ea --- /dev/null +++ b/src/WiFiStorage.cpp @@ -0,0 +1,30 @@ +/* + WiFiStorage.cpp - 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 +*/ + +#include "WiFiStorage.h" + +WiFiStorageFile WiFiStorageClass::open(const char *filename) { + WiFiStorageFile file(filename); + file.size(); + return file; +} + +WiFiStorageFile WiFiStorageClass::open(String filename) { + return open(filename.c_str()); +} \ No newline at end of file diff --git a/src/WiFiStorage.h b/src/WiFiStorage.h new file mode 100644 index 00000000..e762f8fe --- /dev/null +++ b/src/WiFiStorage.h @@ -0,0 +1,133 @@ +/* + 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 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()); + } +}; + +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 \ No newline at end of file diff --git a/src/utility/spi_drv.cpp b/src/utility/spi_drv.cpp index ff89d416..54ec5208 100644 --- a/src/utility/spi_drv.cpp +++ b/src/utility/spi_drv.cpp @@ -449,6 +449,22 @@ int SpiDrv::waitResponse(uint8_t cmd, uint8_t* numParamRead, uint8_t** params, u return 1; } +void SpiDrv::sendParamNoLen(uint8_t* param, size_t param_len, uint8_t lastParam) +{ + int i = 0; + // Send Spi paramLen + sendParamLen8(0); + + // Send Spi param data + for (i=0; i= 0; + }; + + static void applyOTA(); + friend class WiFiUDP; friend class WiFiClient; }; diff --git a/src/utility/wifi_spi.h b/src/utility/wifi_spi.h index e2d6a005..f50f76c2 100644 --- a/src/utility/wifi_spi.h +++ b/src/utility/wifi_spi.h @@ -102,6 +102,15 @@ enum { SET_PIN_MODE = 0x50, SET_DIGITAL_WRITE = 0x51, SET_ANALOG_WRITE = 0x52, + + // regular format commands + WRITE_FILE = 0x60, + READ_FILE = 0x61, + DELETE_FILE = 0x62, + EXISTS_FILE = 0x63, + DOWNLOAD_FILE = 0x64, + APPLY_OTA_COMMAND = 0x65, + RENAME_FILE = 0x66, };