nfc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "nfc_i.h"
  2. #include "api-hal-nfc.h"
  3. bool nfc_custom_event_callback(void* context, uint32_t event) {
  4. furi_assert(context);
  5. Nfc* nfc = (Nfc*)context;
  6. return scene_manager_handle_custom_event(nfc->scene_manager, event);
  7. }
  8. bool nfc_navigation_event_callback(void* context) {
  9. furi_assert(context);
  10. Nfc* nfc = (Nfc*)context;
  11. return scene_manager_handle_navigation_event(nfc->scene_manager);
  12. }
  13. void nfc_tick_event_callback(void* context) {
  14. furi_assert(context);
  15. Nfc* nfc = (Nfc*)context;
  16. scene_manager_handle_tick_event(nfc->scene_manager);
  17. }
  18. Nfc* nfc_alloc() {
  19. Nfc* nfc = furi_alloc(sizeof(Nfc));
  20. nfc->nfc_common.worker = nfc_worker_alloc();
  21. nfc->nfc_common.view_dispatcher = view_dispatcher_alloc();
  22. nfc->scene_manager = scene_manager_alloc(&nfc_scene_handlers, nfc);
  23. view_dispatcher_enable_queue(nfc->nfc_common.view_dispatcher);
  24. view_dispatcher_set_event_callback_context(nfc->nfc_common.view_dispatcher, nfc);
  25. view_dispatcher_set_custom_event_callback(
  26. nfc->nfc_common.view_dispatcher, nfc_custom_event_callback);
  27. view_dispatcher_set_navigation_event_callback(
  28. nfc->nfc_common.view_dispatcher, nfc_navigation_event_callback);
  29. view_dispatcher_set_tick_event_callback(
  30. nfc->nfc_common.view_dispatcher, nfc_tick_event_callback, 300);
  31. // Open GUI record
  32. nfc->gui = furi_record_open("gui");
  33. view_dispatcher_attach_to_gui(
  34. nfc->nfc_common.view_dispatcher, nfc->gui, ViewDispatcherTypeFullscreen);
  35. // Open Notification record
  36. nfc->notifications = furi_record_open("notification");
  37. // Submenu
  38. nfc->submenu = submenu_alloc();
  39. view_dispatcher_add_view(
  40. nfc->nfc_common.view_dispatcher, NfcViewMenu, submenu_get_view(nfc->submenu));
  41. // Dialog
  42. nfc->dialog_ex = dialog_ex_alloc();
  43. view_dispatcher_add_view(
  44. nfc->nfc_common.view_dispatcher, NfcViewDialogEx, dialog_ex_get_view(nfc->dialog_ex));
  45. // Popup
  46. nfc->popup = popup_alloc();
  47. view_dispatcher_add_view(
  48. nfc->nfc_common.view_dispatcher, NfcViewPopup, popup_get_view(nfc->popup));
  49. // Text Input
  50. nfc->text_input = text_input_alloc();
  51. view_dispatcher_add_view(
  52. nfc->nfc_common.view_dispatcher, NfcViewTextInput, text_input_get_view(nfc->text_input));
  53. // Byte Input
  54. nfc->byte_input = byte_input_alloc();
  55. view_dispatcher_add_view(
  56. nfc->nfc_common.view_dispatcher, NfcViewByteInput, byte_input_get_view(nfc->byte_input));
  57. // TextBox
  58. nfc->text_box = text_box_alloc();
  59. view_dispatcher_add_view(
  60. nfc->nfc_common.view_dispatcher, NfcViewTextBox, text_box_get_view(nfc->text_box));
  61. string_init(nfc->text_box_store);
  62. // Detect
  63. nfc->nfc_detect = nfc_detect_alloc(&nfc->nfc_common);
  64. view_dispatcher_add_view(
  65. nfc->nfc_common.view_dispatcher, NfcViewDetect, nfc_detect_get_view(nfc->nfc_detect));
  66. // Emulate
  67. nfc->nfc_emulate = nfc_emulate_alloc(&nfc->nfc_common);
  68. view_dispatcher_add_view(
  69. nfc->nfc_common.view_dispatcher, NfcViewEmulate, nfc_emulate_get_view(nfc->nfc_emulate));
  70. // EMV
  71. nfc->nfc_emv = nfc_emv_alloc(&nfc->nfc_common);
  72. view_dispatcher_add_view(
  73. nfc->nfc_common.view_dispatcher, NfcViewEmv, nfc_emv_get_view(nfc->nfc_emv));
  74. // Mifare Ultralight
  75. nfc->nfc_mifare_ul = nfc_mifare_ul_alloc(&nfc->nfc_common);
  76. view_dispatcher_add_view(
  77. nfc->nfc_common.view_dispatcher,
  78. NfcViewMifareUl,
  79. nfc_mifare_ul_get_view(nfc->nfc_mifare_ul));
  80. return nfc;
  81. }
  82. void nfc_free(Nfc* nfc) {
  83. furi_assert(nfc);
  84. // Submenu
  85. view_dispatcher_remove_view(nfc->nfc_common.view_dispatcher, NfcViewMenu);
  86. submenu_free(nfc->submenu);
  87. // DialogEx
  88. view_dispatcher_remove_view(nfc->nfc_common.view_dispatcher, NfcViewDialogEx);
  89. dialog_ex_free(nfc->dialog_ex);
  90. // Popup
  91. view_dispatcher_remove_view(nfc->nfc_common.view_dispatcher, NfcViewPopup);
  92. popup_free(nfc->popup);
  93. // TextInput
  94. view_dispatcher_remove_view(nfc->nfc_common.view_dispatcher, NfcViewTextInput);
  95. text_input_free(nfc->text_input);
  96. // ByteInput
  97. view_dispatcher_remove_view(nfc->nfc_common.view_dispatcher, NfcViewByteInput);
  98. byte_input_free(nfc->byte_input);
  99. // TextBox
  100. view_dispatcher_remove_view(nfc->nfc_common.view_dispatcher, NfcViewTextBox);
  101. text_box_free(nfc->text_box);
  102. string_clear(nfc->text_box_store);
  103. // Detect
  104. view_dispatcher_remove_view(nfc->nfc_common.view_dispatcher, NfcViewDetect);
  105. nfc_detect_free(nfc->nfc_detect);
  106. // Emulate
  107. view_dispatcher_remove_view(nfc->nfc_common.view_dispatcher, NfcViewEmulate);
  108. nfc_emulate_free(nfc->nfc_emulate);
  109. // EMV
  110. view_dispatcher_remove_view(nfc->nfc_common.view_dispatcher, NfcViewEmv);
  111. nfc_emv_free(nfc->nfc_emv);
  112. // Mifare Ultralight
  113. view_dispatcher_remove_view(nfc->nfc_common.view_dispatcher, NfcViewMifareUl);
  114. nfc_mifare_ul_free(nfc->nfc_mifare_ul);
  115. // Worker
  116. nfc_worker_stop(nfc->nfc_common.worker);
  117. nfc_worker_free(nfc->nfc_common.worker);
  118. // View Dispatcher
  119. view_dispatcher_free(nfc->nfc_common.view_dispatcher);
  120. // Scene Manager
  121. scene_manager_free(nfc->scene_manager);
  122. // GUI
  123. furi_record_close("gui");
  124. nfc->gui = NULL;
  125. // Notifications
  126. furi_record_close("notification");
  127. nfc->notifications = NULL;
  128. free(nfc);
  129. }
  130. int32_t nfc_task(void* p) {
  131. Nfc* nfc = nfc_alloc();
  132. // Check argument and run corresponding scene
  133. if(p && nfc_device_load(&nfc->device, p)) {
  134. scene_manager_next_scene(nfc->scene_manager, NfcSceneEmulateUid);
  135. } else {
  136. scene_manager_next_scene(nfc->scene_manager, NfcSceneStart);
  137. }
  138. view_dispatcher_run(nfc->nfc_common.view_dispatcher);
  139. nfc_free(nfc);
  140. return 0;
  141. }
  142. void nfc_text_store_set(Nfc* nfc, const char* text, ...) {
  143. va_list args;
  144. va_start(args, text);
  145. vsnprintf(nfc->text_store, sizeof(nfc->text_store), text, args);
  146. va_end(args);
  147. }
  148. void nfc_text_store_clear(Nfc* nfc) {
  149. memset(nfc->text_store, 0, sizeof(nfc->text_store));
  150. }