nfc_detect.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "nfc_detect.h"
  2. #include "nfc_i.h"
  3. #include "nfc_types.h"
  4. #include <furi.h>
  5. #include <api-hal.h>
  6. #include <input/input.h>
  7. struct NfcDetect {
  8. NfcCommon* nfc_common;
  9. View* view;
  10. };
  11. typedef struct {
  12. bool found;
  13. NfcDeviceData data;
  14. } NfcDetectModel;
  15. void nfc_detect_draw(Canvas* canvas, NfcDetectModel* model) {
  16. char buffer[32];
  17. canvas_clear(canvas);
  18. canvas_set_font(canvas, FontPrimary);
  19. if(model->found) {
  20. canvas_draw_str(canvas, 0, 12, "Found");
  21. canvas_draw_str(canvas, 32, 12, nfc_get_dev_type(model->data.device));
  22. canvas_set_font(canvas, FontSecondary);
  23. if(model->data.protocol != NfcDeviceProtocolUnknown) {
  24. canvas_draw_str(canvas, 0, 22, nfc_get_protocol(model->data.protocol));
  25. }
  26. // Display UID
  27. for(uint8_t i = 0; i < model->data.uid_len; i++) {
  28. snprintf(buffer + (i * 2), sizeof(buffer) - (i * 2), "%02X", model->data.uid[i]);
  29. buffer[model->data.uid_len * 2] = 0;
  30. }
  31. canvas_draw_str(canvas, 0, 32, "UID: ");
  32. canvas_draw_str(canvas, 22, 32, buffer);
  33. // Display ATQA and SAK
  34. snprintf(
  35. buffer,
  36. sizeof(buffer),
  37. "ATQA: %02X %02X SAK: %02X",
  38. model->data.atqa[1],
  39. model->data.atqa[0],
  40. model->data.sak);
  41. canvas_draw_str(canvas, 0, 42, buffer);
  42. } else {
  43. canvas_draw_str(canvas, 0, 12, "Searching");
  44. canvas_set_font(canvas, FontSecondary);
  45. canvas_draw_str(canvas, 2, 22, "Place card to the back");
  46. }
  47. }
  48. bool nfc_detect_input(InputEvent* event, void* context) {
  49. if(event->key == InputKeyBack) {
  50. return false;
  51. }
  52. return true;
  53. }
  54. void nfc_detect_worker_callback(void* context) {
  55. furi_assert(context);
  56. NfcDetect* nfc_detect = (NfcDetect*)context;
  57. view_dispatcher_send_custom_event(nfc_detect->nfc_common->view_dispatcher, NfcEventDetect);
  58. }
  59. void nfc_detect_view_dispatcher_callback(NfcDetect* nfc_detect, NfcMessage* message) {
  60. furi_assert(nfc_detect);
  61. furi_assert(message);
  62. if(message->found) {
  63. with_view_model(
  64. nfc_detect->view, (NfcDetectModel * model) {
  65. model->found = true;
  66. model->data = message->nfc_detect_data;
  67. return true;
  68. });
  69. } else {
  70. with_view_model(
  71. nfc_detect->view, (NfcDetectModel * model) {
  72. model->found = false;
  73. return true;
  74. });
  75. }
  76. }
  77. void nfc_detect_enter(void* context) {
  78. furi_assert(context);
  79. NfcDetect* nfc_detect = (NfcDetect*)context;
  80. with_view_model(
  81. nfc_detect->view, (NfcDetectModel * model) {
  82. model->found = false;
  83. model->data.protocol = NfcDeviceProtocolUnknown;
  84. return true;
  85. });
  86. nfc_worker_start(
  87. nfc_detect->nfc_common->worker,
  88. NfcWorkerStateDetect,
  89. nfc_detect_worker_callback,
  90. nfc_detect);
  91. }
  92. void nfc_detect_exit(void* context) {
  93. furi_assert(context);
  94. NfcDetect* nfc_detect = (NfcDetect*)context;
  95. nfc_worker_stop(nfc_detect->nfc_common->worker);
  96. }
  97. uint32_t nfc_detect_back(void* context) {
  98. return NfcViewMenu;
  99. }
  100. NfcDetect* nfc_detect_alloc(NfcCommon* nfc_common) {
  101. furi_assert(nfc_common);
  102. NfcDetect* nfc_detect = furi_alloc(sizeof(NfcDetect));
  103. nfc_detect->nfc_common = nfc_common;
  104. // View allocation and configuration
  105. nfc_detect->view = view_alloc();
  106. view_allocate_model(nfc_detect->view, ViewModelTypeLockFree, sizeof(NfcDetectModel));
  107. view_set_context(nfc_detect->view, nfc_detect);
  108. view_set_draw_callback(nfc_detect->view, (ViewDrawCallback)nfc_detect_draw);
  109. view_set_input_callback(nfc_detect->view, nfc_detect_input);
  110. view_set_enter_callback(nfc_detect->view, nfc_detect_enter);
  111. view_set_exit_callback(nfc_detect->view, nfc_detect_exit);
  112. view_set_previous_callback(nfc_detect->view, nfc_detect_back);
  113. return nfc_detect;
  114. }
  115. void nfc_detect_free(NfcDetect* nfc_detect) {
  116. furi_assert(nfc_detect);
  117. view_free(nfc_detect->view);
  118. free(nfc_detect);
  119. }
  120. View* nfc_detect_get_view(NfcDetect* nfc_detect) {
  121. furi_assert(nfc_detect);
  122. return nfc_detect->view;
  123. }