nfc_detect.c 4.1 KB

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