nfc.c 9.1 KB

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