nfc_magic_app.c 9.2 KB

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