sd-filesystem.c 29 KB

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