Skip to content

Commit 10b004f

Browse files
committed
Add initialization check and waiting duration for filesystem access
1 parent 61ada78 commit 10b004f

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

lib/board_adapters/storage/storage.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,24 @@ static std::atomic<bool> requestFileSystemActive;
3232
static std::thread stateMachine;
3333
static std::mutex fileSystemState_mutex; //!< used to lock the actual state
3434
static bool fileSystemIsActive; //!< describes the current state
35+
static bool storageIsInitialized = false;
3536

36-
std::shared_ptr<fs::FS> Storage::getFileSystem_locking()
37+
std::optional<std::shared_ptr<fs::FS>> Storage::getFileSystem_locking(const std::chrono::milliseconds rel_time)
3738
{
39+
if (!storageIsInitialized)
40+
{
41+
return std::nullopt;
42+
}
3843
std::unique_lock fs_state_lock{fileSystemState_mutex};
3944
if (!fileSystemIsActive)
4045
{
41-
stateChanged.wait(fs_state_lock, []() { return fileSystemIsActive; });
46+
if (!stateChanged.wait_for(fs_state_lock, rel_time, []() { return fileSystemIsActive; }))
47+
{
48+
return std::nullopt;
49+
}
4250
}
4351
fs_state_lock.release();
44-
return {&FFat, [](fs::FS *) { fileSystemState_mutex.unlock(); }};
52+
return std::shared_ptr<fs::FS>{&FFat, [](fs::FS *) { fileSystemState_mutex.unlock(); }};
4553
}
4654

4755
static void requestState(const bool fileSystemActive)
@@ -116,8 +124,12 @@ static bool usbMsc_onStartStop(const std::uint8_t power_condition, const bool st
116124
return true;
117125
}
118126

119-
std::size_t Storage::size()
127+
std::optional<std::size_t> Storage::size()
120128
{
129+
if (!storageIsInitialized)
130+
{
131+
return std::nullopt;
132+
}
121133
return FFat.totalBytes();
122134
}
123135

@@ -137,21 +149,20 @@ static void usbStartedCallback(void *, esp_event_base_t, int32_t, void *)
137149
requestState(false);
138150
}
139151

140-
void Storage::begin()
152+
bool Storage::begin()
141153
{
142-
143154
if (!FFat.begin(true))
144155
{
145156
ESP_LOGE(TAG, "Failed to init files system, flash may not be formatted");
146-
return;
157+
return storageIsInitialized = false;
147158
}
148159
ESP_LOGI(TAG, "file system initialized");
149160

150161
partition = check_ffat_partition(FFAT_PARTITION_LABEL);
151162
if (!partition)
152163
{
153164
ESP_LOGE(TAG, "Error with FAT partition");
154-
return;
165+
return storageIsInitialized = false;
155166
}
156167
ESP_LOGI(TAG, "Flash has a size of %u bytes\n", FFat.totalBytes());
157168

@@ -175,11 +186,14 @@ void Storage::begin()
175186
if (!usbMsc.begin(FFat.totalBytes() / blockSize, blockSize))
176187
{
177188
ESP_LOGE(TAG, "starting USB MSC failed");
189+
return storageIsInitialized = false;
178190
}
179191
if (!USB.begin())
180192
{
181193
ESP_LOGE(TAG, "starting USB failed");
194+
return storageIsInitialized = false;
182195
}
196+
return storageIsInitialized = true;
183197
}
184198

185199
void Storage::end()

lib/board_adapters/storage/storage.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma once
2+
#include <chrono>
23
#include <cstddef>
34
#include <memory>
5+
#include <optional>
46

57
namespace fs
68
{
@@ -34,8 +36,9 @@ struct Storage
3436
*
3537
* @post storage may be used afterwards
3638
* @warning do not call again unless end() was called after before
39+
* @retval true in case initialization was successful
3740
*/
38-
static void begin();
41+
static bool begin();
3942

4043
/**
4144
* @brief Disengages filesystem and USB
@@ -46,15 +49,16 @@ struct Storage
4649
* @brief Get locking access to filesystem.
4750
*
4851
* @pre storage must be initialized first
52+
* @param maxWaiting maximum time to wait for a USB host device to release access
4953
* @return smart pointer, locking storage to filesystem until deleted
5054
*/
51-
static std::shared_ptr<fs::FS> getFileSystem_locking();
55+
static std::optional<std::shared_ptr<fs::FS>> getFileSystem_locking(std::chrono::milliseconds maxWaiting = std::chrono::milliseconds::max());
5256

5357
/**
5458
* @brief Gets the total size of memory.
5559
*
5660
* @pre storage must be initialized first
57-
* @return size in bytes
61+
* @return size in bytes if storage is initialized
5862
*/
59-
static std::size_t size();
63+
static std::optional<std::size_t> size();
6064
};

src/main.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
#include <FS.h>
6+
#include <cassert>
67
#include <chrono>
78
#include <esp32-hal-log.h>
89
#include <esp_err.h>
@@ -71,7 +72,7 @@ void setup()
7172
ESP_LOGI(TAG, "Example info");
7273
ESP_LOGD(TAG, "Example debug");
7374
ESP_LOGV(TAG, "Example verbose");
74-
Storage::begin();
75+
assert(Storage::begin());
7576
}
7677

7778
void loop()
@@ -88,6 +89,10 @@ void loop()
8889

8990
presenter.loop();
9091

91-
listFiles("/", Storage::getFileSystem_locking());
92+
const auto fileSystemHandle = Storage::getFileSystem_locking(3s);
93+
if (fileSystemHandle)
94+
{
95+
listFiles("/", fileSystemHandle.value());
96+
}
9297
std::this_thread::sleep_for(3s);
9398
}

0 commit comments

Comments
 (0)