@@ -32,16 +32,24 @@ static std::atomic<bool> requestFileSystemActive;
3232static std::thread stateMachine;
3333static std::mutex fileSystemState_mutex; // !< used to lock the actual state
3434static 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
4755static 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
185199void Storage::end ()
0 commit comments