From 5c6f98643b09190de96b85515fd36dd3d4d82f7b Mon Sep 17 00:00:00 2001 From: Andrew Melvin Date: Thu, 12 Nov 2015 07:27:21 +0000 Subject: [PATCH 1/5] Changes to static request handler --- .../ESP8266WebServer/src/ESP8266WebServer.cpp | 4 +- .../ESP8266WebServer/src/ESP8266WebServer.h | 2 +- .../src/detail/RequestHandlersImpl.h | 38 ++++++++++++++----- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp b/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp index 075437706..cc8410ece 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp @@ -83,8 +83,8 @@ void ESP8266WebServer::_addRequestHandler(RequestHandler* handler) { } } -void ESP8266WebServer::serveStatic(const char* uri, FS& fs, const char* path) { - _addRequestHandler(new StaticRequestHandler(fs, path, uri)); +void ESP8266WebServer::serveStatic(const char* uri, FS& fs, const char* path, const char* cache_header) { + _addRequestHandler(new StaticRequestHandler(fs, path, uri, cache_header)); } void ESP8266WebServer::handleClient() { diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.h b/libraries/ESP8266WebServer/src/ESP8266WebServer.h index 40216f482..e2a8da46c 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.h +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.h @@ -69,7 +69,7 @@ public: void on(const char* uri, THandlerFunction handler); void on(const char* uri, HTTPMethod method, THandlerFunction fn); void addHandler(RequestHandler* handler); - void serveStatic(const char* uri, fs::FS& fs, const char* path); + void serveStatic(const char* uri, fs::FS& fs, const char* path, const char* cache_header = NULL ); void onNotFound(THandlerFunction fn); //called when handler is not assigned void onFileUpload(THandlerFunction fn); //handle file uploads diff --git a/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h b/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h index e5da410f1..accddee21 100644 --- a/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h +++ b/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h @@ -31,39 +31,57 @@ protected: class StaticRequestHandler : public RequestHandler { public: - StaticRequestHandler(FS& fs, const char* path, const char* uri) + StaticRequestHandler(FS& fs, const char* path, const char* uri, const char* cache_header) : _fs(fs) , _uri(uri) , _path(path) + , _cache_header(cache_header) { _isFile = fs.exists(path); - DEBUGV("StaticRequestHandler: path=%s uri=%s isFile=%d\r\n", path, uri, _isFile); - _baseUriLength = _uri.length(); + DEBUGV("StaticRequestHandler: path=%s uri=%s isFile=%d, cache_header=%s\r\n", path, uri, _isFile, cache_header); + _baseUriLength = _uri.length(); } - bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override { if (requestMethod != HTTP_GET) return false; DEBUGV("StaticRequestHandler::handle: request=%s _uri=%s\r\n", requestUri.c_str(), _uri.c_str()); if (!requestUri.startsWith(_uri)) - return false; + return false; - String path(_path); - if (!_isFile) { + String path(_path); + + if(path.endsWith("/")) path += "index.htm"; + + if (!_isFile) { // Base URI doesn't point to a file. Append whatever follows this // URI in request to get the file path. - path += requestUri.substring(_baseUriLength); + path += requestUri.substring(_baseUriLength); } + else if (requestUri != _uri) { // Base URI points to a file but request doesn't match this URI exactly return false; } DEBUGV("StaticRequestHandler::handle: path=%s, isFile=%d\r\n", path.c_str(), _isFile); + + String contentType = getContentType(path); + + // look for gz file, only if the original specified path is not a gz. So part only works to send gzip via content encoding when a non compressed is asked for + // if you point the the path to gzip you will serve the gzip as content type "application/x-gzip", not text or javascript etc... + if (!path.endsWith(".gz") && !SPIFFS.exists(path)) { + String pathWithGz = path + ".gz"; + if(SPIFFS.exists(pathWithGz)) + path += ".gz"; + } + File f = _fs.open(path, "r"); if (!f) return false; - server.streamFile(f, getContentType(path)); + if (_cache_header.length() != 0) + server.sendHeader("Cache-Control", _cache_header); + + server.streamFile(f, contentType); return true; } @@ -81,6 +99,7 @@ public: else if (path.endsWith(".xml")) return "text/xml"; else if (path.endsWith(".pdf")) return "application/pdf"; else if (path.endsWith(".zip")) return "application/zip"; + else if(path.endsWith(".gz")) return "application/x-gzip"; return "application/octet-stream"; } @@ -88,6 +107,7 @@ protected: FS _fs; String _uri; String _path; + String _cache_header; bool _isFile; size_t _baseUriLength; }; From 10be8ec4f9ad3699fb6eac441a15dc4f785f735a Mon Sep 17 00:00:00 2001 From: volca Date: Thu, 12 Nov 2015 16:35:45 +0800 Subject: [PATCH 2/5] add menu "Reset Method" --- boards.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/boards.txt b/boards.txt index 8dc7a6906..ea819444b 100644 --- a/boards.txt +++ b/boards.txt @@ -4,6 +4,7 @@ menu.FlashSize=Flash Size menu.FlashMode=Flash Mode menu.FlashFreq=Flash Frequency menu.UploadTool=Upload Using +menu.ResetMethod=Reset Method ############################################################## generic.name=Generic ESP8266 Module @@ -135,6 +136,11 @@ generic.menu.FlashSize.4M3M.build.spiffs_end=0x3FB000 generic.menu.FlashSize.4M3M.build.spiffs_blocksize=8192 generic.menu.FlashSize.4M3M.upload.maximum_size=1044464 +generic.menu.ResetMethod.ck=ck +generic.menu.ResetMethod.ck.upload.resetmethod=ck +generic.menu.ResetMethod.nodemcu=nodemcu +generic.menu.ResetMethod.nodemcu.upload.resetmethod=nodemcu + # disabled because espressif's bootloader refuses to write above 4M # generic.menu.FlashSize.8M=8M (7M SPIFFS) # generic.menu.FlashSize.8M.build.flash_size=1M From d7e340fe14443d14c96d66464618083a24c69474 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 13 Nov 2015 02:51:44 +0300 Subject: [PATCH 3/5] Update docs to mention new ESP8266FS tool version. --- doc/reference.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/reference.md b/doc/reference.md index b88ccbf07..a648d5743 100644 --- a/doc/reference.md +++ b/doc/reference.md @@ -178,7 +178,7 @@ SweetPea ESP-210 | 4M | 1M, 3M *ESP8266FS* is a tool which integrates into the Arduino IDE. It adds a menu item to *Tools* menu for uploading the contents of sketch data directory into ESP8266 flash file system. -- Download the tool: http://arduino.esp8266.com/ESP8266FS-1.6.5-1105-g98d2458.zip +- Download the tool: https://github.com/esp8266/arduino-esp8266fs-plugin/releases/download/0.1.3/ESP8266FS-0.1.3.zip. - In your Arduino sketchbook directory, create `tools` directory if it doesn't exist yet - Unpack the tool into `tools` directory (the path will look like `/Arduino/tools/ESP8266FS/tool/esp8266fs.jar`) - Restart Arduino IDE From d0a944e4178b6264080bf3ccdb5b737af903ca41 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 13 Nov 2015 13:23:16 +0300 Subject: [PATCH 4/5] Refactoring of FS::info (#779) --- cores/esp8266/FS.cpp | 4 ++-- cores/esp8266/FS.h | 12 +++++++++++- cores/esp8266/FSImpl.h | 2 +- cores/esp8266/spiffs_api.cpp | 14 +++++++++----- doc/reference.md | 26 ++++++++++++++++++++++++++ tests/FSWrapper/FSWrapper.ino | 15 +++++++++++++++ 6 files changed, 64 insertions(+), 9 deletions(-) diff --git a/cores/esp8266/FS.cpp b/cores/esp8266/FS.cpp index 5691c2573..55bae8f5c 100644 --- a/cores/esp8266/FS.cpp +++ b/cores/esp8266/FS.cpp @@ -174,11 +174,11 @@ bool FS::format() { return _impl->format(); } -bool FS::info(uint32_t *total, uint32_t *used){ +bool FS::info(FSInfo& info){ if (!_impl) { return false; } - return _impl->info(total,used); + return _impl->info(info); } File FS::open(const String& path, const char* mode) { diff --git a/cores/esp8266/FS.h b/cores/esp8266/FS.h index c03e50619..07b58c040 100644 --- a/cores/esp8266/FS.h +++ b/cores/esp8266/FS.h @@ -85,6 +85,15 @@ protected: DirImplPtr _impl; }; +struct FSInfo { + size_t totalBytes; + size_t usedBytes; + size_t blockSize; + size_t pageSize; + size_t maxOpenFiles; + size_t maxPathLength; +}; + class FS { public: @@ -93,7 +102,7 @@ public: bool begin(); bool format(); - bool info(uint32_t *total, uint32_t *used); + bool info(FSInfo& info); File open(const char* path, const char* mode); File open(const String& path, const char* mode); @@ -123,6 +132,7 @@ using fs::SeekMode; using fs::SeekSet; using fs::SeekCur; using fs::SeekEnd; +using fs::FSInfo; extern FS SPIFFS; diff --git a/cores/esp8266/FSImpl.h b/cores/esp8266/FSImpl.h index a1124b285..c96ea0721 100644 --- a/cores/esp8266/FSImpl.h +++ b/cores/esp8266/FSImpl.h @@ -64,7 +64,7 @@ class FSImpl { public: virtual bool begin() = 0; virtual bool format() = 0; - virtual bool info(uint32_t *total, uint32_t *used) = 0; + virtual bool info(FSInfo& info) = 0; virtual FileImplPtr open(const char* path, OpenMode openMode, AccessMode accessMode) = 0; virtual bool exists(const char* path) = 0; virtual DirImplPtr openDir(const char* path) = 0; diff --git a/cores/esp8266/spiffs_api.cpp b/cores/esp8266/spiffs_api.cpp index 2d0d90b83..978e2ea0d 100644 --- a/cores/esp8266/spiffs_api.cpp +++ b/cores/esp8266/spiffs_api.cpp @@ -71,11 +71,15 @@ public: } return true; } - - bool info(uint32_t *total, uint32_t *used) override{ - auto rc = SPIFFS_info(&_fs, total, used); - if (rc != SPIFFS_OK) { - DEBUGV("SPIFFS_format: rc=%d, err=%d\r\n", rc, _fs.err_code); + bool info(FSInfo& info) override { + info.maxOpenFiles = _maxOpenFds; + info.blockSize = _blockSize; + info.pageSize = _pageSize; + info.maxOpenFiles = _maxOpenFds; + info.maxPathLength = SPIFFS_OBJ_NAME_LEN; + auto rc = SPIFFS_info(&_fs, &info.totalBytes, &info.usedBytes); + if (rc != SPIFFS_OK) { + DEBUGV("SPIFFS_info: rc=%d, err=%d\r\n", rc, _fs.err_code); return false; } return true; diff --git a/doc/reference.md b/doc/reference.md index a648d5743..10df55219 100644 --- a/doc/reference.md +++ b/doc/reference.md @@ -266,6 +266,32 @@ SPIFFS.rename(pathFrom, pathTo) Renames file from `pathFrom` to `pathTo`. Paths must be absolute. Returns *true* if file was renamed successfully. +#### info + +```c++ +FSInfo fs_info; +SPIFFS.info(fs_info); +``` + +Fills [FSInfo structure](#filesystem-information-structure) with information about +the file system. Returns `true` is successful, `false` otherwise. + +### Filesystem information structure + +```c++ +struct FSInfo { + size_t totalBytes; + size_t usedBytes; + size_t blockSize; + size_t pageSize; + size_t maxOpenFiles; + size_t maxPathLength; +}; +``` + +This is the structure which may be filled using FS::info method. Field names +are self-explanatory. + ### Directory object (Dir) The purpose of *Dir* object is to iterate over files inside a directory. diff --git a/tests/FSWrapper/FSWrapper.ino b/tests/FSWrapper/FSWrapper.ino index 2fefc4ccb..b152093d5 100644 --- a/tests/FSWrapper/FSWrapper.ino +++ b/tests/FSWrapper/FSWrapper.ino @@ -111,6 +111,21 @@ void setup() { } } + { + FSInfo info; + if (!SPIFFS.info(info)) { + fail("info failed"); + } + Serial.printf("Total: %u\nUsed: %u\nBlock: %u\nPage: %u\nMax open files: %u\nMax path len: %u\n", + info.totalBytes, + info.usedBytes, + info.blockSize, + info.pageSize, + info.maxOpenFiles, + info.maxPathLength + ); + } + { if (!SPIFFS.format()) { fail("format failed"); From 5c7b40740aedfdceddc9faf8324076707e8c2164 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 13 Nov 2015 23:41:29 +0300 Subject: [PATCH 5/5] Disable sleep mode before doing OTA (#1005) --- cores/esp8266/Updater.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cores/esp8266/Updater.cpp b/cores/esp8266/Updater.cpp index 7e6b2e4e1..28830fba5 100644 --- a/cores/esp8266/Updater.cpp +++ b/cores/esp8266/Updater.cpp @@ -8,6 +8,7 @@ extern "C" { #include "c_types.h" #include "spi_flash.h" + #include "user_interface.h" } extern "C" uint32_t _SPIFFS_start; @@ -59,6 +60,8 @@ bool UpdaterClass::begin(size_t size, int command) { _reset(); _error = 0; + wifi_set_sleep_type(NONE_SLEEP_T); + uint32_t updateStartAddress = 0; if (command == U_FLASH) { //size of current sketch rounded to a sector