nfc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "nfc_i.h"
  2. #include "api-hal-nfc.h"
  3. #include "app_scene.h"
  4. Nfc* nfc_alloc() {
  5. Nfc* nfc = furi_alloc(sizeof(Nfc));
  6. nfc->nfc_common.worker = nfc_worker_alloc();
  7. nfc->nfc_common.view_dispatcher = view_dispatcher_alloc();
  8. view_dispatcher_enable_queue(nfc->nfc_common.view_dispatcher);
  9. view_dispatcher_enable_navigation(nfc->nfc_common.view_dispatcher, nfc);
  10. // Open GUI record
  11. nfc->gui = furi_record_open("gui");
  12. view_dispatcher_attach_to_gui(
  13. nfc->nfc_common.view_dispatcher, nfc->gui, ViewDispatcherTypeFullscreen);
  14. // Open Notification record
  15. nfc->notifications = furi_record_open("notification");
  16. // Submenu
  17. nfc->submenu = submenu_alloc();
  18. view_dispatcher_add_view(
  19. nfc->nfc_common.view_dispatcher, NfcViewMenu, submenu_get_view(nfc->submenu));
  20. // Dialog
  21. nfc->dialog_ex = dialog_ex_alloc();
  22. view_dispatcher_add_view(
  23. nfc->nfc_common.view_dispatcher, NfcViewDialogEx, dialog_ex_get_view(nfc->dialog_ex));
  24. // Popup
  25. nfc->popup = popup_alloc();
  26. view_dispatcher_add_view(
  27. nfc->nfc_common.view_dispatcher, NfcViewPopup, popup_get_view(nfc->popup));
  28. // Text Input
  29. nfc->text_input = text_input_alloc();
  30. view_dispatcher_add_view(
  31. nfc->nfc_common.view_dispatcher, NfcViewTextInput, text_input_get_view(nfc->text_input));
  32. // Byte Input
  33. nfc->byte_input = byte_input_alloc();
  34. view_dispatcher_add_view(
  35. nfc->nfc_common.view_dispatcher, NfcViewByteInput, byte_input_get_view(nfc->byte_input));
  36. // TextBox
  37. nfc->text_box = text_box_alloc();
  38. view_dispatcher_add_view(
  39. nfc->nfc_common.view_dispatcher, NfcViewTextBox, text_box_get_view(nfc->text_box));
  40. string_init(nfc->text_box_store);
  41. // Detect
  42. nfc->nfc_detect = nfc_detect_alloc(&nfc->nfc_common);
  43. view_dispatcher_add_view(
  44. nfc->nfc_common.view_dispatcher, NfcViewDetect, nfc_detect_get_view(nfc->nfc_detect));
  45. // Emulate
  46. nfc->nfc_emulate = nfc_emulate_alloc(&nfc->nfc_common);
  47. view_dispatcher_add_view(
  48. nfc->nfc_common.view_dispatcher, NfcViewEmulate, nfc_emulate_get_view(nfc->nfc_emulate));
  49. // EMV
  50. nfc->nfc_emv = nfc_emv_alloc(&nfc->nfc_common);
  51. view_dispatcher_add_view(
  52. nfc->nfc_common.view_dispatcher, NfcViewEmv, nfc_emv_get_view(nfc->nfc_emv));
  53. // Mifare Ultralight
  54. nfc->nfc_mifare_ul = nfc_mifare_ul_alloc(&nfc->nfc_common);
  55. view_dispatcher_add_view(
  56. nfc->nfc_common.view_dispatcher,
  57. NfcViewMifareUl,
  58. nfc_mifare_ul_get_view(nfc->nfc_mifare_ul));
  59. // Scene allocation
  60. nfc->scene_start = nfc_scene_start_alloc();
  61. nfc->scene_read_card = nfc_scene_read_card_alloc();
  62. nfc->scene_read_card_success = nfc_scene_read_card_success_alloc();
  63. nfc->scene_card_menu = nfc_scene_card_menu_alloc();
  64. nfc->scene_not_implemented = nfc_scene_not_implemented_alloc();
  65. nfc->scene_debug_menu = nfc_scene_debug_menu_alloc();
  66. nfc->scene_debug_detect = nfc_scene_debug_detect_alloc();
  67. nfc->scene_debug_emulate = nfc_scene_debug_emulate_alloc();
  68. nfc->scene_debug_read_emv = nfc_scene_debug_read_emv_alloc();
  69. nfc->scene_debug_read_mifare_ul = nfc_scene_debug_read_mifare_ul_alloc();
  70. nfc->scene_emulate_uid = nfc_scene_emulate_uid_alloc();
  71. nfc->scene_save_name = nfc_scene_save_name_alloc();
  72. nfc->scene_save_success = nfc_scene_save_success_alloc();
  73. nfc->scene_file_select = nfc_scene_file_select_alloc();
  74. nfc->scene_saved_menu = nfc_scene_saved_menu_alloc();
  75. nfc->scene_set_type = nfc_scene_set_type_alloc();
  76. nfc->scene_set_sak = nfc_scene_set_sak_alloc();
  77. nfc->scene_set_atqa = nfc_scene_set_atqa_alloc();
  78. nfc->scene_set_uid = nfc_scene_set_uid_alloc();
  79. nfc->scene_scripts_menu = nfc_scene_scripts_menu_alloc();
  80. nfc->scene_read_mifare_ul = nfc_scene_read_mifare_ul_alloc();
  81. nfc->scene_read_mifare_ul_success = nfc_scene_read_mifare_ul_success_alloc();
  82. nfc->scene_mifare_ul_menu = nfc_scene_mifare_ul_menu_alloc();
  83. view_dispatcher_add_scene(nfc->nfc_common.view_dispatcher, nfc->scene_start);
  84. return nfc;
  85. }
  86. void nfc_free(Nfc* nfc) {
  87. furi_assert(nfc);
  88. // Submenu
  89. view_dispatcher_remove_view(nfc->nfc_common.view_dispatcher, NfcViewMenu);
  90. submenu_free(nfc->submenu);
  91. // DialogEx
  92. view_dispatcher_remove_view(nfc->nfc_common.view_dispatcher, NfcViewDialogEx);
  93. dialog_ex_free(nfc->dialog_ex);
  94. // Popup
  95. view_dispatcher_remove_view(nfc->nfc_common.view_dispatcher, NfcViewPopup);
  96. popup_free(nfc->popup);
  97. // TextInput
  98. view_dispatcher_remove_view(nfc->nfc_common.view_dispatcher, NfcViewTextInput);
  99. text_input_free(nfc->text_input);
  100. // ByteInput
  101. view_dispatcher_remove_view(nfc->nfc_common.view_dispatcher, NfcViewByteInput);
  102. byte_input_free(nfc->byte_input);
  103. // TextBox
  104. view_dispatcher_remove_view(nfc->nfc_common.view_dispatcher, NfcViewTextBox);
  105. text_box_free(nfc->text_box);
  106. string_clear(nfc->text_box_store);
  107. // Detect
  108. view_dispatcher_remove_view(nfc->nfc_common.view_dispatcher, NfcViewDetect);
  109. nfc_detect_free(nfc->nfc_detect);
  110. // Emulate
  111. view_dispatcher_remove_view(nfc->nfc_common.view_dispatcher, NfcViewEmulate);
  112. nfc_emulate_free(nfc->nfc_emulate);
  113. // EMV
  114. view_dispatcher_remove_view(nfc->nfc_common.view_dispatcher, NfcViewEmv);
  115. nfc_emv_free(nfc->nfc_emv);
  116. // Mifare Ultralight
  117. view_dispatcher_remove_view(nfc->nfc_common.view_dispatcher, NfcViewMifareUl);
  118. nfc_mifare_ul_free(nfc->nfc_mifare_ul);
  119. // Worker
  120. nfc_worker_stop(nfc->nfc_common.worker);
  121. nfc_worker_free(nfc->nfc_common.worker);
  122. // Scenes
  123. nfc_scene_start_free(nfc->scene_start);
  124. nfc_scene_read_card_free(nfc->scene_read_card);
  125. nfc_scene_read_card_success_free(nfc->scene_read_card_success);
  126. nfc_scene_card_menu_free(nfc->scene_card_menu);
  127. nfc_scene_not_implemented_free(nfc->scene_not_implemented);
  128. nfc_scene_debug_menu_free(nfc->scene_debug_menu);
  129. nfc_scene_debug_detect_free(nfc->scene_debug_detect);
  130. nfc_scene_debug_emulate_free(nfc->scene_debug_emulate);
  131. nfc_scene_debug_read_emv_free(nfc->scene_debug_read_emv);
  132. nfc_scene_debug_read_mifare_ul_free(nfc->scene_debug_read_mifare_ul);
  133. nfc_scene_emulate_uid_free(nfc->scene_emulate_uid);
  134. nfc_scene_save_name_free(nfc->scene_save_name);
  135. nfc_scene_save_success_free(nfc->scene_save_success);
  136. nfc_scene_file_select_free(nfc->scene_file_select);
  137. nfc_scene_saved_menu_free(nfc->scene_saved_menu);
  138. nfc_scene_set_type_free(nfc->scene_set_type);
  139. nfc_scene_set_sak_free(nfc->scene_set_sak);
  140. nfc_scene_set_atqa_free(nfc->scene_set_atqa);
  141. nfc_scene_set_uid_free(nfc->scene_set_uid);
  142. nfc_scene_scripts_menu_free(nfc->scene_scripts_menu);
  143. nfc_scene_read_mifare_ul_free(nfc->scene_read_mifare_ul);
  144. nfc_scene_read_mifare_ul_success_free(nfc->scene_read_mifare_ul_success);
  145. nfc_scene_mifare_ul_menu_free(nfc->scene_mifare_ul_menu);
  146. // View Dispatcher
  147. view_dispatcher_free(nfc->nfc_common.view_dispatcher);
  148. // GUI
  149. furi_record_close("gui");
  150. nfc->gui = NULL;
  151. // Notifications
  152. furi_record_close("notification");
  153. nfc->notifications = NULL;
  154. free(nfc);
  155. }
  156. int32_t nfc_task(void* p) {
  157. Nfc* nfc = nfc_alloc();
  158. view_dispatcher_run(nfc->nfc_common.view_dispatcher);
  159. nfc_free(nfc);
  160. return 0;
  161. }
  162. void nfc_set_text_store(Nfc* nfc, const char* text, ...) {
  163. va_list args;
  164. va_start(args, text);
  165. vsnprintf(nfc->text_store, sizeof(nfc->text_store), text, args);
  166. va_end(args);
  167. }