roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
spiffs.cpp
Go to the documentation of this file.
2
3#if defined(ROO_TESTING)
4
5#include "roo_testing/microcontrollers/esp32/fake_esp32.h"
6
7#endif
8
9#if (defined(ESP_PLATFORM) || defined(ROO_TESTING)) && \
10 __has_include("esp_spiffs.h")
11
12#include "esp_spiffs.h"
13#include "esp_task_wdt.h"
15
16#if !defined(MLOG_roo_io_fs)
17#define MLOG_roo_io_fs 0
18#endif
19
20namespace roo_io {
21
22SpiffsFs::SpiffsFs()
23 : mount_point_("/spiffs"),
27 partition_label_("") {}
28
29const char* SpiffsFs::mountPoint() const { return mount_point_.c_str(); }
30
31void SpiffsFs::setMountPoint(const char* mount_point) {
32 mount_point_ = mount_point;
33}
34
35const char* SpiffsFs::partitionLabel() const {
36 return has_partition_label_ ? partition_label_.c_str() : nullptr;
37}
38
39void SpiffsFs::setPartitionLabel(const char* partition_label) {
40 has_partition_label_ = (partition_label != nullptr);
41 partition_label_ = partition_label;
42}
43
44uint8_t SpiffsFs::maxOpenFiles() const { return max_open_files_; }
45
46void SpiffsFs::setMaxOpenFiles(uint8_t max_open_files) {
47 max_open_files_ = max_open_files;
48}
49
50bool SpiffsFs::formatIfMountFailed() const { return format_if_mount_failed_; }
51
52void SpiffsFs::setFormatIfMountFailed(bool format_if_mount_failed) {
53 format_if_mount_failed_ = format_if_mount_failed;
54}
55
56MountImpl::MountResult SpiffsFs::mountImpl(std::function<void()> unmount_fn) {
57 MLOG(roo_io_fs) << "Mounting SPIFFS";
58 std::string mount_base_path;
59#if defined(ROO_TESTING)
60 mount_base_path = FakeEsp32().fs_root();
61#endif
62 mount_base_path.append(mount_point_);
63
64 esp_vfs_spiffs_conf_t conf = {
65 .base_path = mount_base_path.c_str(),
66 .partition_label = partitionLabel(),
67 .max_files = max_open_files_,
68 .format_if_mount_failed = format_if_mount_failed_};
69
70 esp_err_t ret = esp_vfs_spiffs_register(&conf);
71 if (ret == ESP_FAIL && format_if_mount_failed_) {
72 if (format()) {
73 ret = esp_vfs_spiffs_register(&conf);
74 }
75 }
76 if (ret != ESP_OK) {
77 LOG(ERROR) << "Mounting SPIFFS failed! Error: " << esp_err_to_name(ret);
78 if (ret == ESP_ERR_NO_MEM) {
79 return MountImpl::MountError(kOutOfMemory);
80 } else {
81 return MountImpl::MountError(kGenericMountError);
82 }
83 }
84 return MountImpl::Mounted(std::unique_ptr<MountImpl>(
85 new PosixMountImpl(mount_base_path.c_str(), false, unmount_fn)));
86}
87
88void SpiffsFs::unmountImpl() {
89 MLOG(roo_io_fs) << "Unmounting SPIFFS";
90 esp_err_t err = esp_vfs_spiffs_unregister(partitionLabel());
91 if (err) {
92 LOG(ERROR) << "Unmounting SPIFFS failed! Error: " << esp_err_to_name(err);
93 return;
94 }
95 mounted_partition_label_.clear();
96}
97
98Status SpiffsFs::format() {
99#ifndef ROO_TESTING
100 esp_task_wdt_delete(xTaskGetIdleTaskHandleForCore(0));
101#endif
102 esp_err_t err = esp_spiffs_format(partition_label_.c_str());
103#ifndef ROO_TESTING
104 esp_task_wdt_add(xTaskGetIdleTaskHandleForCore(0));
105#endif
106 if (err) {
107 LOG(ERROR) << "Formatting SpiffsFs failed! Error: " << esp_err_to_name(err);
108 return kUnknownIOError;
109 }
110 return kOk;
111}
112
113Filesystem::MediaPresence SpiffsFs::checkMediaPresence() {
114 if (isMounted()) return Filesystem::kMediaPresent;
115 // Mount m = mount();
116 // if (m.ok()) return Filesystem::kMediaPresent;
117 return Filesystem::kMediaPresenceUnknown;
118}
119
120SpiffsFs CreateSpiffsFs() { return SpiffsFs(); }
121
122SpiffsFs SPIFFS = CreateSpiffsFs();
123
124} // namespace roo_io
125
126#endif
Definition byte.h:6
roo::basic_string_view< CharT, Traits > basic_string_view
Definition string_view.h:8
Status
Definition status.h:7
@ kUnknownIOError
Definition status.h:58
@ kOk
Definition status.h:8