sd-filesystem.c 29 KB

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