nfc.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "nfc.h"
  2. #include "nfc_i.h"
  3. #include "nfc_worker.h"
  4. void nfc_draw_callback(CanvasApi* canvas, void* context) {
  5. furi_assert(canvas);
  6. furi_assert(context);
  7. Nfc* nfc = context;
  8. dispatcher_lock(nfc->dispatcher);
  9. canvas->clear(canvas);
  10. canvas->set_color(canvas, ColorBlack);
  11. canvas->set_font(canvas, FontPrimary);
  12. if(nfc->screen == 0) {
  13. char status[128 / 8];
  14. if(nfc->ret == ERR_WRONG_STATE)
  15. canvas->draw_str(canvas, 2, 16, "NFC Wrong State");
  16. else if(nfc->ret == ERR_PARAM)
  17. canvas->draw_str(canvas, 2, 16, "NFC Wrong Param");
  18. else if(nfc->ret == ERR_IO)
  19. canvas->draw_str(canvas, 2, 16, "NFC IO Error");
  20. else if(nfc->ret == ERR_NONE)
  21. canvas->draw_str(canvas, 2, 16, "NFC Device Found");
  22. else if(nfc->ret == ERR_TIMEOUT)
  23. canvas->draw_str(canvas, 2, 16, "NFC Timeout");
  24. else
  25. canvas->draw_str(canvas, 2, 16, "NFC error");
  26. canvas->set_font(canvas, FontSecondary);
  27. snprintf(status, sizeof(status), "Found: %d", nfc->devCnt);
  28. if(nfc->devCnt > 0) {
  29. canvas->draw_str(canvas, 2, 32, status);
  30. canvas->draw_str(canvas, 2, 42, nfc->current);
  31. snprintf(
  32. status,
  33. sizeof(status),
  34. "ATQA:%d SAK:%d",
  35. nfc->first_atqa.anticollisionInfo,
  36. nfc->first_sak.sak);
  37. canvas->draw_str(canvas, 2, 52, status);
  38. }
  39. } else {
  40. canvas->draw_str(canvas, 2, 16, "Not implemented");
  41. }
  42. dispatcher_unlock(nfc->dispatcher);
  43. }
  44. void nfc_input_callback(InputEvent* event, void* context) {
  45. furi_assert(event);
  46. furi_assert(context);
  47. Nfc* nfc = context;
  48. if(!event->state || event->input != InputBack) return;
  49. widget_enabled_set(nfc->widget, false);
  50. }
  51. void nfc_test_callback(void* context) {
  52. furi_assert(context);
  53. Nfc* nfc = context;
  54. dispatcher_lock(nfc->dispatcher);
  55. nfc->screen = 0;
  56. widget_enabled_set(nfc->widget, true);
  57. if(nfc->ret == ERR_NONE && !nfc->worker) {
  58. // TODO change to fuirac_start
  59. nfc->worker = osThreadNew(nfc_worker_task, nfc, &nfc->worker_attr);
  60. }
  61. dispatcher_unlock(nfc->dispatcher);
  62. }
  63. void nfc_field_on_callback(void* context) {
  64. st25r3916OscOn();
  65. st25r3916TxRxOn();
  66. }
  67. void nfc_field_off_callback(void* context) {
  68. st25r3916TxRxOff();
  69. }
  70. void nfc_read_callback(void* context) {
  71. furi_assert(context);
  72. Nfc* nfc = context;
  73. nfc->screen = 1;
  74. widget_enabled_set(nfc->widget, true);
  75. }
  76. void nfc_write_callback(void* context) {
  77. furi_assert(context);
  78. Nfc* nfc = context;
  79. nfc->screen = 1;
  80. widget_enabled_set(nfc->widget, true);
  81. }
  82. void nfc_bridge_callback(void* context) {
  83. furi_assert(context);
  84. Nfc* nfc = context;
  85. nfc->screen = 1;
  86. widget_enabled_set(nfc->widget, true);
  87. }
  88. Nfc* nfc_alloc() {
  89. Nfc* nfc = furi_alloc(sizeof(Nfc));
  90. nfc->dispatcher = dispatcher_alloc(32, sizeof(NfcMessage));
  91. nfc->icon = assets_icons_get(A_NFC_14);
  92. nfc->widget = widget_alloc();
  93. widget_draw_callback_set(nfc->widget, nfc_draw_callback, nfc);
  94. widget_input_callback_set(nfc->widget, nfc_input_callback, nfc);
  95. nfc->menu_vm = furi_open("menu");
  96. furi_check(nfc->menu_vm);
  97. nfc->menu = menu_item_alloc_menu("NFC", nfc->icon);
  98. menu_item_subitem_add(
  99. nfc->menu, menu_item_alloc_function("Test", NULL, nfc_test_callback, nfc));
  100. menu_item_subitem_add(
  101. nfc->menu, menu_item_alloc_function("Field On", NULL, nfc_field_on_callback, nfc));
  102. menu_item_subitem_add(
  103. nfc->menu, menu_item_alloc_function("Field Off", NULL, nfc_field_off_callback, nfc));
  104. menu_item_subitem_add(
  105. nfc->menu, menu_item_alloc_function("Read", NULL, nfc_read_callback, nfc));
  106. menu_item_subitem_add(
  107. nfc->menu, menu_item_alloc_function("Write", NULL, nfc_write_callback, nfc));
  108. menu_item_subitem_add(
  109. nfc->menu, menu_item_alloc_function("Brdige", NULL, nfc_bridge_callback, nfc));
  110. nfc->worker_attr.name = "nfc_worker";
  111. // nfc->worker_attr.attr_bits = osThreadJoinable;
  112. nfc->worker_attr.stack_size = 4096;
  113. return nfc;
  114. }
  115. void nfc_task(void* p) {
  116. Nfc* nfc = nfc_alloc();
  117. FuriRecordSubscriber* gui_record = furi_open_deprecated("gui", false, false, NULL, NULL, NULL);
  118. furi_check(gui_record);
  119. GuiApi* gui = furi_take(gui_record);
  120. furi_check(gui);
  121. widget_enabled_set(nfc->widget, false);
  122. gui->add_widget(gui, nfc->widget, GuiLayerFullscreen);
  123. furi_commit(gui_record);
  124. with_value_mutex(
  125. nfc->menu_vm, (Menu * menu) { menu_item_add(menu, nfc->menu); });
  126. if(!furi_create("nfc", nfc)) {
  127. printf("[nfc_task] cannot create nfc record\n");
  128. furiac_exit(NULL);
  129. }
  130. nfc->ret = rfalNfcInitialize();
  131. rfalLowPowerModeStart();
  132. furiac_ready();
  133. NfcMessage message;
  134. while(1) {
  135. dispatcher_recieve(nfc->dispatcher, (Message*)&message);
  136. if(message.base.type == MessageTypeExit) {
  137. break;
  138. }
  139. }
  140. }