mass_storage_scene_file_name.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. if(!furi_hal_usb_is_locked()) {
  57. scene_manager_next_scene(app->scene_manager, MassStorageSceneWork);
  58. } else {
  59. scene_manager_next_scene(app->scene_manager, MassStorageSceneUsbLocked);
  60. }
  61. } // TODO: error message screen
  62. }
  63. }
  64. return consumed;
  65. }
  66. void mass_storage_scene_file_name_on_exit(void* context) {
  67. UNUSED(context);
  68. MassStorageApp* app = context;
  69. void* validator_context = text_input_get_validator_callback_context(app->text_input);
  70. text_input_set_validator(app->text_input, NULL, NULL);
  71. validator_is_file_free(validator_context);
  72. text_input_reset(app->text_input);
  73. }