@@ -32,16 +32,24 @@ static std::atomic<bool> requestFileSystemActive;
32
32
static std::thread stateMachine;
33
33
static std::mutex fileSystemState_mutex; // !< used to lock the actual state
34
34
static bool fileSystemIsActive; // !< describes the current state
35
+ static bool storageIsInitialized = false ;
35
36
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 )
37
38
{
39
+ if (!storageIsInitialized)
40
+ {
41
+ return std::nullopt;
42
+ }
38
43
std::unique_lock fs_state_lock{fileSystemState_mutex};
39
44
if (!fileSystemIsActive)
40
45
{
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
+ }
42
50
}
43
51
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 (); }};
45
53
}
46
54
47
55
static void requestState (const bool fileSystemActive)
@@ -116,8 +124,12 @@ static bool usbMsc_onStartStop(const std::uint8_t power_condition, const bool st
116
124
return true ;
117
125
}
118
126
119
- std::size_t Storage::size ()
127
+ std::optional<std:: size_t > Storage::size ()
120
128
{
129
+ if (!storageIsInitialized)
130
+ {
131
+ return std::nullopt;
132
+ }
121
133
return FFat.totalBytes ();
122
134
}
123
135
@@ -137,21 +149,20 @@ static void usbStartedCallback(void *, esp_event_base_t, int32_t, void *)
137
149
requestState (false );
138
150
}
139
151
140
- void Storage::begin ()
152
+ bool Storage::begin ()
141
153
{
142
-
143
154
if (!FFat.begin (true ))
144
155
{
145
156
ESP_LOGE (TAG, " Failed to init files system, flash may not be formatted" );
146
- return ;
157
+ return storageIsInitialized = false ;
147
158
}
148
159
ESP_LOGI (TAG, " file system initialized" );
149
160
150
161
partition = check_ffat_partition (FFAT_PARTITION_LABEL);
151
162
if (!partition)
152
163
{
153
164
ESP_LOGE (TAG, " Error with FAT partition" );
154
- return ;
165
+ return storageIsInitialized = false ;
155
166
}
156
167
ESP_LOGI (TAG, " Flash has a size of %u bytes\n " , FFat.totalBytes ());
157
168
@@ -175,11 +186,14 @@ void Storage::begin()
175
186
if (!usbMsc.begin (FFat.totalBytes () / blockSize, blockSize))
176
187
{
177
188
ESP_LOGE (TAG, " starting USB MSC failed" );
189
+ return storageIsInitialized = false ;
178
190
}
179
191
if (!USB.begin ())
180
192
{
181
193
ESP_LOGE (TAG, " starting USB failed" );
194
+ return storageIsInitialized = false ;
182
195
}
196
+ return storageIsInitialized = true ;
183
197
}
184
198
185
199
void Storage::end ()
0 commit comments