mag.c 7.7 KB

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