From 1130036bf32b172dd55c8689f0eb1449acb78086 Mon Sep 17 00:00:00 2001 From: Romain Tartiere Date: Sat, 28 Aug 2010 14:27:33 +0000 Subject: [PATCH] - Detect errors in pn53x_get_reg(); - Add a test case to confirm an endianness problem on little-endian machines. --- libnfc/chips/pn53x.c | 25 ++++++++++++++------- libnfc/chips/pn53x.h | 2 +- test/Makefile.am | 6 ++++- test/test_register_endianness.c | 39 +++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 test/test_register_endianness.c diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index ebb3555..c947152 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -156,27 +156,29 @@ bool pn53x_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxL return (0 == pnd->iLastError); } -byte_t pn53x_get_reg(nfc_device_t* pnd, uint16_t ui16Reg) +bool pn53x_get_reg(nfc_device_t* pnd, uint16_t ui16Reg, uint8_t* ui8Value) { - uint8_t ui8Value; size_t szValueLen = 1; byte_t abtCmd[sizeof(pncmd_get_register)]; memcpy(abtCmd,pncmd_get_register,sizeof(pncmd_get_register)); abtCmd[2] = ui16Reg >> 8; abtCmd[3] = ui16Reg & 0xff; - pn53x_transceive(pnd,abtCmd,4,&ui8Value,&szValueLen); - return ui8Value; + return pn53x_transceive(pnd,abtCmd,4,ui8Value,&szValueLen); } bool pn53x_set_reg(nfc_device_t* pnd, uint16_t ui16Reg, uint8_t ui8SybmolMask, uint8_t ui8Value) { + uint8_t ui8Current; byte_t abtCmd[sizeof(pncmd_set_register)]; memcpy(abtCmd,pncmd_set_register,sizeof(pncmd_set_register)); abtCmd[2] = ui16Reg >> 8; abtCmd[3] = ui16Reg & 0xff; - abtCmd[4] = ui8Value | (pn53x_get_reg(pnd,ui16Reg) & (~ui8SybmolMask)); + if (!pn53x_get_reg(pnd,ui16Reg, &ui8Current)) + return false; + + abtCmd[4] = ui8Value | (ui8Current & (~ui8SybmolMask)); return pn53x_transceive(pnd,abtCmd,5,NULL,NULL); } @@ -784,6 +786,7 @@ bool pn53x_initiator_transceive_bits(nfc_device_t* pnd, const byte_t* pbtTx, con size_t szRxLen; size_t szFrameBits = 0; size_t szFrameBytes = 0; + uint8_t ui8rcc; uint8_t ui8Bits = 0; byte_t abtCmd[sizeof(pncmd_initiator_exchange_raw_data)]; @@ -815,7 +818,9 @@ bool pn53x_initiator_transceive_bits(nfc_device_t* pnd, const byte_t* pbtTx, con if (!pn53x_transceive(pnd,abtCmd,szFrameBytes+2,abtRx,&szRxLen)) return false; // Get the last bit-count that is stored in the received byte - ui8Bits = pn53x_get_reg(pnd,REG_CIU_CONTROL) & SYMBOL_RX_LAST_BITS; + if (!pn53x_get_reg(pnd,REG_CIU_CONTROL, &ui8rcc)) + return false; + ui8Bits = ui8rcc & SYMBOL_RX_LAST_BITS; // Recover the real frame length in bits szFrameBits = ((szRxLen-1-((ui8Bits==0)?0:1))*8)+ui8Bits; @@ -909,6 +914,7 @@ bool pn53x_target_init(nfc_device_t* pnd, byte_t* pbtRx, size_t* pszRxBits) { byte_t abtRx[MAX_FRAME_LEN]; size_t szRxLen; + uint8_t ui8rcc; uint8_t ui8Bits; // Save the current configuration settings bool bCrc = pnd->bCrc; @@ -944,7 +950,8 @@ bool pn53x_target_init(nfc_device_t* pnd, byte_t* pbtRx, size_t* pszRxBits) if (!pn53x_transceive(pnd,abtCmd,39,abtRx,&szRxLen)) return false; // Get the last bit-count that is stored in the received byte - ui8Bits = pn53x_get_reg(pnd,REG_CIU_CONTROL) & SYMBOL_RX_LAST_BITS; + if (!pn53x_get_reg(pnd,REG_CIU_CONTROL, &ui8rcc)) return false; + ui8Bits = ui8rcc & SYMBOL_RX_LAST_BITS; // We are sure the parity is handled by the PN53X chip, so we handle it this way *pszRxBits = ((szRxLen-1-((ui8Bits==0)?0:1))*8)+ui8Bits; @@ -963,13 +970,15 @@ bool pn53x_target_receive_bits(nfc_device_t* pnd, byte_t* pbtRx, size_t* pszRxBi byte_t abtRx[MAX_FRAME_LEN]; size_t szRxLen; size_t szFrameBits; + uint8_t ui8rcc; uint8_t ui8Bits; // Try to gather a received frame from the reader if (!pn53x_transceive(pnd,pncmd_target_receive,2,abtRx,&szRxLen)) return false; // Get the last bit-count that is stored in the received byte - ui8Bits = pn53x_get_reg(pnd,REG_CIU_CONTROL) & SYMBOL_RX_LAST_BITS; + if (!pn53x_get_reg(pnd,REG_CIU_CONTROL, &ui8rcc)) return false; + ui8Bits = ui8rcc & SYMBOL_RX_LAST_BITS; // Recover the real frame length in bits szFrameBits = ((szRxLen-1-((ui8Bits==0)?0:1))*8)+ui8Bits; diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index d8b27d7..0209397 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -81,7 +81,7 @@ bool pn53x_transceive_check_ack_frame_callback(nfc_device_t* pnd, const byte_t *pbtRxFrame, const size_t szRxFrameLen); bool pn53x_transceive_check_error_frame_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); +bool pn53x_get_reg(nfc_device_t* pnd, uint16_t ui16Reg, uint8_t* ui8Value); bool pn53x_set_reg(nfc_device_t* pnd, uint16_t ui16Reg, uint8_t ui8SybmolMask, uint8_t ui8Value); bool pn53x_set_parameters(nfc_device_t* pnd, uint8_t ui8Value); bool pn53x_set_tx_bits(nfc_device_t* pnd, uint8_t ui8Bits); diff --git a/test/Makefile.am b/test/Makefile.am index a55e5cf..4dde6bb 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -8,13 +8,17 @@ TESTS = run-test.sh TESTS_ENVIRONMENT = NO_MAKE=yes CUTTER="$(CUTTER)" noinst_LTLIBRARIES = \ - test_access_storm.la + test_access_storm.la \ + test_register_endianness.la AM_LDFLAGS = -module -rpath $(libdir) -avoid-version -no-undefined test_access_storm_la_SOURCES = test_access_storm.c test_access_storm_la_LIBADD = $(top_builddir)/libnfc/libnfc.la +test_register_endianness_la_SOURCES = test_register_endianness.c +test_register_endianness_la_LIBADD = $(top_builddir)/libnfc/libnfc.la + echo-cutter: @echo $(CUTTER) diff --git a/test/test_register_endianness.c b/test/test_register_endianness.c new file mode 100644 index 0000000..5c4665b --- /dev/null +++ b/test/test_register_endianness.c @@ -0,0 +1,39 @@ +#include + +#include + +#define MAX_DEVICE_COUNT 1 +#define MAX_TARGET_COUNT 1 + +bool pn53x_get_reg(nfc_device_t* pnd, uint16_t ui16Reg, uint8_t* ui8Value); + +void +test_register_endianness (void) +{ + nfc_device_desc_t devices[MAX_DEVICE_COUNT]; + size_t device_count; + bool res; + + nfc_list_devices (devices, MAX_DEVICE_COUNT, &device_count); + if (!device_count) + cut_omit ("No NFC device found"); + + nfc_device_t *device; + + device = nfc_connect (&(devices[0])); + cut_assert_not_null (device, cut_message ("nfc_connect")); + + uint8_t value; + + /* Read valid XRAM memory */ + res = pn53x_get_reg (device, 0xF0FF, &value); + cut_assert_true (res, cut_message ("read register 0xF0FF")); + nfc_perror (device, "get"); + + /* Read invalid SFR register */ + res = pn53x_get_reg (device, 0xFFF0, &value); + cut_assert_false (res, cut_message ("read register 0xFFF0")); + nfc_perror (device, "get"); + + nfc_disconnect (device); +}