nfc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "nfc_i.h"
  2. #include "furi_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_back_event_callback(void* context) {
  9. furi_assert(context);
  10. Nfc* nfc = (Nfc*)context;
  11. return scene_manager_handle_back_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 = malloc(sizeof(Nfc));
  20. nfc->worker = nfc_worker_alloc();
  21. nfc->view_dispatcher = view_dispatcher_alloc();
  22. nfc->scene_manager = scene_manager_alloc(&nfc_scene_handlers, nfc);
  23. view_dispatcher_enable_queue(nfc->view_dispatcher);
  24. view_dispatcher_set_event_callback_context(nfc->view_dispatcher, nfc);
  25. view_dispatcher_set_custom_event_callback(nfc->view_dispatcher, nfc_custom_event_callback);
  26. view_dispatcher_set_navigation_event_callback(nfc->view_dispatcher, nfc_back_event_callback);
  27. view_dispatcher_set_tick_event_callback(nfc->view_dispatcher, nfc_tick_event_callback, 100);
  28. // Nfc device
  29. nfc->dev = nfc_device_alloc();
  30. // Open GUI record
  31. nfc->gui = furi_record_open("gui");
  32. view_dispatcher_attach_to_gui(nfc->view_dispatcher, nfc->gui, ViewDispatcherTypeFullscreen);
  33. // Open Notification record
  34. nfc->notifications = furi_record_open("notification");
  35. // Submenu
  36. nfc->submenu = submenu_alloc();
  37. view_dispatcher_add_view(nfc->view_dispatcher, NfcViewMenu, submenu_get_view(nfc->submenu));
  38. // Dialog
  39. nfc->dialog_ex = dialog_ex_alloc();
  40. view_dispatcher_add_view(
  41. nfc->view_dispatcher, NfcViewDialogEx, dialog_ex_get_view(nfc->dialog_ex));
  42. // Popup
  43. nfc->popup = popup_alloc();
  44. view_dispatcher_add_view(nfc->view_dispatcher, NfcViewPopup, popup_get_view(nfc->popup));
  45. // Text Input
  46. nfc->text_input = text_input_alloc();
  47. view_dispatcher_add_view(
  48. nfc->view_dispatcher, NfcViewTextInput, text_input_get_view(nfc->text_input));
  49. // Byte Input
  50. nfc->byte_input = byte_input_alloc();
  51. view_dispatcher_add_view(
  52. nfc->view_dispatcher, NfcViewByteInput, byte_input_get_view(nfc->byte_input));
  53. // TextBox
  54. nfc->text_box = text_box_alloc();
  55. view_dispatcher_add_view(
  56. nfc->view_dispatcher, NfcViewTextBox, text_box_get_view(nfc->text_box));
  57. string_init(nfc->text_box_store);
  58. // Custom Widget
  59. nfc->widget = widget_alloc();
  60. view_dispatcher_add_view(nfc->view_dispatcher, NfcViewWidget, widget_get_view(nfc->widget));
  61. // Bank Card
  62. nfc->bank_card = bank_card_alloc();
  63. view_dispatcher_add_view(
  64. nfc->view_dispatcher, NfcViewBankCard, bank_card_get_view(nfc->bank_card));
  65. // Dict Attack
  66. nfc->dict_attack = dict_attack_alloc();
  67. view_dispatcher_add_view(
  68. nfc->view_dispatcher, NfcViewDictAttack, dict_attack_get_view(nfc->dict_attack));
  69. return nfc;
  70. }
  71. void nfc_free(Nfc* nfc) {
  72. furi_assert(nfc);
  73. // Nfc device
  74. nfc_device_free(nfc->dev);
  75. // Submenu
  76. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewMenu);
  77. submenu_free(nfc->submenu);
  78. // DialogEx
  79. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewDialogEx);
  80. dialog_ex_free(nfc->dialog_ex);
  81. // Popup
  82. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewPopup);
  83. popup_free(nfc->popup);
  84. // TextInput
  85. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewTextInput);
  86. text_input_free(nfc->text_input);
  87. // ByteInput
  88. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewByteInput);
  89. byte_input_free(nfc->byte_input);
  90. // TextBox
  91. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewTextBox);
  92. text_box_free(nfc->text_box);
  93. string_clear(nfc->text_box_store);
  94. // Custom Widget
  95. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewWidget);
  96. widget_free(nfc->widget);
  97. // Bank Card
  98. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewBankCard);
  99. bank_card_free(nfc->bank_card);
  100. // Dict Attack
  101. view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewDictAttack);
  102. dict_attack_free(nfc->dict_attack);
  103. // Worker
  104. nfc_worker_stop(nfc->worker);
  105. nfc_worker_free(nfc->worker);
  106. // View Dispatcher
  107. view_dispatcher_free(nfc->view_dispatcher);
  108. // Scene Manager
  109. scene_manager_free(nfc->scene_manager);
  110. // GUI
  111. furi_record_close("gui");
  112. nfc->gui = NULL;
  113. // Notifications
  114. furi_record_close("notification");
  115. nfc->notifications = NULL;
  116. free(nfc);
  117. }
  118. void nfc_text_store_set(Nfc* nfc, const char* text, ...) {
  119. va_list args;
  120. va_start(args, text);
  121. vsnprintf(nfc->text_store, sizeof(nfc->text_store), text, args);
  122. va_end(args);
  123. }
  124. void nfc_text_store_clear(Nfc* nfc) {
  125. memset(nfc->text_store, 0, sizeof(nfc->text_store));
  126. }
  127. int32_t nfc_app(void* p) {
  128. Nfc* nfc = nfc_alloc();
  129. char* args = p;
  130. // Check argument and run corresponding scene
  131. if((*args != '\0') && nfc_device_load(nfc->dev, p)) {
  132. if(nfc->dev->format == NfcDeviceSaveFormatMifareUl) {
  133. scene_manager_next_scene(nfc->scene_manager, NfcSceneEmulateMifareUl);
  134. } else {
  135. scene_manager_next_scene(nfc->scene_manager, NfcSceneEmulateUid);
  136. }
  137. } else {
  138. scene_manager_next_scene(nfc->scene_manager, NfcSceneStart);
  139. }
  140. view_dispatcher_run(nfc->view_dispatcher);
  141. nfc_free(nfc);
  142. return 0;
  143. }