bad_usb_app.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "bad_usb_app_i.h"
  2. #include "bad_usb_settings_filename.h"
  3. #include <furi.h>
  4. #include <furi_hal.h>
  5. #include <storage/storage.h>
  6. #include <lib/toolbox/path.h>
  7. #define BAD_USB_SETTINGS_PATH BAD_USB_APP_BASE_FOLDER "/" BAD_USB_SETTINGS_FILE_NAME
  8. static bool bad_usb_app_custom_event_callback(void* context, uint32_t event) {
  9. furi_assert(context);
  10. BadUsbApp* app = context;
  11. return scene_manager_handle_custom_event(app->scene_manager, event);
  12. }
  13. static bool bad_usb_app_back_event_callback(void* context) {
  14. furi_assert(context);
  15. BadUsbApp* app = context;
  16. return scene_manager_handle_back_event(app->scene_manager);
  17. }
  18. static void bad_usb_app_tick_event_callback(void* context) {
  19. furi_assert(context);
  20. BadUsbApp* app = context;
  21. scene_manager_handle_tick_event(app->scene_manager);
  22. }
  23. static void bad_usb_load_settings(BadUsbApp* app) {
  24. File* settings_file = storage_file_alloc(furi_record_open(RECORD_STORAGE));
  25. if(storage_file_open(settings_file, BAD_USB_SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  26. char chr;
  27. while((storage_file_read(settings_file, &chr, 1) == 1) &&
  28. !storage_file_eof(settings_file) && !isspace(chr)) {
  29. furi_string_push_back(app->keyboard_layout, chr);
  30. }
  31. } else {
  32. furi_string_reset(app->keyboard_layout);
  33. }
  34. storage_file_close(settings_file);
  35. storage_file_free(settings_file);
  36. if(!furi_string_empty(app->keyboard_layout)) {
  37. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  38. FileInfo layout_file_info;
  39. FS_Error file_check_err = storage_common_stat(
  40. fs_api, furi_string_get_cstr(app->keyboard_layout), &layout_file_info);
  41. furi_record_close(RECORD_STORAGE);
  42. if(file_check_err != FSE_OK) {
  43. furi_string_reset(app->keyboard_layout);
  44. return;
  45. }
  46. if(layout_file_info.size != 256) {
  47. furi_string_reset(app->keyboard_layout);
  48. }
  49. }
  50. }
  51. static void bad_usb_save_settings(BadUsbApp* app) {
  52. File* settings_file = storage_file_alloc(furi_record_open(RECORD_STORAGE));
  53. if(storage_file_open(settings_file, BAD_USB_SETTINGS_PATH, FSAM_WRITE, FSOM_OPEN_ALWAYS)) {
  54. storage_file_write(
  55. settings_file,
  56. furi_string_get_cstr(app->keyboard_layout),
  57. furi_string_size(app->keyboard_layout));
  58. storage_file_write(settings_file, "\n", 1);
  59. }
  60. storage_file_close(settings_file);
  61. storage_file_free(settings_file);
  62. }
  63. BadUsbApp* bad_usb_app_alloc(char* arg) {
  64. BadUsbApp* app = malloc(sizeof(BadUsbApp));
  65. app->bad_usb_script = NULL;
  66. app->file_path = furi_string_alloc();
  67. app->keyboard_layout = furi_string_alloc();
  68. if(arg && strlen(arg)) {
  69. furi_string_set(app->file_path, arg);
  70. }
  71. bad_usb_load_settings(app);
  72. app->gui = furi_record_open(RECORD_GUI);
  73. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  74. app->dialogs = furi_record_open(RECORD_DIALOGS);
  75. app->view_dispatcher = view_dispatcher_alloc();
  76. view_dispatcher_enable_queue(app->view_dispatcher);
  77. app->scene_manager = scene_manager_alloc(&bad_usb_scene_handlers, app);
  78. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  79. view_dispatcher_set_tick_event_callback(
  80. app->view_dispatcher, bad_usb_app_tick_event_callback, 500);
  81. view_dispatcher_set_custom_event_callback(
  82. app->view_dispatcher, bad_usb_app_custom_event_callback);
  83. view_dispatcher_set_navigation_event_callback(
  84. app->view_dispatcher, bad_usb_app_back_event_callback);
  85. // Custom Widget
  86. app->widget = widget_alloc();
  87. view_dispatcher_add_view(
  88. app->view_dispatcher, BadUsbAppViewError, widget_get_view(app->widget));
  89. app->submenu = submenu_alloc();
  90. view_dispatcher_add_view(
  91. app->view_dispatcher, BadUsbAppViewConfig, submenu_get_view(app->submenu));
  92. app->bad_usb_view = bad_usb_alloc();
  93. view_dispatcher_add_view(
  94. app->view_dispatcher, BadUsbAppViewWork, bad_usb_get_view(app->bad_usb_view));
  95. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  96. if(furi_hal_usb_is_locked()) {
  97. app->error = BadUsbAppErrorCloseRpc;
  98. app->usb_if_prev = NULL;
  99. scene_manager_next_scene(app->scene_manager, BadUsbSceneError);
  100. } else {
  101. app->usb_if_prev = furi_hal_usb_get_config();
  102. furi_check(furi_hal_usb_set_config(NULL, NULL));
  103. if(!furi_string_empty(app->file_path)) {
  104. app->bad_usb_script = bad_usb_script_open(app->file_path);
  105. bad_usb_script_set_keyboard_layout(app->bad_usb_script, app->keyboard_layout);
  106. scene_manager_next_scene(app->scene_manager, BadUsbSceneWork);
  107. } else {
  108. furi_string_set(app->file_path, BAD_USB_APP_BASE_FOLDER);
  109. scene_manager_next_scene(app->scene_manager, BadUsbSceneFileSelect);
  110. }
  111. }
  112. return app;
  113. }
  114. void bad_usb_app_free(BadUsbApp* app) {
  115. furi_assert(app);
  116. if(app->bad_usb_script) {
  117. bad_usb_script_close(app->bad_usb_script);
  118. app->bad_usb_script = NULL;
  119. }
  120. if(app->usb_if_prev) {
  121. furi_check(furi_hal_usb_set_config(app->usb_if_prev, NULL));
  122. }
  123. // Views
  124. view_dispatcher_remove_view(app->view_dispatcher, BadUsbAppViewWork);
  125. bad_usb_free(app->bad_usb_view);
  126. // Custom Widget
  127. view_dispatcher_remove_view(app->view_dispatcher, BadUsbAppViewError);
  128. widget_free(app->widget);
  129. // Submenu
  130. view_dispatcher_remove_view(app->view_dispatcher, BadUsbAppViewConfig);
  131. submenu_free(app->submenu);
  132. // View dispatcher
  133. view_dispatcher_free(app->view_dispatcher);
  134. scene_manager_free(app->scene_manager);
  135. // Close records
  136. furi_record_close(RECORD_GUI);
  137. furi_record_close(RECORD_NOTIFICATION);
  138. furi_record_close(RECORD_DIALOGS);
  139. bad_usb_save_settings(app);
  140. furi_string_free(app->file_path);
  141. furi_string_free(app->keyboard_layout);
  142. free(app);
  143. }
  144. int32_t bad_usb_app(void* p) {
  145. BadUsbApp* bad_usb_app = bad_usb_app_alloc((char*)p);
  146. view_dispatcher_run(bad_usb_app->view_dispatcher);
  147. bad_usb_app_free(bad_usb_app);
  148. return 0;
  149. }