roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
littlefs.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// See https://github.com/joltwallet/esp_littlefs?tab=readme-ov-file#how-to-use
10
11#if (defined(ESP_PLATFORM) || defined(ROO_TESTING)) && \
12 __has_include("esp_littlefs.h")
13
14#include "esp_littlefs.h"
15#include "esp_task_wdt.h"
17
18#if !defined(MLOG_roo_io_fs)
19#define MLOG_roo_io_fs 0
20#endif
21
22namespace roo_io {
23
24LittlefsFs::LittlefsFs()
25 : mount_point_("/littlefs"),
28 partition_label_("") {}
29
30const char* LittlefsFs::mountPoint() const { return mount_point_.c_str(); }
31
32void LittlefsFs::setMountPoint(const char* mount_point) {
33 mount_point_ = mount_point;
34}
35
36const char* LittlefsFs::partitionLabel() const {
37 return has_partition_label_ ? partition_label_.c_str() : nullptr;
38}
39
40void LittlefsFs::setPartitionLabel(const char* partition_label) {
41 has_partition_label_ = (partition_label != nullptr);
42 partition_label_ = partition_label;
43}
44
45bool LittlefsFs::formatIfMountFailed() const { return format_if_mount_failed_; }
46
47void LittlefsFs::setFormatIfMountFailed(bool format_if_mount_failed) {
48 format_if_mount_failed_ = format_if_mount_failed;
49}
50
51MountImpl::MountResult LittlefsFs::mountImpl(std::function<void()> unmount_fn) {
52 MLOG(roo_io_fs) << "Mounting LITTLEFS";
53 std::string mount_base_path;
54#if defined(ROO_TESTING)
55 mount_base_path = FakeEsp32().fs_root();
56#endif
57 mount_base_path.append(mount_point_);
58
59 esp_vfs_littlefs_conf_t conf = {
60 .base_path = mount_base_path.c_str(),
61 .partition_label = partitionLabel(),
62 .format_if_mount_failed = format_if_mount_failed_};
63
64 esp_err_t ret = esp_vfs_littlefs_register(&conf);
65 if (ret == ESP_FAIL && format_if_mount_failed_) {
66 if (format()) {
67 ret = esp_vfs_littlefs_register(&conf);
68 }
69 }
70 if (ret != ESP_OK) {
71 LOG(ERROR) << "Mounting LITTLEFS failed! Error: " << esp_err_to_name(ret);
72 if (ret == ESP_ERR_NO_MEM) {
73 return MountImpl::MountError(kOutOfMemory);
74 } else {
75 return MountImpl::MountError(kGenericMountError);
76 }
77 }
78 return MountImpl::Mounted(std::unique_ptr<MountImpl>(
79 new PosixMountImpl(mount_base_path.c_str(), false, unmount_fn)));
80}
81
82void LittlefsFs::unmountImpl() {
83 MLOG(roo_io_fs) << "Unmounting LITTLEFS";
84 esp_err_t err = esp_vfs_littlefs_unregister(partitionLabel());
85 if (err) {
86 LOG(ERROR) << "Unmounting LITTLEFS failed! Error: " << esp_err_to_name(err);
87 return;
88 }
89 mounted_partition_label_.clear();
90}
91
92Status LittlefsFs::format() {
93#ifndef ROO_TESTING
94 esp_task_wdt_delete(xTaskGetIdleTaskHandleForCore(0));
95#endif
96 esp_err_t err = esp_littlefs_format(partition_label_.c_str());
97#ifndef ROO_TESTING
98 esp_task_wdt_add(xTaskGetIdleTaskHandleForCore(0));
99#endif
100 if (err) {
101 LOG(ERROR) << "Formatting LITTLEFS failed! Error: " << esp_err_to_name(err);
102 return kUnknownIOError;
103 }
104 return kOk;
105}
106
107Filesystem::MediaPresence LittlefsFs::checkMediaPresence() {
108 if (isMounted()) return Filesystem::kMediaPresent;
109 // Mount m = mount();
110 // if (m.ok()) return Filesystem::kMediaPresent;
111 return Filesystem::kMediaPresenceUnknown;
112}
113
114LittlefsFs CreateLittlefsFs() { return LittlefsFs(); }
115
116LittlefsFs LITTLEFS = CreateLittlefsFs();
117
118} // namespace roo_io
119
120#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