Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions include/filesystem/ArduinoFileSystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include "IFileSystem.h"

#if defined(ARCH_PORTDUINO) || defined(HAS_SD_MMC)
#include "filesystem/SdCard.h"

/**
* @brief Arduino File API implementation
*
* Uses Arduino's File API (via SDFs) which provides a common interface
* for ESP32, nRF52, and Portduino platforms.
*/
class ArduinoFileSystem : public IFileSystem
{
public:
virtual ~ArduinoFileSystem() override = default;

virtual bool open(const std::string &path, const std::string &mode) override;
virtual void close() override;
virtual bool isOpen() const override;
virtual int printf(const char *format, ...) override;
virtual int write(const void *data, size_t size) override;
virtual int read(void *buffer, size_t size) override;
virtual bool readLine(char *buffer, size_t maxLen) override;
virtual bool mkdir(const std::string &path) override;
virtual std::string getLastError() const override;

private:
File currentFile;
std::string lastError;
};

#endif // ARCH_PORTDUINO || HAS_SD_MMC
87 changes: 87 additions & 0 deletions include/filesystem/IFileSystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#pragma once

#include <cstdint>
#include <memory>
#include <string>

/**
* @brief Abstract file system interface to handle platform-specific file I/O
*
* Provides a unified abstraction over different file system implementations:
* - P4 POSIX (IDF VFS mounted at /sdcard)
* - Arduino File API (ESP32-S3, nRF52, etc.)
* - SdFat library interface
*/
class IFileSystem
{
public:
virtual ~IFileSystem() = default;

/**
* @brief Open a file
* @param path File path
* @param mode "r" for read, "w" for write, "a" for append
* @return true if successful, false otherwise
*/
virtual bool open(const std::string &path, const std::string &mode) = 0;

/**
* @brief Close current file
*/
virtual void close() = 0;

/**
* @brief Check if file is open
* @return true if a file is currently open
*/
virtual bool isOpen() const = 0;

/**
* @brief Write formatted string to file (like fprintf)
* @return number of characters written, or -1 on error
*/
virtual int printf(const char *format, ...) = 0;

/**
* @brief Write raw bytes to file
* @param data Pointer to data
* @param size Number of bytes to write
* @return number of bytes written, or -1 on error
*/
virtual int write(const void *data, size_t size) = 0;

/**
* @brief Read bytes from file
* @param buffer Buffer to store data
* @param size Maximum bytes to read
* @return number of bytes read, or -1 on error
*/
virtual int read(void *buffer, size_t size) = 0;

/**
* @brief Read a line from file (up to newline)
* @param buffer Buffer to store line
* @param maxLen Maximum length including null terminator
* @return true if line was read, false on EOF or error
*/
virtual bool readLine(char *buffer, size_t maxLen) = 0;

/**
* @brief Create directory recursively
* @param path Directory path
* @return true if successful or already exists, false on error
*/
virtual bool mkdir(const std::string &path) = 0;

/**
* @brief Get last error message
* @return Error string describing last operation failure
*/
virtual std::string getLastError() const = 0;
};

/**
* @brief Get platform-appropriate file system implementation
* @return Unique pointer to IFileSystem instance
*/
std::unique_ptr<IFileSystem> createFileSystem();
30 changes: 30 additions & 0 deletions include/filesystem/PosixFileSystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include "IFileSystem.h"
#include <cstdio>

/**
* @brief POSIX file system implementation for ESP32-P4
*
* Uses POSIX file APIs (fopen, fprintf, fgets, etc.) which work with
* ESP-IDF's VFS-mounted file systems at /sdcard.
*/
class PosixFileSystem : public IFileSystem
{
public:
virtual ~PosixFileSystem() override;

virtual bool open(const std::string &path, const std::string &mode) override;
virtual void close() override;
virtual bool isOpen() const override;
virtual int printf(const char *format, ...) override;
virtual int write(const void *data, size_t size) override;
virtual int read(void *buffer, size_t size) override;
virtual bool readLine(char *buffer, size_t maxLen) override;
virtual bool mkdir(const std::string &path) override;
virtual std::string getLastError() const override;

private:
FILE *currentFile = nullptr;
std::string lastError;
};
File renamed without changes.
34 changes: 34 additions & 0 deletions include/filesystem/SdFatFileSystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include "IFileSystem.h"

#if defined(HAS_SDCARD) && !defined(HAS_SD_MMC)
#include "filesystem/SdCard.h"

/**
* @brief SdFat library implementation
*
* Uses the SdFat library's FsFile interface, typically used on
* platforms with dedicated SD card SPI connections.
*/
class SdFatFileSystem : public IFileSystem
{
public:
virtual ~SdFatFileSystem() override = default;

virtual bool open(const std::string &path, const std::string &mode) override;
virtual void close() override;
virtual bool isOpen() const override;
virtual int printf(const char *format, ...) override;
virtual int write(const void *data, size_t size) override;
virtual int read(void *buffer, size_t size) override;
virtual bool readLine(char *buffer, size_t maxLen) override;
virtual bool mkdir(const std::string &path) override;
virtual std::string getLastError() const override;

private:
FsFile currentFile;
std::string lastError;
};

#endif // HAS_SDCARD
Loading
Loading