From 61c3e5b8145ce5e50affdaf58a3fa5182eb0064c Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 4 Jan 2012 14:59:16 +0000 Subject: [PATCH] pn53x_wrap_frame() and pn53x_unwrap_frame() functions return now frame length in bits on success and libnfc error code on failure. --- libnfc/chips/pn53x.c | 47 ++++++++++++++++++++++++++------------------ libnfc/chips/pn53x.h | 6 ++---- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 5ef63f2..8be568b 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -223,28 +223,29 @@ pn53x_set_tx_bits (struct nfc_device *pnd, const uint8_t ui8Bits) return NFC_SUCCESS; } -bool +int pn53x_wrap_frame (const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, - uint8_t *pbtFrame, size_t *pszFrameBits) + uint8_t *pbtFrame) { uint8_t btFrame; uint8_t btData; uint32_t uiBitPos; uint32_t uiDataPos = 0; size_t szBitsLeft = szTxBits; + size_t szFrameBits = 0; // Make sure we should frame at least something if (szBitsLeft == 0) - return false; + return NFC_ECHIP; // Handle a short response (1byte) as a special case if (szBitsLeft < 9) { *pbtFrame = *pbtTx; - *pszFrameBits = szTxBits; - return true; + szFrameBits = szTxBits; + return szFrameBits; } // We start by calculating the frame length in bits - *pszFrameBits = szTxBits + (szTxBits / 8); + szFrameBits = szTxBits + (szTxBits / 8); // Parse the data bytes and add the parity bits // This is really a sensitive process, mirror the frame bytes and append parity bits @@ -271,7 +272,7 @@ pn53x_wrap_frame (const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pb uiDataPos++; // Test if we are done if (szBitsLeft < 9) - return true; + return szFrameBits; szBitsLeft -= 8; } // Every 8 data bytes we lose one frame byte to the parities @@ -279,9 +280,8 @@ pn53x_wrap_frame (const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pb } } -bool -pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t *pbtRx, size_t *pszRxBits, - uint8_t *pbtRxPar) +int +pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t *pbtRx, uint8_t *pbtRxPar) { uint8_t btFrame; uint8_t btData; @@ -289,19 +289,20 @@ pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t * uint32_t uiDataPos = 0; uint8_t *pbtFramePos = (uint8_t *) pbtFrame; size_t szBitsLeft = szFrameBits; + size_t szRxBits = 0; // Make sure we should frame at least something if (szBitsLeft == 0) - return false; + return NFC_ECHIP; // Handle a short response (1byte) as a special case if (szBitsLeft < 9) { *pbtRx = *pbtFrame; - *pszRxBits = szFrameBits; - return true; + szRxBits = szFrameBits; + return szRxBits; } // Calculate the data length in bits - *pszRxBits = szFrameBits - (szFrameBits / 9); + szRxBits = szFrameBits - (szFrameBits / 9); // Parse the frame bytes, remove the parity bits and store them in the parity array // This process is the reverse of WrapFrame(), look there for more info @@ -318,7 +319,7 @@ pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t * uiDataPos++; // Test if we are done if (szBitsLeft < 9) - return true; + return szRxBits; szBitsLeft -= 9; } // Every 8 data bytes we lose one frame byte to the parities @@ -1121,7 +1122,9 @@ pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, c // Check if we should prepare the parity bits ourself if (!pnd->bPar) { // Convert data with parity to a frame - pn53x_wrap_frame (pbtTx, szTxBits, pbtTxPar, abtCmd + 1, &szFrameBits); + if ((res = pn53x_wrap_frame (pbtTx, szTxBits, pbtTxPar, abtCmd + 1)) < 0) + return res; + szFrameBits = res; } else { szFrameBits = szTxBits; } @@ -1160,7 +1163,9 @@ pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, c // Check if we should recover the parity bits ourself if (!pnd->bPar) { // Unwrap the response frame - pn53x_unwrap_frame (abtRx + 1, szFrameBits, pbtRx, pszRxBits, pbtRxPar); + if ((res = pn53x_unwrap_frame (abtRx + 1, szFrameBits, pbtRx, pbtRxPar)) < 0) + return res; + *pszRxBits = res; } else { // Save the received bits *pszRxBits = szFrameBits; @@ -1773,7 +1778,9 @@ pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx // Check if we should recover the parity bits ourself if (!pnd->bPar) { // Unwrap the response frame - pn53x_unwrap_frame (abtRx + 1, szFrameBits, pbtRx, pszRxBits, pbtRxPar); + if ((res = pn53x_unwrap_frame (abtRx + 1, szFrameBits, pbtRx, pbtRxPar)) < 0) + return res; + *pszRxBits = res; } else { // Save the received bits *pszRxBits = szFrameBits; @@ -1845,7 +1852,9 @@ pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size // Check if we should prepare the parity bits ourself if (!pnd->bPar) { // Convert data with parity to a frame - pn53x_wrap_frame (pbtTx, szTxBits, pbtTxPar, abtCmd + 1, &szFrameBits); + if ((res = pn53x_wrap_frame (pbtTx, szTxBits, pbtTxPar, abtCmd + 1)) < 0) + return res; + szFrameBits = res; } else { szFrameBits = szTxBits; } diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 8fd96ab..85b233c 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -268,10 +268,8 @@ int pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const siz int pn53x_set_parameters (struct nfc_device *pnd, const uint8_t ui8Value, const bool bEnable); int 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, - uint8_t *pbtRxPar); +int pn53x_wrap_frame (const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtFrame); +int pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t *pbtRx, uint8_t *pbtRxPar); bool pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData, pn53x_type chip_type, nfc_modulation_type nmt, nfc_target_info *pnti);