mag.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include "mag_i.h"
  2. #define TAG "Mag"
  3. static bool mag_debug_custom_event_callback(void* context, uint32_t event) {
  4. furi_assert(context);
  5. Mag* mag = context;
  6. return scene_manager_handle_custom_event(mag->scene_manager, event);
  7. }
  8. static bool mag_debug_back_event_callback(void* context) {
  9. furi_assert(context);
  10. Mag* mag = context;
  11. return scene_manager_handle_back_event(mag->scene_manager);
  12. }
  13. static Mag* mag_alloc() {
  14. Mag* mag = malloc(sizeof(Mag));
  15. mag->storage = furi_record_open(RECORD_STORAGE);
  16. mag->dialogs = furi_record_open(RECORD_DIALOGS);
  17. mag->file_name = furi_string_alloc();
  18. mag->file_path = furi_string_alloc_set(MAG_APP_FOLDER);
  19. mag->args = furi_string_alloc();
  20. mag->view_dispatcher = view_dispatcher_alloc();
  21. mag->scene_manager = scene_manager_alloc(&mag_scene_handlers, mag);
  22. view_dispatcher_enable_queue(mag->view_dispatcher);
  23. view_dispatcher_set_event_callback_context(mag->view_dispatcher, mag);
  24. view_dispatcher_set_custom_event_callback(
  25. mag->view_dispatcher, mag_debug_custom_event_callback);
  26. view_dispatcher_set_navigation_event_callback(
  27. mag->view_dispatcher, mag_debug_back_event_callback);
  28. mag->mag_dev = mag_device_alloc();
  29. mag_state_load(&mag->state);
  30. // Open GUI record
  31. mag->gui = furi_record_open(RECORD_GUI);
  32. // Open Notification record
  33. mag->notifications = furi_record_open(RECORD_NOTIFICATION);
  34. // Submenu
  35. mag->submenu = submenu_alloc();
  36. view_dispatcher_add_view(mag->view_dispatcher, MagViewSubmenu, submenu_get_view(mag->submenu));
  37. // Popup
  38. mag->popup = popup_alloc();
  39. view_dispatcher_add_view(mag->view_dispatcher, MagViewPopup, popup_get_view(mag->popup));
  40. // Loading
  41. mag->loading = loading_alloc();
  42. view_dispatcher_add_view(mag->view_dispatcher, MagViewLoading, loading_get_view(mag->loading));
  43. // Widget
  44. mag->widget = widget_alloc();
  45. view_dispatcher_add_view(mag->view_dispatcher, MagViewWidget, widget_get_view(mag->widget));
  46. // Variable Item List
  47. mag->variable_item_list = variable_item_list_alloc();
  48. view_dispatcher_add_view(
  49. mag->view_dispatcher,
  50. MagViewVariableItemList,
  51. variable_item_list_get_view(mag->variable_item_list));
  52. // Text Input
  53. mag->text_input = text_input_alloc();
  54. view_dispatcher_add_view(
  55. mag->view_dispatcher, MagViewTextInput, text_input_get_view(mag->text_input));
  56. // Disable expansion protocol to avoid interference with UART Handle
  57. mag->expansion = furi_record_open(RECORD_EXPANSION);
  58. expansion_disable(mag->expansion);
  59. // Move UART here? conditional upon setting?
  60. return mag;
  61. }
  62. static void mag_free(Mag* mag) {
  63. furi_assert(mag);
  64. furi_string_free(mag->file_name);
  65. furi_string_free(mag->file_path);
  66. furi_string_free(mag->args);
  67. // Mag device
  68. mag_device_free(mag->mag_dev);
  69. mag->mag_dev = NULL;
  70. // Submenu
  71. view_dispatcher_remove_view(mag->view_dispatcher, MagViewSubmenu);
  72. submenu_free(mag->submenu);
  73. // Popup
  74. view_dispatcher_remove_view(mag->view_dispatcher, MagViewPopup);
  75. popup_free(mag->popup);
  76. // Loading
  77. view_dispatcher_remove_view(mag->view_dispatcher, MagViewLoading);
  78. loading_free(mag->loading);
  79. // Widget
  80. view_dispatcher_remove_view(mag->view_dispatcher, MagViewWidget);
  81. widget_free(mag->widget);
  82. // Variable Item List
  83. view_dispatcher_remove_view(mag->view_dispatcher, MagViewVariableItemList);
  84. variable_item_list_free(mag->variable_item_list);
  85. // TextInput
  86. view_dispatcher_remove_view(mag->view_dispatcher, MagViewTextInput);
  87. text_input_free(mag->text_input);
  88. // View Dispatcher
  89. view_dispatcher_free(mag->view_dispatcher);
  90. // Scene Manager
  91. scene_manager_free(mag->scene_manager);
  92. // GUI
  93. furi_record_close(RECORD_GUI);
  94. mag->gui = NULL;
  95. // Notifications
  96. furi_record_close(RECORD_NOTIFICATION);
  97. mag->notifications = NULL;
  98. // Return previous state of expansion
  99. expansion_enable(mag->expansion);
  100. furi_record_close(RECORD_EXPANSION);
  101. furi_record_close(RECORD_STORAGE);
  102. furi_record_close(RECORD_DIALOGS);
  103. free(mag);
  104. }
  105. // entry point for app
  106. int32_t mag_app(void* p) {
  107. const char* args = p;
  108. Mag* mag = mag_alloc();
  109. if(args && strlen(args)) {
  110. furi_string_set(mag->args, args);
  111. }
  112. mag_make_app_folder(mag);
  113. mag_migrate_and_copy_files(mag);
  114. // Enable 5v power, multiple attempts to avoid issues with power chip protection false triggering
  115. uint8_t attempts = 0;
  116. bool otg_was_enabled = furi_hal_power_is_otg_enabled();
  117. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  118. furi_hal_power_enable_otg();
  119. furi_delay_ms(10);
  120. }
  121. view_dispatcher_attach_to_gui(mag->view_dispatcher, mag->gui, ViewDispatcherTypeFullscreen);
  122. if(furi_string_empty(mag->args)) {
  123. scene_manager_next_scene(mag->scene_manager, MagSceneStart);
  124. } else {
  125. mag_device_load_data(mag->mag_dev, mag->args, true);
  126. MagTrackState auto_track = mag_device_autoselect_track_state(mag->mag_dev);
  127. if(auto_track) {
  128. mag->state.track = auto_track;
  129. }
  130. scene_manager_next_scene(mag->scene_manager, MagSceneEmulate);
  131. }
  132. view_dispatcher_run(mag->view_dispatcher);
  133. // Disable 5v power
  134. if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
  135. furi_hal_power_disable_otg();
  136. }
  137. mag_free(mag);
  138. return 0;
  139. }
  140. void mag_make_app_folder(Mag* mag) {
  141. furi_assert(mag);
  142. if(!storage_simply_mkdir(mag->storage, MAG_APP_FOLDER)) {
  143. dialog_message_show_storage_error(mag->dialogs, "Cannot create\napp folder");
  144. }
  145. }
  146. void mag_migrate_and_copy_files(Mag* mag) {
  147. furi_assert(mag);
  148. Storage* storage = mag->storage;
  149. storage_common_migrate(storage, EXT_PATH("magspoof"), STORAGE_APP_DATA_PATH_PREFIX);
  150. storage_common_migrate(storage, EXT_PATH("mag"), STORAGE_APP_DATA_PATH_PREFIX);
  151. if(!storage_common_exists(storage, APP_DATA_PATH(MAG_EXAMPLE_FILE_1))) {
  152. storage_common_copy(
  153. storage, APP_ASSETS_PATH(MAG_EXAMPLE_FILE_1), APP_DATA_PATH(MAG_EXAMPLE_FILE_1));
  154. }
  155. if(!storage_common_exists(storage, APP_DATA_PATH(MAG_EXAMPLE_FILE_2))) {
  156. storage_common_copy(
  157. storage, APP_ASSETS_PATH(MAG_EXAMPLE_FILE_2), APP_DATA_PATH(MAG_EXAMPLE_FILE_2));
  158. }
  159. if(!storage_common_exists(storage, APP_DATA_PATH(MAG_EXAMPLE_FILE_3))) {
  160. storage_common_copy(
  161. storage, APP_ASSETS_PATH(MAG_EXAMPLE_FILE_3), APP_DATA_PATH(MAG_EXAMPLE_FILE_3));
  162. }
  163. }
  164. void mag_text_store_set(Mag* mag, const char* text, ...) {
  165. furi_assert(mag);
  166. va_list args;
  167. va_start(args, text);
  168. vsnprintf(mag->text_store, MAG_TEXT_STORE_SIZE, text, args);
  169. va_end(args);
  170. }
  171. void mag_text_store_clear(Mag* mag) {
  172. furi_assert(mag);
  173. memset(mag->text_store, 0, sizeof(mag->text_store));
  174. }
  175. void mag_popup_timeout_callback(void* context) {
  176. Mag* mag = context;
  177. view_dispatcher_send_custom_event(mag->view_dispatcher, MagEventPopupClosed);
  178. }
  179. void mag_widget_callback(GuiButtonType result, InputType type, void* context) {
  180. Mag* mag = context;
  181. if(type == InputTypeShort) {
  182. view_dispatcher_send_custom_event(mag->view_dispatcher, result);
  183. }
  184. }
  185. void mag_text_input_callback(void* context) {
  186. Mag* mag = context;
  187. view_dispatcher_send_custom_event(mag->view_dispatcher, MagEventNext);
  188. }
  189. void mag_show_loading_popup(void* context, bool show) {
  190. Mag* mag = context;
  191. if(show) {
  192. // Raise timer priority so that animations can play
  193. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  194. view_dispatcher_switch_to_view(mag->view_dispatcher, MagViewLoading);
  195. } else {
  196. // Restore default timer priority
  197. furi_timer_set_thread_priority(FuriTimerThreadPriorityNormal);
  198. }
  199. }