From fc77f2e89cfad8d8e3624c989834f07e2ebe8045 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Thu, 20 Jun 2019 23:34:27 +0200 Subject: [PATCH] littlefs: fixes for mock/emulation, use in FSBrowser example (#6211) * littlefs: fixes for mock/emulation, use in FSBrowser example * emulation: makefile: integrate arch size into object file names --- .../examples/FSBrowser/FSBrowser.ino | 32 ++++++++++++------- tests/host/Makefile | 24 ++++++++------ tests/host/common/ArduinoMainLittlefs.cpp | 17 ++++++++++ 3 files changed, 51 insertions(+), 22 deletions(-) create mode 100644 tests/host/common/ArduinoMainLittlefs.cpp diff --git a/libraries/ESP8266WebServer/examples/FSBrowser/FSBrowser.ino b/libraries/ESP8266WebServer/examples/FSBrowser/FSBrowser.ino index cf1c3786d..69065c43d 100644 --- a/libraries/ESP8266WebServer/examples/FSBrowser/FSBrowser.ino +++ b/libraries/ESP8266WebServer/examples/FSBrowser/FSBrowser.ino @@ -27,6 +27,10 @@ #include #include #include +#include + +//FS* filesystem = &SPIFFS; +FS* filesystem = &LittleFS; #define DBG_OUTPUT_PORT Serial @@ -94,11 +98,11 @@ bool handleFileRead(String path) { } String contentType = getContentType(path); String pathWithGz = path + ".gz"; - if (SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) { - if (SPIFFS.exists(pathWithGz)) { + if (filesystem->exists(pathWithGz) || filesystem->exists(path)) { + if (filesystem->exists(pathWithGz)) { path += ".gz"; } - File file = SPIFFS.open(path, "r"); + File file = filesystem->open(path, "r"); server.streamFile(file, contentType); file.close(); return true; @@ -117,7 +121,7 @@ void handleFileUpload() { filename = "/" + filename; } DBG_OUTPUT_PORT.print("handleFileUpload Name: "); DBG_OUTPUT_PORT.println(filename); - fsUploadFile = SPIFFS.open(filename, "w"); + fsUploadFile = filesystem->open(filename, "w"); filename = String(); } else if (upload.status == UPLOAD_FILE_WRITE) { //DBG_OUTPUT_PORT.print("handleFileUpload Data: "); DBG_OUTPUT_PORT.println(upload.currentSize); @@ -141,10 +145,10 @@ void handleFileDelete() { if (path == "/") { return server.send(500, "text/plain", "BAD PATH"); } - if (!SPIFFS.exists(path)) { + if (!filesystem->exists(path)) { return server.send(404, "text/plain", "FileNotFound"); } - SPIFFS.remove(path); + filesystem->remove(path); server.send(200, "text/plain", ""); path = String(); } @@ -158,10 +162,10 @@ void handleFileCreate() { if (path == "/") { return server.send(500, "text/plain", "BAD PATH"); } - if (SPIFFS.exists(path)) { + if (filesystem->exists(path)) { return server.send(500, "text/plain", "FILE EXISTS"); } - File file = SPIFFS.open(path, "w"); + File file = filesystem->open(path, "w"); if (file) { file.close(); } else { @@ -179,7 +183,7 @@ void handleFileList() { String path = server.arg("dir"); DBG_OUTPUT_PORT.println("handleFileList: " + path); - Dir dir = SPIFFS.openDir(path); + Dir dir = filesystem->openDir(path); path = String(); String output = "["; @@ -192,7 +196,11 @@ void handleFileList() { output += "{\"type\":\""; output += (isDir) ? "dir" : "file"; output += "\",\"name\":\""; - output += String(entry.name()).substring(1); + if (entry.name()[0] == '/') { + output += &(entry.name()[1]); + } else { + output += entry.name(); + } output += "\"}"; entry.close(); } @@ -205,9 +213,9 @@ void setup(void) { DBG_OUTPUT_PORT.begin(115200); DBG_OUTPUT_PORT.print("\n"); DBG_OUTPUT_PORT.setDebugOutput(true); - SPIFFS.begin(); + filesystem->begin(); { - Dir dir = SPIFFS.openDir("/"); + Dir dir = filesystem->openDir("/"); while (dir.next()) { String fileName = dir.fileName(); size_t fileSize = dir.fileSize(); diff --git a/tests/host/Makefile b/tests/host/Makefile index 8344222ef..19444e6c4 100644 --- a/tests/host/Makefile +++ b/tests/host/Makefile @@ -26,6 +26,7 @@ $(warning Cannot compile in 32 bit mode, switching to native mode) else N32 = 32 M32 = -m32 +E32 = .32 endif endif @@ -106,9 +107,9 @@ MOCK_CPP_FILES_EMU := $(MOCK_CPP_FILES_COMMON) $(addprefix common/,\ ArduinoMain.cpp \ ArduinoMainUdp.cpp \ ArduinoMainSpiffs.cpp \ + ArduinoMainLittlefs.cpp \ user_interface.cpp \ ) - #(not in tree) ArduinoMainLittlefs.cpp MOCK_C_FILES := $(addprefix common/,\ md5.c \ @@ -163,10 +164,10 @@ remduplicates = $(strip $(if $1,$(firstword $1) $(call remduplicates,$(filter-ou C_SOURCE_FILES = $(MOCK_C_FILES) $(CORE_C_FILES) CPP_SOURCE_FILES = $(MOCK_CPP_FILES) $(CORE_CPP_FILES) $(TEST_CPP_FILES) -C_OBJECTS = $(C_SOURCE_FILES:.c=.c.o) +C_OBJECTS = $(C_SOURCE_FILES:.c=.c$(E32).o) -CPP_OBJECTS_CORE = $(MOCK_CPP_FILES:.cpp=.cpp.o) $(CORE_CPP_FILES:.cpp=.cpp.o) -CPP_OBJECTS_TESTS = $(TEST_CPP_FILES:.cpp=.cpp.o) +CPP_OBJECTS_CORE = $(MOCK_CPP_FILES:.cpp=.cpp$(E32).o) $(CORE_CPP_FILES:.cpp=.cpp$(E32).o) +CPP_OBJECTS_TESTS = $(TEST_CPP_FILES:.cpp=.cpp$(E32).o) CPP_OBJECTS = $(CPP_OBJECTS_CORE) $(CPP_OBJECTS_TESTS) @@ -183,7 +184,10 @@ doCI: build-info $(OUTPUT_BINARY) valgrind test gcov test: $(OUTPUT_BINARY) # run host test for CI $(OUTPUT_BINARY) -clean: clean-objects clean-coverage # clean everything +clean: + make FORCE32=0 cleanarch; make FORCE32=1 cleanarch + +cleanarch: clean-objects clean-coverage # clean everything rm -rf $(BINDIR) clean-objects: @@ -215,11 +219,11 @@ build-info: # show toolchain version -include $(BINDIR)/.*.d .SUFFIXES: -%.c.o: %.c +%.c$(E32).o: %.c $(VERBC) $(CC) $(PREINCLUDES) $(CFLAGS) $(INC_PATHS) -MD -MF $(BINDIR)/.$(notdir $<).d -c -o $@ $< -.PRECIOUS: %.cpp.o -%.cpp.o: %.cpp +.PRECIOUS: %.cpp$(E32).o +%.cpp$(E32).o: %.cpp $(VERBCXX) $(CXX) $(PREINCLUDES) $(CXXFLAGS) $(INC_PATHS) -MD -MF $(BINDIR)/.$(notdir $<).d -c -o $@ $< $(BINDIR)/core.a: $(C_OBJECTS) $(CPP_OBJECTS_CORE) @@ -307,13 +311,13 @@ USERLIBDIRS = $(shell test -z "$(ULIBPATHS)" || for d in $(ULIBPATHS); do for dd USERLIBSRCS = $(shell test -z "$(ULIBPATHS)" || for d in $(ULIBPATHS); do for ss in $$d/*.cpp $$d/src/*.cpp; do test -r $$ss && echo $$ss; done; done) INC_PATHS += $(USERLIBDIRS) INC_PATHS += -I$(INODIR)/.. -CPP_OBJECTS_CORE_EMU = $(CPP_SOURCES_CORE_EMU:.cpp=.cpp.o) $(USERLIBSRCS:.cpp=.cpp.o) +CPP_OBJECTS_CORE_EMU = $(CPP_SOURCES_CORE_EMU:.cpp=.cpp$(E32).o) $(USERLIBSRCS:.cpp=.cpp$(E32).o) bin/fullcore.a: $(C_OBJECTS) $(CPP_OBJECTS_CORE_EMU) $(VERBAR) ar -rcu $@ $^ $(VERBAR) ranlib -c $@ -%: %.ino.cpp.o bin/fullcore.a +%: %.ino.cpp$(E32).o bin/fullcore.a $(VERBLD) $(CXX) $(LDFLAGS) $< bin/fullcore.a $(LIBSSL) -o $@ @echo "----> $@ <----" diff --git a/tests/host/common/ArduinoMainLittlefs.cpp b/tests/host/common/ArduinoMainLittlefs.cpp new file mode 100644 index 000000000..bf040a2fc --- /dev/null +++ b/tests/host/common/ArduinoMainLittlefs.cpp @@ -0,0 +1,17 @@ + +#include "littlefs_mock.h" + +LittleFSMock* littlefs_mock = nullptr; + +void mock_start_littlefs (const String& fname, size_t size_kb, size_t block_kb, size_t page_b) +{ + littlefs_mock = new LittleFSMock(size_kb * 1024, block_kb * 1024, page_b, fname); +} + +void mock_stop_littlefs () +{ + if (littlefs_mock) + delete littlefs_mock; + littlefs_mock = nullptr; +} +