nfc_scene_detect_reader.c 3.6 KB

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