From 9bdc20353c8202f45455dc89819bd4ffb3bfe399 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Thu, 15 Dec 2011 16:02:38 +0000 Subject: [PATCH] nfc_device struct is not exposed as public API anymore (partial commit) - split libnfc's errors and chip's errors - fix nfc_property enum --- include/nfc/nfc-types.h | 60 +++------- include/nfc/nfc.h | 22 ++-- libnfc/chips/pn53x.c | 224 +++++++++++++++++++----------------- libnfc/chips/pn53x.h | 92 +++++++-------- libnfc/drivers/acr122.c | 18 ++- libnfc/drivers/arygon.c | 46 ++++---- libnfc/drivers/pn532_uart.c | 48 ++++---- libnfc/drivers/pn53x_usb.c | 43 +++---- libnfc/nfc-device.c | 2 +- libnfc/nfc-internal.h | 86 +++++++++----- libnfc/nfc.c | 2 +- 11 files changed, 329 insertions(+), 314 deletions(-) diff --git a/include/nfc/nfc-types.h b/include/nfc/nfc-types.h index 5975d5b..81a1270 100644 --- a/include/nfc/nfc-types.h +++ b/include/nfc/nfc-types.h @@ -32,42 +32,10 @@ # include # include -# define DEVICE_NAME_LENGTH 256 -# define DEVICE_PORT_LENGTH 64 - /** - * @struct nfc_device - * @brief NFC device information + * NFC device */ -typedef struct { -/** Driver's functions for handling device specific wrapping */ - const struct nfc_driver_t *driver; - void *driver_data; - void *chip_data; - -/** Device name string, including device wrapper firmware */ - char acName[DEVICE_NAME_LENGTH]; -/** Is the crc automaticly added, checked and removed from the frames */ - bool bCrc; -/** Does the chip handle parity bits, all parities are handled as data */ - bool bPar; -/** Should the chip handle frames encapsulation and chaining */ - bool bEasyFraming; -/** Should the chip switch automatically activate ISO14443-4 when - selecting tags supporting it? */ - bool bAutoIso14443_4; -/** Supported modulation encoded in a byte */ - uint8_t btSupportByte; -/** Last error reported by the PCD / encountered by the PCD driver - * MSB LSB - * | 00 | 00 | - * || || - * || ++----- Chip-level error (as reported by the PCD) - * |+---------- Driver-level specific error - * +----------- Driver-level general error (common to all drivers) - */ - int iLastError; -} nfc_device; +typedef struct nfc_device nfc_device; /** * Connection string @@ -103,7 +71,7 @@ typedef enum { * incorrect CRC bytes this option should be disabled. Example frames where * this is useful are the ATQA and UID+BCC that are transmitted without CRC * bytes during the anti-collision phase of the ISO14443-A protocol. */ - NP_HANDLE_CRC = 0x00, + NP_HANDLE_CRC, /** Parity bits in the network layer of ISO14443-A are by default generated and * validated in the PN53X chip. This is a very convenient feature. On certain * times though it is useful to get full control of the transmitted data. The @@ -111,31 +79,31 @@ typedef enum { * parity bits. For interoperability it is required to be completely * compatible, including the arbitrary parity bits. When this option is * disabled, the functions to communicating bits should be used. */ - NP_HANDLE_PARITY = 0x01, + NP_HANDLE_PARITY, /** This option can be used to enable or disable the electronic field of the * NFC device. */ - NP_ACTIVATE_FIELD = 0x10, + NP_ACTIVATE_FIELD, /** The internal CRYPTO1 co-processor can be used to transmit messages * encrypted. This option is automatically activated after a successful MIFARE * Classic authentication. */ - NP_ACTIVATE_CRYPTO1 = 0x11, + NP_ACTIVATE_CRYPTO1, /** The default configuration defines that the PN53X chip will try indefinitely * to invite a tag in the field to respond. This could be desired when it is * certain a tag will enter the field. On the other hand, when this is * uncertain, it will block the application. This option could best be compared * to the (NON)BLOCKING option used by (socket)network programming. */ - NP_INFINITE_SELECT = 0x20, + NP_INFINITE_SELECT, /** If this option is enabled, frames that carry less than 4 bits are allowed. * According to the standards these frames should normally be handles as * invalid frames. */ - NP_ACCEPT_INVALID_FRAMES = 0x30, + NP_ACCEPT_INVALID_FRAMES, /** If the NFC device should only listen to frames, it could be useful to let * it gather multiple frames in a sequence. They will be stored in the internal * FIFO of the PN53X chip. This could be retrieved by using the receive data * functions. Note that if the chip runs out of bytes (FIFO = 64 bytes long), * it will overwrite the first received frames, so quick retrieving of the * received data is desirable. */ - NP_ACCEPT_MULTIPLE_FRAMES = 0x31, + NP_ACCEPT_MULTIPLE_FRAMES, /** This option can be used to enable or disable the auto-switching mode to * ISO14443-4 is device is compliant. * In initiator mode, it means that NFC chip will send RATS automatically when @@ -143,15 +111,15 @@ typedef enum { * requested. * In target mode, with a NFC chip compiliant (ie. PN532), the chip will * emulate a 14443-4 PICC using hardware capability */ - NP_AUTO_ISO14443_4 = 0x40, + NP_AUTO_ISO14443_4, /** Use automatic frames encapsulation and chaining. */ - NP_EASY_FRAMING = 0x41, + NP_EASY_FRAMING, /** Force the chip to switch in ISO14443-A */ - NP_FORCE_ISO14443_A = 0x42, + NP_FORCE_ISO14443_A, /** Force the chip to switch in ISO14443-B */ - NP_FORCE_ISO14443_B = 0x43, + NP_FORCE_ISO14443_B, /** Force the chip to run at 106 kbps */ - NP_FORCE_SPEED_106 = 0x50, + NP_FORCE_SPEED_106, } nfc_property; // Compiler directive, set struct alignment to 1 uint8_t for compatibility diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index b55c1b8..64833de 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -91,11 +91,11 @@ extern "C" { /* Error reporting */ NFC_EXPORT const char *nfc_strerror (const nfc_device *pnd); - NFC_EXPORT int nfc_strerror_r (const nfc_device *pnd, char *pcStrErrBuf, size_t szBufLen); - NFC_EXPORT void nfc_perror (const nfc_device *pnd, const char *pcString); + 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); /* Special data accessors */ - NFC_EXPORT const char *nfc_device_name (nfc_device *pnd); + NFC_EXPORT const char *nfc_device_get_name (nfc_device *pnd); /* Properties accessors */ NFC_EXPORT int nfc_device_set_property_int (nfc_device *pnd, const nfc_property property, const int value); @@ -108,11 +108,17 @@ extern "C" { NFC_EXPORT const char *nfc_version (void); /* Error codes */ -#define NFC_SUCCESS 0 // No error -#define NFC_EIO -1 // Input / output error, device will not be usable anymore -#define NFC_ENOTSUP -2 // Operation not supported -#define NFC_EINVARG -3 // Invalid argument(s) -#define NFC_DEVICE_ERROR -4 //Device error +#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_EOVFLOW -5 // Buffer overflow +#define NFC_EOPABORTED -6 // Operation aborted (by user) +#define NFC_ECHIP -7 // Device's internal chip error +#define NFC_EDEVNOTSUPP -8 // Operation not supported by device +#define NFC_ENOTIMPL -9 // Not (yet) implemented + /* PN53x specific errors */ // TODO: Be not PN53x-specific here diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 0a9c2ca..b123d08 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -35,14 +35,14 @@ #include #include -#include #include +#include "nfc/nfc.h" +#include "nfc-internal.h" #include "pn53x.h" #include "pn53x-internal.h" #include "mirror-subr.h" -#include "nfc-internal.h" #define LOG_CATEGORY "libnfc.chip.pn53x" @@ -51,15 +51,15 @@ const uint8_t pn53x_nack_frame[] = { 0x00, 0x00, 0xff, 0xff, 0x00, 0x00 }; static const uint8_t pn53x_error_frame[] = { 0x00, 0x00, 0xff, 0x01, 0xff, 0x7f, 0x81, 0x00 }; /* prototypes */ -bool pn53x_reset_settings (nfc_device *pnd); -bool pn53x_writeback_register (nfc_device *pnd); +bool pn53x_reset_settings (struct nfc_device *pnd); +bool pn53x_writeback_register (struct nfc_device *pnd); nfc_modulation pn53x_ptt_to_nm (const pn53x_target_type ptt); pn53x_modulation pn53x_nm_to_pm (const nfc_modulation nm); pn53x_target_type pn53x_nm_to_ptt (const nfc_modulation nm); bool -pn53x_init(nfc_device *pnd) +pn53x_init(struct nfc_device *pnd) { // GetFirmwareVersion command is used to set PN53x chips type (PN531, PN532 or PN533) char abtFirmwareText[22]; @@ -78,7 +78,9 @@ pn53x_init(nfc_device *pnd) return false; } - pn53x_reset_settings(pnd); + if (!pn53x_reset_settings(pnd)) { + return false; + } // Add the firmware revision to the device name char *pcName; @@ -89,7 +91,7 @@ pn53x_init(nfc_device *pnd) } bool -pn53x_reset_settings(nfc_device *pnd) +pn53x_reset_settings(struct nfc_device *pnd) { // Reset the ending transmission bits register, it is unknown what the last tranmission used there CHIP_DATA (pnd)->ui8TxBits = 0; @@ -100,7 +102,7 @@ pn53x_reset_settings(nfc_device *pnd) } bool -pn53x_transceive (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout) +pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout) { if (CHIP_DATA (pnd)->wb_trigged) { if (!pn53x_writeback_register (pnd)) { @@ -140,7 +142,7 @@ pn53x_transceive (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint return false; } - if (pnd->iLastError) + if (CHIP_DATA(pnd)->last_status_byte) return false; if ((CHIP_DATA(pnd)->type == PN532) && (TgInitAsTarget == pbtTx[0])) { // PN532 automatically wakeup on external RF field @@ -165,36 +167,36 @@ pn53x_transceive (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint case TgSetMetaData: if (pbtRx[0] & 0x80) { abort(); } // NAD detected if (pbtRx[0] & 0x40) { abort(); } // MI detected - pnd->iLastError = pbtRx[0] & 0x3f; + CHIP_DATA(pnd)->last_status_byte = pbtRx[0] & 0x3f; break; case InDeselect: case InRelease: if (CHIP_DATA(pnd)->type == RCS360) { // Error code is in pbtRx[1] but we ignore error code anyway // because other PN53x chips always return 0 on those commands - pnd->iLastError = 0; + CHIP_DATA(pnd)->last_status_byte = 0; break; } - pnd->iLastError = pbtRx[0] & 0x3f; + CHIP_DATA(pnd)->last_status_byte = pbtRx[0] & 0x3f; break; case ReadRegister: case WriteRegister: if (CHIP_DATA(pnd)->type == PN533) { // PN533 prepends its answer by the status byte - pnd->iLastError = pbtRx[0] & 0x3f; + CHIP_DATA(pnd)->last_status_byte = pbtRx[0] & 0x3f; } else { - pnd->iLastError = 0; + CHIP_DATA(pnd)->last_status_byte = 0; } break; default: - pnd->iLastError = 0; + CHIP_DATA(pnd)->last_status_byte = 0; } log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Last command status: %s", pn53x_strerror(pnd)); - return (0 == pnd->iLastError); + return (0 == CHIP_DATA(pnd)->last_status_byte); } bool -pn53x_set_parameters (nfc_device *pnd, const uint8_t ui8Parameter, const bool bEnable) +pn53x_set_parameters (struct nfc_device *pnd, const uint8_t ui8Parameter, const bool bEnable) { uint8_t ui8Value = (bEnable) ? (CHIP_DATA (pnd)->ui8Parameters | ui8Parameter) : (CHIP_DATA (pnd)->ui8Parameters & ~(ui8Parameter)); if (ui8Value != CHIP_DATA (pnd)->ui8Parameters) { @@ -204,7 +206,7 @@ pn53x_set_parameters (nfc_device *pnd, const uint8_t ui8Parameter, const bool bE } bool -pn53x_set_tx_bits (nfc_device *pnd, const uint8_t ui8Bits) +pn53x_set_tx_bits (struct nfc_device *pnd, const uint8_t ui8Bits) { // Test if we need to update the transmission bits register setting if (CHIP_DATA (pnd)->ui8TxBits != ui8Bits) { @@ -466,7 +468,7 @@ pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData, pn53x_typ } bool -pn53x_ReadRegister (nfc_device *pnd, uint16_t ui16RegisterAddress, uint8_t *ui8Value) +pn53x_ReadRegister (struct nfc_device *pnd, uint16_t ui16RegisterAddress, uint8_t *ui8Value) { uint8_t abtCmd[] = { ReadRegister, ui16RegisterAddress >> 8, ui16RegisterAddress & 0xff }; uint8_t abtRegValue[2]; @@ -485,13 +487,13 @@ pn53x_ReadRegister (nfc_device *pnd, uint16_t ui16RegisterAddress, uint8_t *ui8V return true; } -bool pn53x_read_register (nfc_device *pnd, uint16_t ui16RegisterAddress, uint8_t *ui8Value) +bool pn53x_read_register (struct nfc_device *pnd, uint16_t ui16RegisterAddress, uint8_t *ui8Value) { return pn53x_ReadRegister (pnd, ui16RegisterAddress, ui8Value); } bool -pn53x_WriteRegister (nfc_device *pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8Value) +pn53x_WriteRegister (struct nfc_device *pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8Value) { uint8_t abtCmd[] = { WriteRegister, ui16RegisterAddress >> 8, ui16RegisterAddress & 0xff, ui8Value }; PNREG_TRACE (ui16RegisterAddress); @@ -499,7 +501,7 @@ pn53x_WriteRegister (nfc_device *pnd, const uint16_t ui16RegisterAddress, const } bool -pn53x_write_register (nfc_device *pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8SymbolMask, const uint8_t ui8Value) +pn53x_write_register (struct nfc_device *pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8SymbolMask, const uint8_t ui8Value) { if ((ui16RegisterAddress < PN53X_CACHE_REGISTER_MIN_ADDRESS) || (ui16RegisterAddress > PN53X_CACHE_REGISTER_MAX_ADDRESS)) { // Direct write @@ -525,7 +527,7 @@ pn53x_write_register (nfc_device *pnd, const uint16_t ui16RegisterAddress, const } bool -pn53x_writeback_register (nfc_device *pnd) +pn53x_writeback_register (struct nfc_device *pnd) { // TODO Check at each step (ReadRegister, WriteRegister) if we didn't exceed max supported frame length BUFFER_INIT (abtReadRegisterCmd, PN53x_EXTENDED_FRAME__DATA_MAX_LEN); @@ -593,7 +595,7 @@ pn53x_writeback_register (nfc_device *pnd) } bool -pn53x_get_firmware_version (nfc_device *pnd, char abtFirmwareText[22]) +pn53x_get_firmware_version (struct nfc_device *pnd, char abtFirmwareText[22]) { const uint8_t abtCmd[] = { GetFirmwareVersion }; uint8_t abtFw[4]; @@ -659,7 +661,7 @@ pn53x_int_to_timeout (const int ms) } int -pn53x_set_property_int (nfc_device *pnd, const nfc_property property, const int value) +pn53x_set_property_int (struct nfc_device *pnd, const nfc_property property, const int value) { switch (property) { case NP_TIMEOUT_COMMAND: @@ -674,13 +676,13 @@ pn53x_set_property_int (nfc_device *pnd, const nfc_property property, const int return (pn53x_RFConfiguration__Various_timings (pnd, pn53x_int_to_timeout(CHIP_DATA (pnd)->timeout_atr), pn53x_int_to_timeout(CHIP_DATA (pnd)->timeout_communication))) ? NFC_SUCCESS : NFC_EIO; break; default: - return NFC_ENOTSUP; + return NFC_EINVARG; } return NFC_SUCCESS; } int -pn53x_set_property_bool (nfc_device *pnd, const nfc_property property, const bool bEnable) +pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, const bool bEnable) { uint8_t btValue; switch (property) { @@ -693,9 +695,9 @@ pn53x_set_property_bool (nfc_device *pnd, const nfc_property property, const boo // TX and RX are both represented by the symbol 0x80 btValue = (bEnable) ? 0x80 : 0x00; if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_CRC_ENABLE, btValue)) - return NFC_DEVICE_ERROR; + return NFC_ECHIP; if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_CRC_ENABLE, btValue)) - return NFC_DEVICE_ERROR; + return NFC_ECHIP; pnd->bCrc = bEnable; break; @@ -706,7 +708,7 @@ pn53x_set_property_bool (nfc_device *pnd, const nfc_property property, const boo return NFC_SUCCESS; btValue = (bEnable) ? 0x00 : SYMBOL_PARITY_DISABLE; if (!pn53x_write_register (pnd, PN53X_REG_CIU_ManualRCV, SYMBOL_PARITY_DISABLE, btValue)) - return NFC_DEVICE_ERROR; + return NFC_ECHIP; pnd->bPar = bEnable; break; @@ -723,7 +725,7 @@ pn53x_set_property_bool (nfc_device *pnd, const nfc_property property, const boo case NP_ACTIVATE_CRYPTO1: btValue = (bEnable) ? SYMBOL_MF_CRYPTO1_ON : 0x00; if (!pn53x_write_register (pnd, PN53X_REG_CIU_Status2, SYMBOL_MF_CRYPTO1_ON, btValue)) - return NFC_DEVICE_ERROR; + return NFC_ECHIP; break; case NP_INFINITE_SELECT: @@ -742,13 +744,13 @@ pn53x_set_property_bool (nfc_device *pnd, const nfc_property property, const boo case NP_ACCEPT_INVALID_FRAMES: btValue = (bEnable) ? SYMBOL_RX_NO_ERROR : 0x00; if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_NO_ERROR, btValue)) - return NFC_DEVICE_ERROR; + return NFC_ECHIP; break; case NP_ACCEPT_MULTIPLE_FRAMES: btValue = (bEnable) ? SYMBOL_RX_MULTIPLE : 0x00; if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_MULTIPLE, btValue)) - return NFC_DEVICE_ERROR; + return NFC_ECHIP; return NFC_SUCCESS; break; @@ -767,14 +769,14 @@ pn53x_set_property_bool (nfc_device *pnd, const nfc_property property, const boo } // Force pn53x to be in ISO14443-A mode if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_FRAMING, 0x00)) { - return NFC_DEVICE_ERROR; + return NFC_ECHIP; } if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_FRAMING, 0x00)) { - return NFC_DEVICE_ERROR; + return NFC_ECHIP; } // Set the PN53X to force 100% ASK Modified miller decoding (default for 14443A cards) if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxAuto, SYMBOL_FORCE_100_ASK, 0x40)) - return NFC_DEVICE_ERROR; + return NFC_ECHIP; return NFC_SUCCESS; break; @@ -786,10 +788,10 @@ pn53x_set_property_bool (nfc_device *pnd, const nfc_property property, const boo } // Force pn53x to be in ISO14443-B mode if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_FRAMING, 0x03)) { - return NFC_DEVICE_ERROR; + return NFC_ECHIP; } if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_FRAMING, 0x03)) { - return NFC_DEVICE_ERROR; + return NFC_ECHIP; } return NFC_SUCCESS; @@ -802,22 +804,27 @@ pn53x_set_property_bool (nfc_device *pnd, const nfc_property property, const boo } // Force pn53x to be at 106 kbps if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_SPEED, 0x00)) { - return NFC_DEVICE_ERROR; + return NFC_ECHIP; } if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_SPEED, 0x00)) { - return NFC_DEVICE_ERROR; + return NFC_ECHIP; } return NFC_SUCCESS; break; + // Not boolean property + case NP_TIMEOUT_COMMAND: + case NP_TIMEOUT_ATR: + case NP_TIMEOUT_COM: + return NFC_EINVARG; + break; } - // When we reach this, the configuration is completed and successful - return NFC_SUCCESS; + return NFC_EINVARG; } bool -pn53x_idle (nfc_device *pnd) +pn53x_idle (struct nfc_device *pnd) { switch (CHIP_DATA (pnd)->operating_mode) { case TARGET: @@ -865,7 +872,7 @@ pn53x_idle (nfc_device *pnd) } bool -pn53x_check_communication (nfc_device *pnd) +pn53x_check_communication (struct nfc_device *pnd) { const uint8_t abtCmd[] = { Diagnose, 0x00, 'l', 'i', 'b', 'n', 'f', 'c' }; const uint8_t abtExpectedRx[] = { 0x00, 'l', 'i', 'b', 'n', 'f', 'c' }; @@ -879,7 +886,7 @@ pn53x_check_communication (nfc_device *pnd) } int -pn53x_initiator_init (nfc_device *pnd) +pn53x_initiator_init (struct nfc_device *pnd) { pn53x_reset_settings(pnd); @@ -892,7 +899,7 @@ pn53x_initiator_init (nfc_device *pnd) } bool -pn53x_initiator_select_passive_target_ext (nfc_device *pnd, +pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt, @@ -904,7 +911,7 @@ pn53x_initiator_select_passive_target_ext (nfc_device *pnd, if (nm.nmt == NMT_ISO14443BI || nm.nmt == NMT_ISO14443B2SR || nm.nmt == NMT_ISO14443B2CT) { if (CHIP_DATA(pnd)->type == RCS360) { // TODO add support for RC-S360, at the moment it refuses to send raw frames without a first select - pnd->iLastError = ENOTIMPL; + pnd->last_error = ENOTIMPL; return false; } // No native support in InListPassiveTarget so we do discovery by hand @@ -979,7 +986,7 @@ pn53x_initiator_select_passive_target_ext (nfc_device *pnd, const pn53x_modulation pm = pn53x_nm_to_pm(nm); if (PM_UNDEFINED == pm) { - pnd->iLastError = EINVALARG; + pnd->last_error = EINVALARG; return false; } @@ -1002,7 +1009,7 @@ pn53x_initiator_select_passive_target_ext (nfc_device *pnd, } bool -pn53x_initiator_select_passive_target (nfc_device *pnd, +pn53x_initiator_select_passive_target (struct nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt) @@ -1011,7 +1018,7 @@ pn53x_initiator_select_passive_target (nfc_device *pnd, } bool -pn53x_initiator_poll_target (nfc_device *pnd, +pn53x_initiator_poll_target (struct nfc_device *pnd, const nfc_modulation *pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt) @@ -1022,7 +1029,7 @@ pn53x_initiator_poll_target (nfc_device *pnd, for (size_t n=0; niLastError = EINVALARG; + pnd->last_error = NFC_EINVARG; return false; } apttTargetTypes[szTargetTypes] = ptt; @@ -1062,7 +1069,7 @@ pn53x_initiator_poll_target (nfc_device *pnd, const int timeout_ms = uiPeriod * 150; if (!pn53x_initiator_select_passive_target_ext (pnd, pnmModulations[n], pbtInitiatorData, szInitiatorData, pnt, timeout_ms)) { - if (pnd->iLastError != ECOMTIMEOUT) + if (pnd->last_error != NFC_ETIMEOUT) return false; } else { return true; @@ -1075,7 +1082,7 @@ pn53x_initiator_poll_target (nfc_device *pnd, } bool -pn53x_initiator_select_dep_target(nfc_device *pnd, +pn53x_initiator_select_dep_target(struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, @@ -1104,7 +1111,7 @@ pn53x_initiator_select_dep_target(nfc_device *pnd, } bool -pn53x_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, +pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar) { size_t szFrameBits = 0; @@ -1168,7 +1175,7 @@ pn53x_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const si } bool -pn53x_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, +pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout) { size_t szExtraTxLen; @@ -1176,7 +1183,7 @@ pn53x_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const s // We can not just send bytes without parity if while the PN53X expects we handled them if (!pnd->bPar) { - pnd->iLastError = EINVALARG; + pnd->last_error = NFC_EINVARG; return false; } @@ -1214,7 +1221,7 @@ pn53x_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const s return true; } -void __pn53x_init_timer(nfc_device *pnd, const uint32_t max_cycles) +void __pn53x_init_timer(struct nfc_device *pnd, const uint32_t max_cycles) { // The prescaler will dictate what will be the precision and // the largest delay to measure before saturation. Some examples: @@ -1235,7 +1242,7 @@ void __pn53x_init_timer(nfc_device *pnd, const uint32_t max_cycles) pn53x_write_register (pnd, PN53X_REG_CIU_TReloadVal_lo, 0xFF, reloadval & 0xFF); } -uint32_t __pn53x_get_timer(nfc_device *pnd, const uint8_t last_cmd_byte) +uint32_t __pn53x_get_timer(struct nfc_device *pnd, const uint8_t last_cmd_byte) { uint8_t parity; uint8_t counter_hi, counter_lo; @@ -1296,7 +1303,7 @@ uint32_t __pn53x_get_timer(nfc_device *pnd, const uint8_t last_cmd_byte) } bool -pn53x_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, +pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar, uint32_t *cycles) { // TODO Do something with these bytes... @@ -1307,17 +1314,17 @@ pn53x_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, co // Sorry, no arbitrary parity bits support for now if (!pnd->bPar) { - pnd->iLastError = ENOTIMPL; + pnd->last_error = NFC_ENOTIMPL; return false; } // Sorry, no easy framing support if (pnd->bEasyFraming) { - pnd->iLastError = ENOTIMPL; + pnd->last_error = NFC_ENOTIMPL; return false; } // TODO CRC support but it probably doesn't make sense for (szTxBits % 8 != 0) ... if (pnd->bCrc) { - pnd->iLastError = ENOTIMPL; + pnd->last_error = NFC_ENOTIMPL; return false; } @@ -1398,7 +1405,7 @@ pn53x_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, co } bool -pn53x_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, +pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, uint32_t *cycles) { uint16_t i; @@ -1406,13 +1413,13 @@ pn53x_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, c // We can not just send bytes without parity while the PN53X expects we handled them if (!pnd->bPar) { - pnd->iLastError = EINVALARG; + CHIP_DATA(pnd)->last_status_byte = EINVALARG; return false; } // Sorry, no easy framing support // TODO to be changed once we'll provide easy framing support from libnfc itself... if (pnd->bEasyFraming) { - pnd->iLastError = ENOTIMPL; + CHIP_DATA(pnd)->last_status_byte = ENOTIMPL; return false; } @@ -1501,7 +1508,7 @@ pn53x_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, c } bool -pn53x_initiator_deselect_target (nfc_device *pnd) +pn53x_initiator_deselect_target (struct nfc_device *pnd) { return (pn53x_InDeselect (pnd, 0)); // 0 mean deselect all selected targets } @@ -1509,7 +1516,7 @@ pn53x_initiator_deselect_target (nfc_device *pnd) #define SAK_ISO14443_4_COMPLIANT 0x20 #define SAK_ISO18092_COMPLIANT 0x40 bool -pn53x_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx) +pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx) { pn53x_reset_settings(pnd); @@ -1521,7 +1528,7 @@ pn53x_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *psz case NMT_ISO14443A: ptm = PTM_PASSIVE_ONLY; if ((pnt->nti.nai.abtUid[0] != 0x08) || (pnt->nti.nai.szUidLen != 4)) { - pnd->iLastError = ETGUIDNOTSUP; + CHIP_DATA(pnd)->last_status_byte = ETGUIDNOTSUP; return false; } pn53x_set_parameters (pnd, PARAM_AUTO_ATR_RES, false); @@ -1550,7 +1557,7 @@ pn53x_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *psz case NMT_ISO14443B2SR: case NMT_ISO14443B2CT: case NMT_JEWEL: - pnd->iLastError = EDEVNOTSUP; + CHIP_DATA(pnd)->last_status_byte = EDEVNOTSUP; return false; break; } @@ -1652,7 +1659,7 @@ pn53x_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *psz case NMT_ISO14443B2SR: case NMT_ISO14443B2CT: case NMT_JEWEL: - pnd->iLastError = EDEVNOTSUP; + CHIP_DATA(pnd)->last_status_byte = EDEVNOTSUP; return false; break; } @@ -1730,7 +1737,7 @@ pn53x_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *psz } bool -pn53x_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar) +pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar) { uint8_t abtCmd[] = { TgGetInitiatorCommand }; @@ -1765,7 +1772,7 @@ pn53x_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, u } bool -pn53x_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout) +pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout) { uint8_t abtCmd[1]; @@ -1785,7 +1792,7 @@ pn53x_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int break; } else { // TODO Support EasyFraming for other cases by software - pnd->iLastError = ENOTIMPL; + CHIP_DATA(pnd)->last_status_byte = ENOTIMPL; return false; } } @@ -1814,7 +1821,7 @@ pn53x_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int } bool -pn53x_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar) +pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar) { size_t szFrameBits = 0; size_t szFrameBytes = 0; @@ -1852,7 +1859,7 @@ pn53x_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx } bool -pn53x_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout) +pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout) { uint8_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; @@ -1876,7 +1883,7 @@ pn53x_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szT break; } else { // TODO Support EasyFraming for other cases by software - pnd->iLastError = ENOTIMPL; + CHIP_DATA(pnd)->last_status_byte = ENOTIMPL; return false; } } @@ -1951,13 +1958,13 @@ static struct sErrorMessage { }; const char * -pn53x_strerror (const nfc_device *pnd) +pn53x_strerror (const struct nfc_device *pnd) { const char *pcRes = "Unknown error"; size_t i; for (i = 0; i < (sizeof (sErrorMessages) / sizeof (struct sErrorMessage)); i++) { - if (sErrorMessages[i].iErrorCode == pnd->iLastError) { + if (sErrorMessages[i].iErrorCode == CHIP_DATA(pnd)->last_status_byte) { pcRes = sErrorMessages[i].pcErrorMsg; break; } @@ -1967,14 +1974,14 @@ pn53x_strerror (const nfc_device *pnd) } bool -pn53x_RFConfiguration__RF_field (nfc_device *pnd, bool bEnable) +pn53x_RFConfiguration__RF_field (struct nfc_device *pnd, bool bEnable) { uint8_t abtCmd[] = { RFConfiguration, RFCI_FIELD, (bEnable) ? 0x01 : 0x00 }; return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1); } bool -pn53x_RFConfiguration__Various_timings (nfc_device *pnd, const uint8_t fATR_RES_Timeout, const uint8_t fRetryTimeout) +pn53x_RFConfiguration__Various_timings (struct nfc_device *pnd, const uint8_t fATR_RES_Timeout, const uint8_t fRetryTimeout) { uint8_t abtCmd[] = { RFConfiguration, @@ -1987,7 +1994,7 @@ pn53x_RFConfiguration__Various_timings (nfc_device *pnd, const uint8_t fATR_RES_ } bool -pn53x_RFConfiguration__MaxRtyCOM (nfc_device *pnd, const uint8_t MaxRtyCOM) +pn53x_RFConfiguration__MaxRtyCOM (struct nfc_device *pnd, const uint8_t MaxRtyCOM) { uint8_t abtCmd[] = { RFConfiguration, @@ -1998,7 +2005,7 @@ pn53x_RFConfiguration__MaxRtyCOM (nfc_device *pnd, const uint8_t MaxRtyCOM) } bool -pn53x_RFConfiguration__MaxRetries (nfc_device *pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation) +pn53x_RFConfiguration__MaxRetries (struct nfc_device *pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation) { // Retry format: 0x00 means only 1 try, 0xff means infinite uint8_t abtCmd[] = { @@ -2012,7 +2019,7 @@ pn53x_RFConfiguration__MaxRetries (nfc_device *pnd, const uint8_t MxRtyATR, cons } bool -pn53x_SetParameters (nfc_device *pnd, const uint8_t ui8Value) +pn53x_SetParameters (struct nfc_device *pnd, const uint8_t ui8Value) { uint8_t abtCmd[] = { SetParameters, ui8Value }; @@ -2025,14 +2032,14 @@ pn53x_SetParameters (nfc_device *pnd, const uint8_t ui8Value) } bool -pn53x_SAMConfiguration (nfc_device *pnd, const pn532_sam_mode ui8Mode, int timeout) +pn53x_SAMConfiguration (struct nfc_device *pnd, const pn532_sam_mode ui8Mode, int timeout) { uint8_t abtCmd[] = { SAMConfiguration, ui8Mode, 0x00, 0x00 }; size_t szCmd = sizeof(abtCmd); if (CHIP_DATA(pnd)->type != PN532) { // This function is not supported by pn531 neither pn533 - pnd->iLastError = EDEVNOTSUP; + CHIP_DATA(pnd)->last_status_byte = EDEVNOTSUP; return false; } @@ -2047,14 +2054,14 @@ pn53x_SAMConfiguration (nfc_device *pnd, const pn532_sam_mode ui8Mode, int timeo szCmd = 3; break; default: - pnd->iLastError = EINVALARG; + CHIP_DATA(pnd)->last_status_byte = EINVALARG; return false; } return (pn53x_transceive (pnd, abtCmd, szCmd, NULL, NULL, timeout)); } bool -pn53x_PowerDown (nfc_device *pnd) +pn53x_PowerDown (struct nfc_device *pnd) { uint8_t abtCmd[] = { PowerDown, 0xf0 }; return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1)); @@ -2064,7 +2071,7 @@ pn53x_PowerDown (nfc_device *pnd) * @brief C wrapper to InListPassiveTarget command * @return true if command is successfully sent * - * @param pnd nfc_device struct pointer that represent currently used device + * @param pnd struct nfc_device struct pointer that represent currently used device * @param pmInitModulation Desired modulation * @param pbtInitiatorData Optional initiator data used for Felica, ISO14443B, Topaz Polling or for ISO14443A selecting a specific UID * @param szInitiatorData Length of initiator data \a pbtInitiatorData @@ -2075,7 +2082,7 @@ pn53x_PowerDown (nfc_device *pnd) * @note To decode theses TargetData[n], there is @fn pn53x_decode_target_data */ bool -pn53x_InListPassiveTarget (nfc_device *pnd, +pn53x_InListPassiveTarget (struct nfc_device *pnd, const pn53x_modulation pmInitModulation, const uint8_t szMaxTargets, const uint8_t *pbtInitiatorData, const size_t szInitiatorData, uint8_t *pbtTargetsData, size_t *pszTargetsData, @@ -2094,14 +2101,14 @@ pn53x_InListPassiveTarget (nfc_device *pnd, case PM_ISO14443B_106: if (!(pnd->btSupportByte & SUPPORT_ISO14443B)) { // Eg. Some PN532 doesn't support type B! - pnd->iLastError = EDEVNOTSUP; + CHIP_DATA(pnd)->last_status_byte = EDEVNOTSUP; return false; } break; case PM_JEWEL_106: if(CHIP_DATA(pnd)->type == PN531) { // These modulations are not supported by pn531 - pnd->iLastError = EDEVNOTSUP; + CHIP_DATA(pnd)->last_status_byte = EDEVNOTSUP; return false; } break; @@ -2110,12 +2117,12 @@ pn53x_InListPassiveTarget (nfc_device *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 = EDEVNOTSUP; + CHIP_DATA(pnd)->last_status_byte = EDEVNOTSUP; return false; } break; default: - pnd->iLastError = EINVALARG; + CHIP_DATA(pnd)->last_status_byte = EINVALARG; return false; } abtCmd[2] = pmInitModulation; // BrTy, the type of init modulation used for polling a passive tag @@ -2128,7 +2135,7 @@ pn53x_InListPassiveTarget (nfc_device *pnd, } bool -pn53x_InDeselect (nfc_device *pnd, const uint8_t ui8Target) +pn53x_InDeselect (struct nfc_device *pnd, const uint8_t ui8Target) { if (CHIP_DATA(pnd)->type == RCS360) { // We should do act here *only* if a target was previously selected @@ -2150,7 +2157,7 @@ pn53x_InDeselect (nfc_device *pnd, const uint8_t ui8Target) } bool -pn53x_InRelease (nfc_device *pnd, const uint8_t ui8Target) +pn53x_InRelease (struct nfc_device *pnd, const uint8_t ui8Target) { if (CHIP_DATA(pnd)->type == RCS360) { // We should do act here *only* if a target was previously selected @@ -2172,14 +2179,14 @@ pn53x_InRelease (nfc_device *pnd, const uint8_t ui8Target) } bool -pn53x_InAutoPoll (nfc_device *pnd, +pn53x_InAutoPoll (struct nfc_device *pnd, const pn53x_target_type *ppttTargetTypes, const size_t szTargetTypes, const uint8_t btPollNr, const uint8_t btPeriod, nfc_target * pntTargets, size_t *pszTargetFound, const int timeout) { if (CHIP_DATA(pnd)->type != PN532) { // This function is not supported by pn531 neither pn533 - pnd->iLastError = EDEVNOTSUP; + CHIP_DATA(pnd)->last_status_byte = EDEVNOTSUP; return false; } @@ -2235,7 +2242,7 @@ pn53x_InAutoPoll (nfc_device *pnd, * @param[out] pnt \a nfc_target which will be filled by this function */ bool -pn53x_InJumpForDEP (nfc_device *pnd, +pn53x_InJumpForDEP (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const uint8_t *pbtPassiveInitiatorData, @@ -2261,7 +2268,7 @@ pn53x_InJumpForDEP (nfc_device *pnd, break; case NBR_847: case NBR_UNDEFINED: - pnd->iLastError = EINVALARG; + CHIP_DATA(pnd)->last_status_byte = EINVALARG; return false; break; } @@ -2281,7 +2288,7 @@ pn53x_InJumpForDEP (nfc_device *pnd, break; case NBR_847: case NBR_UNDEFINED: - pnd->iLastError = EINVALARG; + CHIP_DATA(pnd)->last_status_byte = EINVALARG; return false; break; } @@ -2331,7 +2338,7 @@ pn53x_InJumpForDEP (nfc_device *pnd, } bool -pn53x_TgInitAsTarget (nfc_device *pnd, pn53x_target_mode ptm, +pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, const uint8_t *pbtMifareParams, const uint8_t *pbtTkt, size_t szTkt, const uint8_t *pbtFeliCaParams, @@ -2402,7 +2409,7 @@ pn53x_TgInitAsTarget (nfc_device *pnd, pn53x_target_mode ptm, } bool -pn53x_check_ack_frame (nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen) +pn53x_check_ack_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen) { if (szRxFrameLen >= sizeof (pn53x_ack_frame)) { if (0 == memcmp (pbtRxFrame, pn53x_ack_frame, sizeof (pn53x_ack_frame))) { @@ -2410,18 +2417,18 @@ pn53x_check_ack_frame (nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t return true; } } - pnd->iLastError = EFRAACKMISMATCH; + CHIP_DATA(pnd)->last_status_byte = EFRAACKMISMATCH; log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unexpected PN53x reply!"); return false; } bool -pn53x_check_error_frame (nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen) +pn53x_check_error_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen) { if (szRxFrameLen >= sizeof (pn53x_error_frame)) { if (0 == memcmp (pbtRxFrame, pn53x_error_frame, sizeof (pn53x_error_frame))) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "PN53x sent an error frame"); - pnd->iLastError = EFRAISERRFRAME; + CHIP_DATA(pnd)->last_status_byte = EFRAISERRFRAME; return false; } } @@ -2651,7 +2658,7 @@ pn53x_nm_to_ptt(const nfc_modulation nm) } void -pn53x_data_new (nfc_device *pnd, const struct pn53x_io *io) +pn53x_data_new (struct nfc_device *pnd, const struct pn53x_io *io) { pnd->chip_data = malloc(sizeof(struct pn53x_data)); @@ -2668,6 +2675,9 @@ pn53x_data_new (nfc_device *pnd, const struct pn53x_io *io) // PN53x starts in initiator mode CHIP_DATA (pnd)->operating_mode = INITIATOR; + // Clear last status byte + CHIP_DATA (pnd)->last_status_byte = 0x00; + // Set current target to NULL CHIP_DATA (pnd)->current_target = NULL; @@ -2686,7 +2696,7 @@ pn53x_data_new (nfc_device *pnd, const struct pn53x_io *io) } void -pn53x_data_free (nfc_device *pnd) +pn53x_data_free (struct nfc_device *pnd) { if (CHIP_DATA (pnd)->current_target) { free (CHIP_DATA (pnd)->current_target); diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index d5f8ce4..9c30d4c 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -127,8 +127,8 @@ typedef enum { } pn53x_operating_mode; struct pn53x_io { - bool (*send)(nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); - int (*receive)(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, int timeout); + bool (*send)(struct nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); + int (*receive)(struct nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, int timeout); }; /* defines */ @@ -146,6 +146,8 @@ struct pn53x_data { nfc_target *current_target; /** PN53x I/O functions stored in struct */ const struct pn53x_io *io; +/** Last status byte returned by PN53x */ + uint8_t last_status_byte; /** Register cache for REG_CIU_BIT_FRAMING, SYMBOL_TX_LAST_BITS: The last TX bits setting, we need to reset this if it does not apply anymore */ uint8_t ui8TxBits; /** Register cache for SetParameters function. */ @@ -261,11 +263,11 @@ typedef enum { extern const uint8_t pn53x_ack_frame[6]; extern const uint8_t pn53x_nack_frame[6]; -bool pn53x_init(nfc_device *pnd); -bool pn53x_transceive (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); +bool pn53x_init(struct nfc_device *pnd); +bool pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); -bool pn53x_set_parameters (nfc_device *pnd, const uint8_t ui8Value, const bool bEnable); -bool pn53x_set_tx_bits (nfc_device *pnd, const uint8_t ui8Bits); +bool pn53x_set_parameters (struct nfc_device *pnd, const uint8_t ui8Value, const bool bEnable); +bool pn53x_set_tx_bits (struct nfc_device *pnd, const uint8_t ui8Bits); bool pn53x_wrap_frame (const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtFrame, size_t *pszFrameBits); bool pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t *pbtRx, size_t *pszRxBits, @@ -273,74 +275,74 @@ bool pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, u bool pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData, pn53x_type chip_type, nfc_modulation_type nmt, nfc_target_info *pnti); -bool pn53x_read_register (nfc_device *pnd, uint16_t ui16Reg, uint8_t *ui8Value); -bool pn53x_write_register (nfc_device *pnd, uint16_t ui16Reg, uint8_t ui8SymbolMask, uint8_t ui8Value); -bool pn53x_get_firmware_version (nfc_device *pnd, char abtFirmwareText[22]); -int pn53x_set_property_int (nfc_device *pnd, const nfc_property property, const int value); -int pn53x_set_property_bool (nfc_device *pnd, const nfc_property property, const bool bEnable); +bool pn53x_read_register (struct nfc_device *pnd, uint16_t ui16Reg, uint8_t *ui8Value); +bool pn53x_write_register (struct nfc_device *pnd, uint16_t ui16Reg, uint8_t ui8SymbolMask, uint8_t ui8Value); +bool pn53x_get_firmware_version (struct nfc_device *pnd, char abtFirmwareText[22]); +int pn53x_set_property_int (struct nfc_device *pnd, const nfc_property property, const int value); +int pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, const bool bEnable); -bool pn53x_check_communication (nfc_device *pnd); -bool pn53x_idle (nfc_device *pnd); +bool pn53x_check_communication (struct nfc_device *pnd); +bool pn53x_idle (struct nfc_device *pnd); // NFC device as Initiator functions -int pn53x_initiator_init (nfc_device *pnd); -bool pn53x_initiator_select_passive_target (nfc_device *pnd, +int pn53x_initiator_init (struct nfc_device *pnd); +bool pn53x_initiator_select_passive_target (struct nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt); -bool pn53x_initiator_poll_target (nfc_device *pnd, +bool pn53x_initiator_poll_target (struct nfc_device *pnd, const nfc_modulation *pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt); -bool pn53x_initiator_select_dep_target (nfc_device *pnd, +bool pn53x_initiator_select_dep_target (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout); -bool pn53x_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, +bool pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); -bool pn53x_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, +bool pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); -bool pn53x_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, +bool pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar, uint32_t *cycles); -bool pn53x_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, +bool pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, uint32_t *cycles); -bool pn53x_initiator_deselect_target (nfc_device *pnd); +bool pn53x_initiator_deselect_target (struct nfc_device *pnd); // NFC device as Target functions -bool pn53x_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx); -bool pn53x_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); -bool pn53x_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); -bool pn53x_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); -bool pn53x_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); +bool pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx); +bool pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); +bool pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); +bool pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); +bool pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); // Error handling functions -const char *pn53x_strerror (const nfc_device *pnd); +const char *pn53x_strerror (const struct nfc_device *pnd); // C wrappers for PN53x commands -bool pn53x_SetParameters (nfc_device *pnd, const uint8_t ui8Value); -bool pn53x_SAMConfiguration (nfc_device *pnd, const pn532_sam_mode mode, int timeout); -bool pn53x_PowerDown (nfc_device *pnd); -bool pn53x_InListPassiveTarget (nfc_device *pnd, const pn53x_modulation pmInitModulation, +bool pn53x_SetParameters (struct nfc_device *pnd, const uint8_t ui8Value); +bool pn53x_SAMConfiguration (struct nfc_device *pnd, const pn532_sam_mode mode, int timeout); +bool pn53x_PowerDown (struct nfc_device *pnd); +bool pn53x_InListPassiveTarget (struct nfc_device *pnd, const pn53x_modulation pmInitModulation, const uint8_t szMaxTargets, const uint8_t *pbtInitiatorData, const size_t szInitiatorDataLen, uint8_t *pbtTargetsData, size_t *pszTargetsData, int timeout); -bool pn53x_InDeselect (nfc_device *pnd, const uint8_t ui8Target); -bool pn53x_InRelease (nfc_device *pnd, const uint8_t ui8Target); -bool pn53x_InAutoPoll (nfc_device *pnd, const pn53x_target_type *ppttTargetTypes, const size_t szTargetTypes, +bool pn53x_InDeselect (struct nfc_device *pnd, const uint8_t ui8Target); +bool pn53x_InRelease (struct nfc_device *pnd, const uint8_t ui8Target); +bool pn53x_InAutoPoll (struct nfc_device *pnd, const pn53x_target_type *ppttTargetTypes, const size_t szTargetTypes, const uint8_t btPollNr, const uint8_t btPeriod, nfc_target *pntTargets, size_t *pszTargetFound, const int timeout); -bool pn53x_InJumpForDEP (nfc_device *pnd, +bool pn53x_InJumpForDEP (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const uint8_t *pbtPassiveInitiatorData, const uint8_t *pbtNFCID3i, const uint8_t *pbtGB, const size_t szGB, nfc_target *pnt, const int timeout); -bool pn53x_TgInitAsTarget (nfc_device *pnd, pn53x_target_mode ptm, +bool pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, const uint8_t *pbtMifareParams, const uint8_t *pbtTkt, size_t szTkt, const uint8_t *pbtFeliCaParams, @@ -348,17 +350,17 @@ bool pn53x_TgInitAsTarget (nfc_device *pnd, pn53x_target_mode ptm, uint8_t *pbtRx, size_t *pszRx, uint8_t *pbtModeByte); // RFConfiguration -bool pn53x_RFConfiguration__RF_field (nfc_device *pnd, bool bEnable); -bool pn53x_RFConfiguration__Various_timings (nfc_device *pnd, const uint8_t fATR_RES_Timeout, const uint8_t fRetryTimeout); -bool pn53x_RFConfiguration__MaxRtyCOM (nfc_device *pnd, const uint8_t MaxRtyCOM); -bool pn53x_RFConfiguration__MaxRetries (nfc_device *pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation); +bool pn53x_RFConfiguration__RF_field (struct nfc_device *pnd, bool bEnable); +bool pn53x_RFConfiguration__Various_timings (struct nfc_device *pnd, const uint8_t fATR_RES_Timeout, const uint8_t fRetryTimeout); +bool pn53x_RFConfiguration__MaxRtyCOM (struct nfc_device *pnd, const uint8_t MaxRtyCOM); +bool pn53x_RFConfiguration__MaxRetries (struct nfc_device *pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation); // Misc -bool pn53x_check_ack_frame (nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen); -bool pn53x_check_error_frame (nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen); +bool pn53x_check_ack_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen); +bool pn53x_check_error_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen); bool pn53x_build_frame (uint8_t *pbtFrame, size_t *pszFrame, const uint8_t *pbtData, const size_t szData); -void pn53x_data_new (nfc_device *pnd, const struct pn53x_io *io); -void pn53x_data_free (nfc_device *pnd); +void pn53x_data_new (struct nfc_device *pnd, const struct pn53x_io *io); +void pn53x_data_free (struct nfc_device *pnd); #endif // __NFC_CHIPS_PN53X_H__ diff --git a/libnfc/drivers/acr122.c b/libnfc/drivers/acr122.c index 48d400b..615f69e 100644 --- a/libnfc/drivers/acr122.c +++ b/libnfc/drivers/acr122.c @@ -317,7 +317,7 @@ acr122_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int t // Make sure the command does not overflow the send buffer if (szData > ACR122_COMMAND_LEN) { - pnd->iLastError = EINVALARG; + pnd->last_error = NFC_EINVARG; return false; } @@ -344,7 +344,7 @@ acr122_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int t * 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 = ECOMIO; + pnd->last_error = NFC_EIO; return false; } } else { @@ -353,7 +353,7 @@ acr122_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int t * 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 = ECOMIO; + pnd->last_error = NFC_EIO; return false; } } @@ -365,12 +365,12 @@ acr122_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int t // Make sure we received the byte-count we expected if (dwRxLen != 2) { - pnd->iLastError = ECOMIO; + pnd->last_error = NFC_EIO; return false; } // Check if the operation was successful, so an answer is available if (DRIVER_DATA (pnd)->abtRx[0] == SCARD_OPERATION_ERROR) { - pnd->iLastError = EFRAISERRFRAME; + pnd->last_error = NFC_EIO; return false; } } else { @@ -396,7 +396,7 @@ acr122_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int time 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 = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } DRIVER_DATA (pnd)->szRx = dwRxLen; @@ -409,15 +409,13 @@ acr122_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int time // 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 = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } // Wipe out the 4 APDU emulation bytes: D5 4B .. .. .. 90 00 len = DRIVER_DATA (pnd)->szRx - 4; memcpy (pbtData, DRIVER_DATA (pnd)->abtRx + 2, len); - // Transmission went successful - pnd->iLastError = 0; return len; } @@ -488,7 +486,7 @@ const struct nfc_driver_t acr122_driver = { .target_receive_bits = pn53x_target_receive_bits, .device_set_property_bool = pn53x_set_property_bool, - .device_set_property_int = pn53x_set_property_int, + .device_set_property_int = pn53x_set_property_int, .abort_command = NULL, // FIXME: abort is not supported in this driver .idle = NULL, // FIXME: idle is not supported in this driver diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 11b4c5d..17d4ec9 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -315,19 +315,19 @@ arygon_tama_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, if (szData > PN53x_NORMAL_FRAME__DATA_MAX_LEN) { // ARYGON Reader with PN532 equipped does not support extended frame (bug in ARYGON firmware?) log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "ARYGON device does not support more than %d bytes as payload (requested: %zd)", PN53x_NORMAL_FRAME__DATA_MAX_LEN, szData); - pnd->iLastError = EDEVNOTSUP; + pnd->last_error = NFC_EDEVNOTSUPP; return false; } if (!pn53x_build_frame (abtFrame + 1, &szFrame, pbtData, szData)) { - pnd->iLastError = EINVALARG; + pnd->last_error = NFC_EINVARG; return false; } int res = uart_send (DRIVER_DATA (pnd)->port, abtFrame, szFrame + 1, timeout); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to transmit data. (TX)"); - pnd->iLastError = res; + pnd->last_error = NFC_EIO; return false; } @@ -335,7 +335,7 @@ arygon_tama_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, res = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, sizeof (abtRxBuf), 0, timeout); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to read ACK"); - pnd->iLastError = res; + pnd->last_error = NFC_EIO; return false; } @@ -378,17 +378,17 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, abort_p = (void*)&(DRIVER_DATA (pnd)->abort_flag); #endif - pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 5, abort_p, timeout); + pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 5, abort_p, timeout); - if (abort_p && (EOPABORT == pnd->iLastError)) { + if (abort_p && (EOPABORT == pnd->last_error)) { arygon_abort (pnd); - /* iLastError got reset by arygon_abort() */ - pnd->iLastError = EOPABORT; + /* last_error got reset by arygon_abort() */ + pnd->last_error = EOPABORT; return -1; } - if (pnd->iLastError != 0) { + if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } @@ -396,7 +396,7 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, const uint8_t pn53x_preamble[3] = { 0x00, 0x00, 0xff }; if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } @@ -404,7 +404,7 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, // Error frame uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 3, 0, timeout); log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected"); - pnd->iLastError = EFRAISERRFRAME; + pnd->last_error = EFRAISERRFRAME; return -1; } else if ((0xff == abtRxBuf[3]) && (0xff == abtRxBuf[4])) { // Extended frame @@ -415,7 +415,7 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, if (256 != (abtRxBuf[3] + abtRxBuf[4])) { // TODO: Retry log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } @@ -425,39 +425,39 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, if (len > szDataLen) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } // TFI + PD0 (CC+1) - pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); - if (pnd->iLastError != 0) { + pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); + if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } if (abtRxBuf[0] != 0xD5) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } if (abtRxBuf[1] != CHIP_DATA (pnd)->ui8LastCommand + 1) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } if (len) { - pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, pbtData, len, 0, timeout); - if (pnd->iLastError != 0) { + pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, pbtData, len, 0, timeout); + if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } } - pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); - if (pnd->iLastError != 0) { + pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); + if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } @@ -470,13 +470,13 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, if (btDCS != abtRxBuf[0]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } if (0x00 != abtRxBuf[1]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = 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 f582d8b..2078a6a 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -321,14 +321,14 @@ pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, i size_t szFrame = 0; if (!pn53x_build_frame (abtFrame, &szFrame, pbtData, szData)) { - pnd->iLastError = EINVALARG; + pnd->last_error = EINVALARG; return false; } int res = uart_send (DRIVER_DATA(pnd)->port, abtFrame, szFrame, timeout); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to transmit data. (TX)"); - pnd->iLastError = res; + pnd->last_error = res; return false; } @@ -336,7 +336,7 @@ pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, i res = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 6, 0, timeout); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to read ACK"); - pnd->iLastError = res; + pnd->last_error = res; return false; } @@ -361,14 +361,14 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i abort_p = (void*)&(DRIVER_DATA (pnd)->abort_flag); #endif - pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 5, abort_p, timeout); + pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 5, abort_p, timeout); - if (abort_p && (EOPABORT == pnd->iLastError)) { + if (abort_p && (EOPABORT == pnd->last_error)) { pn532_uart_ack (pnd); return -1; } - if (pnd->iLastError != 0) { + if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } @@ -376,7 +376,7 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i const uint8_t pn53x_preamble[3] = { 0x00, 0x00, 0xff }; if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } @@ -384,19 +384,19 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i // Error frame uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 3, 0, timeout); log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected"); - pnd->iLastError = EFRAISERRFRAME; + pnd->last_error = EFRAISERRFRAME; return -1; } else if ((0xff == abtRxBuf[3]) && (0xff == abtRxBuf[4])) { // Extended frame - pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 3, 0, timeout); - if (pnd->iLastError) return -1; + pnd->last_error = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 3, 0, timeout); + if (pnd->last_error) return -1; // (abtRxBuf[0] << 8) + abtRxBuf[1] (LEN) include TFI + (CC+1) len = (abtRxBuf[0] << 8) + abtRxBuf[1] - 2; if (((abtRxBuf[0] + abtRxBuf[1] + abtRxBuf[2]) % 256) != 0) { // TODO: Retry log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } } else { @@ -404,7 +404,7 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i if (256 != (abtRxBuf[3] + abtRxBuf[4])) { // TODO: Retry log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } @@ -414,39 +414,39 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i if (len > szDataLen) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } // TFI + PD0 (CC+1) - pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); - if (pnd->iLastError != 0) { + pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); + if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } if (abtRxBuf[0] != 0xD5) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } if (abtRxBuf[1] != CHIP_DATA (pnd)->ui8LastCommand + 1) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } if (len) { - pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, pbtData, len, 0, timeout); - if (pnd->iLastError != 0) { + pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, pbtData, len, 0, timeout); + if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } } - pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); - if (pnd->iLastError != 0) { + pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); + if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } @@ -459,13 +459,13 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i if (btDCS != abtRxBuf[0]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } if (0x00 != abtRxBuf[1]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } // The PN53x command is done and we successfully received the reply @@ -526,7 +526,7 @@ const struct nfc_driver_t pn532_uart_driver = { .target_receive_bits = pn53x_target_receive_bits, .device_set_property_bool = pn53x_set_property_bool, - .device_set_property_int = pn53x_set_property_int, + .device_set_property_int = pn53x_set_property_int, .abort_command = pn532_uart_abort_command, .idle = pn53x_idle, diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index d660e4f..c2e6349 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -516,14 +516,14 @@ pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, co int res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), abtFrame, szFrame, timeout); if (res < 0) { - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return false; } uint8_t abtRxBuf[PN53X_USB_BUFFER_LEN]; res = pn53x_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf), timeout); if (res < 0) { - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; // try to interrupt current device state pn53x_usb_ack(pnd); return false; @@ -541,7 +541,7 @@ pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, co // FIXME Sony reader is also affected by this bug but NACK is not supported int res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), (uint8_t *)pn53x_nack_frame, sizeof(pn53x_nack_frame), timeout); if (res < 0) { - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; // try to interrupt current device state pn53x_usb_ack(pnd); return false; @@ -574,7 +574,7 @@ read: // A user-provided timeout is set, we have to cut it in multiple chunk to be able to keep an nfc_abort_command() mecanism remaining_time -= USB_TIMEOUT_PER_PASS; if (remaining_time <= 0) { - pnd->iLastError = ECOMTIMEOUT; + pnd->last_error = ECOMTIMEOUT; return -1; } else { usb_timeout = MIN(remaining_time, USB_TIMEOUT_PER_PASS); @@ -587,7 +587,7 @@ read: if (DRIVER_DATA (pnd)->abort_flag) { DRIVER_DATA (pnd)->abort_flag = false; pn53x_usb_ack (pnd); - pnd->iLastError = EOPABORT; + pnd->last_error = EOPABORT; return -1; } else { goto read; @@ -595,7 +595,7 @@ read: } if (res < 0) { - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; // try to interrupt current device state pn53x_usb_ack(pnd); return -1; @@ -604,7 +604,7 @@ read: const uint8_t pn53x_preamble[3] = { 0x00, 0x00, 0xff }; if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } offset += 3; @@ -612,7 +612,7 @@ read: if ((0x01 == abtRxBuf[offset]) && (0xff == abtRxBuf[offset + 1])) { // Error frame log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected"); - pnd->iLastError = EFRAISERRFRAME; + pnd->last_error = EFRAISERRFRAME; return -1; } else if ((0xff == abtRxBuf[offset]) && (0xff == abtRxBuf[offset + 1])) { // Extended frame @@ -623,7 +623,7 @@ read: if (((abtRxBuf[offset] + abtRxBuf[offset + 1] + abtRxBuf[offset + 2]) % 256) != 0) { // TODO: Retry log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } offset += 3; @@ -632,7 +632,7 @@ read: if (256 != (abtRxBuf[offset] + abtRxBuf[offset + 1])) { // TODO: Retry log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } @@ -643,21 +643,21 @@ read: if (len > szDataLen) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } // TFI + PD0 (CC+1) if (abtRxBuf[offset] != 0xD5) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } offset += 1; if (abtRxBuf[offset] != CHIP_DATA (pnd)->ui8LastCommand + 1) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } offset += 1; @@ -673,18 +673,18 @@ read: if (btDCS != abtRxBuf[offset]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } offset += 1; if (0x00 != abtRxBuf[offset]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } // The PN53x command is done and we successfully received the reply - pnd->iLastError = 0; + pnd->last_error = 0; return len; } @@ -702,7 +702,7 @@ pn53x_usb_init (nfc_device *pnd) const uint8_t abtCmd[] = { GetFirmwareVersion }; pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0); // ...and we don't care about error - pnd->iLastError = 0; + pnd->last_error = 0; if (SONY_RCS360 == DRIVER_DATA (pnd)->model) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "SONY RC-S360 initialization."); const uint8_t abtCmd2[] = { 0x18, 0x01 }; @@ -754,8 +754,9 @@ On ASK LoGO hardware: int pn53x_usb_set_property_bool (nfc_device *pnd, const nfc_property property, const bool bEnable) { - if (!pn53x_set_property_bool (pnd, property, bEnable)) - return NFC_DEVICE_ERROR; + int res; + if ((res = pn53x_set_property_bool (pnd, property, bEnable)) < 0) + return res; switch (DRIVER_DATA (pnd)->model) { case ASK_LOGO: @@ -763,14 +764,14 @@ pn53x_usb_set_property_bool (nfc_device *pnd, const nfc_property property, const /* Switch on/off LED2 and Progressive Field GPIO according to ACTIVATE_FIELD option */ log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Switch progressive field %s", bEnable ? "On" : "Off"); if (!pn53x_write_register (pnd, PN53X_SFR_P3, _BV(P31) | _BV(P34), bEnable ? _BV (P34) : _BV (P31))) - return NFC_DEVICE_ERROR; + return NFC_ECHIP; } break; case SCM_SCL3711: if (NP_ACTIVATE_FIELD == property) { // Switch on/off LED according to ACTIVATE_FIELD option if (!pn53x_write_register (pnd, PN53X_SFR_P3, _BV (P32), bEnable ? 0 : _BV (P32))) - return NFC_DEVICE_ERROR; + return NFC_ECHIP; } break; default: diff --git a/libnfc/nfc-device.c b/libnfc/nfc-device.c index edf3a17..aad31d8 100644 --- a/libnfc/nfc-device.c +++ b/libnfc/nfc-device.c @@ -49,7 +49,7 @@ nfc_device_new (void) res->bPar = false; res->bEasyFraming = false; res->bAutoIso14443_4 = false; - res->iLastError = 0; + res->last_error = 0; res->driver_data = NULL; res->chip_data = NULL; diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index bfd703e..b67828f 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -23,11 +23,12 @@ */ #ifndef __NFC_INTERNAL_H__ -# define __NFC_INTERNAL_H__ +#define __NFC_INTERNAL_H__ -# include -# include -# include +#include +#include + +#include "nfc/nfc.h" #include "log.h" @@ -35,11 +36,11 @@ * @macro HAL * @brief Execute corresponding driver function if exists. */ -#define HAL( FUNCTION, ... ) pnd->iLastError = 0; \ +#define HAL( FUNCTION, ... ) pnd->last_error = 0; \ if (pnd->driver->FUNCTION) { \ return pnd->driver->FUNCTION( __VA_ARGS__ ); \ } else { \ - pnd->iLastError = EDEVNOTSUP; \ + pnd->last_error = NFC_EDEVNOTSUPP; \ return false; \ } @@ -127,35 +128,64 @@ struct nfc_driver_t { const char *name; bool (*probe)(nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); - nfc_device *(*connect) (const nfc_connstring connstring); - void (*disconnect) (nfc_device *pnd); - const char *(*strerror) (const nfc_device *pnd); + struct nfc_device *(*connect) (const nfc_connstring connstring); + void (*disconnect) (struct nfc_device *pnd); + const char *(*strerror) (const struct nfc_device *pnd); - int (*initiator_init) (nfc_device *pnd); - bool (*initiator_select_passive_target) (nfc_device *pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt); - bool (*initiator_poll_target) (nfc_device *pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt); - bool (*initiator_select_dep_target) (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt, const int timeout); - bool (*initiator_deselect_target) (nfc_device *pnd); - bool (*initiator_transceive_bytes) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); - bool (*initiator_transceive_bits) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); - bool (*initiator_transceive_bytes_timed) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles); - bool (*initiator_transceive_bits_timed) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles); + int (*initiator_init) (struct nfc_device *pnd); + bool (*initiator_select_passive_target) (struct nfc_device *pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt); + bool (*initiator_poll_target) (struct nfc_device *pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt); + bool (*initiator_select_dep_target) (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt, const int timeout); + bool (*initiator_deselect_target) (struct nfc_device *pnd); + bool (*initiator_transceive_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); + bool (*initiator_transceive_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); + bool (*initiator_transceive_bytes_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles); + bool (*initiator_transceive_bits_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles); - bool (*target_init) (nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); - bool (*target_send_bytes) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, int timeout); - bool (*target_receive_bytes) (nfc_device *pnd, uint8_t * pbtRx, size_t * pszRx, int timeout); - bool (*target_send_bits) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); - bool (*target_receive_bits) (nfc_device *pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); + bool (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); + bool (*target_send_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, int timeout); + bool (*target_receive_bytes) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRx, int timeout); + bool (*target_send_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); + bool (*target_receive_bits) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); - int (*device_set_property_bool) (nfc_device *pnd, const nfc_property property, const bool bEnable); - int (*device_set_property_int) (nfc_device *pnd, const nfc_property property, const int value); + int (*device_set_property_bool) (struct nfc_device *pnd, const nfc_property property, const bool bEnable); + int (*device_set_property_int) (struct nfc_device *pnd, const nfc_property property, const int value); - bool (*abort_command) (nfc_device *pnd); - bool (*idle) (nfc_device *pnd); + bool (*abort_command) (struct nfc_device *pnd); + bool (*idle) (struct nfc_device *pnd); +}; + +# define DEVICE_NAME_LENGTH 256 +# define DEVICE_PORT_LENGTH 64 + +/** + * @struct nfc_device + * @brief NFC device information + */ +struct nfc_device { + const struct nfc_driver_t *driver; + void *driver_data; + void *chip_data; + +/** Device name string, including device wrapper firmware */ + char acName[DEVICE_NAME_LENGTH]; +/** Is the CRC automaticly added, checked and removed from the frames */ + bool bCrc; +/** Does the chip handle parity bits, all parities are handled as data */ + bool bPar; +/** Should the chip handle frames encapsulation and chaining */ + bool bEasyFraming; +/** Should the chip switch automatically activate ISO14443-4 when + selecting tags supporting it? */ + bool bAutoIso14443_4; +/** Supported modulation encoded in a byte */ + uint8_t btSupportByte; +/** Last reported error */ + int last_error; }; nfc_device *nfc_device_new (void); -void nfc_device_free (nfc_device *nfc_device); +void nfc_device_free (nfc_device *dev); void iso14443_cascade_uid (const uint8_t abtUID[], const size_t szUID, uint8_t * pbtCascadedUID, size_t * pszCascadedUID); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 48641f3..44d18e2 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -372,7 +372,7 @@ nfc_initiator_list_passive_targets (nfc_device *pnd, size_t szInitDataLen = 0; int res = 0; - pnd->iLastError = 0; + pnd->last_error = 0; // Let the reader only try once to find a tag if ((res = nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, false)) < 0) {