mass_storage_scene_file_name.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "../mass_storage_app_i.h"
  2. #define WRITE_BUF_LEN 4096
  3. static void mass_storage_file_name_text_callback(void* context) {
  4. furi_assert(context);
  5. MassStorageApp* app = context;
  6. view_dispatcher_send_custom_event(app->view_dispatcher, MassStorageCustomEventNameInput);
  7. }
  8. static bool mass_storage_create_image(Storage* storage, const char* file_path, uint32_t size) {
  9. FURI_LOG_I("TAG", "Creating image %s, len:%lu", file_path, size);
  10. File* file = storage_file_alloc(storage);
  11. bool success = false;
  12. uint8_t* buffer = malloc(WRITE_BUF_LEN);
  13. do {
  14. if(!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) break;
  15. if(!storage_file_seek(file, size, true)) break;
  16. if(!storage_file_seek(file, 0, true)) break;
  17. // Zero out first 4k - partition table and adjacent data
  18. if(!storage_file_write(file, buffer, WRITE_BUF_LEN)) break;
  19. success = true;
  20. } while(false);
  21. free(buffer);
  22. storage_file_close(file);
  23. storage_file_free(file);
  24. return success;
  25. }
  26. void mass_storage_scene_file_name_on_enter(void* context) {
  27. MassStorageApp* app = context;
  28. text_input_set_header_text(app->text_input, "Enter image name");
  29. ValidatorIsFile* validator_is_file =
  30. validator_is_file_alloc_init(MASS_STORAGE_APP_PATH_FOLDER, MASS_STORAGE_APP_EXTENSION, "");
  31. text_input_set_validator(app->text_input, validator_is_file_callback, validator_is_file);
  32. text_input_set_result_callback(
  33. app->text_input,
  34. mass_storage_file_name_text_callback,
  35. app,
  36. app->new_file_name,
  37. MASS_STORAGE_FILE_NAME_LEN,
  38. true);
  39. view_dispatcher_switch_to_view(app->view_dispatcher, MassStorageAppViewTextInput);
  40. }
  41. bool mass_storage_scene_file_name_on_event(void* context, SceneManagerEvent event) {
  42. UNUSED(event);
  43. MassStorageApp* app = context;
  44. bool consumed = false;
  45. if(event.type == SceneManagerEventTypeCustom) {
  46. if(event.event == MassStorageCustomEventNameInput) {
  47. mass_storage_app_show_loading_popup(app, true);
  48. furi_string_printf(
  49. app->file_path,
  50. "%s/%s%s",
  51. MASS_STORAGE_APP_PATH_FOLDER,
  52. app->new_file_name,
  53. MASS_STORAGE_APP_EXTENSION);
  54. if(mass_storage_create_image(
  55. app->fs_api, furi_string_get_cstr(app->file_path), app->new_file_size)) {
  56. scene_manager_next_scene(app->scene_manager, MassStorageSceneWork);
  57. } // TODO: error message screen
  58. }
  59. }
  60. return consumed;
  61. }
  62. void mass_storage_scene_file_name_on_exit(void* context) {
  63. UNUSED(context);
  64. MassStorageApp* app = context;
  65. void* validator_context = text_input_get_validator_callback_context(app->text_input);
  66. text_input_set_validator(app->text_input, NULL, NULL);
  67. validator_is_file_free(validator_context);
  68. text_input_reset(app->text_input);
  69. }