sd-filesystem.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. for(char* p = strchr(file_path + 1, '/'); p; p = strchr(p + 1, '/')) {
  271. *p = '\0';
  272. SDError result = f_mkdir(file_path);
  273. if(result != SD_OK) {
  274. if(result != SD_EXIST) {
  275. *p = '/';
  276. free(file_path);
  277. return false;
  278. }
  279. }
  280. *p = '/';
  281. }
  282. free(file_path);
  283. }
  284. return true;
  285. }
  286. /******************* Draw callbacks *******************/
  287. static void sd_icon_draw_callback(Canvas* canvas, void* context) {
  288. furi_assert(canvas);
  289. furi_assert(context);
  290. SdApp* sd_app = context;
  291. switch(sd_app->info.status) {
  292. case SD_NO_CARD:
  293. break;
  294. case SD_OK:
  295. canvas_draw_icon(canvas, 0, 0, sd_app->icon.mounted);
  296. break;
  297. default:
  298. canvas_draw_icon(canvas, 0, 0, sd_app->icon.fail);
  299. break;
  300. }
  301. }
  302. /******************* SD-api callbacks *******************/
  303. bool sd_api_file_select(
  304. SdApp* sd_app,
  305. const char* path,
  306. const char* extension,
  307. char* result,
  308. uint8_t result_size) {
  309. bool retval = false;
  310. osMessageQueueId_t return_event_queue =
  311. osMessageQueueNew(1, sizeof(SdAppFileSelectResultEvent), NULL);
  312. SdAppEvent message = {
  313. .type = SdAppEventTypeFileSelect,
  314. .result_receiver = return_event_queue,
  315. .payload = {
  316. .file_select_data = {
  317. .path = path,
  318. .extension = extension,
  319. .result = result,
  320. .result_size = result_size}}};
  321. furi_check(osMessageQueuePut(sd_app->event_queue, &message, 0, osWaitForever) == osOK);
  322. SdAppFileSelectResultEvent event;
  323. while(1) {
  324. osStatus_t event_status =
  325. osMessageQueueGet(sd_app->event_queue, &event, NULL, osWaitForever);
  326. if(event_status == osOK) {
  327. retval = event.result;
  328. break;
  329. }
  330. }
  331. return retval;
  332. }
  333. /******************* View callbacks *******************/
  334. void app_view_back_callback(void* context) {
  335. furi_assert(context);
  336. SdApp* sd_app = context;
  337. SdAppEvent message = {.type = SdAppEventTypeBack};
  338. furi_check(osMessageQueuePut(sd_app->event_queue, &message, 0, osWaitForever) == osOK);
  339. }
  340. void app_view_dialog_callback(DialogExResult result, void* context) {
  341. furi_assert(context);
  342. SdApp* sd_app = context;
  343. if(result == DialogExResultLeft) {
  344. SdAppEvent message = {.type = SdAppEventTypeBack};
  345. furi_check(osMessageQueuePut(sd_app->event_queue, &message, 0, osWaitForever) == osOK);
  346. } else if(result == DialogExResultRight) {
  347. SdAppEvent message = {.type = SdAppEventTypeOK};
  348. furi_check(osMessageQueuePut(sd_app->event_queue, &message, 0, osWaitForever) == osOK);
  349. }
  350. }
  351. void app_view_file_select_callback(bool result, void* context) {
  352. furi_assert(context);
  353. SdApp* sd_app = context;
  354. if(result) {
  355. SdAppEvent message = {.type = SdAppEventTypeOK};
  356. furi_check(osMessageQueuePut(sd_app->event_queue, &message, 0, osWaitForever) == osOK);
  357. } else {
  358. SdAppEvent message = {.type = SdAppEventTypeBack};
  359. furi_check(osMessageQueuePut(sd_app->event_queue, &message, 0, osWaitForever) == osOK);
  360. }
  361. }
  362. /******************* Menu callbacks *******************/
  363. void app_sd_info_callback(void* context) {
  364. furi_assert(context);
  365. SdApp* sd_app = context;
  366. SdAppEvent message = {.type = SdAppEventTypeInfo};
  367. furi_check(osMessageQueuePut(sd_app->event_queue, &message, 0, osWaitForever) == osOK);
  368. }
  369. void app_sd_format_callback(void* context) {
  370. furi_assert(context);
  371. SdApp* sd_app = context;
  372. SdAppEvent message = {.type = SdAppEventTypeFormat};
  373. furi_check(osMessageQueuePut(sd_app->event_queue, &message, 0, osWaitForever) == osOK);
  374. }
  375. void app_sd_eject_callback(void* context) {
  376. furi_assert(context);
  377. SdApp* sd_app = context;
  378. SdAppEvent message = {.type = SdAppEventTypeEject};
  379. furi_check(osMessageQueuePut(sd_app->event_queue, &message, 0, osWaitForever) == osOK);
  380. }
  381. /******************* Cli callbacks *******************/
  382. static void cli_sd_status(string_t args, void* _ctx) {
  383. SdApp* sd_app = (SdApp*)_ctx;
  384. printf("SD status: ");
  385. printf(fs_error_get_internal_desc(sd_app->info.status));
  386. printf("\r\n");
  387. }
  388. static void cli_sd_format(string_t args, void* _ctx) {
  389. SdApp* sd_app = (SdApp*)_ctx;
  390. printf("formatting SD card, please wait\r\n");
  391. // format card
  392. app_sd_format_internal(sd_app);
  393. if(sd_app->info.status != SD_OK) {
  394. printf("SD card format error: ");
  395. printf(fs_error_get_internal_desc(sd_app->info.status));
  396. printf("\r\n");
  397. } else {
  398. printf("SD card formatted\r\n");
  399. }
  400. }
  401. static void cli_sd_info(string_t args, void* _ctx) {
  402. SdApp* sd_app = (SdApp*)_ctx;
  403. SDInfo sd_info;
  404. get_sd_info(sd_app, &sd_info);
  405. if(sd_info.error == SD_OK) {
  406. const char* fs_type = get_fs_type_text(sd_info.fs_type);
  407. printf("Label: %s\r\n", sd_info.label);
  408. printf("%s\r\n", fs_type);
  409. printf("Cluster: %d sectors\r\n", sd_info.cluster_size);
  410. printf("Sector: %d bytes\r\n", sd_info.sector_size);
  411. printf("%lu KB total\r\n", sd_info.kb_total);
  412. printf("%lu KB free\r\n", sd_info.kb_free);
  413. } else {
  414. printf("SD status error: %s\r\n", fs_error_get_internal_desc(_fs_status(&sd_app->info)));
  415. printf("SD info error: %s\r\n", fs_error_get_internal_desc(sd_info.error));
  416. }
  417. }
  418. /******************* Test *******************/
  419. bool try_to_alloc_view_holder(SdApp* sd_app, Gui* gui) {
  420. bool result = false;
  421. _fs_lock(&sd_app->info);
  422. if(sd_app->view_holder == NULL) {
  423. sd_app->view_holder = view_holder_alloc();
  424. view_holder_attach_to_gui(sd_app->view_holder, gui);
  425. view_holder_set_back_callback(sd_app->view_holder, app_view_back_callback, sd_app);
  426. result = true;
  427. }
  428. _fs_unlock(&sd_app->info);
  429. return result;
  430. }
  431. DialogEx* alloc_and_attach_dialog(SdApp* sd_app) {
  432. DialogEx* dialog = dialog_ex_alloc();
  433. dialog_ex_set_context(dialog, sd_app);
  434. dialog_ex_set_result_callback(dialog, app_view_dialog_callback);
  435. view_holder_set_view(sd_app->view_holder, dialog_ex_get_view(dialog));
  436. view_holder_set_free_callback(sd_app->view_holder, (FreeCallback)dialog_ex_free, dialog);
  437. return dialog;
  438. }
  439. FileSelect* alloc_and_attach_file_select(SdApp* sd_app) {
  440. FileSelect* file_select = file_select_alloc();
  441. file_select_set_callback(file_select, app_view_file_select_callback, sd_app);
  442. view_holder_set_view(sd_app->view_holder, file_select_get_view(file_select));
  443. view_holder_set_free_callback(
  444. sd_app->view_holder, (FreeCallback)file_select_free, file_select);
  445. return file_select;
  446. }
  447. void free_view_holder(SdApp* sd_app) {
  448. _fs_lock(&sd_app->info);
  449. if(sd_app->view_holder) {
  450. view_holder_free(sd_app->view_holder);
  451. sd_app->view_holder = NULL;
  452. }
  453. _fs_unlock(&sd_app->info);
  454. }
  455. void app_reset_state(SdApp* sd_app) {
  456. view_holder_stop(sd_app->view_holder);
  457. free_view_holder(sd_app);
  458. string_set_str(sd_app->text_holder, "");
  459. sd_app->sd_app_state = SdAppStateBackground;
  460. }
  461. /******************* Main app *******************/
  462. int32_t sd_filesystem(void* p) {
  463. SdApp* sd_app = sd_app_alloc();
  464. FS_Api* fs_api = fs_api_alloc();
  465. Gui* gui = furi_record_open("gui");
  466. Cli* cli = furi_record_open("cli");
  467. ValueMutex* menu_vm = furi_record_open("menu");
  468. gui_add_view_port(gui, sd_app->icon.view_port, GuiLayerStatusBarLeft);
  469. cli_add_command(cli, "sd_status", cli_sd_status, sd_app);
  470. cli_add_command(cli, "sd_format", cli_sd_format, sd_app);
  471. cli_add_command(cli, "sd_info", cli_sd_info, sd_app);
  472. // add api record
  473. furi_record_create("sdcard", fs_api);
  474. // init menu
  475. // TODO menu icon
  476. MenuItem* menu_item;
  477. menu_item = menu_item_alloc_menu("SD Card", assets_icons_get(I_SDcardMounted_11x8));
  478. menu_item_subitem_add(
  479. menu_item, menu_item_alloc_function("Info", NULL, app_sd_info_callback, sd_app));
  480. menu_item_subitem_add(
  481. menu_item, menu_item_alloc_function("Format", NULL, app_sd_format_callback, sd_app));
  482. menu_item_subitem_add(
  483. menu_item, menu_item_alloc_function("Eject", NULL, app_sd_eject_callback, sd_app));
  484. // add item to menu
  485. furi_check(menu_vm);
  486. with_value_mutex(
  487. menu_vm, (Menu * menu) { menu_item_add(menu, menu_item); });
  488. printf("[sd_filesystem] start\r\n");
  489. // add api record
  490. furi_record_create("sdcard", fs_api);
  491. furi_record_create("sdcard-ex", &sd_app->sd_card_api);
  492. // sd card cycle
  493. bool sd_was_present = true;
  494. // init detect pins
  495. hal_sd_detect_init();
  496. while(true) {
  497. if(sd_was_present) {
  498. if(hal_sd_detect()) {
  499. printf("[sd_filesystem] card detected\r\n");
  500. app_sd_mount_card(sd_app);
  501. if(sd_app->info.status != SD_OK) {
  502. printf(
  503. "[sd_filesystem] sd init error: %s\r\n",
  504. fs_error_get_internal_desc(sd_app->info.status));
  505. app_sd_notify_error();
  506. } else {
  507. printf("[sd_filesystem] sd init ok\r\n");
  508. app_sd_notify_success();
  509. }
  510. view_port_enabled_set(sd_app->icon.view_port, true);
  511. sd_was_present = false;
  512. if(!hal_sd_detect()) {
  513. printf("[sd_filesystem] card removed\r\n");
  514. view_port_enabled_set(sd_app->icon.view_port, false);
  515. app_sd_unmount_card(sd_app);
  516. sd_was_present = true;
  517. }
  518. }
  519. } else {
  520. if(!hal_sd_detect()) {
  521. printf("[sd_filesystem] card removed\r\n");
  522. view_port_enabled_set(sd_app->icon.view_port, false);
  523. app_sd_unmount_card(sd_app);
  524. sd_was_present = true;
  525. app_sd_notify_eject();
  526. }
  527. }
  528. SdAppEvent event;
  529. osStatus_t event_status = osMessageQueueGet(sd_app->event_queue, &event, NULL, 1000);
  530. const uint8_t y_1_line = 32;
  531. const uint8_t y_2_line = 32;
  532. const uint8_t y_4_line = 26;
  533. if(event_status == osOK) {
  534. switch(event.type) {
  535. case SdAppEventTypeOK:
  536. switch(sd_app->sd_app_state) {
  537. case SdAppStateFormat: {
  538. DialogEx* dialog = view_holder_get_free_context(sd_app->view_holder);
  539. dialog_ex_set_left_button_text(dialog, NULL);
  540. dialog_ex_set_right_button_text(dialog, NULL);
  541. dialog_ex_set_header(
  542. dialog, "Formatting...", 64, y_1_line, AlignCenter, AlignCenter);
  543. dialog_ex_set_text(dialog, NULL, 0, 0, AlignCenter, AlignCenter);
  544. sd_app->sd_app_state = SdAppStateFormatInProgress;
  545. delay(100);
  546. app_sd_format_internal(sd_app);
  547. app_sd_notify_success();
  548. dialog_ex_set_left_button_text(dialog, "Back");
  549. dialog_ex_set_header(
  550. dialog, "SD card formatted", 64, 10, AlignCenter, AlignCenter);
  551. dialog_ex_set_text(
  552. dialog, "Press back to return", 64, y_1_line, AlignCenter, AlignCenter);
  553. sd_app->sd_app_state = SdAppStateFormatCompleted;
  554. }; break;
  555. case SdAppStateEject: {
  556. DialogEx* dialog = view_holder_get_free_context(sd_app->view_holder);
  557. dialog_ex_set_right_button_text(dialog, NULL);
  558. dialog_ex_set_header(
  559. dialog, "SD card ejected", 64, 10, AlignCenter, AlignCenter);
  560. dialog_ex_set_text(
  561. dialog,
  562. "Now the SD card\ncan be removed.",
  563. 64,
  564. y_2_line,
  565. AlignCenter,
  566. AlignCenter);
  567. sd_app->sd_app_state = SdAppStateEjected;
  568. app_sd_unmount_card(sd_app);
  569. app_sd_notify_eject();
  570. }; break;
  571. case SdAppStateFileSelect: {
  572. SdAppFileSelectResultEvent retval = {.result = true};
  573. furi_check(
  574. osMessageQueuePut(event.result_receiver, &retval, 0, osWaitForever) ==
  575. osOK);
  576. app_reset_state(sd_app);
  577. }; break;
  578. default:
  579. break;
  580. }
  581. break;
  582. case SdAppEventTypeBack:
  583. switch(sd_app->sd_app_state) {
  584. case SdAppStateFormatInProgress:
  585. break;
  586. case SdAppStateFileSelect: {
  587. SdAppFileSelectResultEvent retval = {.result = false};
  588. furi_check(
  589. osMessageQueuePut(event.result_receiver, &retval, 0, osWaitForever) ==
  590. osOK);
  591. app_reset_state(sd_app);
  592. }; break;
  593. default:
  594. app_reset_state(sd_app);
  595. break;
  596. }
  597. break;
  598. case SdAppEventTypeFormat:
  599. if(try_to_alloc_view_holder(sd_app, gui)) {
  600. DialogEx* dialog = alloc_and_attach_dialog(sd_app);
  601. dialog_ex_set_left_button_text(dialog, "Back");
  602. dialog_ex_set_right_button_text(dialog, "Format");
  603. dialog_ex_set_header(
  604. dialog, "Format SD card?", 64, 10, AlignCenter, AlignCenter);
  605. dialog_ex_set_text(
  606. dialog, "All data will be lost.", 64, y_1_line, AlignCenter, AlignCenter);
  607. view_holder_start(sd_app->view_holder);
  608. sd_app->sd_app_state = SdAppStateFormat;
  609. }
  610. break;
  611. case SdAppEventTypeInfo:
  612. if(try_to_alloc_view_holder(sd_app, gui)) {
  613. DialogEx* dialog = alloc_and_attach_dialog(sd_app);
  614. dialog_ex_set_left_button_text(dialog, "Back");
  615. SDInfo sd_info;
  616. get_sd_info(sd_app, &sd_info);
  617. if(sd_info.error == SD_OK) {
  618. string_printf(
  619. sd_app->text_holder,
  620. "Label: %s\nType: %s\n%lu KB total\n%lu KB free",
  621. sd_info.label,
  622. get_fs_type_text(sd_info.fs_type),
  623. sd_info.kb_total,
  624. sd_info.kb_free);
  625. dialog_ex_set_text(
  626. dialog,
  627. string_get_cstr(sd_app->text_holder),
  628. 4,
  629. y_4_line,
  630. AlignLeft,
  631. AlignCenter);
  632. view_holder_start(sd_app->view_holder);
  633. } else {
  634. string_printf(
  635. sd_app->text_holder,
  636. "SD status: %s\n SD info: %s",
  637. fs_error_get_internal_desc(_fs_status(&sd_app->info)),
  638. fs_error_get_internal_desc(sd_info.error));
  639. dialog_ex_set_header(dialog, "Error", 64, 10, AlignCenter, AlignCenter);
  640. dialog_ex_set_text(
  641. dialog,
  642. string_get_cstr(sd_app->text_holder),
  643. 64,
  644. y_2_line,
  645. AlignCenter,
  646. AlignCenter);
  647. view_holder_start(sd_app->view_holder);
  648. }
  649. sd_app->sd_app_state = SdAppStateInfo;
  650. }
  651. break;
  652. case SdAppEventTypeEject:
  653. if(try_to_alloc_view_holder(sd_app, gui)) {
  654. DialogEx* dialog = alloc_and_attach_dialog(sd_app);
  655. dialog_ex_set_left_button_text(dialog, "Back");
  656. dialog_ex_set_right_button_text(dialog, "Eject");
  657. dialog_ex_set_header(
  658. dialog, "Eject SD card?", 64, 10, AlignCenter, AlignCenter);
  659. dialog_ex_set_text(
  660. dialog,
  661. "SD card will be\nunavailable",
  662. 64,
  663. y_2_line,
  664. AlignCenter,
  665. AlignCenter);
  666. view_holder_start(sd_app->view_holder);
  667. sd_app->sd_app_state = SdAppStateEject;
  668. }
  669. break;
  670. case SdAppEventTypeFileSelect:
  671. if(!app_sd_make_path(event.payload.file_select_data.path)) {
  672. }
  673. if(try_to_alloc_view_holder(sd_app, gui)) {
  674. sd_app->result_receiver = event.result_receiver;
  675. FileSelect* file_select = alloc_and_attach_file_select(sd_app);
  676. file_select_set_api(file_select, fs_api);
  677. file_select_set_filter(
  678. file_select,
  679. event.payload.file_select_data.path,
  680. event.payload.file_select_data.extension);
  681. file_select_set_result_buffer(
  682. file_select,
  683. event.payload.file_select_data.result,
  684. event.payload.file_select_data.result_size);
  685. if(!file_select_init(file_select)) {
  686. }
  687. sd_app->sd_app_state = SdAppStateFileSelect;
  688. } else {
  689. SdAppFileSelectResultEvent retval = {.result = false};
  690. furi_check(
  691. osMessageQueuePut(event.result_receiver, &retval, 0, osWaitForever) ==
  692. osOK);
  693. }
  694. break;
  695. case SdAppEventTypeCheckError:
  696. break;
  697. }
  698. }
  699. }
  700. return 0;
  701. }