bad_usb_app.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "bad_usb_app_i.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <storage/storage.h>
  5. #include <lib/toolbox/path.h>
  6. static bool bad_usb_app_custom_event_callback(void* context, uint32_t event) {
  7. furi_assert(context);
  8. BadUsbApp* app = context;
  9. return scene_manager_handle_custom_event(app->scene_manager, event);
  10. }
  11. static bool bad_usb_app_back_event_callback(void* context) {
  12. furi_assert(context);
  13. BadUsbApp* app = context;
  14. return scene_manager_handle_back_event(app->scene_manager);
  15. }
  16. static void bad_usb_app_tick_event_callback(void* context) {
  17. furi_assert(context);
  18. BadUsbApp* app = context;
  19. scene_manager_handle_tick_event(app->scene_manager);
  20. }
  21. static bool bad_usb_check_assets() {
  22. Storage* fs_api = furi_record_open("storage");
  23. File* dir = storage_file_alloc(fs_api);
  24. bool ret = false;
  25. if(storage_dir_open(dir, BAD_USB_APP_PATH_FOLDER)) {
  26. ret = true;
  27. }
  28. storage_dir_close(dir);
  29. storage_file_free(dir);
  30. furi_record_close("storage");
  31. return ret;
  32. }
  33. BadUsbApp* bad_usb_app_alloc(char* arg) {
  34. BadUsbApp* app = malloc(sizeof(BadUsbApp));
  35. if(arg != NULL) {
  36. string_t filename;
  37. string_init(filename);
  38. path_extract_filename_no_ext(arg, filename);
  39. strncpy(app->file_name, string_get_cstr(filename), BAD_USB_FILE_NAME_LEN);
  40. string_clear(filename);
  41. }
  42. app->gui = furi_record_open("gui");
  43. app->notifications = furi_record_open("notification");
  44. app->dialogs = furi_record_open("dialogs");
  45. app->view_dispatcher = view_dispatcher_alloc();
  46. view_dispatcher_enable_queue(app->view_dispatcher);
  47. app->scene_manager = scene_manager_alloc(&bad_usb_scene_handlers, app);
  48. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  49. view_dispatcher_set_tick_event_callback(
  50. app->view_dispatcher, bad_usb_app_tick_event_callback, 500);
  51. view_dispatcher_set_custom_event_callback(
  52. app->view_dispatcher, bad_usb_app_custom_event_callback);
  53. view_dispatcher_set_navigation_event_callback(
  54. app->view_dispatcher, bad_usb_app_back_event_callback);
  55. // Custom Widget
  56. app->widget = widget_alloc();
  57. view_dispatcher_add_view(
  58. app->view_dispatcher, BadUsbAppViewError, widget_get_view(app->widget));
  59. app->bad_usb_view = bad_usb_alloc();
  60. view_dispatcher_add_view(
  61. app->view_dispatcher, BadUsbAppViewWork, bad_usb_get_view(app->bad_usb_view));
  62. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  63. if(furi_hal_usb_is_locked()) {
  64. app->error = BadUsbAppErrorCloseRpc;
  65. scene_manager_next_scene(app->scene_manager, BadUsbSceneError);
  66. } else {
  67. if(*app->file_name != '\0') {
  68. scene_manager_next_scene(app->scene_manager, BadUsbSceneWork);
  69. } else if(bad_usb_check_assets()) {
  70. scene_manager_next_scene(app->scene_manager, BadUsbSceneFileSelect);
  71. } else {
  72. app->error = BadUsbAppErrorNoFiles;
  73. scene_manager_next_scene(app->scene_manager, BadUsbSceneError);
  74. }
  75. }
  76. return app;
  77. }
  78. void bad_usb_app_free(BadUsbApp* app) {
  79. furi_assert(app);
  80. // Views
  81. view_dispatcher_remove_view(app->view_dispatcher, BadUsbAppViewFileSelect);
  82. view_dispatcher_remove_view(app->view_dispatcher, BadUsbAppViewWork);
  83. bad_usb_free(app->bad_usb_view);
  84. // Custom Widget
  85. view_dispatcher_remove_view(app->view_dispatcher, BadUsbAppViewError);
  86. widget_free(app->widget);
  87. // View dispatcher
  88. view_dispatcher_free(app->view_dispatcher);
  89. scene_manager_free(app->scene_manager);
  90. // Close records
  91. furi_record_close("gui");
  92. furi_record_close("notification");
  93. furi_record_close("dialogs");
  94. free(app);
  95. }
  96. int32_t bad_usb_app(void* p) {
  97. BadUsbApp* bad_usb_app = bad_usb_app_alloc((char*)p);
  98. view_dispatcher_run(bad_usb_app->view_dispatcher);
  99. bad_usb_app_free(bad_usb_app);
  100. return 0;
  101. }