diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 6b8c588..a39d4ae 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -93,6 +93,7 @@ extern "C" { NFC_EXPORT const char *nfc_strerror (const nfc_device *pnd); NFC_EXPORT int nfc_strerror_r (const nfc_device *pnd, char *buf, size_t buflen); NFC_EXPORT void nfc_perror (const nfc_device *pnd, const char *s); + NFC_EXPORT int nfc_device_get_last_error (const nfc_device *pnd); /* Special data accessors */ NFC_EXPORT const char *nfc_device_get_name (nfc_device *pnd); @@ -111,14 +112,15 @@ extern "C" { #define NFC_SUCCESS 0 // No error #define NFC_EIO -1 // Input / output error, device will not be usable anymore #define NFC_EINVARG -2 // Invalid argument(s) -#define NFC_ENOTSUCHDEV -3 // No such device -#define NFC_ETIMEOUT -4 // Operation timed out +#define NFC_EDEVNOTSUPP -3 // Operation not supported by device +#define NFC_ENOTSUCHDEV -4 // No such device #define NFC_EOVFLOW -5 // Buffer overflow -#define NFC_EOPABORTED -6 // Operation aborted (by user) -#define NFC_ECHIP -7 // Device's internal chip error -#define NFC_ERFTRANS -8 // Error while RF transmission -#define NFC_EDEVNOTSUPP -9 // Operation not supported by device -#define NFC_ENOTIMPL -10 // Not (yet) implemented +#define NFC_ETIMEOUT -6 // Operation timed out +#define NFC_EOPABORTED -7 // Operation aborted (by user) +#define NFC_ENOTIMPL -8 // Not (yet) implemented +#define NFC_ETGRELEASED -10 // Target released +#define NFC_ERFTRANS -20 // Error while RF transmission +#define NFC_ECHIP -90 // Device's internal chip error # ifdef __cplusplus } diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 47851ad..1840710 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -198,10 +198,52 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT } log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Last command status: %s", pn53x_strerror(pnd)); - if (CHIP_DATA(pnd)->last_status_byte) - return NFC_ECHIP; + switch (CHIP_DATA(pnd)->last_status_byte) { + case 0: + res = NFC_SUCCESS; + break; + case ETIMEOUT: + case ECRC: + case EPARITY: + case EBITCOUNT: + case EFRAMING: + case EBITCOLL: + case ERFPROTO: + case ERFTIMEOUT: + case EDEPUNKCMD: + case EDEPINVSTATE: + case ENAD: + case ENFCID3: + case EINVRXFRAM: + case EBCC: + case ECID: + res = NFC_ERFTRANS; + break; + case ESMALLBUF: + case EOVCURRENT: + case EBUFOVF: + case EOVHEAT: + case EINBUFOVF: + res = NFC_ECHIP; + break; + case EINVPARAM: + case EOPNOTALL: + case ECMD: + case ENSECNOTSUPP: + res = NFC_EINVARG; + break; + case ETGREL: + case ECDISCARDED: + res = NFC_ETGRELEASED; + default: + res = NFC_ECHIP; + break; + }; +/* + { EMFAUTH, "Mifare Authentication Error" }, +*/ - return ((0 == CHIP_DATA(pnd)->last_status_byte) ? NFC_SUCCESS : NFC_ECHIP); + return res; } int diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 8836fc9..ce1f8f8 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -433,7 +433,7 @@ nfc_initiator_poll_target (nfc_device *pnd, /** * @brief Select a target and request active or passive mode for D.E.P. (Data Exchange Protocol) - * @return Returns selected D.E.P tagets count on success, otherwise returns libnfc's error code (negative value). + * @return Returns selected D.E.P targets count on success, otherwise returns libnfc's error code (negative value). * * @param pnd \a nfc_device struct pointer that represent currently used device * @param ndm desired D.E.P. mode (\a NDM_ACTIVE or \a NDM_PASSIVE for active, respectively passive mode) @@ -791,6 +791,15 @@ nfc_perror (const nfc_device *pnd, const char *pcString) fprintf (stderr, "%s: %s\n", pcString, nfc_strerror (pnd)); } +/** + * @brief Returns last error occured on a nfc_device + */ +int +nfc_device_get_last_error (const nfc_device *pnd) +{ + return pnd->last_error; +} + /* Special data accessors */ /**