nfc.c 9.2 KB

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