3#if defined(ROO_TESTING)
5#include "roo_testing/microcontrollers/esp32/fake_esp32.h"
11#if (defined(ESP_PLATFORM) || defined(ROO_TESTING)) && \
12 __has_include("esp_littlefs.h")
14#include "esp_littlefs.h"
15#include "esp_task_wdt.h"
18#if !defined(MLOG_roo_io_fs)
19#define MLOG_roo_io_fs 0
24LittlefsFs::LittlefsFs()
30const char* LittlefsFs::mountPoint()
const {
return mount_point_.c_str(); }
32void LittlefsFs::setMountPoint(
const char* mount_point) {
33 mount_point_ = mount_point;
36const char* LittlefsFs::partitionLabel()
const {
37 return has_partition_label_ ? partition_label_.c_str() :
nullptr;
40void LittlefsFs::setPartitionLabel(
const char* partition_label) {
41 has_partition_label_ = (partition_label !=
nullptr);
42 partition_label_ = partition_label;
45bool LittlefsFs::formatIfMountFailed()
const {
return format_if_mount_failed_; }
47void LittlefsFs::setFormatIfMountFailed(
bool format_if_mount_failed) {
48 format_if_mount_failed_ = format_if_mount_failed;
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();
57 mount_base_path.append(mount_point_);
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_};
64 esp_err_t ret = esp_vfs_littlefs_register(&conf);
65 if (ret == ESP_FAIL && format_if_mount_failed_) {
67 ret = esp_vfs_littlefs_register(&conf);
71 LOG(ERROR) <<
"Mounting LITTLEFS failed! Error: " << esp_err_to_name(ret);
72 if (ret == ESP_ERR_NO_MEM) {
73 return MountImpl::MountError(kOutOfMemory);
75 return MountImpl::MountError(kGenericMountError);
78 return MountImpl::Mounted(std::unique_ptr<MountImpl>(
79 new PosixMountImpl(mount_base_path.c_str(),
false, unmount_fn)));
82void LittlefsFs::unmountImpl() {
83 MLOG(roo_io_fs) <<
"Unmounting LITTLEFS";
84 esp_err_t err = esp_vfs_littlefs_unregister(partitionLabel());
86 LOG(ERROR) <<
"Unmounting LITTLEFS failed! Error: " << esp_err_to_name(err);
89 mounted_partition_label_.clear();
92Status LittlefsFs::format() {
94 esp_task_wdt_delete(xTaskGetIdleTaskHandleForCore(0));
96 esp_err_t err = esp_littlefs_format(partition_label_.c_str());
98 esp_task_wdt_add(xTaskGetIdleTaskHandleForCore(0));
101 LOG(ERROR) <<
"Formatting LITTLEFS failed! Error: " << esp_err_to_name(err);
107Filesystem::MediaPresence LittlefsFs::checkMediaPresence() {
108 if (isMounted())
return Filesystem::kMediaPresent;
111 return Filesystem::kMediaPresenceUnknown;
114LittlefsFs CreateLittlefsFs() {
return LittlefsFs(); }
116LittlefsFs LITTLEFS = CreateLittlefsFs();
roo::basic_string_view< CharT, Traits > basic_string_view