2021-08-08 18:03:25 +00:00
|
|
|
#include "furi-hal-nfc.h"
|
2021-05-18 09:23:14 +00:00
|
|
|
#include <st25r3916.h>
|
|
|
|
|
2021-06-16 14:45:35 +00:00
|
|
|
static const uint32_t clocks_in_ms = 64 * 1000;
|
|
|
|
|
2021-08-08 18:03:25 +00:00
|
|
|
void furi_hal_nfc_init() {
|
2021-07-28 08:45:42 +00:00
|
|
|
ReturnCode ret = rfalNfcInitialize();
|
|
|
|
if(ret == ERR_NONE) {
|
2021-08-08 18:03:25 +00:00
|
|
|
furi_hal_nfc_start_sleep();
|
2021-07-28 08:45:42 +00:00
|
|
|
FURI_LOG_I("FuriHalNfc", "Init OK");
|
2021-06-24 08:46:37 +00:00
|
|
|
} else {
|
2021-07-28 08:45:42 +00:00
|
|
|
FURI_LOG_W("FuriHalNfc", "Initialization failed, RFAL returned: %d", ret);
|
2021-05-18 09:23:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-08 18:03:25 +00:00
|
|
|
bool furi_hal_nfc_is_busy() {
|
2021-07-28 08:45:42 +00:00
|
|
|
return rfalNfcGetState() != RFAL_NFC_STATE_IDLE;
|
2021-05-18 09:23:14 +00:00
|
|
|
}
|
|
|
|
|
2021-08-08 18:03:25 +00:00
|
|
|
void furi_hal_nfc_field_on() {
|
|
|
|
furi_hal_nfc_exit_sleep();
|
2021-05-18 09:23:14 +00:00
|
|
|
st25r3916TxRxOn();
|
|
|
|
}
|
|
|
|
|
2021-08-08 18:03:25 +00:00
|
|
|
void furi_hal_nfc_field_off() {
|
2021-05-18 09:23:14 +00:00
|
|
|
st25r3916TxRxOff();
|
2021-08-08 18:03:25 +00:00
|
|
|
furi_hal_nfc_start_sleep();
|
2021-05-18 09:23:14 +00:00
|
|
|
}
|
|
|
|
|
2021-08-08 18:03:25 +00:00
|
|
|
void furi_hal_nfc_start_sleep() {
|
2021-05-18 09:23:14 +00:00
|
|
|
rfalLowPowerModeStart();
|
|
|
|
}
|
|
|
|
|
2021-08-08 18:03:25 +00:00
|
|
|
void furi_hal_nfc_exit_sleep() {
|
2021-05-18 09:23:14 +00:00
|
|
|
rfalLowPowerModeStop();
|
|
|
|
}
|
|
|
|
|
2021-08-08 18:03:25 +00:00
|
|
|
bool furi_hal_nfc_detect(rfalNfcDevice **dev_list, uint8_t* dev_cnt, uint32_t timeout, bool deactivate) {
|
2021-05-18 09:23:14 +00:00
|
|
|
furi_assert(dev_list);
|
|
|
|
furi_assert(dev_cnt);
|
|
|
|
|
|
|
|
rfalLowPowerModeStop();
|
2021-06-16 14:45:35 +00:00
|
|
|
rfalNfcState state = rfalNfcGetState();
|
|
|
|
if(state == RFAL_NFC_STATE_NOTINIT) {
|
2021-05-18 09:23:14 +00:00
|
|
|
rfalNfcInitialize();
|
|
|
|
}
|
|
|
|
rfalNfcDiscoverParam params;
|
|
|
|
params.compMode = RFAL_COMPLIANCE_MODE_EMV;
|
|
|
|
params.techs2Find = RFAL_NFC_POLL_TECH_A | RFAL_NFC_POLL_TECH_B | RFAL_NFC_POLL_TECH_F |
|
|
|
|
RFAL_NFC_POLL_TECH_V | RFAL_NFC_POLL_TECH_AP2P | RFAL_NFC_POLL_TECH_ST25TB;
|
|
|
|
params.totalDuration = 1000;
|
|
|
|
params.devLimit = 3;
|
|
|
|
params.wakeupEnabled = false;
|
|
|
|
params.wakeupConfigDefault = true;
|
|
|
|
params.nfcfBR = RFAL_BR_212;
|
|
|
|
params.ap2pBR = RFAL_BR_424;
|
|
|
|
params.maxBR = RFAL_BR_KEEP;
|
|
|
|
params.GBLen = RFAL_NFCDEP_GB_MAX_LEN;
|
2021-06-16 14:45:35 +00:00
|
|
|
params.notifyCb = NULL;
|
2021-05-18 09:23:14 +00:00
|
|
|
|
2021-06-16 14:45:35 +00:00
|
|
|
uint32_t start = DWT->CYCCNT;
|
2021-05-18 09:23:14 +00:00
|
|
|
rfalNfcDiscover(¶ms);
|
2021-06-16 14:45:35 +00:00
|
|
|
while(state != RFAL_NFC_STATE_ACTIVATED) {
|
2021-05-18 09:23:14 +00:00
|
|
|
rfalNfcWorker();
|
2021-06-16 14:45:35 +00:00
|
|
|
state = rfalNfcGetState();
|
|
|
|
FURI_LOG_D("HAL NFC", "Current state %d", state);
|
|
|
|
if(state == RFAL_NFC_STATE_POLL_ACTIVATION) {
|
|
|
|
start = DWT->CYCCNT;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(state == RFAL_NFC_STATE_POLL_SELECT) {
|
|
|
|
rfalNfcSelect(0);
|
|
|
|
}
|
|
|
|
if(DWT->CYCCNT - start > timeout * clocks_in_ms) {
|
|
|
|
rfalNfcDeactivate(true);
|
|
|
|
FURI_LOG_D("HAL NFC", "Timeout");
|
|
|
|
return false;
|
2021-05-18 09:23:14 +00:00
|
|
|
}
|
2021-06-16 14:45:35 +00:00
|
|
|
osThreadYield();
|
2021-05-18 09:23:14 +00:00
|
|
|
}
|
2021-06-16 14:45:35 +00:00
|
|
|
rfalNfcGetDevicesFound(dev_list, dev_cnt);
|
2021-05-18 18:12:01 +00:00
|
|
|
if(deactivate) {
|
|
|
|
rfalNfcDeactivate(false);
|
|
|
|
rfalLowPowerModeStart();
|
|
|
|
}
|
2021-05-18 09:23:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-05-18 18:12:01 +00:00
|
|
|
|
2021-08-16 21:45:04 +00:00
|
|
|
bool furi_hal_nfc_listen(uint8_t* uid, uint8_t uid_len, uint8_t* atqa, uint8_t sak, bool activate_after_sak, uint32_t timeout) {
|
2021-05-25 23:54:44 +00:00
|
|
|
rfalNfcState state = rfalNfcGetState();
|
|
|
|
if(state == RFAL_NFC_STATE_NOTINIT) {
|
|
|
|
rfalNfcInitialize();
|
|
|
|
} else if(state >= RFAL_NFC_STATE_ACTIVATED) {
|
|
|
|
rfalNfcDeactivate(false);
|
|
|
|
}
|
2021-08-03 17:24:20 +00:00
|
|
|
rfalLowPowerModeStop();
|
2021-06-30 17:43:29 +00:00
|
|
|
rfalNfcDiscoverParam params = {
|
|
|
|
.compMode = RFAL_COMPLIANCE_MODE_NFC,
|
|
|
|
.techs2Find = RFAL_NFC_LISTEN_TECH_A,
|
|
|
|
.totalDuration = 1000,
|
|
|
|
.devLimit = 1,
|
|
|
|
.wakeupEnabled = false,
|
|
|
|
.wakeupConfigDefault = true,
|
|
|
|
.nfcfBR = RFAL_BR_212,
|
|
|
|
.ap2pBR = RFAL_BR_424,
|
|
|
|
.maxBR = RFAL_BR_KEEP,
|
|
|
|
.GBLen = RFAL_NFCDEP_GB_MAX_LEN,
|
|
|
|
.notifyCb = NULL,
|
2021-08-16 21:45:04 +00:00
|
|
|
.activate_after_sak = activate_after_sak,
|
2021-06-30 17:43:29 +00:00
|
|
|
};
|
|
|
|
params.lmConfigPA.nfcidLen = uid_len;
|
|
|
|
memcpy(params.lmConfigPA.nfcid, uid, uid_len);
|
|
|
|
params.lmConfigPA.SENS_RES[0] = atqa[0];
|
|
|
|
params.lmConfigPA.SENS_RES[1] = atqa[1];
|
|
|
|
params.lmConfigPA.SEL_RES = sak;
|
|
|
|
rfalNfcDiscover(¶ms);
|
2021-05-25 23:54:44 +00:00
|
|
|
|
|
|
|
uint32_t start = DWT->CYCCNT;
|
|
|
|
while(state != RFAL_NFC_STATE_ACTIVATED) {
|
|
|
|
rfalNfcWorker();
|
|
|
|
state = rfalNfcGetState();
|
|
|
|
if(DWT->CYCCNT - start > timeout * clocks_in_ms) {
|
|
|
|
rfalNfcDeactivate(true);
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-16 14:45:35 +00:00
|
|
|
osThreadYield();
|
2021-05-25 23:54:44 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-16 21:45:04 +00:00
|
|
|
bool furi_hal_nfc_get_first_frame(uint8_t** rx_buff, uint16_t** rx_len) {
|
|
|
|
ReturnCode ret = rfalNfcDataExchangeStart(NULL, 0, rx_buff, rx_len, 0);
|
|
|
|
return ret == ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2021-08-08 18:03:25 +00:00
|
|
|
ReturnCode furi_hal_nfc_data_exchange(uint8_t* tx_buff, uint16_t tx_len, uint8_t** rx_buff, uint16_t** rx_len, bool deactivate) {
|
2021-05-18 18:12:01 +00:00
|
|
|
furi_assert(rx_buff);
|
|
|
|
furi_assert(rx_len);
|
|
|
|
|
|
|
|
ReturnCode ret;
|
|
|
|
rfalNfcState state = RFAL_NFC_STATE_ACTIVATED;
|
|
|
|
ret = rfalNfcDataExchangeStart(tx_buff, tx_len, rx_buff, rx_len, 0);
|
|
|
|
if(ret != ERR_NONE) {
|
|
|
|
return ret;
|
|
|
|
}
|
2021-05-25 23:54:44 +00:00
|
|
|
uint32_t start = DWT->CYCCNT;
|
2021-05-18 18:12:01 +00:00
|
|
|
while(state != RFAL_NFC_STATE_DATAEXCHANGE_DONE) {
|
|
|
|
rfalNfcWorker();
|
|
|
|
state = rfalNfcGetState();
|
2021-05-25 23:54:44 +00:00
|
|
|
ret = rfalNfcDataExchangeGetStatus();
|
|
|
|
if(ret > ERR_SLEEP_REQ) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if(ret == ERR_BUSY) {
|
|
|
|
if(DWT->CYCCNT - start > 1000 * clocks_in_ms) {
|
|
|
|
return ERR_TIMEOUT;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
start = DWT->CYCCNT;
|
|
|
|
}
|
|
|
|
taskYIELD();
|
2021-05-18 18:12:01 +00:00
|
|
|
}
|
|
|
|
if(deactivate) {
|
|
|
|
rfalNfcDeactivate(false);
|
|
|
|
rfalLowPowerModeStart();
|
|
|
|
}
|
|
|
|
return ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2021-08-08 18:03:25 +00:00
|
|
|
void furi_hal_nfc_deactivate() {
|
2021-05-18 18:12:01 +00:00
|
|
|
rfalNfcDeactivate(false);
|
|
|
|
rfalLowPowerModeStart();
|
|
|
|
}
|