diff --git a/esp3d/configuration.h b/esp3d/configuration.h index 131e4154..06dcc21f 100644 --- a/esp3d/configuration.h +++ b/esp3d/configuration.h @@ -127,6 +127,9 @@ //ESP_LITTLEFS_FILESYSTEM 2 #define FILESYSTEM_FEATURE ESP_SPIFFS_FILESYSTEM +//Allows to mount /FS and /SD under / for FTP server +#define GLOBAL_FILESYSTEM + //DIRECT_PIN_FEATURE: allow to access pin using ESP201 command #define DIRECT_PIN_FEATURE diff --git a/esp3d/src/core/commands.cpp b/esp3d/src/core/commands.cpp index f7dea6bc..49295f0d 100644 --- a/esp3d/src/core/commands.cpp +++ b/esp3d/src/core/commands.cpp @@ -564,6 +564,19 @@ bool Commands::execute_internal_command (int cmd, const char* cmd_params, level_ response = ESP750(cmd_params, auth_type, output); break; #endif //SD_DEVICE +#if defined (GLOBAL_FILESYSTEM) + //List Global Filesystem + //[ESP780] pwd= + case 780: + response = ESP780(cmd_params, auth_type, output); + break; + //Action on Global Filesystem + //rmdir / remove / mkdir / exists + //[ESP790]= pwd= + case 790: + response = ESP790(cmd_params, auth_type, output); + break; +#endif //GLOBAL_FILESYSTEM //Get fw version firmare target and fw version //eventually set time with pc time //output is JSON or plain text according parameter diff --git a/esp3d/src/core/commands.h b/esp3d/src/core/commands.h index c67b8574..0a9e56ea 100644 --- a/esp3d/src/core/commands.h +++ b/esp3d/src/core/commands.h @@ -132,6 +132,10 @@ public: bool ESP750(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); bool ESP740(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); #endif //SD_DEVICE +#if defined (GLOBAL_FILESYSTEM) + bool ESP780(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); + bool ESP790(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); +#endif //GLOBAL_FILESYSTEM bool ESP800(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); bool ESP900(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); #ifdef BUZZER_DEVICE diff --git a/esp3d/src/core/espcmd/ESP780.cpp b/esp3d/src/core/espcmd/ESP780.cpp new file mode 100644 index 00000000..bc496733 --- /dev/null +++ b/esp3d/src/core/espcmd/ESP780.cpp @@ -0,0 +1,110 @@ +/* + ESP780.cpp - ESP3D command class + + Copyright (c) 2014 Luc Lebosse. 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 "../../include/esp3d_config.h" +#if defined (GLOBAL_FILESYSTEM) +#include "../commands.h" +#include "../esp3doutput.h" +#include "../settings_esp3d.h" +#include "../../modules/authentication/authentication_service.h" +#include "../../modules/filesystem/esp_globalFS.h" +#if defined(SD_TIMESTAMP_FEATURE) || defined(FILESYSTEM_TIMESTAMP_FEATURE) +#include "../../modules/time/time_server.h" +#endif //SD_TIMESTAMP_FEATURE || FILESYSTEM_TIMESTAMP_FEATURE +//List SD Filesystem +//[ESP780] pwd= +bool Commands::ESP780(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output) +{ + bool response = true; + String parameter; + parameter = get_param (cmd_params, ""); +#ifdef AUTHENTICATION_FEATURE + if (auth_type != LEVEL_ADMIN) { + output->printERROR("Wrong authentication!", 401); + return false; + } +#else + (void)auth_type; +#endif //AUTHENTICATION_FEATURE + if (parameter.length() == 0) { + parameter = "/"; + } + uint8_t fs = ESP_GBFS::getFSType(parameter.c_str()); + output->printf("Directory on Global FS : %s", parameter.c_str()); + output->printLN(""); + if (ESP_GBFS::exists(parameter.c_str())) { + ESP_GBFile f = ESP_GBFS::open(parameter.c_str(), ESP_FILE_READ); + uint countf = 0; + uint countd = 0; + if (f) { + //Check directories + ESP_GBFile sub = f.openNextFile(); + while (sub) { + if (sub.isDirectory()) { + countd++; + output->print(" \t"); + output->print(sub.name()); + output->print(" \t"); + output->printLN(""); + } + sub.close(); + sub = f.openNextFile(); + } + f.close(); + f = ESP_GBFS::open(parameter.c_str(), ESP_FILE_READ); + //Check files + sub = f.openNextFile(); + while (sub) { + if (!sub.isDirectory()) { + countf++; + output->print(" \t"); + output->print(sub.name()); + output->print(" \t"); + output->print(ESP_GBFS::formatBytes(sub.size()).c_str()); + output->print(" \t"); +#if defined(SD_TIMESTAMP_FEATURE) || defined(FILESYSTEM_TIMESTAMP_FEATURE) + output->print(timeserver.current_time(sub.getLastWrite())); + output->print(" \t"); +#endif //SD_TIMESTAMP_FEATURE || FILESYSTEM_TIMESTAMP_FEATURE + output->printLN(""); + } + sub.close(); + sub = f.openNextFile(); + } + f.close(); + output->printf("%d file%s, %d dir%s", countf, (countf > 1)?"(s)":"", countd, (countd > 1)?"(s)":""); + output->printLN(""); + if (fs != FS_ROOT) { + String t = ESP_GBFS::formatBytes(ESP_GBFS::totalBytes(fs)); + String u = ESP_GBFS::formatBytes(ESP_GBFS::usedBytes(fs)); + String f = ESP_GBFS::formatBytes(ESP_GBFS::freeBytes(fs)); + output->printf("Total %s, Used %s, Available: %s", t.c_str(), u.c_str(),f.c_str()); + output->printLN(""); + } + } else { + output->printERROR ("Invalid directory!"); + } + } else { + output->printERROR ("Invalid directory!"); + response = false; + } + return response; +} + +#endif //GLOBAL_FILESYSTEM diff --git a/esp3d/src/core/espcmd/ESP790.cpp b/esp3d/src/core/espcmd/ESP790.cpp new file mode 100644 index 00000000..1fb23ea0 --- /dev/null +++ b/esp3d/src/core/espcmd/ESP790.cpp @@ -0,0 +1,98 @@ +/* + ESP790.cpp - ESP3D command class + + Copyright (c) 2014 Luc Lebosse. 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 "../../include/esp3d_config.h" +#if defined (GLOBAL_FILESYSTEM) +#include "../commands.h" +#include "../esp3doutput.h" +#include "../settings_esp3d.h" +#include "../../modules/authentication/authentication_service.h" +#include "../../modules/filesystem/esp_globalFS.h" +// Action on Global Filesystem +//rmdir / remove / mkdir / exists /create +//[ESP790]= pwd= +bool Commands::ESP790(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output) +{ + bool response = true; + String parameter; + parameter = get_param (cmd_params, ""); +#ifdef AUTHENTICATION_FEATURE + if (auth_type != LEVEL_ADMIN) { + output->printERROR("Wrong authentication!", 401); + return false; + } +#else + (void)auth_type; +#endif //AUTHENTICATION_FEATURE + parameter = get_param (cmd_params, "mkdir="); + if (parameter.length() != 0) { + if (ESP_GBFS::mkdir(parameter.c_str())) { + output->printMSG ("ok"); + } else { + output->printERROR ("failed!"); + response = false; + } + return response; + } + parameter = get_param (cmd_params, "rmdir="); + if (parameter.length() != 0) { + if (ESP_GBFS::rmdir(parameter.c_str())) { + output->printMSG ("ok"); + } else { + output->printERROR ("failed!"); + response = false; + } + return response; + } + parameter = get_param (cmd_params, "remove="); + if (parameter.length() != 0) { + if (ESP_GBFS::remove(parameter.c_str())) { + output->printMSG ("ok"); + } else { + output->printERROR ("failed!"); + response = false; + } + return response; + } + parameter = get_param (cmd_params, "exists="); + if (parameter.length() != 0) { + if (ESP_GBFS::exists(parameter.c_str())) { + output->printMSG ("yes"); + } else { + output->printMSG ("no"); + } + return response; + } + parameter = get_param (cmd_params, "create="); + if (parameter.length() != 0) { + ESP_GBFile f = ESP_GBFS::open(parameter.c_str(), ESP_FILE_WRITE); + if (f.isOpen()) { + f.close(); + output->printMSG ("ok"); + } else { + output->printERROR ("failed!"); + response = false; + } + return response; + } + output->printERROR ("Incorrect command!"); + return false; +} + +#endif //GLOBAL_FILESYSTEM diff --git a/esp3d/src/include/defines.h b/esp3d/src/include/defines.h index 28a3fd71..da8b624f 100644 --- a/esp3d/src/include/defines.h +++ b/esp3d/src/include/defines.h @@ -125,5 +125,7 @@ #define FS_FLASH 1 #define FS_SD 2 #define FS_USBDISK 3 +#define FS_UNKNOWN 254 +#define MAX_FS 3 #endif //_DEFINES_ESP3D_H diff --git a/esp3d/src/include/version.h b/esp3d/src/include/version.h index ed8a81ab..73918d28 100644 --- a/esp3d/src/include/version.h +++ b/esp3d/src/include/version.h @@ -22,7 +22,7 @@ #define _VERSION_ESP3D_H //version and sources location -#define FW_VERSION "3.0.0.a24" +#define FW_VERSION "3.0.0.a25" #define REPOSITORY "https://github.com/luc-github/ESP3D" #endif //_VERSION_ESP3D_H diff --git a/esp3d/src/modules/filesystem/esp_filesystem.h b/esp3d/src/modules/filesystem/esp_filesystem.h index b42eb56c..5fbe7cad 100644 --- a/esp3d/src/modules/filesystem/esp_filesystem.h +++ b/esp3d/src/modules/filesystem/esp_filesystem.h @@ -25,7 +25,7 @@ #include #endif //FILESYSTEM_TIMESTAMP_FEATURE -#define ESP_FLASH_FS_HEADER "/FS:" +#define ESP_FLASH_FS_HEADER "/FS" #define ESP_MAX_OPENHANDLE 4 @@ -87,6 +87,10 @@ public: static bool mkdir(const char *path); static bool rmdir(const char *path); static void closeAll(); + static bool started() + { + return _started; + } private: static bool _started; }; diff --git a/esp3d/src/modules/filesystem/esp_globalFS.cpp b/esp3d/src/modules/filesystem/esp_globalFS.cpp new file mode 100644 index 00000000..1b0cf0f5 --- /dev/null +++ b/esp3d/src/modules/filesystem/esp_globalFS.cpp @@ -0,0 +1,719 @@ +/* + esp_globalFS.cpp - ESP3D global FS support class + + Copyright (c) 2014 Luc Lebosse. 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 "../../include/esp3d_config.h" +#if defined(GLOBAL_FILESYSTEM) +#include "esp_globalFS.h" +#include "../../core/genLinkedList.h" + + +//to verify FS is accessible +bool ESP_GBFS::isavailable(uint8_t FS) +{ +#ifdef FILESYSTEM_FEATURE + if(FS == FS_FLASH) { + return ESP_FileSystem::started(); + } +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + if(FS == FS_SD) { + return (ESP_SD::getState(true) == ESP_SDCARD_IDLE); + } +#endif //SD_DEVICE + return false; +} + +uint8_t ESP_GBFS::_nbFS = 0; +String ESP_GBFS::_rootlist[MAX_FS]; +//constructor +ESP_GBFS::ESP_GBFS() +{ + _nbFS = 0; +} + +//destructor +ESP_GBFS::~ESP_GBFS() +{ +} + +//helper to format size to readable string +String & ESP_GBFS::formatBytes (uint64_t bytes) +{ + static String res; + if (bytes < 1024) { + res = String ((uint16_t)bytes) + " B"; + } else if (bytes < (1024 * 1024) ) { + res = String ((float)(bytes / 1024.0),2) + " KB"; + } else if (bytes < (1024 * 1024 * 1024) ) { + res = String ((float)(bytes / 1024.0 / 1024.0),2) + " MB"; + } else { + res = String ((float)(bytes / 1024.0 / 1024.0 / 1024.0),2) + " GB"; + } + return res; +} + +//depending FS +uint64_t ESP_GBFS::totalBytes(uint8_t FS) +{ +#ifdef FILESYSTEM_FEATURE + if(FS == FS_FLASH) { + return ESP_FileSystem::totalBytes(); + } +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + if(FS == FS_SD) { + return ESP_SD::totalBytes(); + } +#endif //SD_DEVICE + return 0; +} + +uint64_t ESP_GBFS::usedBytes(uint8_t FS) +{ +#ifdef FILESYSTEM_FEATURE + if(FS == FS_FLASH) { + return ESP_FileSystem::usedBytes(); + } +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + if(FS == FS_SD) { + return ESP_SD::usedBytes(); + } +#endif //SD_DEVICE + return 0; +} + +uint64_t ESP_GBFS::freeBytes(uint8_t FS) +{ +#ifdef FILESYSTEM_FEATURE + if(FS == FS_FLASH) { + return ESP_FileSystem::freeBytes(); + } +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + if(FS == FS_SD) { + return ESP_SD::freeBytes(); + } +#endif //SD_DEVICE + return 0; +} + +//Format is not always available for all FS +bool format(uint8_t FS, ESP3DOutput * output = nullptr) +{ +#ifdef FILESYSTEM_FEATURE + if(FS == FS_FLASH) { + return ESP_FileSystem::format(); + } +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + if(FS == FS_SD) { + return ESP_SD::format(output); + } +#endif //SD_DEVICE + output->printERROR("Not available"); + return false; +} + +//check type of FS according root dir +uint8_t ESP_GBFS::getFSType(const char * path) +{ + String p = path; + p.trim(); + if (p == "/") { + return FS_ROOT; + } +#if defined (FILESYSTEM_FEATURE) + if (p.startsWith(ESP_FLASH_FS_HEADER)) { + return FS_FLASH; + } +#endif //FILESYSTEM_FEATURE +#if defined (SD_DEVICE) + if (p.startsWith(ESP_SD_FS_HEADER)) { + return FS_SD; + } +#endif //SD_DEVICE + return FS_UNKNOWN; +} + +const char * ESP_GBFS::getRealPath(const char * path) +{ + static String p; + uint8_t t = getFSType(path); + p = ""; +#if defined (FILESYSTEM_FEATURE) + if (t == FS_FLASH) { + p = path; + //remove header + p.remove(0,strlen(ESP_FLASH_FS_HEADER)); + //if nothing it is root + if (p.length() == 0) { + p = "/"; + } + } +#endif //FILESYSTEM_FEATURE +#if defined (SD_DEVICE) + if (t == FS_SD) { + p = path; + //remove header + p.remove(0,strlen(ESP_SD_FS_HEADER)); + //if nothing it is root + if (p.length() == 0) { + p = "/"; + } + } +#endif //SD_DEVICE + return p.c_str(); +} + +//path exists on / or SD or FS +bool ESP_GBFS::exists(const char* path) +{ + +#if defined (FILESYSTEM_FEATURE) || defined(SD_DEVICE) + uint8_t t = getFSType(path); + if (t == FS_ROOT) { + return true; + } +#if defined (FILESYSTEM_FEATURE) + if (t == FS_FLASH) { + return ESP_FileSystem::exists(getRealPath(path)); + } +#endif //FILESYSTEM_FEATURE +#if defined (SD_DEVICE) + if (t == FS_SD) { + return ESP_SD::exists(getRealPath(path)); + } +#endif //SD_DEVICE +#endif // FILESYSTEM_FEATURE || SD_DEVICE + return false; +} + +bool ESP_GBFS::remove(const char *path) +{ +#if defined (FILESYSTEM_FEATURE) || defined(SD_DEVICE) + uint8_t t = getFSType(path); + if (t == FS_ROOT) { + return false; + } +#if defined (FILESYSTEM_FEATURE) + if (t == FS_FLASH) { + return ESP_FileSystem::remove(getRealPath(path)); + } +#endif //FILESYSTEM_FEATURE +#if defined (SD_DEVICE) + if (t == FS_SD) { + return ESP_SD::remove(getRealPath(path)); + } +#endif //SD_DEVICE +#endif // FILESYSTEM_FEATURE || SD_DEVICE + return false; +} + +bool ESP_GBFS::mkdir(const char *path) +{ +#if defined (FILESYSTEM_FEATURE) || defined(SD_DEVICE) + uint8_t t = getFSType(path); + if (t == FS_ROOT) { + return false; + } +#if defined (FILESYSTEM_FEATURE) + if (t == FS_FLASH) { + return ESP_FileSystem::mkdir(getRealPath(path)); + } +#endif //FILESYSTEM_FEATURE +#if defined (SD_DEVICE) + if (t == FS_SD) { + return ESP_SD::mkdir(getRealPath(path)); + } +#endif //SD_DEVICE +#endif // FILESYSTEM_FEATURE || SD_DEVICE + return false; +} + +bool ESP_GBFS::rmdir(const char *path) +{ +#if defined (FILESYSTEM_FEATURE) || defined(SD_DEVICE) + uint8_t t = getFSType(path); + if (t == FS_ROOT) { + return false; + } +#if defined (FILESYSTEM_FEATURE) + if (t == FS_FLASH) { + return ESP_FileSystem::rmdir(getRealPath(path)); + } +#endif //FILESYSTEM_FEATURE +#if defined (SD_DEVICE) + if (t == FS_SD) { + return ESP_SD::rmdir(getRealPath(path)); + } +#endif //SD_DEVICE +#endif // FILESYSTEM_FEATURE || SD_DEVICE + return false; +} + +void ESP_GBFS::closeAll() +{ + getNextFS(true); +#if defined (FILESYSTEM_FEATURE) + ESP_FileSystem::closeAll(); +#endif //FILESYSTEM_FEATURE +#if defined (SD_DEVICE) + ESP_SD::closeAll(); +#endif //SD_DEVICE +} + +ESP_GBFile ESP_GBFS::open(const char* path, uint8_t mode) +{ + ESP_GBFile f; +#if defined (FILESYSTEM_FEATURE) || defined(SD_DEVICE) + uint8_t t = getFSType(path); + if ((t == FS_ROOT) && (mode == ESP_FILE_READ)) { + f = ESP_GBFile(FS_ROOT); + //reset index; + getNextFS(true); + } +#if defined (FILESYSTEM_FEATURE) + if (t == FS_FLASH) { + f = ESP_FileSystem::open(getRealPath(path), mode); + } +#endif //FILESYSTEM_FEATURE +#if defined (SD_DEVICE) + if (t == FS_SD) { + f = ESP_SD::open(getRealPath(path), mode); + } +#endif //SD_DEVICE +#endif // FILESYSTEM_FEATURE || SD_DEVICE + return f; +} + + +//Default File is no file +ESP_GBFile::ESP_GBFile() +{ + _type = FS_UNKNOWN; +} + +//File handle for the root +ESP_GBFile::ESP_GBFile(uint8_t FS) +{ + _type = FS; +} + +//Handle for flash file +#ifdef FILESYSTEM_FEATURE +ESP_GBFile::ESP_GBFile(ESP_File & flashFile) +{ + _type = FS_FLASH; + _flashFile = flashFile; +#ifdef SD_DEVICE + _sdFile = ESP_SDFile(); +#endif //SD_DEVICE +} +#endif //FILESYSTEM_FEATURE + +//Handle for SD file +#ifdef SD_DEVICE +ESP_GBFile::ESP_GBFile(ESP_SDFile & sdFile) +{ + _type = FS_SD; + _sdFile = sdFile; +#ifdef FILESYSTEM_FEATURE + _flashFile = ESP_File(); +#endif //FILESYSTEM_FEATURE +} +#endif //SD_DEVICE + +//Destructor +ESP_GBFile::~ESP_GBFile() +{ +} + +ESP_GBFile::operator bool() const +{ +#if defined(FILESYSTEM_FEATURE) || defined(SD_DEVICE) + if (_type == FS_ROOT) { + return true; + } +#endif //FILESYSTEM_FEATURE || SD_DEVICE +#ifdef FILESYSTEM_FEATURE + if (_type == FS_FLASH) { + return _flashFile; + } +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + if (_type == FS_SD) { + return _sdFile; + } +#endif //SD_DEVICE + return false; +} + +void ESP_GBFile::close() +{ +#if defined(FILESYSTEM_FEATURE) || defined(SD_DEVICE) + if (_type == FS_ROOT) { + //TBD + } +#endif //FILESYSTEM_FEATURE || SD_DEVICE +#ifdef FILESYSTEM_FEATURE + if (_type == FS_FLASH) { + _flashFile.close(); + } +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + if (_type == FS_SD) { + return _sdFile.close(); + } +#endif //SD_DEVICE +} + +bool ESP_GBFile::isOpen() +{ +#if defined(FILESYSTEM_FEATURE) || defined(SD_DEVICE) + if (_type == FS_ROOT) { + return true; + } +#endif //FILESYSTEM_FEATURE || SD_DEVICE +#ifdef FILESYSTEM_FEATURE + if (_type == FS_FLASH) { + return _flashFile.isOpen(); + } +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + if (_type == FS_SD) { + return _sdFile.isOpen(); + } +#endif //SD_DEVICE + return false; +} + +const char* ESP_GBFile::name() const +{ + static String s; +#if defined(FILESYSTEM_FEATURE) || defined(SD_DEVICE) + if (_type == FS_ROOT) { + return "/"; + } +#endif //FILESYSTEM_FEATURE || SD_DEVICE +#ifdef FILESYSTEM_FEATURE + if (_type == FS_FLASH) { + if (strcmp(_flashFile.name(), "/") == 0) { + s = ESP_FLASH_FS_HEADER; + s.remove(0,1); + return s.c_str(); + } + return _flashFile.name(); + } +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + if (_type == FS_SD) { + if (strcmp(_sdFile.name(), "/") == 0) { + s = ESP_SD_FS_HEADER; + s.remove(0,1); + return s.c_str(); + } + return _sdFile.name(); + } +#endif //SD_DEVICE + return ""; +} + +const char* ESP_GBFile::filename() const +{ +#if defined(FILESYSTEM_FEATURE) || defined(SD_DEVICE) + static String s; + if (_type == FS_ROOT) { + return "/"; + } +#endif //FILESYSTEM_FEATURE || SD_DEVICE +#ifdef FILESYSTEM_FEATURE + if (_type == FS_FLASH) { + s = ESP_FLASH_FS_HEADER; + s += _flashFile.filename(); + return s.c_str(); + } +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + if (_type == FS_SD) { + s = ESP_SD_FS_HEADER; + s += s += _sdFile.filename(); + return s.c_str(); + } +#endif //SD_DEVICE + return ""; +} + +bool ESP_GBFile::isDirectory() +{ +#if defined(FILESYSTEM_FEATURE) || defined(SD_DEVICE) + if (_type == FS_ROOT) { + return true; + } +#endif //FILESYSTEM_FEATURE || SD_DEVICE +#ifdef FILESYSTEM_FEATURE + if (_type == FS_FLASH) { + return _flashFile.isDirectory(); + } +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + if (_type == FS_SD) { + return _sdFile.isDirectory(); + } +#endif //SD_DEVICE + return false; +} + +size_t ESP_GBFile::size() +{ +#if defined(FILESYSTEM_FEATURE) || defined(SD_DEVICE) + if (_type == FS_ROOT) { + return 0; + } +#endif //FILESYSTEM_FEATURE || SD_DEVICE +#ifdef FILESYSTEM_FEATURE + if (_type == FS_FLASH) { + return _flashFile.size(); + } +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + if (_type == FS_SD) { + return _sdFile.size(); + } +#endif //SD_DEVICE + return 0; +} + +#if defined (SD_TIMESTAMP_FEATURE) || defined(FILESYSTEM_TIMESTAMP_FEATURE) +time_t ESP_GBFile::getLastWrite() +{ +#if defined(FILESYSTEM_FEATURE) && defined(FILESYSTEM_TIMESTAMP_FEATURE) + if (_type == FS_FLASH) { + return _flashFile.getLastWrite(); + } +#endif //FILESYSTEM_FEATURE && FILESYSTEM_TIMESTAMP_FEATURE +#if defined(SD_DEVICE) && defined(SD_TIMESTAMP_FEATURE) + if (_type == FS_SD) { + return _sdFile.getLastWrite(); + } +#endif //SD_DEVICE && SD_TIMESTAMP_FEATURE + return 0; +} +#endif //SD_TIMESTAMP_FEATURE || FILESYSTEM_TIMESTAMP_FEATURE + +int ESP_GBFile::available() +{ +#if defined(FILESYSTEM_FEATURE) || defined(SD_DEVICE) + if (_type == FS_ROOT) { + return 0; + } +#endif //FILESYSTEM_FEATURE || SD_DEVICE +#ifdef FILESYSTEM_FEATURE + if (_type == FS_FLASH) { + return _flashFile.available(); + } +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + if (_type == FS_SD) { + return _sdFile.available(); + } +#endif //SD_DEVICE + return 0; +} + +size_t ESP_GBFile::write(uint8_t i) +{ +#if defined(FILESYSTEM_FEATURE) || defined(SD_DEVICE) + if (_type == FS_ROOT) { + return 0; + } +#endif //FILESYSTEM_FEATURE || SD_DEVICE +#ifdef FILESYSTEM_FEATURE + if (_type == FS_FLASH) { + return _flashFile.write(i); + } +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + if (_type == FS_SD) { + return _sdFile.write(i); + } +#endif //SD_DEVICE + return 0; +} + +size_t ESP_GBFile::write(const uint8_t *buf, size_t size) +{ +#if defined(FILESYSTEM_FEATURE) || defined(SD_DEVICE) + if (_type == FS_ROOT) { + return 0; + } +#endif //FILESYSTEM_FEATURE || SD_DEVICE +#ifdef FILESYSTEM_FEATURE + if (_type == FS_FLASH) { + return _flashFile.write(buf, size); + } +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + if (_type == FS_SD) { + return _sdFile.write(buf, size); + } +#endif //SD_DEVICE + return 0; +} + +int ESP_GBFile::read() +{ +#if defined(FILESYSTEM_FEATURE) || defined(SD_DEVICE) + if (_type == FS_ROOT) { + return -1; + } +#endif //FILESYSTEM_FEATURE || SD_DEVICE +#ifdef FILESYSTEM_FEATURE + if (_type == FS_FLASH) { + return _flashFile.read(); + } +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + if (_type == FS_SD) { + return _sdFile.read(); + } +#endif //SD_DEVICE + return -1; +} + +size_t ESP_GBFile::read(uint8_t* buf, size_t size) +{ +#if defined(FILESYSTEM_FEATURE) || defined(SD_DEVICE) + if (_type == FS_ROOT) { + return -1; + } +#endif //FILESYSTEM_FEATURE || SD_DEVICE +#ifdef FILESYSTEM_FEATURE + if (_type == FS_FLASH) { + return _flashFile.read(buf, size); + } +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + if (_type == FS_SD) { + return _sdFile.read(buf, size); + } +#endif //SD_DEVICE + return -1; +} + +void ESP_GBFile::flush() +{ +#ifdef FILESYSTEM_FEATURE + if (_type == FS_FLASH) { + return _flashFile.flush(); + } +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + if (_type == FS_SD) { + return _sdFile.flush(); + } +#endif //SD_DEVICE + return ; +} + +ESP_GBFile& ESP_GBFile::operator=(const ESP_GBFile & other) +{ +#ifdef FILESYSTEM_FEATURE + _flashFile = other._flashFile; +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + _sdFile = other._sdFile; +#endif //SD_DEVICE + _type = other._type; + return *this; +} + +#ifdef FILESYSTEM_FEATURE +ESP_GBFile & ESP_GBFile::operator=(const ESP_File & other) +{ + _flashFile = other; +#ifdef SD_DEVICE + _sdFile = ESP_SDFile(); +#endif //SD_DEVICE + _type = FS_FLASH; + return *this; +} +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE +ESP_GBFile & ESP_GBFile::operator=(const ESP_SDFile & other) +{ +#ifdef FILESYSTEM_FEATURE + _flashFile = ESP_File(); +#endif //FILESYSTEM_FEATURE + _sdFile = other; + _type = FS_SD; + return *this; +} +#endif //SD_DEVICE + +const char * ESP_GBFS::getNextFS(bool reset) +{ + static uint8_t index = 0; + if (reset) { + index = 0; + if(_nbFS == 0) { +#ifdef FILESYSTEM_FEATURE + _rootlist[_nbFS] = ESP_FLASH_FS_HEADER; + _nbFS++; +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + _rootlist[_nbFS] = ESP_SD_FS_HEADER; + _nbFS++; +#endif //SD_DEVICE + } + return ""; + } + if (index < _nbFS) { + uint8_t i = index; + index++; + return _rootlist[i].c_str(); + } + return ""; +} + +ESP_GBFile ESP_GBFile::openNextFile() +{ + ESP_GBFile f; + if (_type == FS_ROOT) { + String path = ESP_GBFS::getNextFS(); + if (path.length() > 0) { + f = ESP_GBFS::open(path.c_str()); + f.close(); + } + } +#ifdef FILESYSTEM_FEATURE + if (_type == FS_FLASH) { + f = _flashFile.openNextFile(); + } +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + if (_type == FS_SD) { + f = _sdFile.openNextFile(); + } +#endif //SD_DEVICE + return f; +} + +#endif //GLOBAL_FILESYSTEM diff --git a/esp3d/src/modules/filesystem/esp_globalFS.h b/esp3d/src/modules/filesystem/esp_globalFS.h new file mode 100644 index 00000000..2c83b547 --- /dev/null +++ b/esp3d/src/modules/filesystem/esp_globalFS.h @@ -0,0 +1,110 @@ +/* + esp_globalFS.h - ESP3D FS support class + + Copyright (c) 2014 Luc Lebosse. 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 _ESP_GLOBAL_FS_H +#define _ESP_GLOBAL_FS_H +#include "../../include/esp3d_config.h" +#include "../../core/esp3doutput.h" +#if defined(SD_TIMESTAMP_FEATURE) || defined(FILESYSTEM_TIMESTAMP_FEATURE) +#include +#endif //SD_TIMESTAMP_FEATURE || FILESYSTEM_TIMESTAMP_FEATURE +#ifdef FILESYSTEM_FEATURE +#include "esp_filesystem.h" +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE +#include "esp_sd.h" +#endif //SD_DEVICE + + +class ESP_GBFile +{ +public: + ESP_GBFile(); + ESP_GBFile(uint8_t FS); +#ifdef FILESYSTEM_FEATURE + ESP_GBFile(ESP_File & flashFile); +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + ESP_GBFile(ESP_SDFile & sdFile); +#endif //SD_DEVICE + ~ESP_GBFile(); + operator bool() const; + bool isDirectory(); + const char* name() const; + const char* shortname() const; + const char* filename() const; + void close(); + bool isOpen(); + ESP_GBFile & operator=(const ESP_GBFile & other); +#ifdef FILESYSTEM_FEATURE + ESP_GBFile & operator=(const ESP_File & other); +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + ESP_GBFile & operator=(const ESP_SDFile & other); +#endif //SD_DEVICE + size_t size(); +#if defined (SD_TIMESTAMP_FEATURE) || defined(FILESYSTEM_TIMESTAMP_FEATURE) + time_t getLastWrite(); +#endif //SD_TIMESTAMP_FEATURE || FILESYSTEM_TIMESTAMP_FEATURE + int available(); + size_t write(uint8_t i); + size_t write(const uint8_t *buf, size_t size); + int read(); + size_t read(uint8_t* buf, size_t size); + void flush(); + ESP_GBFile openNextFile(); +private: + +#ifdef FILESYSTEM_FEATURE + ESP_File _flashFile; +#endif //FILESYSTEM_FEATURE +#ifdef SD_DEVICE + ESP_SDFile _sdFile; +#endif //SD_DEVICE + uint8_t _type; +}; + +class ESP_GBFS +{ +public: + ESP_GBFS(); + ~ESP_GBFS(); + static bool isavailable(uint8_t FS); + static uint64_t totalBytes(uint8_t FS); + static uint64_t usedBytes(uint8_t FS); + static uint64_t freeBytes(uint8_t FS); + static bool format(uint8_t FS, ESP3DOutput * output = nullptr); + static ESP_GBFile open(const char* path, uint8_t mode = ESP_FILE_READ); + static bool exists(const char* path); + static bool remove(const char *path); + static bool mkdir(const char *path); + static bool rmdir(const char *path); + static void closeAll(); + static String & formatBytes (uint64_t bytes); + static const char * getNextFS(bool reset = false); + static uint8_t getFSType(const char * path); +private: + static const char * getRealPath(const char * path); + static uint8_t _nbFS; + static String _rootlist[MAX_FS]; +}; + + +#endif //_ESP_GLOBAL_FS_H diff --git a/esp3d/src/modules/filesystem/esp_sd.h b/esp3d/src/modules/filesystem/esp_sd.h index c22b01ff..67ab3c15 100644 --- a/esp3d/src/modules/filesystem/esp_sd.h +++ b/esp3d/src/modules/filesystem/esp_sd.h @@ -26,7 +26,7 @@ #include #endif //SD_TIMESTAMP_FEATURE -#define ESP_SD_HEADER "/SD:" +#define ESP_SD_FS_HEADER "/SD" #define ESP_MAX_SD_OPENHANDLE 4 diff --git a/esp3d/src/modules/filesystem/sd/sd_native_esp32.cpp b/esp3d/src/modules/filesystem/sd/sd_native_esp32.cpp index ed6e28e8..521d4039 100644 --- a/esp3d/src/modules/filesystem/sd/sd_native_esp32.cpp +++ b/esp3d/src/modules/filesystem/sd/sd_native_esp32.cpp @@ -239,6 +239,9 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode, const char * if (pos != -1) { _name.remove( 0, pos+1); } + if (_name.length() == 0) { + _name = "/"; + } //size _size = tSDFile_handle[i].size(); //time diff --git a/esp3d/src/modules/filesystem/sd/sd_native_esp8266.cpp b/esp3d/src/modules/filesystem/sd/sd_native_esp8266.cpp index cb4969fa..3a20d284 100644 --- a/esp3d/src/modules/filesystem/sd/sd_native_esp8266.cpp +++ b/esp3d/src/modules/filesystem/sd/sd_native_esp8266.cpp @@ -726,6 +726,9 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode, const char * if (pos != -1) { _name.remove( 0, pos+1); } + if (_name.length() == 0) { + _name = "/"; + } //size _size = tSDFile_handle[i].size(); //time diff --git a/esp3d/src/modules/filesystem/sd/sd_sdfat_esp32.cpp b/esp3d/src/modules/filesystem/sd/sd_sdfat_esp32.cpp index abe72c67..b8b8ad84 100644 --- a/esp3d/src/modules/filesystem/sd/sd_sdfat_esp32.cpp +++ b/esp3d/src/modules/filesystem/sd/sd_sdfat_esp32.cpp @@ -725,6 +725,9 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode, const char * if (pos != -1) { _name.remove( 0, pos+1); } + if (_name.length() == 0) { + _name = "/"; + } //size _size = tSDFile_handle[i].size(); //time diff --git a/esp3d/src/modules/filesystem/sd/sdio_esp32.cpp b/esp3d/src/modules/filesystem/sd/sdio_esp32.cpp index 376d3e56..699bda0f 100644 --- a/esp3d/src/modules/filesystem/sd/sdio_esp32.cpp +++ b/esp3d/src/modules/filesystem/sd/sdio_esp32.cpp @@ -228,6 +228,9 @@ ESP_SDFile::ESP_SDFile(void* handle, bool isdir, bool iswritemode, const char * if (pos != -1) { _name.remove( 0, pos+1); } + if (_name.length() == 0) { + _name = "/"; + } //size _size = tSDFile_handle[i].size(); //time