nfc_worker.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "nfc_worker.h"
  2. #include "nfc.h"
  3. #include "nfc_i.h"
  4. #define EXAMPLE_NFCA_DEVICES 5
  5. // TODO replace with pubsub
  6. static bool isr_enabled = false;
  7. void nfc_isr() {
  8. if(isr_enabled) {
  9. st25r3916Isr();
  10. }
  11. }
  12. void nfc_worker_task(void* context) {
  13. Nfc* nfc = context;
  14. ReturnCode err;
  15. rfalNfcaSensRes sensRes;
  16. rfalNfcaSelRes selRes;
  17. rfalNfcaListenDevice nfcaDevList[EXAMPLE_NFCA_DEVICES];
  18. uint8_t devCnt;
  19. uint8_t devIt;
  20. rfalLowPowerModeStop();
  21. isr_enabled = true;
  22. while(widget_is_enabled(nfc->widget)) {
  23. rfalFieldOff();
  24. platformDelay(500);
  25. nfc->current = "Not detected";
  26. nfc->devCnt = 0;
  27. rfalNfcaPollerInitialize();
  28. rfalFieldOnAndStartGT();
  29. nfc->ret = err = rfalNfcaPollerTechnologyDetection(RFAL_COMPLIANCE_MODE_NFC, &sensRes);
  30. if(err == ERR_NONE) {
  31. err = rfalNfcaPollerFullCollisionResolution(
  32. RFAL_COMPLIANCE_MODE_NFC, EXAMPLE_NFCA_DEVICES, nfcaDevList, &devCnt);
  33. nfc->devCnt = devCnt;
  34. if((err == ERR_NONE) && (devCnt > 0)) {
  35. platformLog("NFC-A device(s) found %d\r\n", devCnt);
  36. devIt = 0;
  37. if(nfcaDevList[devIt].isSleep) {
  38. err = rfalNfcaPollerCheckPresence(
  39. RFAL_14443A_SHORTFRAME_CMD_WUPA, &sensRes); /* Wake up all cards */
  40. if(err != ERR_NONE) {
  41. continue;
  42. }
  43. err = rfalNfcaPollerSelect(
  44. nfcaDevList[devIt].nfcId1,
  45. nfcaDevList[devIt].nfcId1Len,
  46. &selRes); /* Select specific device */
  47. if(err != ERR_NONE) {
  48. continue;
  49. }
  50. }
  51. nfc->first_atqa = nfcaDevList[devIt].sensRes;
  52. nfc->first_sak = nfcaDevList[devIt].selRes;
  53. switch(nfcaDevList[devIt].type) {
  54. case RFAL_NFCA_T1T:
  55. /* No further activation needed for a T1T (RID already performed)*/
  56. platformLog(
  57. "NFC-A T1T device found \r\n"); /* NFC-A T1T device found, NFCID/UID is contained in: t1tRidRes.uid */
  58. nfc->current = "NFC-A T1T";
  59. /* Following communications shall be performed using:
  60. * - Non blocking: rfalStartTransceive() + rfalGetTransceiveState()
  61. * - Blocking: rfalTransceiveBlockingTx() + rfalTransceiveBlockingRx() or rfalTransceiveBlockingTxRx() */
  62. break;
  63. case RFAL_NFCA_T2T:
  64. /* No specific activation needed for a T2T */
  65. platformLog(
  66. "NFC-A T2T device found \r\n"); /* NFC-A T2T device found, NFCID/UID is contained in: nfcaDev.nfcid */
  67. nfc->current = "NFC-A T2T";
  68. /* Following communications shall be performed using:
  69. * - Non blocking: rfalStartTransceive() + rfalGetTransceiveState()
  70. * - Blocking: rfalTransceiveBlockingTx() + rfalTransceiveBlockingRx() or rfalTransceiveBlockingTxRx() */
  71. break;
  72. case RFAL_NFCA_T4T:
  73. platformLog(
  74. "NFC-A T4T (ISO-DEP) device found \r\n"); /* NFC-A T4T device found, NFCID/UID is contained in: nfcaDev.nfcid */
  75. nfc->current = "NFC-A T4T";
  76. /* Activation should continue using rfalIsoDepPollAHandleActivation(), see exampleRfalPoller.c */
  77. break;
  78. case RFAL_NFCA_T4T_NFCDEP: /* Device supports T4T and NFC-DEP */
  79. case RFAL_NFCA_NFCDEP: /* Device supports NFC-DEP */
  80. platformLog(
  81. "NFC-A P2P (NFC-DEP) device found \r\n"); /* NFC-A P2P device found, NFCID/UID is contained in: nfcaDev.nfcid */
  82. nfc->current = "NFC-A P2P";
  83. /* Activation should continue using rfalNfcDepInitiatorHandleActivation(), see exampleRfalPoller.c */
  84. break;
  85. }
  86. rfalNfcaPollerSleep(); /* Put device to sleep / HLTA (useless as the field will be turned off anyhow) */
  87. }
  88. }
  89. widget_update(nfc->widget);
  90. }
  91. isr_enabled = false;
  92. rfalFieldOff();
  93. rfalLowPowerModeStart();
  94. nfc->ret = ERR_NONE;
  95. nfc->worker = NULL;
  96. osThreadExit();
  97. }