bad_usb_app.c 3.4 KB

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