- Detect errors in pn53x_get_reg();

- Add a test case to confirm an endianness problem on little-endian machines.
This commit is contained in:
Romain Tartiere 2010-08-28 14:27:33 +00:00
parent af291c5a16
commit 1130036bf3
4 changed files with 62 additions and 10 deletions

View file

@ -156,27 +156,29 @@ bool pn53x_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxL
return (0 == pnd->iLastError); 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; size_t szValueLen = 1;
byte_t abtCmd[sizeof(pncmd_get_register)]; byte_t abtCmd[sizeof(pncmd_get_register)];
memcpy(abtCmd,pncmd_get_register,sizeof(pncmd_get_register)); memcpy(abtCmd,pncmd_get_register,sizeof(pncmd_get_register));
abtCmd[2] = ui16Reg >> 8; abtCmd[2] = ui16Reg >> 8;
abtCmd[3] = ui16Reg & 0xff; abtCmd[3] = ui16Reg & 0xff;
pn53x_transceive(pnd,abtCmd,4,&ui8Value,&szValueLen); return pn53x_transceive(pnd,abtCmd,4,ui8Value,&szValueLen);
return ui8Value;
} }
bool pn53x_set_reg(nfc_device_t* pnd, uint16_t ui16Reg, uint8_t ui8SybmolMask, uint8_t ui8Value) 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)]; byte_t abtCmd[sizeof(pncmd_set_register)];
memcpy(abtCmd,pncmd_set_register,sizeof(pncmd_set_register)); memcpy(abtCmd,pncmd_set_register,sizeof(pncmd_set_register));
abtCmd[2] = ui16Reg >> 8; abtCmd[2] = ui16Reg >> 8;
abtCmd[3] = ui16Reg & 0xff; 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); 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 szRxLen;
size_t szFrameBits = 0; size_t szFrameBits = 0;
size_t szFrameBytes = 0; size_t szFrameBytes = 0;
uint8_t ui8rcc;
uint8_t ui8Bits = 0; uint8_t ui8Bits = 0;
byte_t abtCmd[sizeof(pncmd_initiator_exchange_raw_data)]; 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; if (!pn53x_transceive(pnd,abtCmd,szFrameBytes+2,abtRx,&szRxLen)) return false;
// Get the last bit-count that is stored in the received byte // 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 // Recover the real frame length in bits
szFrameBits = ((szRxLen-1-((ui8Bits==0)?0:1))*8)+ui8Bits; 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]; byte_t abtRx[MAX_FRAME_LEN];
size_t szRxLen; size_t szRxLen;
uint8_t ui8rcc;
uint8_t ui8Bits; uint8_t ui8Bits;
// Save the current configuration settings // Save the current configuration settings
bool bCrc = pnd->bCrc; 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; if (!pn53x_transceive(pnd,abtCmd,39,abtRx,&szRxLen)) return false;
// Get the last bit-count that is stored in the received byte // 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 // 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; *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]; byte_t abtRx[MAX_FRAME_LEN];
size_t szRxLen; size_t szRxLen;
size_t szFrameBits; size_t szFrameBits;
uint8_t ui8rcc;
uint8_t ui8Bits; uint8_t ui8Bits;
// Try to gather a received frame from the reader // Try to gather a received frame from the reader
if (!pn53x_transceive(pnd,pncmd_target_receive,2,abtRx,&szRxLen)) return false; if (!pn53x_transceive(pnd,pncmd_target_receive,2,abtRx,&szRxLen)) return false;
// Get the last bit-count that is stored in the received byte // 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 // Recover the real frame length in bits
szFrameBits = ((szRxLen-1-((ui8Bits==0)?0:1))*8)+ui8Bits; szFrameBits = ((szRxLen-1-((ui8Bits==0)?0:1))*8)+ui8Bits;

View file

@ -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_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_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); 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_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_parameters(nfc_device_t* pnd, uint8_t ui8Value);
bool pn53x_set_tx_bits(nfc_device_t* pnd, uint8_t ui8Bits); bool pn53x_set_tx_bits(nfc_device_t* pnd, uint8_t ui8Bits);

View file

@ -8,13 +8,17 @@ TESTS = run-test.sh
TESTS_ENVIRONMENT = NO_MAKE=yes CUTTER="$(CUTTER)" TESTS_ENVIRONMENT = NO_MAKE=yes CUTTER="$(CUTTER)"
noinst_LTLIBRARIES = \ noinst_LTLIBRARIES = \
test_access_storm.la test_access_storm.la \
test_register_endianness.la
AM_LDFLAGS = -module -rpath $(libdir) -avoid-version -no-undefined AM_LDFLAGS = -module -rpath $(libdir) -avoid-version -no-undefined
test_access_storm_la_SOURCES = test_access_storm.c test_access_storm_la_SOURCES = test_access_storm.c
test_access_storm_la_LIBADD = $(top_builddir)/libnfc/libnfc.la 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:
@echo $(CUTTER) @echo $(CUTTER)

View file

@ -0,0 +1,39 @@
#include <cutter.h>
#include <nfc/nfc.h>
#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);
}