1414#define TAG "SDMounter"
1515
1616Storage * storage ;
17- FuriThreadId * storage_thread ;
17+ FuriThreadId * storage_thread ;
1818
1919bool find_storage_thread ();
2020void do_mass_storage (FuriHalSdInfo card_info );
2121int32_t sd_mounter_app (void * p );
2222void cleanup ();
2323
2424bool find_storage_thread () {
25- FuriThreadList * threads = furi_thread_list_alloc ();
25+ FuriThreadList * threads = furi_thread_list_alloc ();
2626 furi_thread_enumerate (threads );
2727
28- FuriThread * result = NULL ;
28+ FuriThread * result = NULL ;
2929
3030 for (size_t i = 0 ; i < furi_thread_list_size (threads ); i ++ ) {
3131 const FuriThreadListItem * item = furi_thread_list_get_at (threads , i );
32-
33- if (strcmp (item -> app_id , "storage" ) == 0 ) {
34- if (result == NULL ) {
32+
33+ if (strcmp (item -> app_id , "storage" ) == 0 ) {
34+ if (result == NULL ) {
3535 result = item -> thread ;
3636 } else {
37- show_error_and_wait ("More than one storage thread!\nPlease report this error to the\napplication developer." , & I_Error_62x31 , 62 );
37+ show_error_and_wait (
38+ "More than one storage thread!\nPlease report this error to the\napplication developer." ,
39+ & I_Error_62x31 ,
40+ 62 );
3841 return false;
3942 }
4043 }
4144
42- if (strcmp (item -> name , "MassStorageUsb" ) == 0 || strcmp (item -> name , "SD Card Mounter" ) == 0 ) {
43- FURI_LOG_E ("DBG" ,"%-20s -> ID %p" ,
44- item -> name ,
45- furi_thread_get_id (item -> thread ));
45+ if (strcmp (item -> name , "MassStorageUsb" ) == 0 ||
46+ strcmp (item -> name , "SD Card Mounter" ) == 0 ) {
47+ FURI_LOG_E ("DBG" , "%-20s -> ID %p" , item -> name , furi_thread_get_id (item -> thread ));
4648 }
4749 }
4850
49- if (result != NULL ) {
51+ if (result != NULL ) {
5052 storage_thread = furi_thread_get_id (result );
5153 return storage_thread != NULL ;
5254 }
@@ -79,9 +81,12 @@ void do_mass_storage(FuriHalSdInfo card_info) {
7981 };
8082 furi_hal_usb_unlock ();
8183
82- MassStorageUsb * usb = mass_storage_usb_start (card_desc , fn );
83- if (usb == NULL ) {
84- show_error_and_wait ("Unable to connect to computer.\nPlease try again." , & I_SDQuestion_35x43 , 35 ); // TODO: Diff icon?
84+ MassStorageUsb * usb = mass_storage_usb_start (card_desc , fn );
85+ if (usb == NULL ) {
86+ show_error_and_wait (
87+ "Unable to connect to computer.\nPlease try again." ,
88+ & I_SDQuestion_35x43 ,
89+ 35 ); // TODO: Diff icon?
8590 return ;
8691 }
8792
@@ -90,8 +95,8 @@ void do_mass_storage(FuriHalSdInfo card_info) {
9095 uint64_t last_read = 0 ;
9196
9297 // Wait for the card to be ejected
93- FuriString * message = furi_string_alloc ();
94- while (1 ) {
98+ FuriString * message = furi_string_alloc ();
99+ while (1 ) {
95100 // Update bytes counter
96101 furi_string_printf (
97102 message ,
@@ -103,7 +108,7 @@ void do_mass_storage(FuriHalSdInfo card_info) {
103108 ctx .bytes_written / 1024 );
104109 update_existing_popup (furi_string_get_cstr (message ));
105110
106- if (last_read != ctx .bytes_read ) {
111+ if (last_read != ctx .bytes_read ) {
107112 last_read = ctx .bytes_read ;
108113 notify (NULL );
109114 furi_delay_ms (2 );
@@ -112,11 +117,12 @@ void do_mass_storage(FuriHalSdInfo card_info) {
112117 }
113118
114119 // Quit if the card is removed
115- if (!furi_hal_sd_is_present ()) break ;
120+ if (!furi_hal_sd_is_present ()) break ;
116121
117122 // Stop if the card is ejected (from the computer) or the back button is pressed
118- uint32_t flags = furi_thread_flags_wait (FlagEject | FlagBackButtonPressed , FuriFlagNoClear , 50 );
119- if (flags != FuriFlagErrorTimeout ) break ;
123+ uint32_t flags =
124+ furi_thread_flags_wait (FlagEject | FlagBackButtonPressed , FuriFlagNoClear , 50 );
125+ if (flags != FuriFlagErrorTimeout ) break ;
120126 }
121127
122128 update_existing_popup ("Ejecting card, please wait..." );
@@ -129,31 +135,40 @@ int32_t sd_mounter_app(void* p) {
129135 UNUSED (p );
130136
131137 ui_init ();
132-
138+
133139 /*
134140 * Initialize Storage
135141 * Unmounts SD card and suspends the Storage thred
136142 */
137143 storage = furi_record_open (RECORD_STORAGE );
138144 FS_Error error = storage_sd_unmount (storage );
139- switch (error ) {
140- case FSE_OK : // Success
141- case FSE_NOT_READY : // Already unmounted
142- break ;
143-
144- case FSE_DENIED : // Can't unmount, open files
145- show_error_and_wait ("Can't unmount SD\ncard. Please close all\nopen files." , & I_SDQuestion_35x43 , 35 ); // TODO: Diff icon?
146- cleanup ();
147- return -1 ;
148-
149- default :
150- show_error_and_wait ("Can't unmount SD\ncard due to an\nunknown error" , & I_SDQuestion_35x43 , 35 ); // TODO: Diff icon?
151- cleanup ();
152- return -1 ;
145+ switch (error ) {
146+ case FSE_OK : // Success
147+ case FSE_NOT_READY : // Already unmounted
148+ break ;
149+
150+ case FSE_DENIED : // Can't unmount, open files
151+ show_error_and_wait (
152+ "Can't unmount SD\ncard. Please close all\nopen files." ,
153+ & I_SDQuestion_35x43 ,
154+ 35 ); // TODO: Diff icon?
155+ cleanup ();
156+ return -1 ;
157+
158+ default :
159+ show_error_and_wait (
160+ "Can't unmount SD\ncard due to an\nunknown error" ,
161+ & I_SDQuestion_35x43 ,
162+ 35 ); // TODO: Diff icon?
163+ cleanup ();
164+ return -1 ;
153165 }
154166
155- if (!find_storage_thread ()) {
156- show_error_and_wait ("Unable to find storage thread!\nPlease report this error to the\napplication developer." , & I_Error_62x31 , 62 );
167+ if (!find_storage_thread ()) {
168+ show_error_and_wait (
169+ "Unable to find storage thread!\nPlease report this error to the\napplication developer." ,
170+ & I_Error_62x31 ,
171+ 62 );
157172 cleanup ();
158173 return -1 ;
159174 }
@@ -168,49 +183,52 @@ int32_t sd_mounter_app(void* p) {
168183 * Stores infomation about the current SD card, to check whether it has been reinserted
169184 */
170185
171- FuriHalSdInfo * target_card_info = NULL ;
186+ FuriHalSdInfo * target_card_info = NULL ;
172187 show ("Scanning card, please wait...\nDO NOT REMOVE CARD!" );
173188 target_card_info = try_get_sd_info ();
174- if (target_card_info == NULL ) {
175- show_error_and_wait ("Error getting SD card\ninformation!" , & I_SDQuestion_35x43 , 35 ); // TODO: Diff icon?
189+ if (target_card_info == NULL ) {
190+ show_error_and_wait (
191+ "Error getting SD card\ninformation!" , & I_SDQuestion_35x43 , 35 ); // TODO: Diff icon?
176192 cleanup ();
177193 return -1 ;
178194 }
179- FuriString * original_card_desc = get_card_desc (* target_card_info );
195+ FuriString * original_card_desc = get_card_desc (* target_card_info );
180196
181197 FURI_LOG_D (TAG , "Original card info: '%s'" , furi_string_get_cstr (original_card_desc ));
182-
198+
183199 /*
184200 * Main Loop
185201 * This loop runs until the user presses the Back button
186202 */
187- FuriHalSdInfo * card_info = NULL ;
188- FuriString * card_desc = NULL ;
189- while (!back_button_was_pressed (true)) {
203+ FuriHalSdInfo * card_info = NULL ;
204+ FuriString * card_desc = NULL ;
205+ while (!back_button_was_pressed (true)) {
190206 show ("Insert an SD card or\npress Back to exit." );
191207 // Wait for the card to be inserted or the back button to be pressed
192208 notify (& led_blink_cyan );
193- while (!furi_hal_sd_is_present () && !back_button_was_pressed (false)) furi_thread_yield ();
209+ while (!furi_hal_sd_is_present () && !back_button_was_pressed (false))
210+ furi_thread_yield ();
194211
195212 // If the back button was pressed, attempt to quit the app
196- if (back_button_was_pressed (true)) break ;
213+ if (back_button_was_pressed (true)) break ;
197214
198215 show ("Scanning card, please wait...\nDO NOT REMOVE CARD!" );
199216
200217 // Try to fetch the card info
201218 card_info = try_get_sd_info ();
202219
203220 // Fetching the card info can take a while so check again if the back button was pressed
204- if (back_button_was_pressed (true)) break ;
221+ if (back_button_was_pressed (true)) break ;
205222
206- if (card_info == NULL ) {
223+ if (card_info == NULL ) {
207224 show ("Failed to get card info!\nRemove card and try again." );
208225 // Wait for card to be removed before continuing
209226 notify (& led_red );
210- while (furi_hal_sd_is_present ()) furi_thread_yield ();
227+ while (furi_hal_sd_is_present ())
228+ furi_thread_yield ();
211229 continue ;
212230 }
213-
231+
214232 card_desc = get_card_desc (* card_info );
215233 notify (& led_green );
216234
@@ -222,7 +240,8 @@ int32_t sd_mounter_app(void* p) {
222240 // Card ejected, wait for it to be removed
223241 show ("SD Card ejected,\nplease remove it.\nPress Back to exit." );
224242 notify (& led_blink_yellow_slow );
225- while (furi_hal_sd_is_present () && !back_button_was_pressed (false)) furi_thread_yield ();
243+ while (furi_hal_sd_is_present () && !back_button_was_pressed (false))
244+ furi_thread_yield ();
226245 };
227246
228247 /*
@@ -231,35 +250,36 @@ int32_t sd_mounter_app(void* p) {
231250 card_info = NULL ;
232251 card_desc = NULL ;
233252 notify (& led_blink_cyan );
234- while (1 ) {
253+ while (1 ) {
235254 show ("Please reinsert the\nFlipper's original SD\ncard to exit this app." );
236- if (furi_hal_sd_is_present ()) {
255+ if (furi_hal_sd_is_present ()) {
237256 show ("Scanning card, please wait...\nDO NOT REMOVE CARD!" );
238257 card_info = try_get_sd_info ();
239- if (card_info != NULL ) {
258+ if (card_info != NULL ) {
240259 card_desc = get_card_desc (* card_info );
241260 }
242-
261+
243262 // If the card is the correct one, exit the loop
244- if (card_info != NULL && card_desc != NULL ) {
245- if (furi_string_equal (card_desc , original_card_desc )) {
263+ if (card_info != NULL && card_desc != NULL ) {
264+ if (furi_string_equal (card_desc , original_card_desc )) {
246265 notify (& led_green );
247266 break ;
248267 }
249268 }
250-
269+
251270 // Otherwise, show a message and wait for the card to be removed
252271 notify (& led_red );
253272 show ("Wrong SD card. Reinsert\nthe Flipper's original SD\ncard to exit this app." );
254- while (furi_hal_sd_is_present ()) furi_thread_yield ();
273+ while (furi_hal_sd_is_present ())
274+ furi_thread_yield ();
255275 notify (& led_blink_cyan );
256276 }
257277 }
258278
259279 notify (NULL );
260280 show ("Flipper card detected.\nGoodbye!" );
261281 furi_delay_ms (500 );
262-
282+
263283 cleanup ();
264284 return 0 ;
265285}
@@ -274,16 +294,16 @@ void cleanup() {
274294 sd_init ();
275295
276296 // If the storage thread was found, resume it
277- if (storage_thread != NULL ) {
297+ if (storage_thread != NULL ) {
278298 furi_thread_resume (storage_thread );
279299 }
280300
281301 // Make sure the SD card is mounted before exiting
282302 storage_sd_mount (storage );
283-
284- if (storage != NULL ) {
303+
304+ if (storage != NULL ) {
285305 furi_record_close (RECORD_STORAGE );
286306 }
287307
288308 ui_cleanup ();
289- }
309+ }
0 commit comments