spiffs.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #ifndef KBF_SPIFFS_H
  2. #define KBF_SPIFFS_H
  3. #include <string>
  4. #include <esp_spiffs.h>
  5. using std::string;
  6. namespace kbf {
  7. /**
  8. * Filesystem information.
  9. */
  10. struct FsInfo {
  11. /** Total size in bytes. */
  12. size_t total;
  13. /** Bytes used. */
  14. size_t used;
  15. /** Bytes free. */
  16. size_t free;
  17. };
  18. /**
  19. * Serial Peripheral Interface Flash File System functions.
  20. */
  21. class SPIFFS {
  22. public:
  23. /** Tag used for logging. */
  24. static constexpr const char *TAG = "kbf::SPIFFS";
  25. /**
  26. * Creates configuration.
  27. *
  28. * @note To actually mount the partition, call this->mount().
  29. *
  30. * @param mountPoint
  31. * @param label partition label; if nullptr, the first partition with subtype=spiffs will be used
  32. */
  33. explicit SPIFFS(const string &mountPoint, const char *label = nullptr);
  34. /**
  35. * Destructor. Calls unmount().
  36. */
  37. ~SPIFFS();
  38. /**
  39. * Mounts the filesystem.
  40. *
  41. * If the mount fails, the error value is saved in this->error
  42. *
  43. * @return true on success; false on failure
  44. */
  45. bool mount();
  46. /**
  47. * Unmounts the filesystem.
  48. *
  49. * @note Called automatically from destructor.
  50. */
  51. void unmount() const;
  52. /**
  53. * Retrieves filesystem usage information.
  54. *
  55. * @return FsInfo object
  56. */
  57. [[nodiscard]] FsInfo info() const;
  58. /** error code for mount failure */
  59. esp_err_t error{};
  60. private:
  61. esp_vfs_spiffs_conf_t config;
  62. };
  63. }
  64. #endif //KBF_SPIFFS_H