nfc_scene_detect_reader.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. NfcDeviceData* dev_data = &nfc->dev->dev_data;
  26. if(dev_data->nfc_data.uid_len) {
  27. detect_reader_set_uid(
  28. nfc->detect_reader, dev_data->nfc_data.uid, dev_data->nfc_data.uid_len);
  29. }
  30. // Store number of collected nonces in scene state
  31. scene_manager_set_scene_state(nfc->scene_manager, NfcSceneDetectReader, 0);
  32. notification_message(nfc->notifications, &sequence_detect_reader);
  33. nfc_worker_start(
  34. nfc->worker,
  35. NfcWorkerStateAnalyzeReader,
  36. &nfc->dev->dev_data,
  37. nfc_detect_reader_worker_callback,
  38. nfc);
  39. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewDetectReader);
  40. }
  41. bool nfc_scene_detect_reader_on_event(void* context, SceneManagerEvent event) {
  42. Nfc* nfc = context;
  43. bool consumed = false;
  44. uint32_t nonces_collected =
  45. scene_manager_get_scene_state(nfc->scene_manager, NfcSceneDetectReader);
  46. if(event.type == SceneManagerEventTypeCustom) {
  47. if(event.event == NfcCustomEventViewExit) {
  48. nfc_worker_stop(nfc->worker);
  49. scene_manager_next_scene(nfc->scene_manager, NfcSceneMfkeyNoncesInfo);
  50. consumed = true;
  51. } else if(event.event == NfcWorkerEventDetectReaderMfkeyCollected) {
  52. nonces_collected += 2;
  53. scene_manager_set_scene_state(
  54. nfc->scene_manager, NfcSceneDetectReader, nonces_collected);
  55. detect_reader_set_nonces_collected(nfc->detect_reader, nonces_collected);
  56. if(nonces_collected >= NFC_SCENE_DETECT_READER_PAIR_NONCES_MAX) {
  57. detect_reader_set_state(nfc->detect_reader, DetectReaderStateDone);
  58. nfc_blink_stop(nfc);
  59. notification_message(nfc->notifications, &sequence_single_vibro);
  60. notification_message(nfc->notifications, &sequence_set_green_255);
  61. nfc_worker_stop(nfc->worker);
  62. }
  63. consumed = true;
  64. } else if(event.event == NfcWorkerEventDetectReaderDetected) {
  65. if(nonces_collected < NFC_SCENE_DETECT_READER_PAIR_NONCES_MAX) {
  66. notification_message(nfc->notifications, &sequence_blink_start_cyan);
  67. detect_reader_set_state(nfc->detect_reader, DetectReaderStateReaderDetected);
  68. }
  69. } else if(event.event == NfcWorkerEventDetectReaderLost) {
  70. if(nonces_collected < NFC_SCENE_DETECT_READER_PAIR_NONCES_MAX) {
  71. nfc_blink_stop(nfc);
  72. notification_message(nfc->notifications, &sequence_detect_reader);
  73. detect_reader_set_state(nfc->detect_reader, DetectReaderStateReaderLost);
  74. }
  75. }
  76. }
  77. return consumed;
  78. }
  79. void nfc_scene_detect_reader_on_exit(void* context) {
  80. Nfc* nfc = context;
  81. // Stop worker
  82. nfc_worker_stop(nfc->worker);
  83. // Clear view
  84. detect_reader_reset(nfc->detect_reader);
  85. // Stop notifications
  86. nfc_blink_stop(nfc);
  87. notification_message(nfc->notifications, &sequence_reset_green);
  88. }