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