Error handling improvement:
- Attempt to sort errors depending their source - Rename DE* errors to differenciate Device-Error and Driver-Error - Add ENOTIMPL error to raise a error when a feature is not (yet) implemented in libnfc
This commit is contained in:
@@ -254,7 +254,7 @@ acr122_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData)
|
||||
{
|
||||
// Make sure the command does not overflow the send buffer
|
||||
if (szData > ACR122_COMMAND_LEN) {
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = EINVALARG;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -283,7 +283,7 @@ acr122_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData)
|
||||
* field.
|
||||
*/
|
||||
if (SCardControl (DRIVER_DATA (pnd)->hCard, IOCTL_CCID_ESCAPE_SCARD_CTL_CODE, abtTxBuf, szTxBuf, DRIVER_DATA (pnd)->abtRx, ACR122_RESPONSE_LEN, &dwRxLen) != SCARD_S_SUCCESS) {
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
@@ -292,7 +292,7 @@ acr122_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData)
|
||||
* receive the response from the PN532.
|
||||
*/
|
||||
if (SCardTransmit (DRIVER_DATA (pnd)->hCard, &(DRIVER_DATA (pnd)->ioCard), abtTxBuf, szTxBuf, NULL, DRIVER_DATA (pnd)->abtRx, &dwRxLen) != SCARD_S_SUCCESS) {
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -304,12 +304,12 @@ acr122_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData)
|
||||
|
||||
// Make sure we received the byte-count we expected
|
||||
if (dwRxLen != 2) {
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return false;
|
||||
}
|
||||
// Check if the operation was successful, so an answer is available
|
||||
if (DRIVER_DATA (pnd)->abtRx[0] == SCARD_OPERATION_ERROR) {
|
||||
pnd->iLastError = DEISERRFRAME;
|
||||
pnd->iLastError = EFRAISERRFRAME;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
@@ -332,7 +332,7 @@ acr122_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData)
|
||||
DWORD dwRxLen = sizeof (DRIVER_DATA (pnd)->abtRx);
|
||||
abtRxCmd[4] = DRIVER_DATA (pnd)->abtRx[1];
|
||||
if (SCardTransmit (DRIVER_DATA (pnd)->hCard, &(DRIVER_DATA (pnd)->ioCard), abtRxCmd, sizeof (abtRxCmd), NULL, DRIVER_DATA (pnd)->abtRx, &dwRxLen) != SCARD_S_SUCCESS) {
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
DRIVER_DATA (pnd)->szRx = dwRxLen;
|
||||
@@ -347,7 +347,7 @@ acr122_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData)
|
||||
|
||||
// Make sure we have an emulated answer that fits the return buffer
|
||||
if (DRIVER_DATA (pnd)->szRx < 4 || (DRIVER_DATA (pnd)->szRx - 4) > szData) {
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
// Wipe out the 4 APDU emulation bytes: D5 4B .. .. .. 90 00
|
||||
|
||||
+12
-12
@@ -244,12 +244,12 @@ arygon_tama_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szDat
|
||||
if (szData > PN53x_NORMAL_FRAME__DATA_MAX_LEN) {
|
||||
// ARYGON Reader with PN532 equipped does not support extended frame (bug in ARYGON firmware?)
|
||||
DBG ("ARYGON device does not support more than %d bytes as payload (requested: %zd)", PN53x_NORMAL_FRAME__DATA_MAX_LEN, szData);
|
||||
pnd->iLastError = DEINVAL;
|
||||
pnd->iLastError = EDEVNOTSUP;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!pn53x_build_frame (abtFrame + 1, &szFrame, pbtData, szData)) {
|
||||
pnd->iLastError = DEINVAL;
|
||||
pnd->iLastError = EINVALARG;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -319,11 +319,11 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe
|
||||
|
||||
pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 5, abort_p);
|
||||
|
||||
if (abort_p && (DEABORT == pnd->iLastError)) {
|
||||
if (abort_p && (EOPABORT == pnd->iLastError)) {
|
||||
arygon_abort (pnd);
|
||||
|
||||
/* iLastError got reset by arygon_abort() */
|
||||
pnd->iLastError = DEABORT;
|
||||
pnd->iLastError = EOPABORT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -335,7 +335,7 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe
|
||||
const byte_t pn53x_preamble[3] = { 0x00, 0x00, 0xff };
|
||||
if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) {
|
||||
ERR ("%s", "Frame preamble+start code mismatch");
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -343,7 +343,7 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe
|
||||
// Error frame
|
||||
uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 3, 0);
|
||||
ERR ("%s", "Application level error detected");
|
||||
pnd->iLastError = DEISERRFRAME;
|
||||
pnd->iLastError = EFRAISERRFRAME;
|
||||
return -1;
|
||||
} else if ((0xff == abtRxBuf[3]) && (0xff == abtRxBuf[4])) {
|
||||
// Extended frame
|
||||
@@ -354,7 +354,7 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe
|
||||
if (256 != (abtRxBuf[3] + abtRxBuf[4])) {
|
||||
// TODO: Retry
|
||||
ERR ("%s", "Length checksum mismatch");
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -364,7 +364,7 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe
|
||||
|
||||
if (len > szDataLen) {
|
||||
ERR ("Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len);
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -377,13 +377,13 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe
|
||||
|
||||
if (abtRxBuf[0] != 0xD5) {
|
||||
ERR ("%s", "TFI Mismatch");
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (abtRxBuf[1] != CHIP_DATA (pnd)->ui8LastCommand + 1) {
|
||||
ERR ("%s", "Command Code verification failed");
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -409,13 +409,13 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe
|
||||
|
||||
if (btDCS != abtRxBuf[0]) {
|
||||
ERR ("%s", "Data checksum mismatch");
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (0x00 != abtRxBuf[1]) {
|
||||
ERR ("%s", "Frame postamble mismatch");
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
// The PN53x command is done and we successfully received the reply
|
||||
|
||||
+11
-11
@@ -250,7 +250,7 @@ pn532_uart_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData
|
||||
size_t szFrame = 0;
|
||||
|
||||
if (!pn53x_build_frame (abtFrame, &szFrame, pbtData, szData)) {
|
||||
pnd->iLastError = DEINVAL;
|
||||
pnd->iLastError = EINVALARG;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -302,7 +302,7 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen
|
||||
|
||||
pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 5, abort_p);
|
||||
|
||||
if (abort_p && (DEABORT == pnd->iLastError)) {
|
||||
if (abort_p && (EOPABORT == pnd->iLastError)) {
|
||||
pn532_uart_ack (pnd);
|
||||
return -1;
|
||||
}
|
||||
@@ -315,7 +315,7 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen
|
||||
const byte_t pn53x_preamble[3] = { 0x00, 0x00, 0xff };
|
||||
if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) {
|
||||
ERR ("%s", "Frame preamble+start code mismatch");
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -323,7 +323,7 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen
|
||||
// Error frame
|
||||
uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 3, 0);
|
||||
ERR ("%s", "Application level error detected");
|
||||
pnd->iLastError = DEISERRFRAME;
|
||||
pnd->iLastError = EFRAISERRFRAME;
|
||||
return -1;
|
||||
} else if ((0xff == abtRxBuf[3]) && (0xff == abtRxBuf[4])) {
|
||||
// Extended frame
|
||||
@@ -335,7 +335,7 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen
|
||||
if (((abtRxBuf[0] + abtRxBuf[1] + abtRxBuf[2]) % 256) != 0) {
|
||||
// TODO: Retry
|
||||
ERR ("%s", "Length checksum mismatch");
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
@@ -343,7 +343,7 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen
|
||||
if (256 != (abtRxBuf[3] + abtRxBuf[4])) {
|
||||
// TODO: Retry
|
||||
ERR ("%s", "Length checksum mismatch");
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -353,7 +353,7 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen
|
||||
|
||||
if (len > szDataLen) {
|
||||
ERR ("Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len);
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -366,13 +366,13 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen
|
||||
|
||||
if (abtRxBuf[0] != 0xD5) {
|
||||
ERR ("%s", "TFI Mismatch");
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (abtRxBuf[1] != CHIP_DATA (pnd)->ui8LastCommand + 1) {
|
||||
ERR ("%s", "Command Code verification failed");
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -398,13 +398,13 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen
|
||||
|
||||
if (btDCS != abtRxBuf[0]) {
|
||||
ERR ("%s", "Data checksum mismatch");
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (0x00 != abtRxBuf[1]) {
|
||||
ERR ("%s", "Frame postamble mismatch");
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
// The PN53x command is done and we successfully received the reply
|
||||
|
||||
+14
-14
@@ -423,14 +423,14 @@ pn53x_usb_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData)
|
||||
int res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), abtFrame, szFrame);
|
||||
|
||||
if (res < 0) {
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return false;
|
||||
}
|
||||
|
||||
byte_t abtRxBuf[PN53X_USB_BUFFER_LEN];
|
||||
res = pn53x_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf));
|
||||
if (res < 0) {
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
// try to interrupt current device state
|
||||
pn53x_usb_ack(pnd);
|
||||
return false;
|
||||
@@ -447,7 +447,7 @@ pn53x_usb_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData)
|
||||
// packet.
|
||||
int res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), (byte_t *)pn53x_nack_frame, sizeof(pn53x_nack_frame));
|
||||
if (res < 0) {
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
// try to interrupt current device state
|
||||
pn53x_usb_ack(pnd);
|
||||
return false;
|
||||
@@ -485,7 +485,7 @@ read:
|
||||
if (DRIVER_DATA (pnd)->abort_flag) {
|
||||
DRIVER_DATA (pnd)->abort_flag = false;
|
||||
pn53x_usb_ack (pnd);
|
||||
pnd->iLastError = DEABORT;
|
||||
pnd->iLastError = EOPABORT;
|
||||
return -1;
|
||||
} else {
|
||||
goto read;
|
||||
@@ -493,7 +493,7 @@ read:
|
||||
}
|
||||
|
||||
if (res < 0) {
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
// try to interrupt current device state
|
||||
pn53x_usb_ack(pnd);
|
||||
return -1;
|
||||
@@ -502,7 +502,7 @@ read:
|
||||
const byte_t pn53x_preamble[3] = { 0x00, 0x00, 0xff };
|
||||
if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) {
|
||||
ERR ("%s", "Frame preamble+start code mismatch");
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
offset += 3;
|
||||
@@ -510,7 +510,7 @@ read:
|
||||
if ((0x01 == abtRxBuf[offset]) && (0xff == abtRxBuf[offset + 1])) {
|
||||
// Error frame
|
||||
ERR ("%s", "Application level error detected");
|
||||
pnd->iLastError = DEISERRFRAME;
|
||||
pnd->iLastError = EFRAISERRFRAME;
|
||||
return -1;
|
||||
} else if ((0xff == abtRxBuf[offset]) && (0xff == abtRxBuf[offset + 1])) {
|
||||
// Extended frame
|
||||
@@ -521,7 +521,7 @@ read:
|
||||
if (((abtRxBuf[offset] + abtRxBuf[offset + 1] + abtRxBuf[offset + 2]) % 256) != 0) {
|
||||
// TODO: Retry
|
||||
ERR ("%s", "Length checksum mismatch");
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
offset += 3;
|
||||
@@ -530,7 +530,7 @@ read:
|
||||
if (256 != (abtRxBuf[offset] + abtRxBuf[offset + 1])) {
|
||||
// TODO: Retry
|
||||
ERR ("%s", "Length checksum mismatch");
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -541,21 +541,21 @@ read:
|
||||
|
||||
if (len > szDataLen) {
|
||||
ERR ("Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len);
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// TFI + PD0 (CC+1)
|
||||
if (abtRxBuf[offset] != 0xD5) {
|
||||
ERR ("%s", "TFI Mismatch");
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
offset += 1;
|
||||
|
||||
if (abtRxBuf[offset] != CHIP_DATA (pnd)->ui8LastCommand + 1) {
|
||||
ERR ("%s", "Command Code verification failed");
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
offset += 1;
|
||||
@@ -571,14 +571,14 @@ read:
|
||||
|
||||
if (btDCS != abtRxBuf[offset]) {
|
||||
ERR ("%s", "Data checksum mismatch");
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
offset += 1;
|
||||
|
||||
if (0x00 != abtRxBuf[offset]) {
|
||||
ERR ("%s", "Frame postamble mismatch");
|
||||
pnd->iLastError = DEIO;
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
// The PN53x command is done and we successfully received the reply
|
||||
|
||||
Reference in New Issue
Block a user