mag.c 8.1 KB

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