From eb70f3842ed5bb410d03fa21e87acab45c2b45bc Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Mon, 27 Jun 2011 16:31:10 +0000 Subject: [PATCH] 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 --- configure.ac | 3 +- include/nfc/nfc.h | 20 +++++--- libnfc/buses/uart_posix.c | 12 ++--- libnfc/buses/uart_win32.c | 10 ++-- libnfc/chips/pn53x.c | 92 +++++++++++++++++-------------------- libnfc/chips/pn53x.h | 5 -- libnfc/drivers/acr122.c | 14 +++--- libnfc/drivers/arygon.c | 24 +++++----- libnfc/drivers/pn532_uart.c | 22 ++++----- libnfc/drivers/pn53x_usb.c | 28 +++++------ libnfc/nfc-internal.h | 5 +- 11 files changed, 113 insertions(+), 122 deletions(-) diff --git a/configure.ac b/configure.ac index 23e787a..d5cb851 100644 --- a/configure.ac +++ b/configure.ac @@ -52,8 +52,7 @@ AC_CHECK_FUNCS([memmove memset select strdup strerror strstr strtol usleep], AC_DEFINE(_NETBSD_SOURCE, 1, [Define on NetBSD to activate all library features]) AC_DEFINE(_DARWIN_C_SOURCE, 1, [Define on Darwin to activate all library features]) -# XXX malloc function should be tested -# but it produces some error while cross-compiling with MinGW +# Note: malloc function should be tested but it produces some error while cross-compiling with MinGW # AC_FUNC_MALLOC # Checks for types diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index bd27089..51b427d 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -131,14 +131,20 @@ extern "C" { #define EOVCURRENT 0x2d #define ENAD 0x2e -/* Software level errors */ -#define ETGUIDNOTSUP 0x0100 /* Target UID not supported */ +/* PN53x framing-level errors */ +#define EFRAACKMISMATCH 0x0100 /* Unexpected data */ +#define EFRAISERRFRAME 0x0101 /* Error frame */ -/* Common device-level errors */ -#define DEIO 0x1000 /* Input/output error */ -#define DEINVAL 0x2000 /* Invalid argument */ -#define DETIMEOUT 0x3000 /* Operation timeout */ -#define DEABORT 0x4000 /* Operation aborted */ +/* Communication-level errors */ +#define ECOMIO 0x1000 /* Input/output error */ +#define ECOMTIMEOUT 0x1001 /* Operation timeout */ + +/* Software level errors */ +#define ETGUIDNOTSUP 0xFF00 /* Target UID not supported */ +#define EOPABORT 0xFF01 /* Operation aborted */ +#define EINVALARG 0xFF02 /* Invalid argument */ +#define EDEVNOTSUP 0xFF03 /* Not supported by device */ +#define ENOTIMPL 0xFF04 /* Not (yet) implemented in libnfc */ # ifdef __cplusplus } diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index 3f175da..97ecc36 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -276,32 +276,32 @@ select: // Read error if (res < 0) { DBG ("%s", "RX error."); - return DEIO; + return ECOMIO; } // Read time-out if (res == 0) { DBG ("Timeout!"); - return DETIMEOUT; + return ECOMTIMEOUT; } if (FD_ISSET (iAbortFd, &rfds)) { // Abort requested DBG ("Abort!"); close (iAbortFd); - return DEABORT; + return EOPABORT; } // Retrieve the count of the incoming bytes res = ioctl (((serial_port_unix *) sp)->fd, FIONREAD, &available_bytes_count); if (res != 0) { - return DEIO; + return ECOMIO; } // There is something available, read the data // DBG ("expected bytes: %zu, received bytes: %d, available bytes: %d", szRx, received_bytes_count, available_bytes_count); res = read (((serial_port_unix *) sp)->fd, pbtRx + received_bytes_count, MIN(available_bytes_count, (expected_bytes_count - received_bytes_count))); // Stop if the OS has some troubles reading the data if (res <= 0) { - return DEIO; + return ECOMIO; } received_bytes_count += res; @@ -322,7 +322,7 @@ uart_send (serial_port sp, const byte_t * pbtTx, const size_t szTx) if ((int) szTx == write (((serial_port_unix *) sp)->fd, pbtTx, szTx)) return 0; else - return DEIO; + return ECOMIO; } char ** diff --git a/libnfc/buses/uart_win32.c b/libnfc/buses/uart_win32.c index 4e4716b..bbda0d9 100644 --- a/libnfc/buses/uart_win32.c +++ b/libnfc/buses/uart_win32.c @@ -165,18 +165,18 @@ uart_receive (serial_port sp, byte_t * pbtRx, const size_t szRx, void * abort_p) if (!res) { WARN("ReadFile returned error\n"); - return DEIO; + return ECOMIO; } if (((DWORD)szRx) > dwTotalBytesReceived) { dwBytesToGet -= dwBytesReceived; } if (abort_flag_p != NULL && (*abort_flag_p) && dwTotalBytesReceived == 0) { - return DEABORT; + return EOPABORT; } } while (((DWORD)szRx) > dwTotalBytesReceived); - return (dwTotalBytesReceived == (DWORD) szRx) ? 0 : DEIO; + return (dwTotalBytesReceived == (DWORD) szRx) ? 0 : ECOMIO; } int @@ -184,10 +184,10 @@ uart_send (serial_port sp, const byte_t * pbtTx, const size_t szTx) { DWORD dwTxLen = 0; if (!WriteFile (((serial_port_windows *) sp)->hPort, pbtTx, szTx, &dwTxLen, NULL)) { - return DEIO; + return ECOMIO; } if (!dwTxLen) - return DEIO; + return ECOMIO; return 0; } diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 1c674a1..10554aa 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -935,7 +935,7 @@ pn53x_initiator_select_passive_target (nfc_device_t * pnd, const pn53x_modulation_t pm = pn53x_nm_to_pm(nm); if (PM_UNDEFINED == pm) { - pnd->iLastError = DENOTSUP; + pnd->iLastError = EINVALARG; return false; } @@ -968,7 +968,7 @@ pn53x_initiator_poll_targets (nfc_device_t * pnd, for (size_t n=0; niLastError = DENOTSUP; + pnd->iLastError = EINVALARG; return false; } apttTargetTypes[szTargetTypes] = ptt; @@ -1084,7 +1084,7 @@ pn53x_initiator_transceive_bytes (nfc_device_t * pnd, const byte_t * pbtTx, cons // We can not just send bytes without parity if while the PN53X expects we handled them if (!pnd->bPar) { - pnd->iLastError = DENOTSUP; + pnd->iLastError = EINVALARG; return false; } @@ -1215,18 +1215,17 @@ pn53x_initiator_transceive_bits_timed (nfc_device_t * pnd, const byte_t * pbtTx, // Sorry, no arbitrary parity bits support for now if (!pnd->bPar) { - pnd->iLastError = DENOTSUP; + pnd->iLastError = ENOTIMPL; return false; } // Sorry, no easy framing support if (pnd->bEasyFraming) { - pnd->iLastError = DENOTSUP; + pnd->iLastError = ENOTIMPL; return false; } - // Sorry, no CRC support - // TODO but it probably doesn't make sense for (szTxBits % 8 != 0) ... + // TODO CRC support but it probably doesn't make sense for (szTxBits % 8 != 0) ... if (pnd->bCrc) { - pnd->iLastError = DENOTSUP; + pnd->iLastError = ENOTIMPL; return false; } @@ -1315,13 +1314,13 @@ pn53x_initiator_transceive_bytes_timed (nfc_device_t * pnd, const byte_t * pbtTx // We can not just send bytes without parity while the PN53X expects we handled them if (!pnd->bPar) { - pnd->iLastError = DENOTSUP; + pnd->iLastError = EINVALARG; return false; } // Sorry, no easy framing support - // TODO: to be changed once we'll provide easy framing support from libnfc itself... + // TODO to be changed once we'll provide easy framing support from libnfc itself... if (pnd->bEasyFraming) { - pnd->iLastError = DENOTSUP; + pnd->iLastError = ENOTIMPL; return false; } @@ -1459,7 +1458,7 @@ pn53x_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_ case NMT_ISO14443B2SR: case NMT_ISO14443B2CT: case NMT_JEWEL: - pnd->iLastError = DENOTSUP; + pnd->iLastError = EDEVNOTSUP; return false; break; } @@ -1561,7 +1560,7 @@ pn53x_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_ case NMT_ISO14443B2SR: case NMT_ISO14443B2CT: case NMT_JEWEL: - pnd->iLastError = DENOTSUP; + pnd->iLastError = EDEVNOTSUP; return false; break; } @@ -1686,7 +1685,7 @@ pn53x_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx) abtCmd[0] = TgGetData; } else { // TODO Support EasyFraming for other cases by software - pnd->iLastError = DENOTSUP; + pnd->iLastError = ENOTIMPL; return false; } } else { @@ -1702,8 +1701,6 @@ pn53x_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx) // Save the received byte count *pszRx = szRx - 1; - // FIXME szRx can be 0 - // Copy the received bytes memcpy (pbtRx, abtRx + 1, *pszRx); @@ -1766,7 +1763,7 @@ pn53x_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t abtCmd[0] = TgSetData; } else { // TODO Support EasyFraming for other cases by software - pnd->iLastError = DENOTSUP; + pnd->iLastError = ENOTIMPL; return false; } } else { @@ -1788,7 +1785,7 @@ static struct sErrorMessage { int iErrorCode; const char *pcErrorMsg; } sErrorMessages[] = { - /* Chip-level errors */ + /* Chip-level errors (internal errors, RF errors, etc.) */ { 0x00, "Success" }, { ETIMEOUT, "Timeout" }, // Time Out, the target has not answered { ECRC, "CRC Error" }, // A CRC error has been detected by the CIU @@ -1803,39 +1800,36 @@ static struct sErrorMessage { { EOVHEAT, "Chip Overheating" }, // Temperature error: the internal temperature sensor has detected overheating, and therefore has automatically switched off the antenna drivers { EINBUFOVF, "Internal Buffer overflow."}, // Internal buffer overflow { EINVPARAM, "Invalid Parameter"}, // Invalid parameter (range, format, …) - /* DEP Errors */ + { EOPNOTALL, "Operation Not Allowed" }, // Operation not allowed in this configuration (host controller interface) + { ECMD, "Command Not Acceptable" }, // Command is not acceptable due to the current context + { EOVCURRENT, "Over Current" }, + /* DEP errors */ { EDEPUNKCMD, "Unknown DEP Command" }, + { EDEPINVSTATE, "Invalid DEP State" }, // DEP Protocol: Invalid device state, the system is in a state which does not allow the operation + { ENAD, "NAD Missing in DEP Frame" }, /* MIFARE */ { EMFAUTH, "Mifare Authentication Error" }, - /* */ + /* Misc */ { EINVRXFRAM, "Invalid Received Frame" }, // DEP Protocol, Mifare or ISO/IEC14443-4: The data format does not match to the specification. { ENSECNOTSUPP, "NFC Secure not supported" }, // Target or Initiator does not support NFC Secure { EBCC, "Wrong UID Check Byte (BCC)" }, // ISO/IEC14443-3: UID Check byte is wrong - { EDEPINVSTATE, "Invalid DEP State" }, // DEP Protocol: Invalid device state, the system is in a state which does not allow the operation - { EOPNOTALL, "Operation Not Allowed" }, // Operation not allowed in this configuration (host controller interface) - { ECMD, "Command Not Acceptable" }, // Command is not acceptable due to the current context { ETGREL, "Target Released" }, // Target have been released by initiator - // FIXME: Errors can be grouped (DEP-related, MIFARE-related, ISO14443B-related, etc.) - // Purposal: Use prefix/suffix to identify them { ECID, "Card ID Mismatch" }, // ISO14443 type B: Card ID mismatch, meaning that the expected card has been exchanged with another one. { ECDISCARDED, "Card Discarded" }, // ISO/IEC14443 type B: the card previously activated has disappeared. { ENFCID3, "NFCID3 Mismatch" }, - { EOVCURRENT, "Over Current" }, - { ENAD, "NAD Missing in DEP Frame" }, /* Software level errors */ { ETGUIDNOTSUP, "Target UID not supported" }, // In target mode, PN53x only support 4 bytes UID and the first byte must start with 0x08 - /* Driver-level errors */ - { DENACK, "Received NACK" }, - { DEACKMISMATCH, "Expected ACK/NACK" }, - { DEISERRFRAME, "Received an error frame" }, - // TODO: Move me in more generic code for libnfc 1.6 - // FIXME: Driver-errors and Device-errors have the same prefix (DE*) - // eg. DENACK means Driver Error NACK while DEIO means Device Error I/O - { DEINVAL, "Invalid argument" }, - { DEIO, "Input/output error" }, - { DETIMEOUT, "Operation timed-out" }, - { DEABORT, "Operation aborted" }, - { DENOTSUP, "Operation not supported" } + { EOPABORT, "Operation aborted" }, // Error used to catch a user-requested command abort + { EINVALARG, "Invalid argument" }, // Function called with invalid argument(s) + { ENOTIMPL, "Not (yet) implemented in library" }, + /* Framming-level errors */ + { EFRAACKMISMATCH, "Expected ACK frame" }, + { EFRAISERRFRAME, "Received an error frame" }, + /* Communication-level errors */ + { ECOMIO, "Input/output error" }, // Communication I/O errors: connection broken, read/write failure, unreacheable device, etc. + { ECOMTIMEOUT, "Operation timed-out" }, // A timeout occured while reading device's reply + /* Device-level errors */ + { EDEVNOTSUP, "Operation not supported by device" } // Requested task can not be done by current device }; const char * @@ -1891,7 +1885,7 @@ pn53x_SAMConfiguration (nfc_device_t * pnd, const uint8_t ui8Mode) // TODO: Handle these SAM mode break; default: - pnd->iLastError = DENOTSUP; + pnd->iLastError = EINVALARG; return false; } return (pn53x_transceive (pnd, abtCmd, szCmd, NULL, NULL)); @@ -1938,14 +1932,14 @@ pn53x_InListPassiveTarget (nfc_device_t * pnd, case PM_ISO14443B_106: if (!(pnd->btSupportByte & SUPPORT_ISO14443B)) { // Eg. Some PN532 doesn't support type B! - pnd->iLastError = DENOTSUP; + pnd->iLastError = EDEVNOTSUP; return false; } break; case PM_JEWEL_106: if(CHIP_DATA(pnd)->type == PN531) { // These modulations are not supported by pn531 - pnd->iLastError = DENOTSUP; + pnd->iLastError = EDEVNOTSUP; return false; } break; @@ -1954,12 +1948,12 @@ pn53x_InListPassiveTarget (nfc_device_t * pnd, case PM_ISO14443B_847: if((CHIP_DATA(pnd)->type != PN533) || (!(pnd->btSupportByte & SUPPORT_ISO14443B))) { // These modulations are not supported by pn531 neither pn532 - pnd->iLastError = DENOTSUP; + pnd->iLastError = EDEVNOTSUP; return false; } break; default: - pnd->iLastError = DENOTSUP; + pnd->iLastError = EINVALARG; return false; } abtCmd[2] = pmInitModulation; // BrTy, the type of init modulation used for polling a passive tag @@ -2022,7 +2016,7 @@ pn53x_InAutoPoll (nfc_device_t * pnd, { if (CHIP_DATA(pnd)->type != PN532) { // This function is not supported by pn531 neither pn533 - pnd->iLastError = DENOTSUP; + pnd->iLastError = EDEVNOTSUP; return false; } @@ -2103,7 +2097,7 @@ pn53x_InJumpForDEP (nfc_device_t * pnd, break; case NBR_847: case NBR_UNDEFINED: - pnd->iLastError = DENOTSUP; + pnd->iLastError = EINVALARG; return false; break; } @@ -2123,7 +2117,7 @@ pn53x_InJumpForDEP (nfc_device_t * pnd, break; case NBR_847: case NBR_UNDEFINED: - pnd->iLastError = DENOTSUP; + pnd->iLastError = EINVALARG; return false; break; } @@ -2251,7 +2245,7 @@ pn53x_check_ack_frame (nfc_device_t * pnd, const byte_t * pbtRxFrame, const size return true; } } - pnd->iLastError = DEACKMISMATCH; + pnd->iLastError = EFRAACKMISMATCH; ERR ("%s", "Unexpected PN53x reply!"); return false; } @@ -2262,7 +2256,7 @@ pn53x_check_error_frame (nfc_device_t * pnd, const byte_t * pbtRxFrame, const si 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; + pnd->iLastError = EFRAISERRFRAME; return false; } } diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 8ba8589..8b6fcb7 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -114,11 +114,6 @@ # define RFCI_ANALOG_TYPE_B 0x0C // 3 # define RFCI_ANALOG_TYPE_14443_4 0x0D // 9 -/* PN53x specific device-level errors */ -# define DENACK 0x0100/* NACK */ -# define DEACKMISMATCH 0x0200/* Unexpected data */ -# define DEISERRFRAME 0x0300/* Error frame */ - typedef enum { NORMAL, // In that case, there is no power saved but the PN53x reacts as fast as possible on the host controller interface. POWERDOWN, // Only on PN532, need to be wake up to process commands with a long preamble diff --git a/libnfc/drivers/acr122.c b/libnfc/drivers/acr122.c index 4ea502f..540a9f5 100644 --- a/libnfc/drivers/acr122.c +++ b/libnfc/drivers/acr122.c @@ -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 diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 1a232f1..3de056c 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -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 diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index fbf63f6..32f23a7 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -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 diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 7ff2dfd..f757bd1 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -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 diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 96c8f27..bbfaf8c 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -29,9 +29,6 @@ # include # include -// TODO: Put generic errors here -# define DENOTSUP 0x0400/* Not supported */ - /** * @macro PRINT_HEX * @brief Print a byte-array in hexadecimal format (only in DEBUG mode) @@ -96,7 +93,7 @@ if (pnd->driver->FUNCTION) { \ return pnd->driver->FUNCTION( __VA_ARGS__ ); \ } else { \ - pnd->iLastError = DENOTSUP; \ + pnd->iLastError = EDEVNOTSUP; \ return false; \ }