diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 7c23a14..2ea3d49 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -68,9 +68,10 @@ const byte_t pncmd_target_get_status [ 2] = { 0xD4,0x8A }; static const byte_t pn53x_ack_frame[] = { 0x00,0x00,0xff,0x00,0xff,0x00 }; static const byte_t pn53x_nack_frame[] = { 0x00,0x00,0xff,0xff,0x00,0x00 }; +static const byte_t pn53x_error_frame[] = { 0x00,0x00,0xff,0x01,0xff,0x7f,0x81,0x00 }; // XXX: Is this function correctly named ? -bool pn53x_transceive_callback(nfc_device_t* pnd, const byte_t *pbtRxFrame, const size_t szRxFrameLen) +bool pn53x_transceive_check_ack_frame_callback(nfc_device_t* pnd, const byte_t *pbtRxFrame, const size_t szRxFrameLen) { if (szRxFrameLen >= sizeof (pn53x_ack_frame)) { if (0 == memcmp (pbtRxFrame, pn53x_ack_frame, sizeof (pn53x_ack_frame))) { @@ -93,6 +94,19 @@ bool pn53x_transceive_callback(nfc_device_t* pnd, const byte_t *pbtRxFrame, cons return false; } +bool pn53x_transceive_check_error_frame_callback(nfc_device_t* pnd, const byte_t *pbtRxFrame, const size_t szRxFrameLen) +{ + if (szRxFrameLen >= sizeof (pn53x_error_frame)) { + if (0 == memcmp (pbtRxFrame, pn53x_error_frame, sizeof (pn53x_error_frame))) { + DBG("%s", "PN53x sent an error frame"); + pnd->iLastError = DEISERRFRAME; + return false; + } + } + + return true; +} + bool pn53x_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen) { byte_t abtRx[MAX_FRAME_LEN]; diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 8dea5e8..9d976d8 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -74,7 +74,8 @@ #define DEACKMISMATCH 0x0200 /* Unexpected data */ #define DEISERRFRAME 0x0300 /* Error frame */ -bool pn53x_transceive_callback(nfc_device_t* pnd, const byte_t *pbtRxFrame, const size_t szRxFrameLen); +bool pn53x_transceive_check_ack_frame_callback(nfc_device_t* pnd, const byte_t *pbtRxFrame, const size_t szRxFrameLen); +bool pn53x_transceive_check_error_frame_callback(nfc_device_t* pnd, const byte_t *pbtRxFrame, const size_t szRxFrameLen); bool pn53x_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen); byte_t pn53x_get_reg(nfc_device_t* pnd, uint16_t ui16Reg); bool pn53x_set_reg(nfc_device_t* pnd, uint16_t ui16Reg, uint8_t ui8SybmolMask, uint8_t ui8Value); diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 0316719..7cde7c0 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -285,7 +285,7 @@ bool pn53x_usb_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t s PRINT_HEX("RX", abtRx,ret); #endif - if (!pn53x_transceive_callback (pnd, abtRx, ret)) + if (!pn53x_transceive_check_ack_frame_callback (pnd, abtRx, ret)) return false; ret = usb_bulk_read(pus->pudh, pus->uiEndPointIn, (char*)abtRx, BUFFER_LENGTH, USB_TIMEOUT); @@ -305,6 +305,9 @@ bool pn53x_usb_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t s #endif usb_bulk_write(pus->pudh, pus->uiEndPointOut, (char *)ack_frame, 6, USB_TIMEOUT); + if (!pn53x_transceive_check_error_frame_callback (pnd, abtRx, ret)) + return false; + // When the answer should be ignored, just return a succesful result if(pbtRx == NULL || pszRxLen == NULL) return true; @@ -329,8 +332,5 @@ bool pn53x_usb_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t s memcpy( pbtRx, abtRx + 7, *pszRxLen); - if (abtRx[5] != pbtTx[0] + 1) { - pnd->iLastError = DEISERRFRAME; - } return true; }