nfc_magic_app.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #include "nfc_magic_app_i.h"
  2. #include "magic/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 source device (file)
  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. // NFC target device (tag)
  48. instance->target_dev = nfc_device_alloc();
  49. // Open GUI record
  50. instance->gui = furi_record_open(RECORD_GUI);
  51. view_dispatcher_attach_to_gui(
  52. instance->view_dispatcher, instance->gui, ViewDispatcherTypeFullscreen);
  53. // Open Notification record
  54. instance->notifications = furi_record_open(RECORD_NOTIFICATION);
  55. // Open Dialogs
  56. instance->dialogs = furi_record_open(RECORD_DIALOGS);
  57. // Open Storage
  58. instance->storage = furi_record_open(RECORD_STORAGE);
  59. // Submenu
  60. instance->submenu = submenu_alloc();
  61. view_dispatcher_add_view(
  62. instance->view_dispatcher, NfcMagicAppViewMenu, submenu_get_view(instance->submenu));
  63. // Popup
  64. instance->popup = popup_alloc();
  65. view_dispatcher_add_view(
  66. instance->view_dispatcher, NfcMagicAppViewPopup, popup_get_view(instance->popup));
  67. // Loading
  68. instance->loading = loading_alloc();
  69. view_dispatcher_add_view(
  70. instance->view_dispatcher, NfcMagicAppViewLoading, loading_get_view(instance->loading));
  71. // Text Input
  72. instance->text_input = text_input_alloc();
  73. view_dispatcher_add_view(
  74. instance->view_dispatcher,
  75. NfcMagicAppViewTextInput,
  76. text_input_get_view(instance->text_input));
  77. // Byte Input
  78. instance->byte_input = byte_input_alloc();
  79. view_dispatcher_add_view(
  80. instance->view_dispatcher,
  81. NfcMagicAppViewByteInput,
  82. byte_input_get_view(instance->byte_input));
  83. // Custom Widget
  84. instance->widget = widget_alloc();
  85. view_dispatcher_add_view(
  86. instance->view_dispatcher, NfcMagicAppViewWidget, widget_get_view(instance->widget));
  87. instance->gen4_data = gen4_alloc();
  88. // Dict attack
  89. instance->dict_attack = dict_attack_alloc();
  90. view_dispatcher_add_view(
  91. instance->view_dispatcher,
  92. NfcMagicAppViewDictAttack,
  93. dict_attack_get_view(instance->dict_attack));
  94. // Write problems
  95. instance->write_problems = write_problems_alloc();
  96. view_dispatcher_add_view(
  97. instance->view_dispatcher,
  98. NfcMagicAppViewWriteProblems,
  99. write_problems_get_view(instance->write_problems));
  100. instance->nfc = nfc_alloc();
  101. instance->scanner = nfc_magic_scanner_alloc(instance->nfc);
  102. return instance;
  103. }
  104. void nfc_magic_app_free(NfcMagicApp* instance) {
  105. furi_assert(instance);
  106. // Nfc source device
  107. nfc_device_free(instance->source_dev);
  108. furi_string_free(instance->file_name);
  109. furi_string_free(instance->file_path);
  110. // Nfc target device
  111. nfc_device_free(instance->target_dev);
  112. // Submenu
  113. view_dispatcher_remove_view(instance->view_dispatcher, NfcMagicAppViewMenu);
  114. submenu_free(instance->submenu);
  115. // Popup
  116. view_dispatcher_remove_view(instance->view_dispatcher, NfcMagicAppViewPopup);
  117. popup_free(instance->popup);
  118. // Loading
  119. view_dispatcher_remove_view(instance->view_dispatcher, NfcMagicAppViewLoading);
  120. loading_free(instance->loading);
  121. // Text Input
  122. view_dispatcher_remove_view(instance->view_dispatcher, NfcMagicAppViewTextInput);
  123. text_input_free(instance->text_input);
  124. // Byte Input
  125. view_dispatcher_remove_view(instance->view_dispatcher, NfcMagicAppViewByteInput);
  126. byte_input_free(instance->byte_input);
  127. // Custom Widget
  128. view_dispatcher_remove_view(instance->view_dispatcher, NfcMagicAppViewWidget);
  129. widget_free(instance->widget);
  130. // Dict attack
  131. view_dispatcher_remove_view(instance->view_dispatcher, NfcMagicAppViewDictAttack);
  132. dict_attack_free(instance->dict_attack);
  133. // Write problems
  134. view_dispatcher_remove_view(instance->view_dispatcher, NfcMagicAppViewWriteProblems);
  135. write_problems_free(instance->write_problems);
  136. // View Dispatcher
  137. view_dispatcher_free(instance->view_dispatcher);
  138. // Scene Manager
  139. scene_manager_free(instance->scene_manager);
  140. // GUI
  141. furi_record_close(RECORD_GUI);
  142. instance->gui = NULL;
  143. // Notifications
  144. furi_record_close(RECORD_NOTIFICATION);
  145. instance->notifications = NULL;
  146. // Dialogs
  147. furi_record_close(RECORD_DIALOGS);
  148. instance->dialogs = NULL;
  149. // Storage
  150. furi_record_close(RECORD_STORAGE);
  151. instance->storage = NULL;
  152. gen4_free(instance->gen4_data);
  153. nfc_magic_scanner_free(instance->scanner);
  154. nfc_free(instance->nfc);
  155. free(instance);
  156. }
  157. static const NotificationSequence nfc_magic_sequence_blink_start_cyan = {
  158. &message_blink_start_10,
  159. &message_blink_set_color_cyan,
  160. &message_do_not_reset,
  161. NULL,
  162. };
  163. static const NotificationSequence nfc_magic_sequence_blink_stop = {
  164. &message_blink_stop,
  165. NULL,
  166. };
  167. void nfc_magic_app_blink_start(NfcMagicApp* instance) {
  168. notification_message(instance->notifications, &nfc_magic_sequence_blink_start_cyan);
  169. }
  170. void nfc_magic_app_blink_stop(NfcMagicApp* instance) {
  171. notification_message(instance->notifications, &nfc_magic_sequence_blink_stop);
  172. }
  173. static bool nfc_magic_set_shadow_file_path(FuriString* file_path, FuriString* shadow_file_path) {
  174. furi_assert(file_path);
  175. furi_assert(shadow_file_path);
  176. bool shadow_file_path_set = false;
  177. if(furi_string_end_with(file_path, NFC_APP_SHADOW_EXTENSION)) {
  178. furi_string_set(shadow_file_path, file_path);
  179. shadow_file_path_set = true;
  180. } else if(furi_string_end_with(file_path, NFC_APP_EXTENSION)) {
  181. size_t path_len = furi_string_size(file_path);
  182. // Cut .nfc
  183. furi_string_set_n(shadow_file_path, file_path, 0, path_len - 4);
  184. furi_string_cat_printf(shadow_file_path, "%s", NFC_APP_SHADOW_EXTENSION);
  185. shadow_file_path_set = true;
  186. }
  187. return shadow_file_path_set;
  188. }
  189. static bool nfc_magic_has_shadow_file_internal(NfcMagicApp* instance, FuriString* path) {
  190. furi_assert(path);
  191. bool has_shadow_file = false;
  192. FuriString* shadow_file_path = furi_string_alloc();
  193. do {
  194. if(furi_string_empty(path)) break;
  195. if(!nfc_magic_set_shadow_file_path(path, shadow_file_path)) break;
  196. has_shadow_file =
  197. storage_common_exists(instance->storage, furi_string_get_cstr(shadow_file_path));
  198. } while(false);
  199. furi_string_free(shadow_file_path);
  200. return has_shadow_file;
  201. }
  202. bool nfc_magic_load_file(NfcMagicApp* instance, FuriString* path, bool show_dialog) {
  203. furi_assert(instance);
  204. furi_assert(path);
  205. bool result = false;
  206. FuriString* load_path = furi_string_alloc();
  207. if(nfc_magic_has_shadow_file_internal(instance, path)) {
  208. nfc_magic_set_shadow_file_path(path, load_path);
  209. } else if(furi_string_end_with(path, NFC_APP_SHADOW_EXTENSION)) {
  210. size_t path_len = furi_string_size(path);
  211. furi_string_set_n(load_path, path, 0, path_len - 4);
  212. furi_string_cat_printf(load_path, "%s", NFC_APP_EXTENSION);
  213. } else {
  214. furi_string_set(load_path, path);
  215. }
  216. result = nfc_device_load(instance->source_dev, furi_string_get_cstr(load_path));
  217. if(result) {
  218. path_extract_filename(load_path, instance->file_name, true);
  219. }
  220. if((!result) && (show_dialog)) {
  221. dialog_message_show_storage_error(instance->dialogs, "Cannot load\nkey file");
  222. }
  223. furi_string_free(load_path);
  224. return result;
  225. }
  226. bool nfc_magic_load_from_file_select(NfcMagicApp* instance) {
  227. furi_assert(instance);
  228. DialogsFileBrowserOptions browser_options;
  229. dialog_file_browser_set_basic_options(&browser_options, NFC_APP_EXTENSION, &I_Nfc_10px);
  230. browser_options.base_path = NFC_APP_FOLDER;
  231. browser_options.hide_dot_files = true;
  232. // Input events and views are managed by file_browser
  233. bool result = dialog_file_browser_show(
  234. instance->dialogs, instance->file_path, instance->file_path, &browser_options);
  235. if(result) {
  236. result = nfc_magic_load_file(instance, instance->file_path, true);
  237. }
  238. return result;
  239. }
  240. int32_t nfc_magic_app(void* p) {
  241. UNUSED(p);
  242. NfcMagicApp* instance = nfc_magic_app_alloc();
  243. scene_manager_next_scene(instance->scene_manager, NfcMagicSceneStart);
  244. view_dispatcher_run(instance->view_dispatcher);
  245. nfc_magic_app_free(instance);
  246. return 0;
  247. }