nfc_magic_app.c 10 KB

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