diff --git a/examples/nfc-sam.c b/examples/nfc-sam.c index d2581ec..fb32f5e 100644 --- a/examples/nfc-sam.c +++ b/examples/nfc-sam.c @@ -45,6 +45,7 @@ #include #include #include "nfc-utils.h" +#include "chips/pn53x.h" #define MAX_FRAME_LEN 264 #define TIMEOUT 60 // secs. @@ -80,7 +81,7 @@ bool sam_connection(nfc_device_t* pnd, int mode) break; } - if (!pnd->pdc->transceive(pnd->nds,pncmd_sam_config,szCmd,abtRx,&szRxLen)) { + if (!pn53x_transceive(pnd,pncmd_sam_config,szCmd,abtRx,&szRxLen)) { ERR("%s %d", "Unable to execute SAMConfiguration command with mode byte:", mode); return false; } diff --git a/examples/pn53x-diagnose.c b/examples/pn53x-diagnose.c index 4436503..f3fc0d6 100644 --- a/examples/pn53x-diagnose.c +++ b/examples/pn53x-diagnose.c @@ -78,19 +78,19 @@ int main(int argc, const char* argv[]) printf("NFC device [%s] connected.\n",pnd->acName); - result = pnd->pdc->transceive(pnd->nds,pncmd_diagnose_communication_line_test,sizeof(pncmd_diagnose_communication_line_test),abtRx,&szRxLen); + result = pn53x_transceive(pnd,pncmd_diagnose_communication_line_test,sizeof(pncmd_diagnose_communication_line_test),abtRx,&szRxLen); if ( result ) { result = (memcmp(pncmd_diagnose_communication_line_test+2, abtRx, sizeof(pncmd_diagnose_communication_line_test)-2 ) == 0); } printf(" Communication line test: %s\n", result ? "OK" : "Failed"); - result = pnd->pdc->transceive(pnd->nds,pncmd_diagnose_rom_test,sizeof(pncmd_diagnose_rom_test),abtRx,&szRxLen); + result = pn53x_transceive(pnd,pncmd_diagnose_rom_test,sizeof(pncmd_diagnose_rom_test),abtRx,&szRxLen); if ( result ) { result = ((szRxLen == 1) && (abtRx[0] == 0x00)); } printf(" ROM test: %s\n", result ? "OK" : "Failed"); - result = pnd->pdc->transceive(pnd->nds,pncmd_diagnose_ram_test,sizeof(pncmd_diagnose_ram_test),abtRx,&szRxLen); + result = pn53x_transceive(pnd,pncmd_diagnose_ram_test,sizeof(pncmd_diagnose_ram_test),abtRx,&szRxLen); if ( result ) { result = ((szRxLen == 1) && (abtRx[0] == 0x00)); } diff --git a/include/nfc/nfc-types.h b/include/nfc/nfc-types.h index 2b0c4a8..45df928 100644 --- a/include/nfc/nfc-types.h +++ b/include/nfc/nfc-types.h @@ -118,7 +118,7 @@ struct driver_callbacks { /** Connect callback */ nfc_device_t* (*connect)(const nfc_device_desc_t* pndd); /** Transceive callback */ - bool (*transceive)(const nfc_device_spec_t nds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen); + bool (*transceive)(nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen); /** Disconnect callback */ void (*disconnect)(nfc_device_t* pnd); }; diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 6f00b01..a106dba 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -60,8 +60,8 @@ NFC_EXPORT void nfc_disconnect(nfc_device_t* pnd); NFC_EXPORT bool nfc_configure(nfc_device_t* pnd, const nfc_device_option_t ndo, const bool bEnable); /* NFC initiator: act as "reader" */ -NFC_EXPORT bool nfc_initiator_init(const nfc_device_t* pnd); -NFC_EXPORT bool nfc_initiator_select_passive_target(const nfc_device_t* pnd, const nfc_modulation_t nmInitModulation, const byte_t* pbtInitData, const size_t szInitDataLen, nfc_target_info_t* pti); +NFC_EXPORT bool nfc_initiator_init(nfc_device_t* pnd); +NFC_EXPORT bool nfc_initiator_select_passive_target(nfc_device_t* pnd, const nfc_modulation_t nmInitModulation, const byte_t* pbtInitData, const size_t szInitDataLen, nfc_target_info_t* pti); NFC_EXPORT bool nfc_initiator_list_passive_targets(nfc_device_t* pnd, const nfc_modulation_t nmInitModulation, nfc_target_info_t anti[], const size_t szTargets, size_t *pszTargetFound ); NFC_EXPORT bool nfc_initiator_poll_targets(nfc_device_t* pnd, const nfc_target_type_t* pnttTargetTypes, const size_t szTargetTypes, const byte_t btPollNr, const byte_t btPeriod, nfc_target_t* pntTargets, size_t* pszTargetFound); NFC_EXPORT bool nfc_initiator_select_dep_target(nfc_device_t* pnd, const nfc_modulation_t nmInitModulation, const byte_t* pbtPidData, const size_t szPidDataLen, const byte_t* pbtNFCID3i, const size_t szNFCID3iDataLen, const byte_t *pbtGbData, const size_t szGbDataLen, nfc_target_info_t* pti); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index dd77460..df60767 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -26,8 +26,12 @@ #include "config.h" #endif // HAVE_CONFIG_H -#include #include +#include +#include + +// FIXME: WTF are doing debug macros in this file? +#include #include "pn53x.h" #include "../mirror-subr.h" @@ -60,6 +64,30 @@ const byte_t pncmd_target_receive [ 2] = { 0xD4,0x88 }; const byte_t pncmd_target_send [264] = { 0xD4,0x90 }; const byte_t pncmd_target_get_status [ 2] = { 0xD4,0x8A }; +static const byte_t pn53x_ack_frame[] = { 0x00,0x00,0xff,0x00,0xff,0x00 }; +static const byte_t pn53x_nack_frame[] = { 0x00,0x00,0xff,0xff,0x00,0x00 }; + +bool pn53x_transceive_callback(nfc_device_t* pnd, const byte_t *pbtRxFrame, const size_t szRxFrameLen) +{ + (void) pnd; // I guess we will want to set some error here at some point + + if (szRxFrameLen == sizeof (pn53x_ack_frame)) { + if (0 == memcmp (pbtRxFrame, pn53x_ack_frame, sizeof (pn53x_ack_frame))) { + DBG("%s", "PN53x ACKed"); + return true; + } else if (0 == memcmp (pbtRxFrame, pn53x_nack_frame, sizeof (pn53x_nack_frame))) { + DBG("%s", "PN53x NACKed"); + return false; + } + } + ERR("%s", "Unexpected PN53x reply!"); +#if defined(DEBUG) + // coredump so that we can have a backtrace about how this code was reached. + abort(); +#endif + return false; +} + bool pn53x_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen) { byte_t abtRx[MAX_FRAME_LEN]; @@ -74,7 +102,7 @@ bool pn53x_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxL *pszRxLen = MAX_FRAME_LEN; // Call the tranceive callback function of the current device - if (!pnd->pdc->transceive(pnd->nds,pbtTx,szTxLen,pbtRx,pszRxLen)) return false; + if (!pnd->pdc->transceive(pnd,pbtTx,szTxLen,pbtRx,pszRxLen)) return false; switch (pbtTx[1]) { case 0x16: // PowerDown @@ -102,7 +130,7 @@ bool pn53x_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxL return (0 == pnd->iErrorCode); } -byte_t pn53x_get_reg(const nfc_device_t* pnd, uint16_t ui16Reg) +byte_t pn53x_get_reg(nfc_device_t* pnd, uint16_t ui16Reg) { uint8_t ui8Value; size_t szValueLen = 1; @@ -111,12 +139,11 @@ byte_t pn53x_get_reg(const nfc_device_t* pnd, uint16_t ui16Reg) abtCmd[2] = ui16Reg >> 8; abtCmd[3] = ui16Reg & 0xff; - // We can not use pn53x_transceive() because abtRx[0] gives no status info - pnd->pdc->transceive(pnd->nds,abtCmd,4,&ui8Value,&szValueLen); + pn53x_transceive(pnd,abtCmd,4,&ui8Value,&szValueLen); return ui8Value; } -bool pn53x_set_reg(const 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) { byte_t abtCmd[sizeof(pncmd_set_register)]; memcpy(abtCmd,pncmd_set_register,sizeof(pncmd_set_register)); @@ -124,21 +151,19 @@ bool pn53x_set_reg(const nfc_device_t* pnd, uint16_t ui16Reg, uint8_t ui8SybmolM abtCmd[2] = ui16Reg >> 8; abtCmd[3] = ui16Reg & 0xff; abtCmd[4] = ui8Value | (pn53x_get_reg(pnd,ui16Reg) & (~ui8SybmolMask)); - // We can not use pn53x_transceive() because abtRx[0] gives no status info - return pnd->pdc->transceive(pnd->nds,abtCmd,5,NULL,NULL); + return pn53x_transceive(pnd,abtCmd,5,NULL,NULL); } -bool pn53x_set_parameters(const nfc_device_t* pnd, uint8_t ui8Value) +bool pn53x_set_parameters(nfc_device_t* pnd, uint8_t ui8Value) { byte_t abtCmd[sizeof(pncmd_set_parameters)]; memcpy(abtCmd,pncmd_set_parameters,sizeof(pncmd_set_parameters)); abtCmd[2] = ui8Value; - // We can not use pn53x_transceive() because abtRx[0] gives no status info - return pnd->pdc->transceive(pnd->nds,abtCmd,3,NULL,NULL); + return pn53x_transceive(pnd,abtCmd,3,NULL,NULL); } -bool pn53x_set_tx_bits(const nfc_device_t* pnd, uint8_t ui8Bits) +bool pn53x_set_tx_bits(nfc_device_t* pnd, uint8_t ui8Bits) { // Test if we need to update the transmission bits register setting if (pnd->ui8TxBits != ui8Bits) @@ -307,7 +332,7 @@ pn53x_decode_target_data(const byte_t* pbtRawData, size_t szDataLen, nfc_chip_t * @note To decode theses TargetData[n], there is @fn pn53x_decode_target_data */ bool -pn53x_InListPassiveTarget(const nfc_device_t* pnd, +pn53x_InListPassiveTarget(nfc_device_t* pnd, const nfc_modulation_t nmInitModulation, const byte_t szMaxTargets, const byte_t* pbtInitiatorData, const size_t szInitiatorDataLen, byte_t* pbtTargetsData, size_t* pszTargetsData) @@ -324,8 +349,7 @@ pn53x_InListPassiveTarget(const nfc_device_t* pnd, // Try to find a tag, call the tranceive callback function of the current device size_t szRxLen = MAX_FRAME_LEN; - // We can not use pn53x_transceive() because abtRx[0] gives no status info - if(pnd->pdc->transceive(pnd->nds,abtCmd,4+szInitiatorDataLen,pbtTargetsData,&szRxLen)) { + if(pn53x_transceive(pnd,abtCmd,4+szInitiatorDataLen,pbtTargetsData,&szRxLen)) { *pszTargetsData = szRxLen; return true; } else { diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index efef7d7..1f1a578 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -69,16 +69,17 @@ #define RFCI_ANALOG_TYPE_B 0x0C // 3 #define RFCI_ANALOG_TYPE_14443_4 0x0D // 9 +bool pn53x_transceive_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(const nfc_device_t* pnd, uint16_t ui16Reg); -bool pn53x_set_reg(const nfc_device_t* pnd, uint16_t ui16Reg, uint8_t ui8SybmolMask, uint8_t ui8Value); -bool pn53x_set_parameters(const nfc_device_t* pnd, uint8_t ui8Value); -bool pn53x_set_tx_bits(const nfc_device_t* pnd, uint8_t ui8Bits); +byte_t pn53x_get_reg(nfc_device_t* pnd, uint16_t ui16Reg); +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); bool pn53x_wrap_frame(const byte_t* pbtTx, const size_t szTxBits, const byte_t* pbtTxPar, byte_t* pbtFrame, size_t* pszFrameBits); bool pn53x_unwrap_frame(const byte_t* pbtFrame, const size_t szFrameBits, byte_t* pbtRx, size_t* pszRxBits, byte_t* pbtRxPar); bool pn53x_decode_target_data(const byte_t* pbtRawData, size_t szDataLen, nfc_chip_t nc, nfc_target_type_t ntt, nfc_target_info_t* pnti); -bool pn53x_InListPassiveTarget(const nfc_device_t* pnd, const nfc_modulation_t nmInitModulation, const byte_t szMaxTargets, const byte_t* pbtInitiatorData, const size_t szInitiatorDataLen, byte_t* pbtTargetsData, size_t* pszTargetsData); +bool pn53x_InListPassiveTarget(nfc_device_t* pnd, const nfc_modulation_t nmInitModulation, const byte_t szMaxTargets, const byte_t* pbtInitiatorData, const size_t szInitiatorDataLen, byte_t* pbtTargetsData, size_t* pszTargetsData); bool pn53x_InDeselect(nfc_device_t* pnd, const uint8_t ui8Target); const char *pn53x_strerror (const nfc_device_t *pnd); diff --git a/libnfc/drivers/acr122.c b/libnfc/drivers/acr122.c index 47357e8..78a97b1 100644 --- a/libnfc/drivers/acr122.c +++ b/libnfc/drivers/acr122.c @@ -259,14 +259,14 @@ void acr122_disconnect(nfc_device_t* pnd) free(pnd); } -bool acr122_transceive(const nfc_device_spec_t nds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen) +bool acr122_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen) { byte_t abtRxCmd[5] = { 0xFF,0xC0,0x00,0x00 }; size_t szRxCmdLen = sizeof(abtRxCmd); byte_t abtRxBuf[ACR122_RESPONSE_LEN]; size_t szRxBufLen; byte_t abtTxBuf[ACR122_WRAP_LEN+ACR122_COMMAND_LEN] = { 0xFF, 0x00, 0x00, 0x00 }; - acr122_spec_t* pas = (acr122_spec_t*)nds; + acr122_spec_t* pas = (acr122_spec_t*)pnd->nds; // Make sure the command does not overflow the send buffer if (szTxLen > ACR122_COMMAND_LEN) return false; diff --git a/libnfc/drivers/acr122.h b/libnfc/drivers/acr122.h index 69ab71e..ac3b1c2 100644 --- a/libnfc/drivers/acr122.h +++ b/libnfc/drivers/acr122.h @@ -40,7 +40,7 @@ nfc_device_t* acr122_connect(const nfc_device_desc_t* pndd); void acr122_disconnect(nfc_device_t* pnd); // Callback function used by libnfc to transmit commands to the PN53X chip -bool acr122_transceive(const nfc_device_spec_t nds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen); +bool acr122_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen); // Various additional features this device supports char* acr122_firmware(const nfc_device_spec_t nds); diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 2d5fbd6..c6161d9 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -214,7 +214,7 @@ void arygon_disconnect(nfc_device_t* pnd) free(pnd); } -bool arygon_transceive(const nfc_device_spec_t nds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen) +bool arygon_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen) { byte_t abtTxBuf[BUFFER_LENGTH] = { DEV_ARYGON_PROTOCOL_TAMA, 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff" byte_t abtRxBuf[BUFFER_LENGTH]; @@ -244,12 +244,12 @@ bool arygon_transceive(const nfc_device_spec_t nds, const byte_t* pbtTx, const s #ifdef DEBUG PRINT_HEX("TX", abtTxBuf,szTxLen+8); #endif - if (!uart_send((serial_port)nds,abtTxBuf,szTxLen+8)) { + if (!uart_send((serial_port)pnd->nds,abtTxBuf,szTxLen+8)) { ERR("%s", "Unable to transmit data. (TX)"); return false; } - if (!uart_receive((serial_port)nds,abtRxBuf,&szRxBufLen)) { + if (!uart_receive((serial_port)pnd->nds,abtRxBuf,&szRxBufLen)) { ERR("%s", "Unable to receive data. (RX)"); return false; } @@ -279,7 +279,7 @@ bool arygon_transceive(const nfc_device_spec_t nds, const byte_t* pbtTx, const s if(szRxBufLen == 0) { // There was no more data than ACK frame, we need to wait next frame DBG("%s", "There was no more data than ACK frame, we need to wait next frame"); - while (!uart_receive((serial_port)nds,abtRxBuf,&szRxBufLen)) { + while (!uart_receive((serial_port)pnd->nds,abtRxBuf,&szRxBufLen)) { delay_ms(10); } } diff --git a/libnfc/drivers/arygon.h b/libnfc/drivers/arygon.h index 8d419ca..47d3a9f 100644 --- a/libnfc/drivers/arygon.h +++ b/libnfc/drivers/arygon.h @@ -36,7 +36,7 @@ nfc_device_t* arygon_connect(const nfc_device_desc_t* pndd); void arygon_disconnect(nfc_device_t* pnd); // Callback function used by libnfc to transmit commands to the PN53X chip -bool arygon_transceive(const nfc_device_spec_t nds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen); +bool arygon_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen); #endif // ! __NFC_DRIVER_ARYGON_H__ diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 981a7dc..e8013f1 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -29,6 +29,7 @@ #endif // HAVE_CONFIG_H #include "../drivers.h" +#include "../chips/pn53x.h" #include #include @@ -197,14 +198,12 @@ void pn532_uart_disconnect(nfc_device_t* pnd) free(pnd); } -bool pn532_uart_transceive(const nfc_device_spec_t nds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen) +bool pn532_uart_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen) { byte_t abtTxBuf[BUFFER_LENGTH] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff" byte_t abtRxBuf[BUFFER_LENGTH]; size_t szRxBufLen = BUFFER_LENGTH; size_t szPos; - const byte_t pn53x_ack_frame[] = { 0x00,0x00,0xff,0x00,0xff,0x00 }; - const byte_t pn53x_nack_frame[] = { 0x00,0x00,0xff,0xff,0x00,0x00 }; // Packet length = data length (len) + checksum (1) + end of stream marker (1) abtTxBuf[3] = szTxLen; @@ -226,12 +225,13 @@ bool pn532_uart_transceive(const nfc_device_spec_t nds, const byte_t* pbtTx, con #ifdef DEBUG PRINT_HEX("TX", abtTxBuf,szTxLen+7); #endif - if (!uart_send((serial_port)nds,abtTxBuf,szTxLen+7)) { + if (!uart_send((serial_port)pnd->nds,abtTxBuf,szTxLen+7)) { ERR("%s", "Unable to transmit data. (TX)"); return false; } - if (!uart_receive((serial_port)nds,abtRxBuf,&szRxBufLen)) { + szRxBufLen = 6; + if (!uart_receive((serial_port)pnd->nds,abtRxBuf,&szRxBufLen)) { ERR("%s", "Unable to receive data. (RX)"); return false; } @@ -240,31 +240,14 @@ bool pn532_uart_transceive(const nfc_device_spec_t nds, const byte_t* pbtTx, con PRINT_HEX("RX", abtRxBuf,szRxBufLen); #endif - if(szRxBufLen >= sizeof(pn53x_ack_frame)) { + if (!pn53x_transceive_callback(pnd, abtRxBuf, szRxBufLen)) + return false; - // Check if PN53x reply ACK - if(0!=memcmp(pn53x_ack_frame, abtRxBuf, sizeof(pn53x_ack_frame))) { - DBG("%s", "PN53x doesn't respond ACK frame."); - if (0==memcmp(pn53x_nack_frame, abtRxBuf, sizeof(pn53x_nack_frame))) { - ERR("%s", "PN53x reply NACK frame."); - // FIXME Handle NACK frame i.e. resend frame, PN53x doesn't received it correctly - } - return false; - } + szRxBufLen = BUFFER_LENGTH; - szRxBufLen -= sizeof(pn53x_ack_frame); - if(szRxBufLen) { - memmove(abtRxBuf, abtRxBuf+sizeof(pn53x_ack_frame), szRxBufLen); - } - } - - if(szRxBufLen == 0) { - // There was no more data than ACK frame, we need to wait next frame - DBG("%s", "There was no more data than ACK frame, we need to wait next frame"); - while (!uart_receive((serial_port)nds,abtRxBuf,&szRxBufLen)) { + while (!uart_receive((serial_port)pnd->nds,abtRxBuf,&szRxBufLen)) { delay_ms(10); } - } #ifdef DEBUG PRINT_HEX("RX", abtRxBuf,szRxBufLen); diff --git a/libnfc/drivers/pn532_uart.h b/libnfc/drivers/pn532_uart.h index d364f10..d192fa5 100644 --- a/libnfc/drivers/pn532_uart.h +++ b/libnfc/drivers/pn532_uart.h @@ -36,7 +36,7 @@ nfc_device_t* pn532_uart_connect(const nfc_device_desc_t* pndd); void pn532_uart_disconnect(nfc_device_t* pnd); // Callback function used by libnfc to transmit commands to the PN53X chip -bool pn532_uart_transceive(const nfc_device_spec_t nds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen); +bool pn532_uart_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen); #endif // ! __NFC_DRIVER_PN532_UART_H__ diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 1fd9d39..4c5156c 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -38,6 +38,7 @@ Thanks to d18c7db and Okko for example code #include #include "../drivers.h" +#include "../chips/pn53x.h" #include @@ -235,13 +236,13 @@ void pn53x_usb_disconnect(nfc_device_t* pnd) free(pnd); } -bool pn53x_usb_transceive(const nfc_device_spec_t nds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen) +bool pn53x_usb_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen) { size_t uiPos = 0; int ret = 0; byte_t abtTx[BUFFER_LENGTH] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff" byte_t abtRx[BUFFER_LENGTH]; - usb_spec_t* pus = (usb_spec_t*)nds; + usb_spec_t* pus = (usb_spec_t*)pnd->nds; // Packet length = data length (len) + checksum (1) + end of stream marker (1) abtTx[3] = szTxLen; @@ -282,8 +283,9 @@ bool pn53x_usb_transceive(const nfc_device_spec_t nds, const byte_t* pbtTx, cons PRINT_HEX("RX", abtRx,ret); #endif - if( ret == 6 ) - { + if (!pn53x_transceive_callback (pnd, abtRx, ret)) + return false; + ret = usb_bulk_read(pus->pudh, pus->uiEndPointIn, (char*)abtRx, BUFFER_LENGTH, USB_TIMEOUT); if( ret < 0 ) { @@ -294,7 +296,6 @@ bool pn53x_usb_transceive(const nfc_device_spec_t nds, const byte_t* pbtTx, cons #ifdef DEBUG PRINT_HEX("RX", abtRx,ret); #endif - } // When the answer should be ignored, just return a succesful result if(pbtRx == NULL || pszRxLen == NULL) return true; diff --git a/libnfc/drivers/pn53x_usb.h b/libnfc/drivers/pn53x_usb.h index 00a3091..e2c7ba8 100644 --- a/libnfc/drivers/pn53x_usb.h +++ b/libnfc/drivers/pn53x_usb.h @@ -37,5 +37,5 @@ typedef struct { nfc_device_t* pn53x_usb_connect(const nfc_device_desc_t* pndd,const char * target_name, int target_chip); void get_end_points(struct usb_device *dev, usb_spec_t* pus); void pn53x_usb_disconnect(nfc_device_t* pnd); -bool pn53x_usb_transceive(const nfc_device_spec_t nds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen); +bool pn53x_usb_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen); bool pn53x_usb_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *pszDeviceFound,usb_candidate_t candidates[], int num_candidates, char * target_name); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 6888ebd..72ca52c 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -318,7 +318,7 @@ bool nfc_configure(nfc_device_t* pnd, const nfc_device_option_t ndo, const bool * After initialization it can be used to communicate to passive RFID tags and active NFC devices. * The reader will act as initiator to communicate peer 2 peer (NFCIP) to other active NFC devices. */ -bool nfc_initiator_init(const nfc_device_t* pnd) +bool nfc_initiator_init(nfc_device_t* pnd) { // Make sure we are dealing with a active device if (!pnd->bActive) return false; @@ -412,7 +412,7 @@ bool nfc_initiator_select_dep_target(nfc_device_t* pnd, const nfc_modulation_t n * @note For every initial modulation type there is a different collection of information returned (in nfc_target_info_t pointer pti) They all fit in the data-type which is called nfc_target_info_t. This is a union which contains the tag information that belongs to the according initial modulation type. */ bool -nfc_initiator_select_passive_target(const nfc_device_t* pnd, +nfc_initiator_select_passive_target(nfc_device_t* pnd, const nfc_modulation_t nmInitModulation, const byte_t* pbtInitData, const size_t szInitDataLen, nfc_target_info_t* pnti)