pn53x_wrap_frame() and pn53x_unwrap_frame() functions return now frame length in bits on success and libnfc error code on failure.
This commit is contained in:
parent
0de1136037
commit
61c3e5b814
2 changed files with 30 additions and 23 deletions
|
@ -223,28 +223,29 @@ pn53x_set_tx_bits (struct nfc_device *pnd, const uint8_t ui8Bits)
|
||||||
return NFC_SUCCESS;
|
return NFC_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
int
|
||||||
pn53x_wrap_frame (const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar,
|
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 btFrame;
|
||||||
uint8_t btData;
|
uint8_t btData;
|
||||||
uint32_t uiBitPos;
|
uint32_t uiBitPos;
|
||||||
uint32_t uiDataPos = 0;
|
uint32_t uiDataPos = 0;
|
||||||
size_t szBitsLeft = szTxBits;
|
size_t szBitsLeft = szTxBits;
|
||||||
|
size_t szFrameBits = 0;
|
||||||
|
|
||||||
// Make sure we should frame at least something
|
// Make sure we should frame at least something
|
||||||
if (szBitsLeft == 0)
|
if (szBitsLeft == 0)
|
||||||
return false;
|
return NFC_ECHIP;
|
||||||
|
|
||||||
// Handle a short response (1byte) as a special case
|
// Handle a short response (1byte) as a special case
|
||||||
if (szBitsLeft < 9) {
|
if (szBitsLeft < 9) {
|
||||||
*pbtFrame = *pbtTx;
|
*pbtFrame = *pbtTx;
|
||||||
*pszFrameBits = szTxBits;
|
szFrameBits = szTxBits;
|
||||||
return true;
|
return szFrameBits;
|
||||||
}
|
}
|
||||||
// We start by calculating the frame length in bits
|
// 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
|
// Parse the data bytes and add the parity bits
|
||||||
// This is really a sensitive process, mirror the frame bytes and append 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++;
|
uiDataPos++;
|
||||||
// Test if we are done
|
// Test if we are done
|
||||||
if (szBitsLeft < 9)
|
if (szBitsLeft < 9)
|
||||||
return true;
|
return szFrameBits;
|
||||||
szBitsLeft -= 8;
|
szBitsLeft -= 8;
|
||||||
}
|
}
|
||||||
// Every 8 data bytes we lose one frame byte to the parities
|
// 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
|
int
|
||||||
pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t *pbtRx, size_t *pszRxBits,
|
pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t *pbtRx, uint8_t *pbtRxPar)
|
||||||
uint8_t *pbtRxPar)
|
|
||||||
{
|
{
|
||||||
uint8_t btFrame;
|
uint8_t btFrame;
|
||||||
uint8_t btData;
|
uint8_t btData;
|
||||||
|
@ -289,19 +289,20 @@ pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t *
|
||||||
uint32_t uiDataPos = 0;
|
uint32_t uiDataPos = 0;
|
||||||
uint8_t *pbtFramePos = (uint8_t *) pbtFrame;
|
uint8_t *pbtFramePos = (uint8_t *) pbtFrame;
|
||||||
size_t szBitsLeft = szFrameBits;
|
size_t szBitsLeft = szFrameBits;
|
||||||
|
size_t szRxBits = 0;
|
||||||
|
|
||||||
// Make sure we should frame at least something
|
// Make sure we should frame at least something
|
||||||
if (szBitsLeft == 0)
|
if (szBitsLeft == 0)
|
||||||
return false;
|
return NFC_ECHIP;
|
||||||
|
|
||||||
// Handle a short response (1byte) as a special case
|
// Handle a short response (1byte) as a special case
|
||||||
if (szBitsLeft < 9) {
|
if (szBitsLeft < 9) {
|
||||||
*pbtRx = *pbtFrame;
|
*pbtRx = *pbtFrame;
|
||||||
*pszRxBits = szFrameBits;
|
szRxBits = szFrameBits;
|
||||||
return true;
|
return szRxBits;
|
||||||
}
|
}
|
||||||
// Calculate the data length in bits
|
// 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
|
// 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
|
// 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++;
|
uiDataPos++;
|
||||||
// Test if we are done
|
// Test if we are done
|
||||||
if (szBitsLeft < 9)
|
if (szBitsLeft < 9)
|
||||||
return true;
|
return szRxBits;
|
||||||
szBitsLeft -= 9;
|
szBitsLeft -= 9;
|
||||||
}
|
}
|
||||||
// Every 8 data bytes we lose one frame byte to the parities
|
// 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
|
// Check if we should prepare the parity bits ourself
|
||||||
if (!pnd->bPar) {
|
if (!pnd->bPar) {
|
||||||
// Convert data with parity to a frame
|
// 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 {
|
} else {
|
||||||
szFrameBits = szTxBits;
|
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
|
// Check if we should recover the parity bits ourself
|
||||||
if (!pnd->bPar) {
|
if (!pnd->bPar) {
|
||||||
// Unwrap the response frame
|
// 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 {
|
} else {
|
||||||
// Save the received bits
|
// Save the received bits
|
||||||
*pszRxBits = szFrameBits;
|
*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
|
// Check if we should recover the parity bits ourself
|
||||||
if (!pnd->bPar) {
|
if (!pnd->bPar) {
|
||||||
// Unwrap the response frame
|
// 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 {
|
} else {
|
||||||
// Save the received bits
|
// Save the received bits
|
||||||
*pszRxBits = szFrameBits;
|
*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
|
// Check if we should prepare the parity bits ourself
|
||||||
if (!pnd->bPar) {
|
if (!pnd->bPar) {
|
||||||
// Convert data with parity to a frame
|
// 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 {
|
} else {
|
||||||
szFrameBits = szTxBits;
|
szFrameBits = szTxBits;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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_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);
|
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,
|
int pn53x_wrap_frame (const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtFrame);
|
||||||
size_t *pszFrameBits);
|
int pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t *pbtRx, uint8_t *pbtRxPar);
|
||||||
bool pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t *pbtRx, size_t *pszRxBits,
|
|
||||||
uint8_t *pbtRxPar);
|
|
||||||
bool pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData,
|
bool pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData,
|
||||||
pn53x_type chip_type, nfc_modulation_type nmt,
|
pn53x_type chip_type, nfc_modulation_type nmt,
|
||||||
nfc_target_info *pnti);
|
nfc_target_info *pnti);
|
||||||
|
|
Loading…
Add table
Reference in a new issue