*_is_present(): more robust detection of tag gone

This commit is contained in:
Philippe Teuwen 2014-01-30 18:30:45 +01:00
parent 9a84bb0ba0
commit 7d41ed34a3

View file

@ -1737,17 +1737,23 @@ static int pn53x_Diagnose06(struct nfc_device *pnd)
const uint8_t abtCmd[] = { Diagnose, 0x06 }; const uint8_t abtCmd[] = { Diagnose, 0x06 };
uint8_t abtRx[1]; uint8_t abtRx[1];
int ret = 0; int ret = 0;
int failures = 0;
// Card Presence command can take more time than default one: when a card is // Card Presence command can take more time than default one: when a card is
// removed from the field, the PN53x took few hundred ms more to reply // removed from the field, the PN53x took few hundred ms more to reply
// correctly. (ie. 700 ms should be enough to detect all tested cases) // correctly. (ie. 700 ms should be enough to detect all tested cases)
if ((ret = pn53x_transceive(pnd, abtCmd, sizeof(abtCmd), abtRx, sizeof(abtRx), 700)) != 1) {
// When it fails with a timeout (0x01) chip error, it means the target is not reacheable anymore while (failures < 2) {
if ((ret == NFC_ERFTRANS) && (CHIP_DATA(pnd)->last_status_byte == 0x01)) { if ((ret = pn53x_transceive(pnd, abtCmd, sizeof(abtCmd), abtRx, sizeof(abtRx), 700)) != 1) {
ret = NFC_ETGRELEASED; // When it fails with a timeout (0x01) chip error, it means the target is not reacheable anymore
if ((ret == NFC_ERFTRANS) && (CHIP_DATA(pnd)->last_status_byte == 0x01)) {
return NFC_ETGRELEASED;
} else { // Other errors can appear when card is tired-off, let's try again
failures++;
}
} else {
return NFC_SUCCESS;
} }
} else {
ret = NFC_SUCCESS;
} }
return ret; return ret;
} }
@ -1773,10 +1779,17 @@ static int pn53x_ISO14443A_MFUL_is_present(struct nfc_device *pnd)
ret = pn53x_Diagnose06(pnd); ret = pn53x_Diagnose06(pnd);
} else { } else {
uint8_t abtCmd[2] = {0x30, 0x00}; uint8_t abtCmd[2] = {0x30, 0x00};
if (nfc_initiator_transceive_bytes(pnd, abtCmd, sizeof(abtCmd), NULL, 0, -1) > 0) { int failures = 0;
ret = NFC_SUCCESS; while (failures < 2) {
} else { if ((ret = nfc_initiator_transceive_bytes(pnd, abtCmd, sizeof(abtCmd), NULL, 0, -1)) < 1) {
ret = NFC_ETGRELEASED; if ((ret == NFC_ERFTRANS) && (CHIP_DATA(pnd)->last_status_byte == 0x01)) { // Timeout
return NFC_ETGRELEASED;
} else { // Other errors can appear when card is tired-off, let's try again
failures++;
}
} else {
return NFC_SUCCESS;
}
} }
} }
return ret; return ret;
@ -1847,10 +1860,20 @@ static int pn53x_ISO14443B_4_is_present(struct nfc_device *pnd)
return ret; return ret;
// uint8_t abtCmd[1] = {0xb2}; // if on PN533, CID=0 // uint8_t abtCmd[1] = {0xb2}; // if on PN533, CID=0
uint8_t abtCmd[2] = {0xba, 0x01}; // if on PN532, CID=1 uint8_t abtCmd[2] = {0xba, 0x01}; // if on PN532, CID=1
if (nfc_initiator_transceive_bytes(pnd, abtCmd, sizeof(abtCmd), NULL, 0, 300) > 0) int failures = 0;
ret = NFC_SUCCESS; while (failures < 2) {
else if ((ret = nfc_initiator_transceive_bytes(pnd, abtCmd, sizeof(abtCmd), NULL, 0, 300)) < 1) {
ret = NFC_ETGRELEASED; if ((ret == NFC_ERFTRANS) && (CHIP_DATA(pnd)->last_status_byte == 0x01)) { // Timeout
ret = NFC_ETGRELEASED;
break;
} else { // Other errors can appear when card is tired-off, let's try again
failures++;
}
} else {
ret = NFC_SUCCESS;
break;
}
}
int ret2; int ret2;
if ((ret2 = pn53x_set_property_bool(pnd, NP_EASY_FRAMING, true)) < 0) if ((ret2 = pn53x_set_property_bool(pnd, NP_EASY_FRAMING, true)) < 0)
ret = ret2; ret = ret2;