nfc.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #include "nfc.h"
  2. #include <flipper_v2.h>
  3. #include <gui/gui.h>
  4. #include <gui/widget.h>
  5. #include <gui/canvas.h>
  6. #include <assets_icons.h>
  7. #include <menu/menu.h>
  8. #include <menu/menu_item.h>
  9. #include <rfal_analogConfig.h>
  10. #include <rfal_rf.h>
  11. #include <rfal_nfc.h>
  12. #include <rfal_nfca.h>
  13. #include "dispatcher.h"
  14. typedef enum {
  15. MessageTypeBase,
  16. } NfcMessageType;
  17. typedef struct {
  18. Message base;
  19. void* data;
  20. } NfcMessage;
  21. struct Nfc {
  22. Dispatcher* dispatcher;
  23. Icon* icon;
  24. Widget* widget;
  25. ValueMutex* menu_vm;
  26. MenuItem* menu;
  27. rfalNfcDiscoverParam* disParams;
  28. osThreadAttr_t worker_attr;
  29. osThreadId_t worker;
  30. uint8_t screen;
  31. uint8_t ret;
  32. uint8_t devCnt;
  33. uint8_t ticker;
  34. char* current;
  35. };
  36. #define EXAMPLE_NFCA_DEVICES 5
  37. // TODO replace with pubsub
  38. static bool isr_enabled = false;
  39. void nfc_isr() {
  40. if(isr_enabled) {
  41. st25r3916Isr();
  42. }
  43. }
  44. void nfc_worker_task(void* context) {
  45. Nfc* nfc = context;
  46. ReturnCode err;
  47. rfalNfcaSensRes sensRes;
  48. rfalNfcaSelRes selRes;
  49. rfalNfcaListenDevice nfcaDevList[EXAMPLE_NFCA_DEVICES];
  50. uint8_t devCnt;
  51. uint8_t devIt;
  52. rfalLowPowerModeStop();
  53. nfc->ticker = 0;
  54. isr_enabled = true;
  55. while(widget_is_enabled(nfc->widget)) {
  56. rfalFieldOff();
  57. platformDelay(1000);
  58. nfc->ticker += 1;
  59. nfc->current = "Not detected";
  60. nfc->devCnt = 0;
  61. rfalNfcaPollerInitialize();
  62. rfalFieldOnAndStartGT();
  63. nfc->ret = err = rfalNfcaPollerTechnologyDetection(RFAL_COMPLIANCE_MODE_NFC, &sensRes);
  64. if(err == ERR_NONE) {
  65. err = rfalNfcaPollerFullCollisionResolution(
  66. RFAL_COMPLIANCE_MODE_NFC, EXAMPLE_NFCA_DEVICES, nfcaDevList, &devCnt);
  67. nfc->devCnt = devCnt;
  68. if((err == ERR_NONE) && (devCnt > 0)) {
  69. platformLog("NFC-A device(s) found %d\r\n", devCnt);
  70. devIt = 0;
  71. if(nfcaDevList[devIt].isSleep) {
  72. err = rfalNfcaPollerCheckPresence(
  73. RFAL_14443A_SHORTFRAME_CMD_WUPA, &sensRes); /* Wake up all cards */
  74. if(err != ERR_NONE) {
  75. continue;
  76. }
  77. err = rfalNfcaPollerSelect(
  78. nfcaDevList[devIt].nfcId1,
  79. nfcaDevList[devIt].nfcId1Len,
  80. &selRes); /* Select specific device */
  81. if(err != ERR_NONE) {
  82. continue;
  83. }
  84. }
  85. switch(nfcaDevList[devIt].type) {
  86. case RFAL_NFCA_T1T:
  87. /* No further activation needed for a T1T (RID already performed)*/
  88. platformLog(
  89. "NFC-A T1T device found \r\n"); /* NFC-A T1T device found, NFCID/UID is contained in: t1tRidRes.uid */
  90. nfc->current = "NFC-A T1T";
  91. /* Following communications shall be performed using:
  92. * - Non blocking: rfalStartTransceive() + rfalGetTransceiveState()
  93. * - Blocking: rfalTransceiveBlockingTx() + rfalTransceiveBlockingRx() or rfalTransceiveBlockingTxRx() */
  94. break;
  95. case RFAL_NFCA_T2T:
  96. /* No specific activation needed for a T2T */
  97. platformLog(
  98. "NFC-A T2T device found \r\n"); /* NFC-A T2T device found, NFCID/UID is contained in: nfcaDev.nfcid */
  99. nfc->current = "NFC-A T2T";
  100. /* Following communications shall be performed using:
  101. * - Non blocking: rfalStartTransceive() + rfalGetTransceiveState()
  102. * - Blocking: rfalTransceiveBlockingTx() + rfalTransceiveBlockingRx() or rfalTransceiveBlockingTxRx() */
  103. break;
  104. case RFAL_NFCA_T4T:
  105. platformLog(
  106. "NFC-A T4T (ISO-DEP) device found \r\n"); /* NFC-A T4T device found, NFCID/UID is contained in: nfcaDev.nfcid */
  107. nfc->current = "NFC-A T4T";
  108. /* Activation should continue using rfalIsoDepPollAHandleActivation(), see exampleRfalPoller.c */
  109. break;
  110. case RFAL_NFCA_T4T_NFCDEP: /* Device supports T4T and NFC-DEP */
  111. case RFAL_NFCA_NFCDEP: /* Device supports NFC-DEP */
  112. platformLog(
  113. "NFC-A P2P (NFC-DEP) device found \r\n"); /* NFC-A P2P device found, NFCID/UID is contained in: nfcaDev.nfcid */
  114. nfc->current = "NFC-A P2P";
  115. /* Activation should continue using rfalNfcDepInitiatorHandleActivation(), see exampleRfalPoller.c */
  116. break;
  117. }
  118. rfalNfcaPollerSleep(); /* Put device to sleep / HLTA (useless as the field will be turned off anyhow) */
  119. }
  120. }
  121. widget_update(nfc->widget);
  122. }
  123. isr_enabled = false;
  124. rfalFieldOff();
  125. rfalLowPowerModeStart();
  126. nfc->ret = ERR_NONE;
  127. nfc->worker = NULL;
  128. osThreadExit();
  129. }
  130. void nfc_draw_callback(CanvasApi* canvas, void* context) {
  131. furi_assert(canvas);
  132. furi_assert(context);
  133. Nfc* nfc = context;
  134. dispatcher_lock(nfc->dispatcher);
  135. canvas->clear(canvas);
  136. canvas->set_color(canvas, ColorBlack);
  137. canvas->set_font(canvas, FontPrimary);
  138. if(nfc->screen == 0) {
  139. char status[128 / 8];
  140. if(nfc->ret == ERR_WRONG_STATE)
  141. canvas->draw_str(canvas, 2, 16, "NFC Wrong State");
  142. else if(nfc->ret == ERR_PARAM)
  143. canvas->draw_str(canvas, 2, 16, "NFC Wrong Param");
  144. else if(nfc->ret == ERR_IO)
  145. canvas->draw_str(canvas, 2, 16, "NFC IO Error");
  146. else if(nfc->ret == ERR_NONE)
  147. canvas->draw_str(canvas, 2, 16, "NFC Device Found");
  148. else if(nfc->ret == ERR_TIMEOUT)
  149. canvas->draw_str(canvas, 2, 16, "NFC Timeout");
  150. else
  151. canvas->draw_str(canvas, 2, 16, "NFC error");
  152. snprintf(status, sizeof(status), "Tck:%d Cnt:%d", nfc->ticker, nfc->devCnt);
  153. canvas->draw_str(canvas, 2, 32, status);
  154. canvas->draw_str(canvas, 2, 46, nfc->current);
  155. } else {
  156. canvas->draw_str(canvas, 2, 16, "Not implemented");
  157. }
  158. dispatcher_unlock(nfc->dispatcher);
  159. }
  160. void nfc_input_callback(InputEvent* event, void* context) {
  161. furi_assert(event);
  162. furi_assert(context);
  163. Nfc* nfc = context;
  164. if(!event->state) return;
  165. widget_enabled_set(nfc->widget, false);
  166. }
  167. void nfc_test_callback(void* context) {
  168. furi_assert(context);
  169. Nfc* nfc = context;
  170. dispatcher_lock(nfc->dispatcher);
  171. nfc->screen = 0;
  172. widget_enabled_set(nfc->widget, true);
  173. if(nfc->ret == ERR_NONE && !nfc->worker) {
  174. // TODO change to fuirac_start
  175. nfc->worker = osThreadNew(nfc_worker_task, nfc, &nfc->worker_attr);
  176. }
  177. dispatcher_unlock(nfc->dispatcher);
  178. }
  179. void nfc_read_callback(void* context) {
  180. furi_assert(context);
  181. Nfc* nfc = context;
  182. nfc->screen = 1;
  183. widget_enabled_set(nfc->widget, true);
  184. }
  185. void nfc_write_callback(void* context) {
  186. furi_assert(context);
  187. Nfc* nfc = context;
  188. nfc->screen = 1;
  189. widget_enabled_set(nfc->widget, true);
  190. }
  191. void nfc_bridge_callback(void* context) {
  192. furi_assert(context);
  193. Nfc* nfc = context;
  194. nfc->screen = 1;
  195. widget_enabled_set(nfc->widget, true);
  196. }
  197. Nfc* nfc_alloc() {
  198. Nfc* nfc = furi_alloc(sizeof(Nfc));
  199. nfc->dispatcher = dispatcher_alloc(32, sizeof(NfcMessage));
  200. nfc->icon = assets_icons_get(A_NFC_14);
  201. nfc->widget = widget_alloc();
  202. widget_draw_callback_set(nfc->widget, nfc_draw_callback, nfc);
  203. widget_input_callback_set(nfc->widget, nfc_input_callback, nfc);
  204. nfc->menu_vm = furi_open("menu");
  205. furi_check(nfc->menu_vm);
  206. nfc->menu = menu_item_alloc_menu("NFC", nfc->icon);
  207. menu_item_subitem_add(
  208. nfc->menu, menu_item_alloc_function("Test", NULL, nfc_test_callback, nfc));
  209. menu_item_subitem_add(
  210. nfc->menu, menu_item_alloc_function("Read", NULL, nfc_read_callback, nfc));
  211. menu_item_subitem_add(
  212. nfc->menu, menu_item_alloc_function("Write", NULL, nfc_write_callback, nfc));
  213. menu_item_subitem_add(
  214. nfc->menu, menu_item_alloc_function("Brdige", NULL, nfc_bridge_callback, nfc));
  215. nfc->worker_attr.name = "nfc_worker";
  216. // nfc->worker_attr.attr_bits = osThreadJoinable;
  217. nfc->worker_attr.stack_size = 4096;
  218. return nfc;
  219. }
  220. void nfc_task(void* p) {
  221. Nfc* nfc = nfc_alloc();
  222. FuriRecordSubscriber* gui_record = furi_open_deprecated("gui", false, false, NULL, NULL, NULL);
  223. furi_check(gui_record);
  224. GuiApi* gui = furi_take(gui_record);
  225. furi_check(gui);
  226. widget_enabled_set(nfc->widget, false);
  227. gui->add_widget(gui, nfc->widget, GuiLayerFullscreen);
  228. furi_commit(gui_record);
  229. with_value_mutex(
  230. nfc->menu_vm, (Menu * menu) { menu_item_add(menu, nfc->menu); });
  231. if(!furi_create("nfc", nfc)) {
  232. printf("[nfc_task] cannot create nfc record\n");
  233. furiac_exit(NULL);
  234. }
  235. nfc->ret = rfalNfcInitialize();
  236. rfalLowPowerModeStart();
  237. furiac_ready();
  238. NfcMessage message;
  239. while(1) {
  240. dispatcher_recieve(nfc->dispatcher, (Message*)&message);
  241. if(message.base.type == MessageTypeExit) {
  242. break;
  243. }
  244. }
  245. }