Factorise code.
Avoid redundant code in PN53x usb and uart drivers. Since it makes sense to report errors at the nfc_device_t level, pass it directly to pn53x_transceive(). Programs using the libnfc MAY use pn53x_transceive() to communicate with a NFC device, and SHALL not use anymore pnd->pdc->transceive(). Code in the library itself SHOULD avoid calling pnd->pdc->transceive(), so such construct have been updated accordingly.
This commit is contained in:
parent
301d692e8a
commit
79aeaa6287
15 changed files with 80 additions and 70 deletions
|
|
@ -29,6 +29,7 @@
|
|||
#endif // HAVE_CONFIG_H
|
||||
|
||||
#include "../drivers.h"
|
||||
#include "../chips/pn53x.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue