nfc_scene_detect_reader.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "../nfc_i.h"
  2. #include <dolphin/dolphin.h>
  3. #define NFC_SCENE_DETECT_READER_PAIR_NONCES_MAX (10U)
  4. static const NotificationSequence sequence_detect_reader = {
  5. &message_green_255,
  6. &message_blue_255,
  7. &message_do_not_reset,
  8. NULL,
  9. };
  10. bool nfc_detect_reader_worker_callback(NfcWorkerEvent event, void* context) {
  11. UNUSED(event);
  12. furi_assert(context);
  13. Nfc* nfc = context;
  14. view_dispatcher_send_custom_event(nfc->view_dispatcher, event);
  15. return true;
  16. }
  17. void nfc_scene_detect_reader_callback(void* context) {
  18. furi_assert(context);
  19. Nfc* nfc = context;
  20. view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventViewExit);
  21. }
  22. void nfc_scene_detect_reader_on_enter(void* context) {
  23. Nfc* nfc = context;
  24. DOLPHIN_DEED(DolphinDeedNfcDetectReader);
  25. detect_reader_set_callback(nfc->detect_reader, nfc_scene_detect_reader_callback, nfc);
  26. detect_reader_set_nonces_max(nfc->detect_reader, NFC_SCENE_DETECT_READER_PAIR_NONCES_MAX);
  27. // Store number of collected nonces in scene state
  28. scene_manager_set_scene_state(nfc->scene_manager, NfcSceneDetectReader, 0);
  29. notification_message(nfc->notifications, &sequence_detect_reader);
  30. nfc_worker_start(
  31. nfc->worker,
  32. NfcWorkerStateAnalyzeReader,
  33. &nfc->dev->dev_data,
  34. nfc_detect_reader_worker_callback,
  35. nfc);
  36. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewDetectReader);
  37. }
  38. bool nfc_scene_detect_reader_on_event(void* context, SceneManagerEvent event) {
  39. Nfc* nfc = context;
  40. bool consumed = false;
  41. uint32_t nonces_collected =
  42. scene_manager_get_scene_state(nfc->scene_manager, NfcSceneDetectReader);
  43. if(event.type == SceneManagerEventTypeCustom) {
  44. if(event.event == NfcCustomEventViewExit) {
  45. nfc_worker_stop(nfc->worker);
  46. scene_manager_next_scene(nfc->scene_manager, NfcSceneMfkeyNoncesInfo);
  47. consumed = true;
  48. } else if(event.event == NfcWorkerEventDetectReaderMfkeyCollected) {
  49. nonces_collected += 2;
  50. scene_manager_set_scene_state(
  51. nfc->scene_manager, NfcSceneDetectReader, nonces_collected);
  52. detect_reader_set_nonces_collected(nfc->detect_reader, nonces_collected);
  53. if(nonces_collected >= NFC_SCENE_DETECT_READER_PAIR_NONCES_MAX) {
  54. detect_reader_set_state(nfc->detect_reader, DetectReaderStateDone);
  55. nfc_blink_stop(nfc);
  56. notification_message(nfc->notifications, &sequence_single_vibro);
  57. notification_message(nfc->notifications, &sequence_set_green_255);
  58. nfc_worker_stop(nfc->worker);
  59. }
  60. consumed = true;
  61. } else if(event.event == NfcWorkerEventDetectReaderDetected) {
  62. if(nonces_collected < NFC_SCENE_DETECT_READER_PAIR_NONCES_MAX) {
  63. notification_message(nfc->notifications, &sequence_blink_start_cyan);
  64. detect_reader_set_state(nfc->detect_reader, DetectReaderStateReaderDetected);
  65. }
  66. } else if(event.event == NfcWorkerEventDetectReaderLost) {
  67. if(nonces_collected < NFC_SCENE_DETECT_READER_PAIR_NONCES_MAX) {
  68. nfc_blink_stop(nfc);
  69. notification_message(nfc->notifications, &sequence_detect_reader);
  70. detect_reader_set_state(nfc->detect_reader, DetectReaderStateReaderLost);
  71. }
  72. }
  73. }
  74. return consumed;
  75. }
  76. void nfc_scene_detect_reader_on_exit(void* context) {
  77. Nfc* nfc = context;
  78. // Stop worker
  79. nfc_worker_stop(nfc->worker);
  80. // Clear view
  81. detect_reader_reset(nfc->detect_reader);
  82. // Stop notifications
  83. nfc_blink_stop(nfc);
  84. notification_message(nfc->notifications, &sequence_reset_green);
  85. }