@@ -27,98 +27,54 @@ static constexpr std::uint16_t blockSize = 512; // Should be 512
2727static const esp_partition_t *partition = nullptr ;
2828static const char *const TAG = " STORAGE" ;
2929
30- /* *
31- * Lists files and directories at path.
32- */
33- static void listFiles (const char *const dirname, const std::shared_ptr<fs::FS> fs)
30+ static std::condition_variable stateChangeRequested;
31+ static std::condition_variable stateChanged;
32+ static std::atomic<bool > requestFileSystemActive;
33+ static std::thread stateMachine;
34+ static std::mutex fileSystemState_mutex; // !< used to lock the actual state
35+ static bool fileSystemIsActive; // !< describes the current state
36+
37+ std::shared_ptr<fs::FS> Storage::getFileSystem_locking ()
3438{
35- std::cout << " Directory: '" << dirname << " '" << std::endl;
36- File root = fs->open (dirname);
37- if (!root || !root.isDirectory ())
38- {
39- ESP_LOGE (TAG, " Error: '%s' is not a directory!\n " , dirname);
40- return ;
41- }
42-
43- File file = root.openNextFile ();
44- while (file)
39+ std::unique_lock fs_state_lock{fileSystemState_mutex};
40+ if (!fileSystemIsActive)
4541 {
46- std::cout << " \t " << file.name () << " (" << (file.isDirectory () ? " d" : " f" ) << " , " << file.size () << " Bytes)"
47- << std::endl;
48- file.close ();
49- file = root.openNextFile ();
42+ stateChanged.wait (fs_state_lock, []() { return fileSystemIsActive; });
5043 }
51- file. close ();
52- root. close () ;
44+ fs_state_lock. release ();
45+ return {&FFat, [](fs::FS *) { fileSystemState_mutex. unlock (); }} ;
5346}
5447
55- class FileSystemSwitcher
48+ static void requestState ( const bool fileSystemActive)
5649{
57- public:
58- void begin (const bool fsIsActive)
59- {
60- fileSystemIsActive = fsIsActive;
61- requestFileSystemActive = fsIsActive;
62- stateMachine = std::thread (&FileSystemSwitcher::processStateRequests, this );
63- }
50+ requestFileSystemActive = fileSystemActive;
51+ ESP_LOGD (TAG, " request new state: %s" , fileSystemActive ? " true" : " false" );
52+ stateChangeRequested.notify_all ();
53+ }
6454
65- std::shared_ptr<fs::FS> getFileSystem_locking ()
55+ static void processStateRequests ()
56+ {
57+ while (true )
6658 {
6759 std::unique_lock fs_state_lock{fileSystemState_mutex};
68- if (!fileSystemIsActive)
60+ ESP_LOGD (TAG, " waiting for state change request" );
61+ stateChangeRequested.wait (fs_state_lock, []() { return requestFileSystemActive != fileSystemIsActive; });
62+ if (fileSystemIsActive = requestFileSystemActive)
6963 {
70- stateChanged.wait (fs_state_lock, [this ]() { return fileSystemIsActive; });
64+ ESP_LOGI (TAG, " mount FS" );
65+ usbMsc.mediaPresent (false );
66+ FFat.end (); // invalidate cache
67+ assert (FFat.begin ()); // update data
7168 }
72- fs_state_lock.release ();
73- return {&FFat, [this ](fs::FS *) { fileSystemState_mutex.unlock (); }};
74- }
75- void requestState (const bool fileSystemActive)
76- {
77- requestFileSystemActive = fileSystemActive;
78- ESP_LOGD (TAG, " request new state: %s" , fileSystemActive ? " true" : " false" );
79- stateChangeRequested.notify_all ();
80- }
81-
82- protected:
83- bool isFileSystemActive () const
84- {
85- return fileSystemIsActive;
86- }
87- void processStateRequests ()
88- {
89- while (true )
69+ else
9070 {
91- std::unique_lock fs_state_lock{fileSystemState_mutex};
92- ESP_LOGD (TAG, " waiting for state change request" );
93- stateChangeRequested.wait (fs_state_lock,
94- [this ]() { return requestFileSystemActive != fileSystemIsActive; });
95- if (fileSystemIsActive = requestFileSystemActive)
96- {
97- ESP_LOGD (TAG, " mount FS" );
98- usbMsc.mediaPresent (false );
99- FFat.end (); // invalidate cache
100- assert (FFat.begin ()); // update data
101- }
102- else
103- {
104- ESP_LOGD (TAG, " unmount FS" );
105- FFat.end (); // flush and unmount
106- usbMsc.mediaPresent (true );
107- }
108- stateChanged.notify_all ();
71+ ESP_LOGI (TAG, " unmount FS" );
72+ FFat.end (); // flush and unmount
73+ usbMsc.mediaPresent (true );
10974 }
75+ stateChanged.notify_all ();
11076 }
111-
112- private:
113- std::condition_variable stateChangeRequested;
114- std::condition_variable stateChanged;
115- std::atomic<bool > requestFileSystemActive;
116- std::thread stateMachine;
117- std::mutex fileSystemState_mutex; // !< used to lock the actual state
118- bool fileSystemIsActive; // !< describes the current state
119- };
120-
121- static FileSystemSwitcher fileSystemSwitcher;
77+ }
12278
12379/* *
12480 * Callback invoked when received WRITE10 command.
@@ -174,12 +130,12 @@ static void usbStoppedCallback(void *, esp_event_base_t, int32_t, void *)
174130 return ;
175131 }
176132 usbIsRunning = false ;
177- fileSystemSwitcher. requestState (true );
133+ requestState (true );
178134}
179135static void usbStartedCallback (void *, esp_event_base_t , int32_t , void *)
180136{
181137 usbIsRunning = true ;
182- fileSystemSwitcher. requestState (false );
138+ requestState (false );
183139}
184140
185141void Storage::begin ()
@@ -190,7 +146,7 @@ void Storage::begin()
190146 ESP_LOGE (TAG, " Failed to init files system, flash may not be formatted" );
191147 return ;
192148 }
193- ESP_LOGI (TAG, " FatFS erfolgreich gemountet. " );
149+ ESP_LOGI (TAG, " file system initialized " );
194150
195151 partition = check_ffat_partition (FFAT_PARTITION_LABEL);
196152 if (!partition)
@@ -200,7 +156,10 @@ void Storage::begin()
200156 }
201157 ESP_LOGI (TAG, " Flash has a size of %u bytes\n " , FFat.totalBytes ());
202158
203- fileSystemSwitcher.begin (true ); // define state before callbacks are activated
159+ // define state before callbacks are activated
160+ fileSystemIsActive = true ;
161+ requestFileSystemActive = true ;
162+ stateMachine = std::thread (processStateRequests);
204163
205164 usbMsc.vendorID (" ESP32" ); // max 8 chars
206165 usbMsc.productID (" USB_MSC" ); // max 16 chars
@@ -230,9 +189,3 @@ void Storage::end()
230189 usbIsRunning = false ;
231190 FFat.end ();
232191}
233-
234- void Storage::test ()
235- {
236- auto fs_p = fileSystemSwitcher.getFileSystem_locking ();
237- listFiles (" /" , fs_p);
238- }
0 commit comments