nfc_magic_scene_wipe.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "../nfc_magic_app_i.h"
  2. enum {
  3. NfcMagicSceneWipeStateCardSearch,
  4. NfcMagicSceneWipeStateCardFound,
  5. };
  6. NfcCommand nfc_magic_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_magic_scene_wipe_gen2_poller_callback(Gen2PollerEvent event, void* context) {
  27. NfcMagicApp* instance = context;
  28. NfcCommand command = NfcCommandContinue;
  29. if(event.type == Gen2PollerEventTypeDetected) {
  30. view_dispatcher_send_custom_event(
  31. instance->view_dispatcher, NfcMagicCustomEventCardDetected);
  32. } else if(event.type == Gen2PollerEventTypeRequestMode) {
  33. event.data->poller_mode.mode = Gen2PollerModeWipe;
  34. } else if(event.type == Gen2PollerEventTypeRequestTargetData) {
  35. const MfClassicData* mfc_data =
  36. nfc_device_get_data(instance->target_dev, NfcProtocolMfClassic);
  37. event.data->target_data.mfc_data = mfc_data;
  38. } else if(event.type == Gen2PollerEventTypeSuccess) {
  39. view_dispatcher_send_custom_event(
  40. instance->view_dispatcher, NfcMagicCustomEventWorkerSuccess);
  41. command = NfcCommandStop;
  42. } else if(event.type == Gen2PollerEventTypeFail) {
  43. view_dispatcher_send_custom_event(
  44. instance->view_dispatcher, NfcMagicCustomEventWorkerFail);
  45. command = NfcCommandStop;
  46. }
  47. return command;
  48. }
  49. NfcCommand nfc_magic_scene_wipe_gen4_poller_callback(Gen4PollerEvent event, void* context) {
  50. NfcMagicApp* instance = context;
  51. NfcCommand command = NfcCommandContinue;
  52. if(event.type == Gen4PollerEventTypeCardDetected) {
  53. view_dispatcher_send_custom_event(
  54. instance->view_dispatcher, NfcMagicCustomEventCardDetected);
  55. } else if(event.type == Gen4PollerEventTypeRequestMode) {
  56. event.data->request_mode.mode = Gen4PollerModeWipe;
  57. } else if(event.type == Gen4PollerEventTypeSuccess) {
  58. view_dispatcher_send_custom_event(
  59. instance->view_dispatcher, NfcMagicCustomEventWorkerSuccess);
  60. command = NfcCommandStop;
  61. } else if(event.type == Gen4PollerEventTypeFail) {
  62. view_dispatcher_send_custom_event(
  63. instance->view_dispatcher, NfcMagicCustomEventWorkerFail);
  64. command = NfcCommandStop;
  65. }
  66. return command;
  67. }
  68. static void nfc_magic_scene_wipe_setup_view(NfcMagicApp* instance) {
  69. Popup* popup = instance->popup;
  70. popup_reset(popup);
  71. uint32_t state = scene_manager_get_scene_state(instance->scene_manager, NfcMagicSceneWipe);
  72. if(state == NfcMagicSceneWipeStateCardSearch) {
  73. popup_set_icon(instance->popup, 0, 8, &I_NFC_manual_60x50);
  74. popup_set_text(
  75. instance->popup, "Apply the\nsame card\nto the back", 128, 32, AlignRight, AlignCenter);
  76. } else {
  77. popup_set_icon(popup, 12, 23, &I_Loading_24);
  78. popup_set_header(popup, "Wiping\nDon't move...", 52, 32, AlignLeft, AlignCenter);
  79. }
  80. view_dispatcher_switch_to_view(instance->view_dispatcher, NfcMagicAppViewPopup);
  81. }
  82. void nfc_magic_scene_wipe_on_enter(void* context) {
  83. NfcMagicApp* instance = context;
  84. scene_manager_set_scene_state(
  85. instance->scene_manager, NfcMagicSceneWipe, NfcMagicSceneWipeStateCardSearch);
  86. nfc_magic_scene_wipe_setup_view(instance);
  87. nfc_magic_app_blink_start(instance);
  88. if(instance->protocol == NfcMagicProtocolGen1) {
  89. instance->gen1a_poller = gen1a_poller_alloc(instance->nfc);
  90. gen1a_poller_start(
  91. instance->gen1a_poller, nfc_magic_scene_wipe_gen1_poller_callback, instance);
  92. } else if(instance->protocol == NfcMagicProtocolGen2) {
  93. instance->gen2_poller = gen2_poller_alloc(instance->nfc);
  94. gen2_poller_start(
  95. instance->gen2_poller, nfc_magic_scene_wipe_gen2_poller_callback, instance);
  96. } else if(instance->protocol == NfcMagicProtocolClassic) {
  97. instance->gen2_poller = gen2_poller_alloc(instance->nfc);
  98. gen2_poller_start(
  99. instance->gen2_poller, nfc_magic_scene_wipe_gen2_poller_callback, instance);
  100. } else if(instance->protocol == NfcMagicProtocolGen4) {
  101. instance->gen4_poller = gen4_poller_alloc(instance->nfc);
  102. gen4_poller_set_password(instance->gen4_poller, instance->gen4_password);
  103. gen4_poller_start(
  104. instance->gen4_poller, nfc_magic_scene_wipe_gen4_poller_callback, instance);
  105. }
  106. }
  107. bool nfc_magic_scene_wipe_on_event(void* context, SceneManagerEvent event) {
  108. NfcMagicApp* instance = context;
  109. bool consumed = false;
  110. if(event.type == SceneManagerEventTypeCustom) {
  111. if(event.event == NfcMagicCustomEventCardDetected) {
  112. scene_manager_set_scene_state(
  113. instance->scene_manager, NfcMagicSceneWipe, NfcMagicSceneWipeStateCardFound);
  114. nfc_magic_scene_wipe_setup_view(instance);
  115. consumed = true;
  116. } else if(event.event == NfcMagicCustomEventCardLost) {
  117. scene_manager_set_scene_state(
  118. instance->scene_manager, NfcMagicSceneWipe, NfcMagicSceneWipeStateCardSearch);
  119. nfc_magic_scene_wipe_setup_view(instance);
  120. consumed = true;
  121. } else if(event.event == NfcMagicCustomEventWorkerSuccess) {
  122. scene_manager_next_scene(instance->scene_manager, NfcMagicSceneSuccess);
  123. consumed = true;
  124. } else if(event.event == NfcMagicCustomEventWorkerFail) {
  125. scene_manager_next_scene(instance->scene_manager, NfcMagicSceneWipeFail);
  126. consumed = true;
  127. }
  128. }
  129. return consumed;
  130. }
  131. void nfc_magic_scene_wipe_on_exit(void* context) {
  132. NfcMagicApp* instance = context;
  133. if(instance->protocol == NfcMagicProtocolGen1) {
  134. gen1a_poller_stop(instance->gen1a_poller);
  135. gen1a_poller_free(instance->gen1a_poller);
  136. } else if(instance->protocol == NfcMagicProtocolGen2) {
  137. gen2_poller_stop(instance->gen2_poller);
  138. gen2_poller_free(instance->gen2_poller);
  139. } else if(instance->protocol == NfcMagicProtocolClassic) {
  140. gen2_poller_stop(instance->gen2_poller);
  141. gen2_poller_free(instance->gen2_poller);
  142. } else if(instance->protocol == NfcMagicProtocolGen4) {
  143. gen4_poller_stop(instance->gen4_poller);
  144. gen4_poller_free(instance->gen4_poller);
  145. }
  146. scene_manager_set_scene_state(
  147. instance->scene_manager, NfcMagicSceneWipe, NfcMagicSceneWipeStateCardSearch);
  148. // Clear view
  149. popup_reset(instance->popup);
  150. nfc_magic_app_blink_stop(instance);
  151. }