Second part of error handling.

- Define two sets of DE<FOOBAR> macros: the first one for 'generic' errors
  which could be encountered regardless of the NFC device the library is acting
  with (0xX000), and ont set for device-dependant errors (0x0X00).
- Make some more functions accept a nfc_device_t* as first argument to have
  access to the iLastError;
- Reset errors when entering public API functions;
- Save errors when applicable;
- Distinguish system-level errors (e.g. I/O error) and operational errors (the
  PCD returns an unexpected value);
- Minor tweaks.

Update issue 65
Status: Feedback

New review:
Owner: rconty@il4p.fr
Cc: rtartiere@il4p.fr
Summary: Review the error-handling code.
Branch: /branches/libnfc-error-handling

For this development, a strong emphasis has been set on making changes that
will not go through our way on the way to libnfc-1.6+.  For this reason, some
constructs are not natural (e.g. error codes defined in two different places),
please keep this in mind when reviewing.
This commit is contained in:
Romain Tartiere 2010-08-15 14:08:29 +00:00
parent d7e0b926ac
commit 08eb21aa9d
9 changed files with 166 additions and 31 deletions

View file

@ -30,6 +30,7 @@
#include <stdlib.h>
#include <string.h>
#include <nfc/nfc.h>
// FIXME: WTF are doing debug macros in this file?
#include <nfc/nfc-messages.h>
@ -69,17 +70,19 @@ static const byte_t pn53x_nack_frame[] = { 0x00,0x00,0xff,0xff,0x00,0x00 };
bool pn53x_transceive_callback(nfc_device_t* pnd, const byte_t *pbtRxFrame, const size_t szRxFrameLen)
{
(void) pnd; // I guess we will want to set some error here at some point
if (szRxFrameLen == sizeof (pn53x_ack_frame)) {
if (0 == memcmp (pbtRxFrame, pn53x_ack_frame, sizeof (pn53x_ack_frame))) {
DBG("%s", "PN53x ACKed");
return true;
} else if (0 == memcmp (pbtRxFrame, pn53x_nack_frame, sizeof (pn53x_nack_frame))) {
DBG("%s", "PN53x NACKed");
// TODO: Try to recover
// A counter could allow the command to be sent again (e.g. max 3 times)
pnd->iLastError = DENACK;
return false;
}
}
pnd->iLastError = DEACKMISMATCH;
ERR("%s", "Unexpected PN53x reply!");
#if defined(DEBUG)
// coredump so that we can have a backtrace about how this code was reached.
@ -382,6 +385,7 @@ static struct sErrorMessage {
int iErrorCode;
const char *pcErrorMsg;
} sErrorMessages[] = {
/* Chip-level errors */
{ 0x00, "Success" },
{ 0x01, "Timeout" },
{ 0x02, "CRC Error" },
@ -411,7 +415,16 @@ static struct sErrorMessage {
{ 0x2B, "Card Discarded" },
{ 0x2C, "NFCID3 Mismatch" },
{ 0x2D, "Over Current" },
{ 0x2E, "NAD Missing in DEP Frame" }
{ 0x2E, "NAD Missing in DEP Frame" },
/* Driver-level error */
{ DENACK, "Received NACK" },
{ DEACKMISMATCH, "Expected ACK/NACK" },
{ DEISERRFRAME, "Received an error frame" },
/* TODO: Move me in more generic code for libnfc 1.6 */
{ DEINVAL, "Invalid argument" },
{ DEIO, "Input/output error" },
{ DETIMEOUT, "Operation timed-out" }
};
const char *

View file

@ -69,6 +69,11 @@
#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 */
bool pn53x_transceive_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);