nfc_magic_scene_wipe.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "../nfc_magic_app_i.h"
  2. enum {
  3. NfcMagicSceneWipeStateCardSearch,
  4. NfcMagicSceneWipeStateCardFound,
  5. };
  6. NfcCommand nfc_mafic_scene_wipe_gen1_poller_callback(Gen1aPollerEvent event, void* context) {
  7. NfcMagicApp* instance = context;
  8. furi_assert(event.data);
  9. NfcCommand command = NfcCommandContinue;
  10. if(event.type == Gen1aPollerEventTypeDetected) {
  11. view_dispatcher_send_custom_event(
  12. instance->view_dispatcher, NfcMagicCustomEventCardDetected);
  13. } else if(event.type == Gen1aPollerEventTypeRequestMode) {
  14. event.data->request_mode.mode = Gen1aPollerModeWipe;
  15. } else if(event.type == Gen1aPollerEventTypeSuccess) {
  16. view_dispatcher_send_custom_event(
  17. instance->view_dispatcher, NfcMagicCustomEventWorkerSuccess);
  18. command = NfcCommandStop;
  19. } else if(event.type == Gen1aPollerEventTypeFail) {
  20. view_dispatcher_send_custom_event(
  21. instance->view_dispatcher, NfcMagicCustomEventWorkerFail);
  22. command = NfcCommandStop;
  23. }
  24. return command;
  25. }
  26. NfcCommand nfc_mafic_scene_wipe_gen4_poller_callback(Gen4PollerEvent event, void* context) {
  27. NfcMagicApp* instance = context;
  28. NfcCommand command = NfcCommandContinue;
  29. if(event.type == Gen4PollerEventTypeCardDetected) {
  30. view_dispatcher_send_custom_event(
  31. instance->view_dispatcher, NfcMagicCustomEventCardDetected);
  32. } else if(event.type == Gen4PollerEventTypeRequestMode) {
  33. event.data->request_mode.mode = Gen4PollerModeWipe;
  34. } else if(event.type == Gen4PollerEventTypeSuccess) {
  35. view_dispatcher_send_custom_event(
  36. instance->view_dispatcher, NfcMagicCustomEventWorkerSuccess);
  37. command = NfcCommandStop;
  38. } else if(event.type == Gen4PollerEventTypeFail) {
  39. view_dispatcher_send_custom_event(
  40. instance->view_dispatcher, NfcMagicCustomEventWorkerFail);
  41. command = NfcCommandStop;
  42. }
  43. return command;
  44. }
  45. static void nfc_magic_scene_wipe_setup_view(NfcMagicApp* instance) {
  46. Popup* popup = instance->popup;
  47. popup_reset(popup);
  48. uint32_t state = scene_manager_get_scene_state(instance->scene_manager, NfcMagicSceneWipe);
  49. if(state == NfcMagicSceneWipeStateCardSearch) {
  50. popup_set_icon(instance->popup, 0, 8, &I_NFC_manual_60x50);
  51. popup_set_text(
  52. instance->popup, "Apply the\nsame card\nto the back", 128, 32, AlignRight, AlignCenter);
  53. } else {
  54. popup_set_icon(popup, 12, 23, &I_Loading_24);
  55. popup_set_header(popup, "Wiping\nDon't move...", 52, 32, AlignLeft, AlignCenter);
  56. }
  57. view_dispatcher_switch_to_view(instance->view_dispatcher, NfcMagicAppViewPopup);
  58. }
  59. void nfc_magic_scene_wipe_on_enter(void* context) {
  60. NfcMagicApp* instance = context;
  61. scene_manager_set_scene_state(
  62. instance->scene_manager, NfcMagicSceneWipe, NfcMagicSceneWipeStateCardSearch);
  63. nfc_magic_scene_wipe_setup_view(instance);
  64. nfc_magic_app_blink_start(instance);
  65. if(instance->protocol == NfcMagicProtocolGen1) {
  66. instance->gen1a_poller = gen1a_poller_alloc(instance->nfc);
  67. gen1a_poller_start(
  68. instance->gen1a_poller, nfc_mafic_scene_wipe_gen1_poller_callback, instance);
  69. } else {
  70. instance->gen4_poller = gen4_poller_alloc(instance->nfc);
  71. gen4_poller_set_password(instance->gen4_poller, instance->gen4_password);
  72. gen4_poller_start(
  73. instance->gen4_poller, nfc_mafic_scene_wipe_gen4_poller_callback, instance);
  74. }
  75. }
  76. bool nfc_magic_scene_wipe_on_event(void* context, SceneManagerEvent event) {
  77. NfcMagicApp* instance = context;
  78. bool consumed = false;
  79. if(event.type == SceneManagerEventTypeCustom) {
  80. if(event.event == NfcMagicCustomEventCardDetected) {
  81. scene_manager_set_scene_state(
  82. instance->scene_manager, NfcMagicSceneWipe, NfcMagicSceneWipeStateCardFound);
  83. nfc_magic_scene_wipe_setup_view(instance);
  84. consumed = true;
  85. } else if(event.event == NfcMagicCustomEventCardLost) {
  86. scene_manager_set_scene_state(
  87. instance->scene_manager, NfcMagicSceneWipe, NfcMagicSceneWipeStateCardSearch);
  88. nfc_magic_scene_wipe_setup_view(instance);
  89. consumed = true;
  90. } else if(event.event == NfcMagicCustomEventWorkerSuccess) {
  91. scene_manager_next_scene(instance->scene_manager, NfcMagicSceneSuccess);
  92. consumed = true;
  93. } else if(event.event == NfcMagicCustomEventWorkerFail) {
  94. scene_manager_next_scene(instance->scene_manager, NfcMagicSceneWipeFail);
  95. consumed = true;
  96. }
  97. }
  98. return consumed;
  99. }
  100. void nfc_magic_scene_wipe_on_exit(void* context) {
  101. NfcMagicApp* instance = context;
  102. if(instance->protocol == NfcMagicProtocolGen1) {
  103. gen1a_poller_stop(instance->gen1a_poller);
  104. gen1a_poller_free(instance->gen1a_poller);
  105. } else {
  106. gen4_poller_stop(instance->gen4_poller);
  107. gen4_poller_free(instance->gen4_poller);
  108. }
  109. scene_manager_set_scene_state(
  110. instance->scene_manager, NfcMagicSceneWipe, NfcMagicSceneWipeStateCardSearch);
  111. // Clear view
  112. popup_reset(instance->popup);
  113. nfc_magic_app_blink_stop(instance);
  114. }