diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 47264ff..50a5834 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -967,3 +967,59 @@ bool pn53x_target_init(nfc_device_t* pnd, byte_t* pbtRx, size_t* pszRxBits) return true; } + +bool pn53x_target_receive_bits(nfc_device_t* pnd, byte_t* pbtRx, size_t* pszRxBits, byte_t* pbtRxPar) +{ + byte_t abtRx[MAX_FRAME_LEN]; + size_t szRxLen; + size_t szFrameBits; + uint8_t ui8Bits; + + pnd->iLastError = 0; + + + // 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; + + // Recover the real frame length in bits + szFrameBits = ((szRxLen-1-((ui8Bits==0)?0:1))*8)+ui8Bits; + + // Ignore the status byte from the PN53X here, it was checked earlier in pn53x_transceive() + // 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); + } else { + // Save the received bits + *pszRxBits = szFrameBits; + // Copy the received bytes + memcpy(pbtRx,abtRx+1,szRxLen-1); + } + // Everyting seems ok, return true + return true; +} + +bool pn53x_target_receive_bytes(nfc_device_t* pnd, byte_t* pbtRx, size_t* pszRxLen) +{ + byte_t abtRx[MAX_FRAME_LEN]; + size_t szRxLen; + + pnd->iLastError = 0; + + + // Try to gather a received frame from the reader + if (!pn53x_transceive(pnd,pncmd_target_receive,2,abtRx,&szRxLen)) return false; + + // Save the received byte count + *pszRxLen = szRxLen-1; + + // Copy the received bytes + memcpy(pbtRx,abtRx+1,*pszRxLen); + + // Everyting seems ok, return true + return true; +} diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 2621b6e..298a283 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -104,6 +104,8 @@ bool pn53x_transceive_bytes(nfc_device_t* pnd, const byte_t* pbtTx, const size_t bool pn53x_target_receive_dep_bytes(nfc_device_t* pnd, byte_t* pbtRx, size_t* pszRxLen); bool pn53x_target_send_dep_bytes(nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxLen); bool pn53x_target_init(nfc_device_t* pnd, byte_t* pbtRx, size_t* pszRxBits); +bool pn53x_target_receive_bits(nfc_device_t* pnd, byte_t* pbtRx, size_t* pszRxBits, byte_t* pbtRxPar); +bool pn53x_target_receive_bytes(nfc_device_t* pnd, byte_t* pbtRx, size_t* pszRxLen); const char *pn53x_strerror (const nfc_device_t *pnd); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 7b87e3b..4892ca3 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -72,7 +72,7 @@ nfc_device_desc_t * nfc_pick_device (void); // extern const byte_t pncmd_target_set_data [264]; // extern const byte_t pncmd_target_init [ 39]; // extern const byte_t pncmd_target_virtual_card [ 4]; -extern const byte_t pncmd_target_receive [ 2]; +// extern const byte_t pncmd_target_receive [ 2]; extern const byte_t pncmd_target_send [264]; // extern const byte_t pncmd_target_get_status [ 2]; @@ -538,37 +538,7 @@ bool nfc_target_init(nfc_device_t* pnd, byte_t* pbtRx, size_t* pszRxBits) */ bool nfc_target_receive_bits(nfc_device_t* pnd, byte_t* pbtRx, size_t* pszRxBits, byte_t* pbtRxPar) { - byte_t abtRx[MAX_FRAME_LEN]; - size_t szRxLen; - size_t szFrameBits; - uint8_t ui8Bits; - - pnd->iLastError = 0; - - - // 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; - - // Recover the real frame length in bits - szFrameBits = ((szRxLen-1-((ui8Bits==0)?0:1))*8)+ui8Bits; - - // Ignore the status byte from the PN53X here, it was checked earlier in pn53x_transceive() - // 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); - } else { - // Save the received bits - *pszRxBits = szFrameBits; - // Copy the received bytes - memcpy(pbtRx,abtRx+1,szRxLen-1); - } - // Everyting seems ok, return true - return true; + return pn53x_target_receive_bits (pnd, pbtRx, pszRxBits, pbtRxPar); } /** @@ -590,23 +560,7 @@ bool nfc_target_receive_dep_bytes(nfc_device_t* pnd, byte_t* pbtRx, size_t* pszR */ bool nfc_target_receive_bytes(nfc_device_t* pnd, byte_t* pbtRx, size_t* pszRxLen) { - byte_t abtRx[MAX_FRAME_LEN]; - size_t szRxLen; - - pnd->iLastError = 0; - - - // Try to gather a received frame from the reader - if (!pn53x_transceive(pnd,pncmd_target_receive,2,abtRx,&szRxLen)) return false; - - // Save the received byte count - *pszRxLen = szRxLen-1; - - // Copy the received bytes - memcpy(pbtRx,abtRx+1,*pszRxLen); - - // Everyting seems ok, return true - return true; + return pn53x_target_receive_bytes(pnd, pbtRx, pszRxLen); } /**