WIP: Continued work of persistence and refactoring

This commit is contained in:
attermann committed 2023-12-11 13:42:20 -07:00
1 parent f9a679ffc2
commit 162dcff6cd
25 files changed
+1026 -315

No files matched your search

+43 -21
View File
@@ -4,7 +4,7 @@
#ifdef ARDUINO
#include <FS.h>
#include <LittleFS.h>
#include <SPIFFS.h>
#else
#include <stdio.h>
#include <sys/types.h>
@@ -15,27 +15,44 @@
using namespace RNS;
using namespace RNS::Utilities;
#ifdef ARDUINO
void listDir(char * dir){
Serial.print("DIR: ");
Serial.println(dir);
File root = SPIFFS.open(dir);
File file = root.openNextFile();
while(file){
Serial.print(" FILE: ");
Serial.println(file.name());
file = root.openNextFile();
}
}
#endif
/*static*/ void OS::setup() {
#ifdef ARDUINO
// Setup filesystem
if (!LittleFS.begin(true)){
Serial.println("LittleFS Mount Failed");
return;
}
if (!SPIFFS.begin(true, "")){
error("SPIFFS filesystem mount failed");
}
else {
listDir("/");
debug("SPIFFS filesystem is ready");
}
#endif
}
/*static*/ bool OS::file_exists(const char* file_path) {
#ifdef ARDUINO
File file = LittleFS.open(file_path, FILE_READ);
File file = SPIFFS.open(file_path, FILE_READ);
if (file) {
#else
FILE* file = fopen(file_path, "r");
if (file != nullptr) {
#endif
//RNS::extreme("file_exists: file exists, closing file");
//extreme("file_exists: file exists, closing file");
#ifdef ARDUINO
file.close();
#else
@@ -44,7 +61,7 @@ using namespace RNS::Utilities;
return true;
}
else {
RNS::extreme("file_exists: failed to open file " + std::string(file_path));
error("file_exists: failed to open file " + std::string(file_path));
return false;
}
}
@@ -52,7 +69,7 @@ using namespace RNS::Utilities;
/*static*/ const Bytes OS::read_file(const char* file_path) {
Bytes data;
#ifdef ARDUINO
File file = LittleFS.open(file_path, FILE_READ);
File file = SPIFFS.open(file_path, FILE_READ);
if (file) {
size_t size = file.size();
size_t read = file.readBytes((char*)data.writable(size), size);
@@ -65,12 +82,12 @@ using namespace RNS::Utilities;
//size_t read = fread(data.writable(size), size, 1, file);
size_t read = fread(data.writable(size), 1, size, file);
#endif
RNS::extreme("read_file: read " + std::to_string(read) + " bytes from file " + std::string(file_path));
extreme("read_file: read " + std::to_string(read) + " bytes from file " + std::string(file_path));
if (read != size) {
RNS::extreme("read_file: failed to read file " + std::string(file_path));
error("read_file: failed to read file " + std::string(file_path));
data.clear();
}
//RNS::extreme("read_file: closing input file");
//extreme("read_file: closing input file");
#ifdef ARDUINO
file.close();
#else
@@ -78,7 +95,7 @@ using namespace RNS::Utilities;
#endif
}
else {
RNS::extreme("read_file: failed to open input file " + std::string(file_path));
error("read_file: failed to open input file " + std::string(file_path));
}
return data;
}
@@ -86,7 +103,7 @@ using namespace RNS::Utilities;
/*static*/ bool OS::write_file(const Bytes& data, const char* file_path) {
bool success = false;
#ifdef ARDUINO
File file = LittleFS.open(file_path, FILE_WRITE);
File file = SPIFFS.open(file_path, FILE_WRITE);
if (file) {
size_t wrote = file.write(data.data(), data.size());
#else
@@ -95,14 +112,14 @@ using namespace RNS::Utilities;
//size_t wrote = fwrite(data.data(), data.size(), 1, file);
size_t wrote = fwrite(data.data(), 1, data.size(), file);
#endif
RNS::extreme("write_file: wrote " + std::to_string(wrote) + " bytes to file " + std::string(file_path));
extreme("write_file: wrote " + std::to_string(wrote) + " bytes to file " + std::string(file_path));
if (wrote == data.size()) {
success = true;
}
else {
RNS::extreme("write_file: failed to write file " + std::string(file_path));
error("write_file: failed to write file " + std::string(file_path));
}
//RNS::extreme("write_file: closing output file");
//extreme("write_file: closing output file");
#ifdef ARDUINO
file.close();
#else
@@ -110,14 +127,14 @@ using namespace RNS::Utilities;
#endif
}
else {
RNS::extreme("write_file: failed to open output file " + std::string(file_path));
error("write_file: failed to open output file " + std::string(file_path));
}
return success;
}
/*static*/ bool OS::remove_file(const char* file_path) {
#ifdef ARDUINO
return LittleFS.remove(file_path);
return SPIFFS.remove(file_path);
#else
return (remove(file_path) == 0);
#endif
@@ -125,7 +142,7 @@ using namespace RNS::Utilities;
/*static*/ bool OS::rename_file(const char* from_file_path, const char* to_file_path) {
#ifdef ARDUINO
return LittleFS.rename(from_file_path, to_file_path);
return SPIFFS.rename(from_file_path, to_file_path);
#else
return (rename(from_file_path, to_file_path) == 0);
#endif
@@ -133,7 +150,12 @@ using namespace RNS::Utilities;
/*static*/ bool OS::create_directory(const char* directory_path) {
#ifdef ARDUINO
return LittleFS.mkdir(directory_path);
if (SPIFFS.mkdir(directory_path)) {
return true;
}
else {
error("create_directory: failed to create directorty " + std::string(directory_path));
}
#else
struct stat st = {0};
if (stat(directory_path, &st) == 0) {