nfc_magic_app.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include "nfc_magic_app_i.h"
  2. bool nfc_magic_app_custom_event_callback(void* context, uint32_t event) {
  3. furi_assert(context);
  4. NfcMagicApp* instance = context;
  5. return scene_manager_handle_custom_event(instance->scene_manager, event);
  6. }
  7. bool nfc_magic_app_back_event_callback(void* context) {
  8. furi_assert(context);
  9. NfcMagicApp* instance = context;
  10. return scene_manager_handle_back_event(instance->scene_manager);
  11. }
  12. void nfc_magic_app_tick_event_callback(void* context) {
  13. furi_assert(context);
  14. NfcMagicApp* instance = context;
  15. scene_manager_handle_tick_event(instance->scene_manager);
  16. }
  17. void nfc_magic_app_show_loading_popup(void* context, bool show) {
  18. NfcMagicApp* instance = context;
  19. if(show) {
  20. // Raise timer priority so that animations can play
  21. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  22. view_dispatcher_switch_to_view(instance->view_dispatcher, NfcMagicAppViewLoading);
  23. } else {
  24. // Restore default timer priority
  25. furi_timer_set_thread_priority(FuriTimerThreadPriorityNormal);
  26. }
  27. }
  28. NfcMagicApp* nfc_magic_app_alloc() {
  29. NfcMagicApp* instance = malloc(sizeof(NfcMagicApp));
  30. instance->view_dispatcher = view_dispatcher_alloc();
  31. instance->scene_manager = scene_manager_alloc(&nfc_magic_scene_handlers, instance);
  32. view_dispatcher_enable_queue(instance->view_dispatcher);
  33. view_dispatcher_set_event_callback_context(instance->view_dispatcher, instance);
  34. view_dispatcher_set_custom_event_callback(
  35. instance->view_dispatcher, nfc_magic_app_custom_event_callback);
  36. view_dispatcher_set_navigation_event_callback(
  37. instance->view_dispatcher, nfc_magic_app_back_event_callback);
  38. view_dispatcher_set_tick_event_callback(
  39. instance->view_dispatcher, nfc_magic_app_tick_event_callback, 100);
  40. // Nfc device
  41. instance->source_dev = nfc_device_alloc();
  42. nfc_device_set_loading_callback(
  43. instance->source_dev, nfc_magic_app_show_loading_popup, instance);
  44. instance->file_path = furi_string_alloc_set(NFC_APP_FOLDER);
  45. instance->file_name = furi_string_alloc();
  46. // Open GUI record
  47. instance->gui = furi_record_open(RECORD_GUI);
  48. view_dispatcher_attach_to_gui(
  49. instance->view_dispatcher, instance->gui, ViewDispatcherTypeFullscreen);
  50. // Open Notification record
  51. instance->notifications = furi_record_open(RECORD_NOTIFICATION);
  52. // Open Dialogs
  53. instance->dialogs = furi_record_open(RECORD_DIALOGS);
  54. // Open Storage
  55. instance->storage = furi_record_open(RECORD_STORAGE);
  56. // Submenu
  57. instance->submenu = submenu_alloc();
  58. view_dispatcher_add_view(
  59. instance->view_dispatcher, NfcMagicAppViewMenu, submenu_get_view(instance->submenu));
  60. // Popup
  61. instance->popup = popup_alloc();
  62. view_dispatcher_add_view(
  63. instance->view_dispatcher, NfcMagicAppViewPopup, popup_get_view(instance->popup));
  64. // Loading
  65. instance->loading = loading_alloc();
  66. view_dispatcher_add_view(
  67. instance->view_dispatcher, NfcMagicAppViewLoading, loading_get_view(instance->loading));
  68. // Text Input
  69. instance->text_input = text_input_alloc();
  70. view_dispatcher_add_view(
  71. instance->view_dispatcher,
  72. NfcMagicAppViewTextInput,
  73. text_input_get_view(instance->text_input));
  74. // Byte Input
  75. instance->byte_input = byte_input_alloc();
  76. view_dispatcher_add_view(
  77. instance->view_dispatcher,
  78. NfcMagicAppViewByteInput,
  79. byte_input_get_view(instance->byte_input));
  80. // Custom Widget
  81. instance->widget = widget_alloc();
  82. view_dispatcher_add_view(
  83. instance->view_dispatcher, NfcMagicAppViewWidget, widget_get_view(instance->widget));
  84. instance->nfc = nfc_alloc();
  85. instance->scanner = nfc_magic_scanner_alloc(instance->nfc);
  86. return instance;
  87. }
  88. void nfc_magic_app_free(NfcMagicApp* instance) {
  89. furi_assert(instance);
  90. // Nfc device
  91. nfc_device_free(instance->source_dev);
  92. furi_string_free(instance->file_name);
  93. furi_string_free(instance->file_path);
  94. // Submenu
  95. view_dispatcher_remove_view(instance->view_dispatcher, NfcMagicAppViewMenu);
  96. submenu_free(instance->submenu);
  97. // Popup
  98. view_dispatcher_remove_view(instance->view_dispatcher, NfcMagicAppViewPopup);
  99. popup_free(instance->popup);
  100. // Loading
  101. view_dispatcher_remove_view(instance->view_dispatcher, NfcMagicAppViewLoading);
  102. loading_free(instance->loading);
  103. // Text Input
  104. view_dispatcher_remove_view(instance->view_dispatcher, NfcMagicAppViewTextInput);
  105. text_input_free(instance->text_input);
  106. // Byte Input
  107. view_dispatcher_remove_view(instance->view_dispatcher, NfcMagicAppViewByteInput);
  108. byte_input_free(instance->byte_input);
  109. // Custom Widget
  110. view_dispatcher_remove_view(instance->view_dispatcher, NfcMagicAppViewWidget);
  111. widget_free(instance->widget);
  112. // View Dispatcher
  113. view_dispatcher_free(instance->view_dispatcher);
  114. // Scene Manager
  115. scene_manager_free(instance->scene_manager);
  116. // GUI
  117. furi_record_close(RECORD_GUI);
  118. instance->gui = NULL;
  119. // Notifications
  120. furi_record_close(RECORD_NOTIFICATION);
  121. instance->notifications = NULL;
  122. // Dialogs
  123. furi_record_close(RECORD_DIALOGS);
  124. instance->dialogs = NULL;
  125. // Storage
  126. furi_record_close(RECORD_STORAGE);
  127. instance->storage = NULL;
  128. nfc_magic_scanner_free(instance->scanner);
  129. nfc_free(instance->nfc);
  130. free(instance);
  131. }
  132. static const NotificationSequence nfc_magic_sequence_blink_start_cyan = {
  133. &message_blink_start_10,
  134. &message_blink_set_color_cyan,
  135. &message_do_not_reset,
  136. NULL,
  137. };
  138. static const NotificationSequence nfc_magic_sequence_blink_stop = {
  139. &message_blink_stop,
  140. NULL,
  141. };
  142. void nfc_magic_app_blink_start(NfcMagicApp* instance) {
  143. notification_message(instance->notifications, &nfc_magic_sequence_blink_start_cyan);
  144. }
  145. void nfc_magic_app_blink_stop(NfcMagicApp* instance) {
  146. notification_message(instance->notifications, &nfc_magic_sequence_blink_stop);
  147. }
  148. static bool nfc_magic_set_shadow_file_path(FuriString* file_path, FuriString* shadow_file_path) {
  149. furi_assert(file_path);
  150. furi_assert(shadow_file_path);
  151. bool shadow_file_path_set = false;
  152. if(furi_string_end_with(file_path, NFC_APP_SHADOW_EXTENSION)) {
  153. furi_string_set(shadow_file_path, file_path);
  154. shadow_file_path_set = true;
  155. } else if(furi_string_end_with(file_path, NFC_APP_EXTENSION)) {
  156. size_t path_len = furi_string_size(file_path);
  157. // Cut .nfc
  158. furi_string_set_n(shadow_file_path, file_path, 0, path_len - 4);
  159. furi_string_cat_printf(shadow_file_path, "%s", NFC_APP_SHADOW_EXTENSION);
  160. shadow_file_path_set = true;
  161. }
  162. return shadow_file_path_set;
  163. }
  164. static bool nfc_magic_has_shadow_file_internal(NfcMagicApp* instance, FuriString* path) {
  165. furi_assert(path);
  166. bool has_shadow_file = false;
  167. FuriString* shadow_file_path = furi_string_alloc();
  168. do {
  169. if(furi_string_empty(path)) break;
  170. if(!nfc_magic_set_shadow_file_path(path, shadow_file_path)) break;
  171. has_shadow_file =
  172. storage_common_exists(instance->storage, furi_string_get_cstr(shadow_file_path));
  173. } while(false);
  174. furi_string_free(shadow_file_path);
  175. return has_shadow_file;
  176. }
  177. bool nfc_magic_load_file(NfcMagicApp* instance, FuriString* path, bool show_dialog) {
  178. furi_assert(instance);
  179. furi_assert(path);
  180. bool result = false;
  181. FuriString* load_path = furi_string_alloc();
  182. if(nfc_magic_has_shadow_file_internal(instance, path)) {
  183. nfc_magic_set_shadow_file_path(path, load_path);
  184. } else if(furi_string_end_with(path, NFC_APP_SHADOW_EXTENSION)) {
  185. size_t path_len = furi_string_size(path);
  186. furi_string_set_n(load_path, path, 0, path_len - 4);
  187. furi_string_cat_printf(load_path, "%s", NFC_APP_EXTENSION);
  188. } else {
  189. furi_string_set(load_path, path);
  190. }
  191. result = nfc_device_load(instance->source_dev, furi_string_get_cstr(load_path));
  192. if(result) {
  193. path_extract_filename(load_path, instance->file_name, true);
  194. }
  195. if((!result) && (show_dialog)) {
  196. dialog_message_show_storage_error(instance->dialogs, "Cannot load\nkey file");
  197. }
  198. furi_string_free(load_path);
  199. return result;
  200. }
  201. bool nfc_magic_load_from_file_select(NfcMagicApp* instance) {
  202. furi_assert(instance);
  203. DialogsFileBrowserOptions browser_options;
  204. dialog_file_browser_set_basic_options(&browser_options, NFC_APP_EXTENSION, &I_Nfc_10px);
  205. browser_options.base_path = NFC_APP_FOLDER;
  206. browser_options.hide_dot_files = true;
  207. // Input events and views are managed by file_browser
  208. bool result = dialog_file_browser_show(
  209. instance->dialogs, instance->file_path, instance->file_path, &browser_options);
  210. if(result) {
  211. result = nfc_magic_load_file(instance, instance->file_path, true);
  212. }
  213. return result;
  214. }
  215. int32_t nfc_magic_app(void* p) {
  216. UNUSED(p);
  217. NfcMagicApp* instance = nfc_magic_app_alloc();
  218. scene_manager_next_scene(instance->scene_manager, NfcMagicSceneStart);
  219. view_dispatcher_run(instance->view_dispatcher);
  220. nfc_magic_app_free(instance);
  221. return 0;
  222. }