From f025588d23ed0c92cf6914cb748999f192a74425 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Wed, 13 Jun 2018 18:18:44 +0200 Subject: [PATCH 01/11] Implement FS operations (read/write/delete) --- src/WiFi.h | 1 + src/WiFiStorage.cpp | 7 +++ src/WiFiStorage.h | 96 ++++++++++++++++++++++++++++++++++++++++ src/utility/wifi_drv.cpp | 65 +++++++++++++++++++++++++++ src/utility/wifi_drv.h | 20 +++++++++ src/utility/wifi_spi.h | 7 +++ 6 files changed, 196 insertions(+) create mode 100644 src/WiFiStorage.cpp create mode 100644 src/WiFiStorage.h diff --git a/src/WiFi.h b/src/WiFi.h index 3bb5db60..04c77db3 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..00efac93 --- /dev/null +++ b/src/WiFiStorage.cpp @@ -0,0 +1,7 @@ +#include "WiFiStorage.h" + +WiFiStorageFile WiFiStorageClass::open(const char *filename) { + WiFiStorageFile file(filename); + file.size(); + return file; +} diff --git a/src/WiFiStorage.h b/src/WiFiStorage.h new file mode 100644 index 00000000..d7479195 --- /dev/null +++ b/src/WiFiStorage.h @@ -0,0 +1,96 @@ +#include "utility/wifi_drv.h" + +class WiFiStorageFile; + +class WiFiStorageClass +{ +public: + static bool begin(); + + static WiFiStorageFile open(const char *filename); + static bool exists(const char *filename, size_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 read(const char *filename, size_t offset, uint8_t* buffer, size_t buffer_len) { + WiFiDrv::readFile(filename, strlen(filename), offset, buffer, buffer_len); + return true; + } + static bool write(const char *filename, size_t offset, uint8_t* buffer, size_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 read(String filename, size_t offset, uint8_t* buffer, size_t buffer_len) { + return read(filename.c_str(), offset, buffer, buffer_len); + } + static bool write(String filename, size_t offset, uint8_t* buffer, size_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; + size_t offset = 0; + size_t length = 0; + const char* filename; +}; diff --git a/src/utility/wifi_drv.cpp b/src/utility/wifi_drv.cpp index 3575d797..e0da1ceb 100644 --- a/src/utility/wifi_drv.cpp +++ b/src/utility/wifi_drv.cpp @@ -1078,4 +1078,69 @@ void WiFiDrv::analogWrite(uint8_t pin, uint8_t value) SpiDrv::spiSlaveDeselect(); } +int8_t WiFiDrv::downloadFile(const char* url, uint8_t url_len, const char *filename, uint8_t filename_len) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(DOWNLOAD_FILE, PARAM_NUMS_2); + SpiDrv::sendParam((uint8_t*)url, url_len, NO_LAST_PARAM); + SpiDrv::sendParam((uint8_t*)filename, filename_len, LAST_PARAM); + + // pad to multiple of 4 + int commandSize = 6 + url_len + filename_len; + while (commandSize % 4) { + SpiDrv::readChar(); + commandSize++; + } + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(DOWNLOAD_FILE, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + _data = WL_FAILURE; + } + SpiDrv::spiSlaveDeselect(); + return _data; +} + +int8_t WiFiDrv::fileOperation(uint8_t operation, const char *filename, uint8_t filename_len, size_t offset, uint8_t* buffer, uint8_t len) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(operation, PARAM_NUMS_3); + SpiDrv::sendParam((uint8_t*)&offset, sizeof(offset), NO_LAST_PARAM); + SpiDrv::sendParam((uint8_t*)&len, sizeof(len), NO_LAST_PARAM); + SpiDrv::sendParam((uint8_t*)filename, filename_len, (operation == WRITE_FILE) ? NO_LAST_PARAM : LAST_PARAM); + if (operation == WRITE_FILE) { + SpiDrv::sendParam((uint8_t*)buffer, len, LAST_PARAM); + } + + // pad to multiple of 4 + int commandSize = 7 + sizeof(offset) + sizeof(len) + filename_len; + while (commandSize % 4) { + SpiDrv::readChar(); + commandSize++; + } + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + SpiDrv::waitResponseCmd(operation, PARAM_NUMS_1, (operation == WRITE_FILE) ? &_data : buffer, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + return _dataLen; +} + WiFiDrv wiFiDrv; diff --git a/src/utility/wifi_drv.h b/src/utility/wifi_drv.h index d9bfeff2..1226e11a 100644 --- a/src/utility/wifi_drv.h +++ b/src/utility/wifi_drv.h @@ -286,6 +286,26 @@ class WiFiDrv static void digitalWrite(uint8_t pin, uint8_t value); static void analogWrite(uint8_t pin, uint8_t value); + static int8_t downloadFile(const char* url, uint8_t url_len, const char *filename, uint8_t filename_len); + + static int8_t fileOperation(uint8_t operation, const char *filename, uint8_t filename_len, size_t offset, uint8_t* buffer, uint8_t len); + + static int8_t readFile(const char *filename, uint8_t filename_len, size_t offset, uint8_t* buffer, size_t buffer_len) { + return fileOperation(READ_FILE, filename, filename_len, offset, buffer, buffer_len); + }; + static int8_t writeFile(const char *filename, uint8_t filename_len, size_t offset, uint8_t* buffer, size_t buffer_len) { + return fileOperation(WRITE_FILE, filename, filename_len, offset, buffer, buffer_len); + }; + static int8_t deleteFile(const char *filename, uint8_t filename_len) { + return fileOperation(DELETE_FILE, filename, filename_len, 0, NULL, 0); + }; + static int8_t existsFile(const char *filename, uint8_t filename_len, size_t* len) { + int32_t length = 0; + fileOperation(EXISTS_FILE, filename, filename_len, 0, (uint8_t*)&length, sizeof(length)); + *len = length; + return length >= 0; + }; + friend class WiFiUDP; friend class WiFiClient; }; diff --git a/src/utility/wifi_spi.h b/src/utility/wifi_spi.h index 7402e434..fd8f5e77 100644 --- a/src/utility/wifi_spi.h +++ b/src/utility/wifi_spi.h @@ -101,6 +101,13 @@ enum { SET_PIN_MODE = 0x50, SET_DIGITAL_WRITE = 0x51, SET_ANALOG_WRITE = 0x52, + + // regular format commands + WRITE_FILE = 0x53, + READ_FILE = 0x54, + DELETE_FILE = 0x55, + EXISTS_FILE = 0x56, + DOWNLOAD_FILE = 0x57, }; From b6daf51f234fe4c6109d86920e20164d35ee5007 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Wed, 13 Jun 2018 18:31:11 +0200 Subject: [PATCH 02/11] Readd exists() standard API --- src/WiFiStorage.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/WiFiStorage.h b/src/WiFiStorage.h index d7479195..07eeac2c 100644 --- a/src/WiFiStorage.h +++ b/src/WiFiStorage.h @@ -8,6 +8,11 @@ class WiFiStorageClass static bool begin(); static WiFiStorageFile open(const char *filename); + + static bool exists(const char *filename) { + size_t len; + return (WiFiDrv::existsFile(filename, strlen(filename), &len) > 0); + } static bool exists(const char *filename, size_t* len) { return (WiFiDrv::existsFile(filename, strlen(filename), len) > 0); } From f0543a20d9ba950d7ff727f1e685a78b67f5ca54 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Fri, 28 Sep 2018 10:10:34 +0200 Subject: [PATCH 03/11] Add overloaded open for String filenames --- src/WiFiStorage.cpp | 4 ++++ src/WiFiStorage.h | 1 + 2 files changed, 5 insertions(+) diff --git a/src/WiFiStorage.cpp b/src/WiFiStorage.cpp index 00efac93..9d2ea4b7 100644 --- a/src/WiFiStorage.cpp +++ b/src/WiFiStorage.cpp @@ -5,3 +5,7 @@ WiFiStorageFile WiFiStorageClass::open(const char *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 index 07eeac2c..2479c44c 100644 --- a/src/WiFiStorage.h +++ b/src/WiFiStorage.h @@ -8,6 +8,7 @@ class WiFiStorageClass static bool begin(); static WiFiStorageFile open(const char *filename); + static WiFiStorageFile open(String filename); static bool exists(const char *filename) { size_t len; From 844a00e5f00968362d9ec1300b1f72474d20dc01 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Wed, 13 Jun 2018 18:31:45 +0200 Subject: [PATCH 04/11] Add WiFiStorage example --- examples/WiFiStorage/WiFiStorage.ino | 42 ++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 examples/WiFiStorage/WiFiStorage.ino diff --git a/examples/WiFiStorage/WiFiStorage.ino b/examples/WiFiStorage/WiFiStorage.ino new file mode 100644 index 00000000..faab757d --- /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("/storage/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: + +} From 84969aafc22c9ec685ebe0cde4f4cdb2473c4b72 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Fri, 28 Sep 2018 10:10:55 +0200 Subject: [PATCH 05/11] Fix file write() for len > 255 --- src/utility/spi_drv.cpp | 16 ++++++++++++++++ src/utility/spi_drv.h | 2 ++ src/utility/wifi_drv.cpp | 13 +++++++++---- src/utility/wifi_drv.h | 2 +- 4 files changed, 28 insertions(+), 5 deletions(-) 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 Date: Fri, 28 Sep 2018 10:43:57 +0200 Subject: [PATCH 06/11] Fix WiFIStorage example for FW 1.1.0 --- examples/WiFiStorage/WiFiStorage.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/WiFiStorage/WiFiStorage.ino b/examples/WiFiStorage/WiFiStorage.ino index faab757d..71ef56ef 100644 --- a/examples/WiFiStorage/WiFiStorage.ino +++ b/examples/WiFiStorage/WiFiStorage.ino @@ -3,7 +3,7 @@ APIs are modeled on SerialFlash library (not on SD) to speedup operations and avoid buffers. */ -#include +#include void setup() { @@ -17,7 +17,7 @@ void setup() { while (true); } - WiFiStorageFile file = WiFiStorage.open("/storage/testfile"); + WiFiStorageFile file = WiFiStorage.open("/fs/testfile"); if (file) { file.erase(); From 82534dbbe6de9d121a965eeb8db901b6ce20f9e8 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Tue, 11 Dec 2018 17:16:15 +0100 Subject: [PATCH 07/11] Adapt APIs to latest NINA FW --- src/utility/wifi_spi.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/utility/wifi_spi.h b/src/utility/wifi_spi.h index fd8f5e77..14b10106 100644 --- a/src/utility/wifi_spi.h +++ b/src/utility/wifi_spi.h @@ -103,11 +103,12 @@ enum { SET_ANALOG_WRITE = 0x52, // regular format commands - WRITE_FILE = 0x53, - READ_FILE = 0x54, - DELETE_FILE = 0x55, - EXISTS_FILE = 0x56, - DOWNLOAD_FILE = 0x57, + WRITE_FILE = 0x60, + READ_FILE = 0x61, + DELETE_FILE = 0x62, + EXISTS_FILE = 0x63, + DOWNLOAD_FILE = 0x64, + APPLY_OTA_COMMAND = 0x65, }; From 32de7058b54511e6525278fec4ae69fff0d4228d Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Tue, 11 Dec 2018 17:17:00 +0100 Subject: [PATCH 08/11] Add applyOTA API --- src/utility/wifi_drv.cpp | 12 ++++++++++++ src/utility/wifi_drv.h | 2 ++ 2 files changed, 14 insertions(+) diff --git a/src/utility/wifi_drv.cpp b/src/utility/wifi_drv.cpp index 55bd813d..336076c7 100644 --- a/src/utility/wifi_drv.cpp +++ b/src/utility/wifi_drv.cpp @@ -1148,4 +1148,16 @@ int8_t WiFiDrv::fileOperation(uint8_t operation, const char *filename, uint8_t f return _dataLen; } +void WiFiDrv::applyOTA() { + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(APPLY_OTA_COMMAND, PARAM_NUMS_0); + + SpiDrv::spiSlaveDeselect(); + + // don't wait for return; OTA operation should be fire and forget :) +} + + WiFiDrv wiFiDrv; diff --git a/src/utility/wifi_drv.h b/src/utility/wifi_drv.h index 33db0141..f10af3c0 100644 --- a/src/utility/wifi_drv.h +++ b/src/utility/wifi_drv.h @@ -306,6 +306,8 @@ class WiFiDrv return length >= 0; }; + static void applyOTA(); + friend class WiFiUDP; friend class WiFiClient; }; From 2219137a3e03457bc8eb1e7ca9ab03db1ec0c20d Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Tue, 11 Dec 2018 17:17:23 +0100 Subject: [PATCH 09/11] Fix filesystem operation for 8bit architectures --- src/WiFiStorage.cpp | 19 +++++++++++++++++++ src/WiFiStorage.h | 41 ++++++++++++++++++++++++++++++++-------- src/utility/wifi_drv.cpp | 2 +- src/utility/wifi_drv.h | 8 ++++---- 4 files changed, 57 insertions(+), 13 deletions(-) diff --git a/src/WiFiStorage.cpp b/src/WiFiStorage.cpp index 9d2ea4b7..9d3d13ea 100644 --- a/src/WiFiStorage.cpp +++ b/src/WiFiStorage.cpp @@ -1,3 +1,22 @@ +/* + 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) { diff --git a/src/WiFiStorage.h b/src/WiFiStorage.h index 2479c44c..72e1975f 100644 --- a/src/WiFiStorage.h +++ b/src/WiFiStorage.h @@ -1,3 +1,25 @@ +/* + 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; @@ -11,21 +33,21 @@ class WiFiStorageClass static WiFiStorageFile open(String filename); static bool exists(const char *filename) { - size_t len; + uint32_t len; return (WiFiDrv::existsFile(filename, strlen(filename), &len) > 0); } - static bool exists(const char *filename, size_t* len) { + 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 read(const char *filename, size_t offset, uint8_t* buffer, size_t buffer_len) { + 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, size_t offset, uint8_t* buffer, size_t buffer_len) { + 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; } @@ -37,10 +59,10 @@ class WiFiStorageClass static bool remove(String filename) { return remove(filename.c_str()); } - static bool read(String filename, size_t offset, uint8_t* buffer, size_t buffer_len) { + 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, size_t offset, uint8_t* buffer, size_t 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) { @@ -55,6 +77,7 @@ class WiFiStorageFile { public: constexpr WiFiStorageFile(const char* _filename) : filename(_filename) { } + operator bool() { return WiFiStorage.exists(filename, &length); } @@ -96,7 +119,9 @@ class WiFiStorageFile } protected: friend class WiFiStorageClass; - size_t offset = 0; - size_t length = 0; + uint32_t offset = 0; + uint32_t length = 0; const char* filename; }; + +#endif \ No newline at end of file diff --git a/src/utility/wifi_drv.cpp b/src/utility/wifi_drv.cpp index 336076c7..de856e24 100644 --- a/src/utility/wifi_drv.cpp +++ b/src/utility/wifi_drv.cpp @@ -1110,7 +1110,7 @@ int8_t WiFiDrv::downloadFile(const char* url, uint8_t url_len, const char *filen return _data; } -int8_t WiFiDrv::fileOperation(uint8_t operation, const char *filename, uint8_t filename_len, size_t offset, uint8_t* buffer, size_t len) +int8_t WiFiDrv::fileOperation(uint8_t operation, const char *filename, uint8_t filename_len, uint32_t offset, uint8_t* buffer, uint32_t len) { WAIT_FOR_SLAVE_SELECT(); // Send Command diff --git a/src/utility/wifi_drv.h b/src/utility/wifi_drv.h index f10af3c0..883b7e68 100644 --- a/src/utility/wifi_drv.h +++ b/src/utility/wifi_drv.h @@ -288,18 +288,18 @@ class WiFiDrv static int8_t downloadFile(const char* url, uint8_t url_len, const char *filename, uint8_t filename_len); - static int8_t fileOperation(uint8_t operation, const char *filename, uint8_t filename_len, size_t offset, uint8_t* buffer, size_t len); + static int8_t fileOperation(uint8_t operation, const char *filename, uint8_t filename_len, uint32_t offset, uint8_t* buffer, uint32_t len); - static int8_t readFile(const char *filename, uint8_t filename_len, size_t offset, uint8_t* buffer, size_t buffer_len) { + static int8_t readFile(const char *filename, uint8_t filename_len, uint32_t offset, uint8_t* buffer, uint32_t buffer_len) { return fileOperation(READ_FILE, filename, filename_len, offset, buffer, buffer_len); }; - static int8_t writeFile(const char *filename, uint8_t filename_len, size_t offset, uint8_t* buffer, size_t buffer_len) { + static int8_t writeFile(const char *filename, uint8_t filename_len, uint32_t offset, uint8_t* buffer, uint32_t buffer_len) { return fileOperation(WRITE_FILE, filename, filename_len, offset, buffer, buffer_len); }; static int8_t deleteFile(const char *filename, uint8_t filename_len) { return fileOperation(DELETE_FILE, filename, filename_len, 0, NULL, 0); }; - static int8_t existsFile(const char *filename, uint8_t filename_len, size_t* len) { + static int8_t existsFile(const char *filename, uint8_t filename_len, uint32_t* len) { int32_t length = 0; fileOperation(EXISTS_FILE, filename, filename_len, 0, (uint8_t*)&length, sizeof(length)); *len = length; From 49f9738894aa2a245082294b4013bb7ce7e06233 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 10 Jun 2020 11:13:49 +0200 Subject: [PATCH 10/11] Adding 'rename' operation necessary for using the WiFiNiNa for OTA --- src/WiFiStorage.h | 6 ++++++ src/utility/wifi_drv.cpp | 32 ++++++++++++++++++++++++++++++++ src/utility/wifi_drv.h | 1 + src/utility/wifi_spi.h | 1 + 4 files changed, 40 insertions(+) diff --git a/src/WiFiStorage.h b/src/WiFiStorage.h index 72e1975f..e762f8fe 100644 --- a/src/WiFiStorage.h +++ b/src/WiFiStorage.h @@ -43,6 +43,9 @@ class WiFiStorageClass 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; @@ -59,6 +62,9 @@ class WiFiStorageClass 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); } diff --git a/src/utility/wifi_drv.cpp b/src/utility/wifi_drv.cpp index de856e24..307f8925 100644 --- a/src/utility/wifi_drv.cpp +++ b/src/utility/wifi_drv.cpp @@ -1110,6 +1110,38 @@ int8_t WiFiDrv::downloadFile(const char* url, uint8_t url_len, const char *filen return _data; } +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) +{ + WAIT_FOR_SLAVE_SELECT(); + /* Send Command */ + SpiDrv::sendCmd(RENAME_FILE, PARAM_NUMS_2); + SpiDrv::sendParam((uint8_t*)old_file_name, old_file_name_len, NO_LAST_PARAM); + SpiDrv::sendParam((uint8_t*)new_file_name, new_file_name_len, LAST_PARAM); + + /* pad to multiple of 4 */ + int commandSize = 6 + old_file_name_len + new_file_name_len; + while (commandSize % 4) { + SpiDrv::readChar(); + commandSize++; + } + + SpiDrv::spiSlaveDeselect(); + /* Wait the reply elaboration */ + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + /* Wait for reply */ + uint8_t data = 0; + uint8_t dataLen = 0; + if (!SpiDrv::waitResponseCmd(DOWNLOAD_FILE, PARAM_NUMS_1, &data, &dataLen)) + { + WARN("error waitResponse"); + data = WL_FAILURE; + } + SpiDrv::spiSlaveDeselect(); + return data; +} + int8_t WiFiDrv::fileOperation(uint8_t operation, const char *filename, uint8_t filename_len, uint32_t offset, uint8_t* buffer, uint32_t len) { WAIT_FOR_SLAVE_SELECT(); diff --git a/src/utility/wifi_drv.h b/src/utility/wifi_drv.h index 883b7e68..2e93936e 100644 --- a/src/utility/wifi_drv.h +++ b/src/utility/wifi_drv.h @@ -287,6 +287,7 @@ class WiFiDrv static void analogWrite(uint8_t pin, uint8_t value); static int8_t downloadFile(const char* url, uint8_t url_len, const char *filename, uint8_t filename_len); + 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); static int8_t fileOperation(uint8_t operation, const char *filename, uint8_t filename_len, uint32_t offset, uint8_t* buffer, uint32_t len); diff --git a/src/utility/wifi_spi.h b/src/utility/wifi_spi.h index 14b10106..7611fc98 100644 --- a/src/utility/wifi_spi.h +++ b/src/utility/wifi_spi.h @@ -109,6 +109,7 @@ enum { EXISTS_FILE = 0x63, DOWNLOAD_FILE = 0x64, APPLY_OTA_COMMAND = 0x65, + RENAME_FILE = 0x66, }; From 1de666aa9d93378296f66048c10b37d5fd7431e9 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 24 Jun 2020 09:49:58 +0200 Subject: [PATCH 11/11] Fixing 2 bugs * Prepending 'renameFile' with 'WiFiDrv' so that 'WiFiDrv::renameFile', otherwise a linker error concurs. * Setting correct command response for function SpiDrv::waitResponseCmd --- src/utility/wifi_drv.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utility/wifi_drv.cpp b/src/utility/wifi_drv.cpp index 307f8925..4a5c7303 100644 --- a/src/utility/wifi_drv.cpp +++ b/src/utility/wifi_drv.cpp @@ -1110,7 +1110,7 @@ int8_t WiFiDrv::downloadFile(const char* url, uint8_t url_len, const char *filen return _data; } -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) +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) { WAIT_FOR_SLAVE_SELECT(); /* Send Command */ @@ -1133,7 +1133,7 @@ int8_t renameFile(const char * old_file_name, uint8_t const old_file_name_len, c /* Wait for reply */ uint8_t data = 0; uint8_t dataLen = 0; - if (!SpiDrv::waitResponseCmd(DOWNLOAD_FILE, PARAM_NUMS_1, &data, &dataLen)) + if (!SpiDrv::waitResponseCmd(RENAME_FILE, PARAM_NUMS_1, &data, &dataLen)) { WARN("error waitResponse"); data = WL_FAILURE;