nfc_initiator_target_is_present(): better error handling

This commit is contained in:
Philippe Teuwen 2014-01-30 11:37:37 +01:00
parent 05579fde09
commit d6330a55d0

View file

@ -1736,20 +1736,20 @@ static int pn53x_Diagnose06(struct nfc_device *pnd)
// Send Card Presence command
const uint8_t abtCmd[] = { Diagnose, 0x06 };
uint8_t abtRx[1];
int res = 0;
int ret = 0;
// 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
// correctly. (ie. 700 ms should be enough to detect all tested cases)
if ((res = pn53x_transceive(pnd, abtCmd, sizeof(abtCmd), abtRx, sizeof(abtRx), 700)) < 0)
return res;
if (res == 1) {
return NFC_SUCCESS;
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
if ((ret == NFC_ERFTRANS) && (CHIP_DATA(pnd)->last_status_byte == 0x01)) {
ret = NFC_ETGRELEASED;
}
} else {
ret = NFC_SUCCESS;
}
// Target is not reachable anymore
pn53x_current_target_free(pnd);
return NFC_ETGRELEASED;
return ret;
}
int
@ -1780,8 +1780,6 @@ pn53x_initiator_target_is_present(struct nfc_device *pnd, const nfc_target *pnt)
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "target_is_present(): Ping -4A");
if ((CHIP_DATA(pnd)->type == PN532) || (CHIP_DATA(pnd)->type == PN533)) {
ret = pn53x_Diagnose06(pnd);
// When pn53x_Diagnose06() 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)) ret = NFC_ETGRELEASED;
} else {
ret = NFC_EDEVNOTSUPP;
}
@ -1871,6 +1869,8 @@ pn53x_initiator_target_is_present(struct nfc_device *pnd, const nfc_target *pnt)
ret = NFC_EDEVNOTSUPP;
break;
}
if (ret == NFC_ETGRELEASED)
pn53x_current_target_free(pnd);
return pnd->last_error = ret;
}