mag.c 8.0 KB

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