nfc.c 9.0 KB

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