sd-filesystem.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. #include "fatfs.h"
  2. #include "filesystem-api.h"
  3. #include "sd-filesystem.h"
  4. #include "menu/menu.h"
  5. #include "menu/menu_item.h"
  6. #include "cli/cli.h"
  7. #include "api-hal-sd.h"
  8. #include <gui/modules/dialog_ex.h>
  9. #include <gui/modules/file_select.h>
  10. typedef enum {
  11. FST_FAT12 = FS_FAT12,
  12. FST_FAT16 = FS_FAT16,
  13. FST_FAT32 = FS_FAT32,
  14. FST_EXFAT = FS_EXFAT,
  15. } SDFsType;
  16. typedef struct {
  17. SDFsType fs_type;
  18. uint32_t kb_total;
  19. uint32_t kb_free;
  20. uint16_t cluster_size;
  21. uint16_t sector_size;
  22. char label[34];
  23. SDError error;
  24. } SDInfo;
  25. typedef enum {
  26. SdAppEventTypeBack,
  27. SdAppEventTypeOK,
  28. SdAppEventTypeFormat,
  29. SdAppEventTypeInfo,
  30. SdAppEventTypeEject,
  31. SdAppEventTypeFileSelect,
  32. SdAppEventTypeCheckError,
  33. } SdAppEventType;
  34. typedef struct {
  35. const char* path;
  36. const char* extension;
  37. char* result;
  38. uint8_t result_size;
  39. } SdAppFileSelectData;
  40. typedef struct {
  41. bool result;
  42. } SdAppFileSelectResultEvent;
  43. typedef struct {
  44. SdAppEventType type;
  45. osMessageQueueId_t result_receiver;
  46. union {
  47. SdAppFileSelectData file_select_data;
  48. } payload;
  49. } SdAppEvent;
  50. static void sd_icon_draw_callback(Canvas* canvas, void* context);
  51. bool sd_api_file_select(
  52. SdApp* sd_app,
  53. const char* path,
  54. const char* extension,
  55. char* result,
  56. uint8_t result_size);
  57. /******************* Allocators *******************/
  58. FS_Api* fs_api_alloc() {
  59. FS_Api* fs_api = furi_alloc(sizeof(FS_Api));
  60. // fill file api
  61. fs_api->file.open = fs_file_open;
  62. fs_api->file.close = fs_file_close;
  63. fs_api->file.read = fs_file_read;
  64. fs_api->file.write = fs_file_write;
  65. fs_api->file.seek = fs_file_seek;
  66. fs_api->file.tell = fs_file_tell;
  67. fs_api->file.truncate = fs_file_truncate;
  68. fs_api->file.size = fs_file_size;
  69. fs_api->file.sync = fs_file_sync;
  70. fs_api->file.eof = fs_file_eof;
  71. // fill dir api
  72. fs_api->dir.open = fs_dir_open;
  73. fs_api->dir.close = fs_dir_close;
  74. fs_api->dir.read = fs_dir_read;
  75. fs_api->dir.rewind = fs_dir_rewind;
  76. // fill common api
  77. fs_api->common.info = fs_common_info;
  78. fs_api->common.remove = fs_common_remove;
  79. fs_api->common.rename = fs_common_rename;
  80. fs_api->common.set_attr = fs_common_set_attr;
  81. fs_api->common.mkdir = fs_common_mkdir;
  82. fs_api->common.set_time = fs_common_set_time;
  83. fs_api->common.get_fs_info = fs_get_fs_info;
  84. // fill errors api
  85. fs_api->error.get_desc = fs_error_get_desc;
  86. fs_api->error.get_internal_desc = fs_error_get_internal_desc;
  87. return fs_api;
  88. }
  89. SdApp* sd_app_alloc() {
  90. SdApp* sd_app = furi_alloc(sizeof(SdApp));
  91. // init inner fs data
  92. furi_check(_fs_init(&sd_app->info));
  93. sd_app->event_queue = osMessageQueueNew(8, sizeof(SdAppEvent), NULL);
  94. // init icon view_port
  95. sd_app->icon.view_port = view_port_alloc();
  96. sd_app->icon.mounted = assets_icons_get(I_SDcardMounted_11x8);
  97. sd_app->icon.fail = assets_icons_get(I_SDcardFail_11x8);
  98. view_port_set_width(sd_app->icon.view_port, icon_get_width(sd_app->icon.mounted));
  99. view_port_draw_callback_set(sd_app->icon.view_port, sd_icon_draw_callback, sd_app);
  100. view_port_enabled_set(sd_app->icon.view_port, false);
  101. // init sd card api
  102. sd_app->sd_card_api.context = sd_app;
  103. sd_app->sd_card_api.file_select = sd_api_file_select;
  104. sd_app->sd_app_state = SdAppStateBackground;
  105. string_init(sd_app->text_holder);
  106. return sd_app;
  107. }
  108. /******************* Internal sd card related fns *******************/
  109. void get_sd_info(SdApp* sd_app, SDInfo* sd_info) {
  110. uint32_t free_clusters, free_sectors, total_sectors;
  111. FATFS* fs;
  112. // clean data
  113. memset(sd_info, 0, sizeof(SDInfo));
  114. // get fs info
  115. _fs_lock(&sd_app->info);
  116. sd_info->error = f_getlabel(sd_app->info.path, sd_info->label, NULL);
  117. if(sd_info->error == SD_OK) {
  118. sd_info->error = f_getfree(sd_app->info.path, &free_clusters, &fs);
  119. }
  120. _fs_unlock(&sd_app->info);
  121. if(sd_info->error == SD_OK) {
  122. // calculate size
  123. total_sectors = (fs->n_fatent - 2) * fs->csize;
  124. free_sectors = free_clusters * fs->csize;
  125. uint16_t sector_size = _MAX_SS;
  126. #if _MAX_SS != _MIN_SS
  127. sector_size = fs->ssize;
  128. #endif
  129. sd_info->fs_type = fs->fs_type;
  130. sd_info->kb_total = total_sectors / 1024 * sector_size;
  131. sd_info->kb_free = free_sectors / 1024 * sector_size;
  132. sd_info->cluster_size = fs->csize;
  133. sd_info->sector_size = sector_size;
  134. }
  135. }
  136. const char* get_fs_type_text(SDFsType fs_type) {
  137. switch(fs_type) {
  138. case(FST_FAT12):
  139. return "FAT12";
  140. break;
  141. case(FST_FAT16):
  142. return "FAT16";
  143. break;
  144. case(FST_FAT32):
  145. return "FAT32";
  146. break;
  147. case(FST_EXFAT):
  148. return "EXFAT";
  149. break;
  150. default:
  151. return "UNKNOWN";
  152. break;
  153. }
  154. }
  155. void app_sd_format_internal(SdApp* sd_app) {
  156. uint8_t* work_area;
  157. _fs_lock(&sd_app->info);
  158. work_area = malloc(_MAX_SS);
  159. if(work_area == NULL) {
  160. sd_app->info.status = SD_NOT_ENOUGH_CORE;
  161. } else {
  162. sd_app->info.status = f_mkfs(sd_app->info.path, FM_ANY, 0, work_area, _MAX_SS);
  163. free(work_area);
  164. if(sd_app->info.status == SD_OK) {
  165. // set label and mount card
  166. f_setlabel("Flipper SD");
  167. sd_app->info.status = f_mount(&sd_app->info.fat_fs, sd_app->info.path, 1);
  168. }
  169. }
  170. _fs_unlock(&sd_app->info);
  171. }
  172. void app_sd_notify_wait_on() {
  173. api_hal_light_set(LightRed, 0xFF);
  174. api_hal_light_set(LightBlue, 0xFF);
  175. }
  176. void app_sd_notify_wait_off() {
  177. api_hal_light_set(LightRed, 0x00);
  178. api_hal_light_set(LightBlue, 0x00);
  179. }
  180. void app_sd_notify_success() {
  181. for(uint8_t i = 0; i < 3; i++) {
  182. delay(50);
  183. api_hal_light_set(LightGreen, 0xFF);
  184. delay(50);
  185. api_hal_light_set(LightGreen, 0x00);
  186. }
  187. }
  188. void app_sd_notify_eject() {
  189. for(uint8_t i = 0; i < 3; i++) {
  190. delay(50);
  191. api_hal_light_set(LightBlue, 0xFF);
  192. delay(50);
  193. api_hal_light_set(LightBlue, 0x00);
  194. }
  195. }
  196. void app_sd_notify_error() {
  197. for(uint8_t i = 0; i < 3; i++) {
  198. delay(50);
  199. api_hal_light_set(LightRed, 0xFF);
  200. delay(50);
  201. api_hal_light_set(LightRed, 0x00);
  202. }
  203. }
  204. bool app_sd_mount_card(SdApp* sd_app) {
  205. bool result = false;
  206. const uint8_t max_init_counts = 10;
  207. uint8_t counter = max_init_counts;
  208. uint8_t bsp_result;
  209. _fs_lock(&sd_app->info);
  210. while(result == false && counter > 0 && hal_sd_detect()) {
  211. app_sd_notify_wait_on();
  212. if((counter % 10) == 0) {
  213. // power reset sd card
  214. bsp_result = BSP_SD_Init(true);
  215. } else {
  216. bsp_result = BSP_SD_Init(false);
  217. }
  218. if(bsp_result) {
  219. // bsp error
  220. sd_app->info.status = SD_LOW_LEVEL_ERR;
  221. } else {
  222. sd_app->info.status = f_mount(&sd_app->info.fat_fs, sd_app->info.path, 1);
  223. if(sd_app->info.status == SD_OK || sd_app->info.status == SD_NO_FILESYSTEM) {
  224. FATFS* fs;
  225. uint32_t free_clusters;
  226. sd_app->info.status = f_getfree(sd_app->info.path, &free_clusters, &fs);
  227. if(sd_app->info.status == SD_OK || sd_app->info.status == SD_NO_FILESYSTEM) {
  228. result = true;
  229. }
  230. }
  231. }
  232. app_sd_notify_wait_off();
  233. if(!result) {
  234. delay(1000);
  235. printf(
  236. "[sd_filesystem] init(%d), error: %s\r\n",
  237. counter,
  238. fs_error_get_internal_desc(sd_app->info.status));
  239. counter--;
  240. }
  241. }
  242. _fs_unlock(&sd_app->info);
  243. return result;
  244. }
  245. void app_sd_unmount_card(SdApp* sd_app) {
  246. _fs_lock(&sd_app->info);
  247. // set status
  248. sd_app->info.status = SD_NO_CARD;
  249. view_port_enabled_set(sd_app->icon.view_port, false);
  250. // close files
  251. for(uint8_t index = 0; index < SD_FS_MAX_FILES; index++) {
  252. FileData* filedata = &sd_app->info.files[index];
  253. if(filedata->thread_id != NULL) {
  254. if(filedata->is_dir) {
  255. f_closedir(&filedata->data.dir);
  256. } else {
  257. f_close(&filedata->data.file);
  258. }
  259. filedata->thread_id = NULL;
  260. }
  261. }
  262. // unmount volume
  263. f_mount(0, sd_app->info.path, 0);
  264. _fs_unlock(&sd_app->info);
  265. }
  266. bool app_sd_make_path(const char* path) {
  267. furi_assert(path);
  268. if(*path) {
  269. char* file_path = strdup(path);
  270. // Make parent directories
  271. for(char* p = strchr(file_path + 1, '/'); p; p = strchr(p + 1, '/')) {
  272. *p = '\0';
  273. SDError result = f_mkdir(file_path);
  274. if(result != SD_OK) {
  275. if(result != SD_EXIST) {
  276. *p = '/';
  277. free(file_path);
  278. return false;
  279. }
  280. }
  281. *p = '/';
  282. }
  283. // Make origin directory
  284. SDError result = f_mkdir(file_path);
  285. if(result != SD_OK) {
  286. if(result != SD_EXIST) {
  287. free(file_path);
  288. return false;
  289. }
  290. }
  291. free(file_path);
  292. }
  293. return true;
  294. }
  295. /******************* Draw callbacks *******************/
  296. static void sd_icon_draw_callback(Canvas* canvas, void* context) {
  297. furi_assert(canvas);
  298. furi_assert(context);
  299. SdApp* sd_app = context;
  300. switch(sd_app->info.status) {
  301. case SD_NO_CARD:
  302. break;
  303. case SD_OK:
  304. canvas_draw_icon(canvas, 0, 0, sd_app->icon.mounted);
  305. break;
  306. default:
  307. canvas_draw_icon(canvas, 0, 0, sd_app->icon.fail);
  308. break;
  309. }
  310. }
  311. /******************* SD-api callbacks *******************/
  312. bool sd_api_file_select(
  313. SdApp* sd_app,
  314. const char* path,
  315. const char* extension,
  316. char* result,
  317. uint8_t result_size) {
  318. bool retval = false;
  319. osMessageQueueId_t return_event_queue =
  320. osMessageQueueNew(1, sizeof(SdAppFileSelectResultEvent), NULL);
  321. SdAppEvent message = {
  322. .type = SdAppEventTypeFileSelect,
  323. .result_receiver = return_event_queue,
  324. .payload = {
  325. .file_select_data = {
  326. .path = path,
  327. .extension = extension,
  328. .result = result,
  329. .result_size = result_size}}};
  330. furi_check(osMessageQueuePut(sd_app->event_queue, &message, 0, osWaitForever) == osOK);
  331. SdAppFileSelectResultEvent event;
  332. while(1) {
  333. osStatus_t event_status =
  334. osMessageQueueGet(return_event_queue, &event, NULL, osWaitForever);
  335. if(event_status == osOK) {
  336. retval = event.result;
  337. break;
  338. }
  339. }
  340. return retval;
  341. }
  342. /******************* View callbacks *******************/
  343. void app_view_back_callback(void* context) {
  344. furi_assert(context);
  345. SdApp* sd_app = context;
  346. SdAppEvent message = {.type = SdAppEventTypeBack};
  347. furi_check(osMessageQueuePut(sd_app->event_queue, &message, 0, osWaitForever) == osOK);
  348. }
  349. void app_view_dialog_callback(DialogExResult result, void* context) {
  350. furi_assert(context);
  351. SdApp* sd_app = context;
  352. if(result == DialogExResultLeft) {
  353. SdAppEvent message = {.type = SdAppEventTypeBack};
  354. furi_check(osMessageQueuePut(sd_app->event_queue, &message, 0, osWaitForever) == osOK);
  355. } else if(result == DialogExResultRight) {
  356. SdAppEvent message = {.type = SdAppEventTypeOK};
  357. furi_check(osMessageQueuePut(sd_app->event_queue, &message, 0, osWaitForever) == osOK);
  358. }
  359. }
  360. void app_view_file_select_callback(bool result, void* context) {
  361. furi_assert(context);
  362. SdApp* sd_app = context;
  363. if(result) {
  364. SdAppEvent message = {.type = SdAppEventTypeOK};
  365. furi_check(osMessageQueuePut(sd_app->event_queue, &message, 0, osWaitForever) == osOK);
  366. } else {
  367. SdAppEvent message = {.type = SdAppEventTypeBack};
  368. furi_check(osMessageQueuePut(sd_app->event_queue, &message, 0, osWaitForever) == osOK);
  369. }
  370. }
  371. /******************* Menu callbacks *******************/
  372. void app_sd_info_callback(void* context) {
  373. furi_assert(context);
  374. SdApp* sd_app = context;
  375. SdAppEvent message = {.type = SdAppEventTypeInfo};
  376. furi_check(osMessageQueuePut(sd_app->event_queue, &message, 0, osWaitForever) == osOK);
  377. }
  378. void app_sd_format_callback(void* context) {
  379. furi_assert(context);
  380. SdApp* sd_app = context;
  381. SdAppEvent message = {.type = SdAppEventTypeFormat};
  382. furi_check(osMessageQueuePut(sd_app->event_queue, &message, 0, osWaitForever) == osOK);
  383. }
  384. void app_sd_eject_callback(void* context) {
  385. furi_assert(context);
  386. SdApp* sd_app = context;
  387. SdAppEvent message = {.type = SdAppEventTypeEject};
  388. furi_check(osMessageQueuePut(sd_app->event_queue, &message, 0, osWaitForever) == osOK);
  389. }
  390. /******************* Cli callbacks *******************/
  391. static void cli_sd_status(string_t args, void* _ctx) {
  392. SdApp* sd_app = (SdApp*)_ctx;
  393. printf("SD status: ");
  394. printf(fs_error_get_internal_desc(sd_app->info.status));
  395. printf("\r\n");
  396. }
  397. static void cli_sd_format(string_t args, void* _ctx) {
  398. SdApp* sd_app = (SdApp*)_ctx;
  399. printf("formatting SD card, please wait\r\n");
  400. // format card
  401. app_sd_format_internal(sd_app);
  402. if(sd_app->info.status != SD_OK) {
  403. printf("SD card format error: ");
  404. printf(fs_error_get_internal_desc(sd_app->info.status));
  405. printf("\r\n");
  406. } else {
  407. printf("SD card formatted\r\n");
  408. }
  409. }
  410. static void cli_sd_info(string_t args, void* _ctx) {
  411. SdApp* sd_app = (SdApp*)_ctx;
  412. SDInfo sd_info;
  413. get_sd_info(sd_app, &sd_info);
  414. if(sd_info.error == SD_OK) {
  415. const char* fs_type = get_fs_type_text(sd_info.fs_type);
  416. printf("Label: %s\r\n", sd_info.label);
  417. printf("%s\r\n", fs_type);
  418. printf("Cluster: %d sectors\r\n", sd_info.cluster_size);
  419. printf("Sector: %d bytes\r\n", sd_info.sector_size);
  420. printf("%lu KB total\r\n", sd_info.kb_total);
  421. printf("%lu KB free\r\n", sd_info.kb_free);
  422. } else {
  423. printf("SD status error: %s\r\n", fs_error_get_internal_desc(_fs_status(&sd_app->info)));
  424. printf("SD info error: %s\r\n", fs_error_get_internal_desc(sd_info.error));
  425. }
  426. }
  427. /******************* Test *******************/
  428. bool try_to_alloc_view_holder(SdApp* sd_app, Gui* gui) {
  429. bool result = false;
  430. _fs_lock(&sd_app->info);
  431. if(sd_app->view_holder == NULL) {
  432. sd_app->view_holder = view_holder_alloc();
  433. view_holder_attach_to_gui(sd_app->view_holder, gui);
  434. view_holder_set_back_callback(sd_app->view_holder, app_view_back_callback, sd_app);
  435. result = true;
  436. }
  437. _fs_unlock(&sd_app->info);
  438. return result;
  439. }
  440. DialogEx* alloc_and_attach_dialog(SdApp* sd_app) {
  441. DialogEx* dialog = dialog_ex_alloc();
  442. dialog_ex_set_context(dialog, sd_app);
  443. dialog_ex_set_result_callback(dialog, app_view_dialog_callback);
  444. view_holder_set_view(sd_app->view_holder, dialog_ex_get_view(dialog));
  445. view_holder_set_free_callback(sd_app->view_holder, (FreeCallback)dialog_ex_free, dialog);
  446. return dialog;
  447. }
  448. FileSelect* alloc_and_attach_file_select(SdApp* sd_app) {
  449. FileSelect* file_select = file_select_alloc();
  450. file_select_set_callback(file_select, app_view_file_select_callback, sd_app);
  451. view_holder_set_view(sd_app->view_holder, file_select_get_view(file_select));
  452. view_holder_set_free_callback(
  453. sd_app->view_holder, (FreeCallback)file_select_free, file_select);
  454. return file_select;
  455. }
  456. void free_view_holder(SdApp* sd_app) {
  457. _fs_lock(&sd_app->info);
  458. if(sd_app->view_holder) {
  459. view_holder_free(sd_app->view_holder);
  460. sd_app->view_holder = NULL;
  461. }
  462. _fs_unlock(&sd_app->info);
  463. }
  464. void app_reset_state(SdApp* sd_app) {
  465. view_holder_stop(sd_app->view_holder);
  466. free_view_holder(sd_app);
  467. string_set_str(sd_app->text_holder, "");
  468. sd_app->sd_app_state = SdAppStateBackground;
  469. }
  470. /******************* Main app *******************/
  471. int32_t sd_filesystem(void* p) {
  472. SdApp* sd_app = sd_app_alloc();
  473. FS_Api* fs_api = fs_api_alloc();
  474. Gui* gui = furi_record_open("gui");
  475. Cli* cli = furi_record_open("cli");
  476. ValueMutex* menu_vm = furi_record_open("menu");
  477. gui_add_view_port(gui, sd_app->icon.view_port, GuiLayerStatusBarLeft);
  478. cli_add_command(cli, "sd_status", cli_sd_status, sd_app);
  479. cli_add_command(cli, "sd_format", cli_sd_format, sd_app);
  480. cli_add_command(cli, "sd_info", cli_sd_info, sd_app);
  481. // add api record
  482. furi_record_create("sdcard", fs_api);
  483. // init menu
  484. // TODO menu icon
  485. MenuItem* menu_item;
  486. menu_item = menu_item_alloc_menu("SD Card", assets_icons_get(I_SDcardMounted_11x8));
  487. menu_item_subitem_add(
  488. menu_item, menu_item_alloc_function("Info", NULL, app_sd_info_callback, sd_app));
  489. menu_item_subitem_add(
  490. menu_item, menu_item_alloc_function("Format", NULL, app_sd_format_callback, sd_app));
  491. menu_item_subitem_add(
  492. menu_item, menu_item_alloc_function("Eject", NULL, app_sd_eject_callback, sd_app));
  493. // add item to menu
  494. furi_check(menu_vm);
  495. with_value_mutex(
  496. menu_vm, (Menu * menu) { menu_item_add(menu, menu_item); });
  497. printf("[sd_filesystem] start\r\n");
  498. // add api record
  499. furi_record_create("sdcard", fs_api);
  500. furi_record_create("sdcard-ex", &sd_app->sd_card_api);
  501. // sd card cycle
  502. bool sd_was_present = true;
  503. // init detect pins
  504. hal_sd_detect_init();
  505. while(true) {
  506. if(sd_was_present) {
  507. if(hal_sd_detect()) {
  508. printf("[sd_filesystem] card detected\r\n");
  509. app_sd_mount_card(sd_app);
  510. if(sd_app->info.status != SD_OK) {
  511. printf(
  512. "[sd_filesystem] sd init error: %s\r\n",
  513. fs_error_get_internal_desc(sd_app->info.status));
  514. app_sd_notify_error();
  515. } else {
  516. printf("[sd_filesystem] sd init ok\r\n");
  517. app_sd_notify_success();
  518. }
  519. view_port_enabled_set(sd_app->icon.view_port, true);
  520. sd_was_present = false;
  521. if(!hal_sd_detect()) {
  522. printf("[sd_filesystem] card removed\r\n");
  523. view_port_enabled_set(sd_app->icon.view_port, false);
  524. app_sd_unmount_card(sd_app);
  525. sd_was_present = true;
  526. }
  527. }
  528. } else {
  529. if(!hal_sd_detect()) {
  530. printf("[sd_filesystem] card removed\r\n");
  531. view_port_enabled_set(sd_app->icon.view_port, false);
  532. app_sd_unmount_card(sd_app);
  533. sd_was_present = true;
  534. app_sd_notify_eject();
  535. }
  536. }
  537. SdAppEvent event;
  538. osStatus_t event_status = osMessageQueueGet(sd_app->event_queue, &event, NULL, 1000);
  539. const uint8_t y_1_line = 32;
  540. const uint8_t y_2_line = 32;
  541. const uint8_t y_4_line = 26;
  542. if(event_status == osOK) {
  543. switch(event.type) {
  544. case SdAppEventTypeOK:
  545. switch(sd_app->sd_app_state) {
  546. case SdAppStateFormat: {
  547. DialogEx* dialog = view_holder_get_free_context(sd_app->view_holder);
  548. dialog_ex_set_left_button_text(dialog, NULL);
  549. dialog_ex_set_right_button_text(dialog, NULL);
  550. dialog_ex_set_header(
  551. dialog, "Formatting...", 64, y_1_line, AlignCenter, AlignCenter);
  552. dialog_ex_set_text(dialog, NULL, 0, 0, AlignCenter, AlignCenter);
  553. sd_app->sd_app_state = SdAppStateFormatInProgress;
  554. delay(100);
  555. app_sd_format_internal(sd_app);
  556. app_sd_notify_success();
  557. dialog_ex_set_left_button_text(dialog, "Back");
  558. dialog_ex_set_header(
  559. dialog, "SD card formatted", 64, 10, AlignCenter, AlignCenter);
  560. dialog_ex_set_text(
  561. dialog, "Press back to return", 64, y_1_line, AlignCenter, AlignCenter);
  562. sd_app->sd_app_state = SdAppStateFormatCompleted;
  563. }; break;
  564. case SdAppStateEject: {
  565. DialogEx* dialog = view_holder_get_free_context(sd_app->view_holder);
  566. dialog_ex_set_right_button_text(dialog, NULL);
  567. dialog_ex_set_header(
  568. dialog, "SD card ejected", 64, 10, AlignCenter, AlignCenter);
  569. dialog_ex_set_text(
  570. dialog,
  571. "Now the SD card\ncan be removed.",
  572. 64,
  573. y_2_line,
  574. AlignCenter,
  575. AlignCenter);
  576. sd_app->sd_app_state = SdAppStateEjected;
  577. app_sd_unmount_card(sd_app);
  578. app_sd_notify_eject();
  579. }; break;
  580. case SdAppStateFileSelect: {
  581. SdAppFileSelectResultEvent retval = {.result = true};
  582. furi_check(
  583. osMessageQueuePut(sd_app->result_receiver, &retval, 0, osWaitForever) ==
  584. osOK);
  585. app_reset_state(sd_app);
  586. }; break;
  587. default:
  588. break;
  589. }
  590. break;
  591. case SdAppEventTypeBack:
  592. switch(sd_app->sd_app_state) {
  593. case SdAppStateFormatInProgress:
  594. break;
  595. case SdAppStateFileSelect: {
  596. SdAppFileSelectResultEvent retval = {.result = false};
  597. furi_check(
  598. osMessageQueuePut(sd_app->result_receiver, &retval, 0, osWaitForever) ==
  599. osOK);
  600. app_reset_state(sd_app);
  601. }; break;
  602. default:
  603. app_reset_state(sd_app);
  604. break;
  605. }
  606. break;
  607. case SdAppEventTypeFormat:
  608. if(try_to_alloc_view_holder(sd_app, gui)) {
  609. DialogEx* dialog = alloc_and_attach_dialog(sd_app);
  610. dialog_ex_set_left_button_text(dialog, "Back");
  611. dialog_ex_set_right_button_text(dialog, "Format");
  612. dialog_ex_set_header(
  613. dialog, "Format SD card?", 64, 10, AlignCenter, AlignCenter);
  614. dialog_ex_set_text(
  615. dialog, "All data will be lost.", 64, y_1_line, AlignCenter, AlignCenter);
  616. view_holder_start(sd_app->view_holder);
  617. sd_app->sd_app_state = SdAppStateFormat;
  618. }
  619. break;
  620. case SdAppEventTypeInfo:
  621. if(try_to_alloc_view_holder(sd_app, gui)) {
  622. DialogEx* dialog = alloc_and_attach_dialog(sd_app);
  623. dialog_ex_set_left_button_text(dialog, "Back");
  624. SDInfo sd_info;
  625. get_sd_info(sd_app, &sd_info);
  626. if(sd_info.error == SD_OK) {
  627. string_printf(
  628. sd_app->text_holder,
  629. "Label: %s\nType: %s\n%lu KB total\n%lu KB free",
  630. sd_info.label,
  631. get_fs_type_text(sd_info.fs_type),
  632. sd_info.kb_total,
  633. sd_info.kb_free);
  634. dialog_ex_set_text(
  635. dialog,
  636. string_get_cstr(sd_app->text_holder),
  637. 4,
  638. y_4_line,
  639. AlignLeft,
  640. AlignCenter);
  641. view_holder_start(sd_app->view_holder);
  642. } else {
  643. string_printf(
  644. sd_app->text_holder,
  645. "SD status: %s\n SD info: %s",
  646. fs_error_get_internal_desc(_fs_status(&sd_app->info)),
  647. fs_error_get_internal_desc(sd_info.error));
  648. dialog_ex_set_header(dialog, "Error", 64, 10, AlignCenter, AlignCenter);
  649. dialog_ex_set_text(
  650. dialog,
  651. string_get_cstr(sd_app->text_holder),
  652. 64,
  653. y_2_line,
  654. AlignCenter,
  655. AlignCenter);
  656. view_holder_start(sd_app->view_holder);
  657. }
  658. sd_app->sd_app_state = SdAppStateInfo;
  659. }
  660. break;
  661. case SdAppEventTypeEject:
  662. if(try_to_alloc_view_holder(sd_app, gui)) {
  663. DialogEx* dialog = alloc_and_attach_dialog(sd_app);
  664. dialog_ex_set_left_button_text(dialog, "Back");
  665. dialog_ex_set_right_button_text(dialog, "Eject");
  666. dialog_ex_set_header(
  667. dialog, "Eject SD card?", 64, 10, AlignCenter, AlignCenter);
  668. dialog_ex_set_text(
  669. dialog,
  670. "SD card will be\nunavailable",
  671. 64,
  672. y_2_line,
  673. AlignCenter,
  674. AlignCenter);
  675. view_holder_start(sd_app->view_holder);
  676. sd_app->sd_app_state = SdAppStateEject;
  677. }
  678. break;
  679. case SdAppEventTypeFileSelect:
  680. if(!app_sd_make_path(event.payload.file_select_data.path)) {
  681. SdAppFileSelectResultEvent retval = {.result = false};
  682. furi_check(
  683. osMessageQueuePut(sd_app->result_receiver, &retval, 0, osWaitForever) ==
  684. osOK);
  685. app_reset_state(sd_app);
  686. }
  687. if(try_to_alloc_view_holder(sd_app, gui)) {
  688. sd_app->result_receiver = event.result_receiver;
  689. FileSelect* file_select = alloc_and_attach_file_select(sd_app);
  690. file_select_set_api(file_select, fs_api);
  691. file_select_set_filter(
  692. file_select,
  693. event.payload.file_select_data.path,
  694. event.payload.file_select_data.extension);
  695. file_select_set_result_buffer(
  696. file_select,
  697. event.payload.file_select_data.result,
  698. event.payload.file_select_data.result_size);
  699. if(!file_select_init(file_select)) {
  700. SdAppFileSelectResultEvent retval = {.result = false};
  701. furi_check(
  702. osMessageQueuePut(
  703. sd_app->result_receiver, &retval, 0, osWaitForever) == osOK);
  704. app_reset_state(sd_app);
  705. } else {
  706. sd_app->sd_app_state = SdAppStateFileSelect;
  707. view_holder_start(sd_app->view_holder);
  708. }
  709. } else {
  710. SdAppFileSelectResultEvent retval = {.result = false};
  711. furi_check(
  712. osMessageQueuePut(event.result_receiver, &retval, 0, osWaitForever) ==
  713. osOK);
  714. }
  715. break;
  716. case SdAppEventTypeCheckError:
  717. break;
  718. }
  719. }
  720. }
  721. return 0;
  722. }