Rename chip_type to nfc_chip_t.

Rename dev_spec to nfc_device_spec_t.
Update configure.ac and autotools related files.
This commit is contained in:
Romuald Conty 2009-11-18 10:52:13 +00:00
parent 8a579c3aab
commit 2a0ff6c5d0
15 changed files with 713 additions and 441 deletions

View file

@ -59,7 +59,7 @@ typedef struct {
SCARDCONTEXT hCtx;
SCARDHANDLE hCard;
SCARD_IO_REQUEST ioCard;
} dev_spec_acr122;
} acr122_spec_t;
nfc_device_t* acr122_connect(const nfc_device_desc_t* pndd)
{
@ -71,18 +71,18 @@ nfc_device_t* acr122_connect(const nfc_device_desc_t* pndd)
uint32_t uiReader;
uint32_t uiDevIndex;
nfc_device_t* pnd;
dev_spec_acr122* pdsa;
dev_spec_acr122 dsa;
acr122_spec_t* pas;
acr122_spec_t as;
char* pcFirmware;
// Clear the reader list
memset(acList,0x00,szListLen);
// Test if context succeeded
if (SCardEstablishContext(SCARD_SCOPE_USER,NULL,NULL,&(dsa.hCtx)) != SCARD_S_SUCCESS) return INVALID_DEVICE_INFO;
if (SCardEstablishContext(SCARD_SCOPE_USER,NULL,NULL,&(as.hCtx)) != SCARD_S_SUCCESS) return INVALID_DEVICE_INFO;
// Retrieve the string array of all available pcsc readers
if (SCardListReaders(dsa.hCtx,NULL,acList,(void*)&szListLen) != SCARD_S_SUCCESS) return INVALID_DEVICE_INFO;
if (SCardListReaders(as.hCtx,NULL,acList,(void*)&szListLen) != SCARD_S_SUCCESS) return INVALID_DEVICE_INFO;
DBG("PCSC reports following device(s):");
DBG("- %s",acList);
@ -121,20 +121,20 @@ nfc_device_t* acr122_connect(const nfc_device_desc_t* pndd)
for (uiReader=0; uiReader<uiReaderCount; uiReader++)
{
// Test if we were able to connect to the "emulator" card
if (SCardConnect(dsa.hCtx,pacReaders[uiReader],SCARD_SHARE_EXCLUSIVE,SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,&(dsa.hCard),(void*)&(dsa.ioCard.dwProtocol)) != SCARD_S_SUCCESS)
if (SCardConnect(as.hCtx,pacReaders[uiReader],SCARD_SHARE_EXCLUSIVE,SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,&(as.hCard),(void*)&(as.ioCard.dwProtocol)) != SCARD_S_SUCCESS)
{
// Connect to ACR122 firmware version >2.0
if (SCardConnect(dsa.hCtx,pacReaders[uiReader],SCARD_SHARE_DIRECT,0,&(dsa.hCard),(void*)&(dsa.ioCard.dwProtocol)) != SCARD_S_SUCCESS)
if (SCardConnect(as.hCtx,pacReaders[uiReader],SCARD_SHARE_DIRECT,0,&(as.hCard),(void*)&(as.ioCard.dwProtocol)) != SCARD_S_SUCCESS)
{
// We can not connect to this device, we will just ignore it
continue;
}
}
// Configure I/O settings for card communication
dsa.ioCard.cbPciLength = sizeof(SCARD_IO_REQUEST);
as.ioCard.cbPciLength = sizeof(SCARD_IO_REQUEST);
// Retrieve the current firmware version
pcFirmware = acr122_firmware((nfc_device_t*)&dsa);
pcFirmware = acr122_firmware((nfc_device_t*)&as);
if (strstr(pcFirmware,FIRMWARE_TEXT) != NULL)
{
// We found a occurence, test if it has the right index
@ -146,14 +146,14 @@ nfc_device_t* acr122_connect(const nfc_device_desc_t* pndd)
}
// Allocate memory and store the device specification
pdsa = malloc(sizeof(dev_spec_acr122));
*pdsa = dsa;
pas = malloc(sizeof(acr122_spec_t));
*pas = as;
// Done, we found the reader we are looking for
pnd = malloc(sizeof(nfc_device_t));
strcpy(pnd->acName,pcFirmware);
pnd->ct = CT_PN532;
pnd->ds = (dev_spec)pdsa;
pnd->nc = NC_PN532;
pnd->nds = (nfc_device_spec_t)pas;
pnd->bActive = true;
pnd->bCrc = true;
pnd->bPar = true;
@ -168,21 +168,21 @@ nfc_device_t* acr122_connect(const nfc_device_desc_t* pndd)
void acr122_disconnect(nfc_device_t* pnd)
{
dev_spec_acr122* pdsa = (dev_spec_acr122*)pnd->ds;
SCardDisconnect(pdsa->hCard,SCARD_LEAVE_CARD);
SCardReleaseContext(pdsa->hCtx);
free(pdsa);
acr122_spec_t* pas = (acr122_spec_t*)pnd->nds;
SCardDisconnect(pas->hCard,SCARD_LEAVE_CARD);
SCardReleaseContext(pas->hCtx);
free(pas);
free(pnd);
}
bool acr122_transceive(const dev_spec ds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen)
bool acr122_transceive(const nfc_device_spec_t nds, 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 };
dev_spec_acr122* pdsa = (dev_spec_acr122*)ds;
acr122_spec_t* pas = (acr122_spec_t*)nds;
// Make sure the command does not overflow the send buffer
if (szTxLen > ACR122_COMMAND_LEN) return false;
@ -198,14 +198,14 @@ bool acr122_transceive(const dev_spec ds, const byte_t* pbtTx, const size_t szTx
print_hex(abtTxBuf,szTxLen+5);
#endif
if (pdsa->ioCard.dwProtocol == SCARD_PROTOCOL_UNDEFINED)
if (pas->ioCard.dwProtocol == SCARD_PROTOCOL_UNDEFINED)
{
if (SCardControl(pdsa->hCard,IOCTL_CCID_ESCAPE_SCARD_CTL_CODE,abtTxBuf,szTxLen+5,abtRxBuf,szRxBufLen,(void*)&szRxBufLen) != SCARD_S_SUCCESS) return false;
if (SCardControl(pas->hCard,IOCTL_CCID_ESCAPE_SCARD_CTL_CODE,abtTxBuf,szTxLen+5,abtRxBuf,szRxBufLen,(void*)&szRxBufLen) != SCARD_S_SUCCESS) return false;
} else {
if (SCardTransmit(pdsa->hCard,&(pdsa->ioCard),abtTxBuf,szTxLen+5,NULL,abtRxBuf,(void*)&szRxBufLen) != SCARD_S_SUCCESS) return false;
if (SCardTransmit(pas->hCard,&(pas->ioCard),abtTxBuf,szTxLen+5,NULL,abtRxBuf,(void*)&szRxBufLen) != SCARD_S_SUCCESS) return false;
}
if (pdsa->ioCard.dwProtocol == SCARD_PROTOCOL_T0)
if (pas->ioCard.dwProtocol == SCARD_PROTOCOL_T0)
{
// Make sure we received the byte-count we expected
if (szRxBufLen != 2) return false;
@ -216,7 +216,7 @@ bool acr122_transceive(const dev_spec ds, const byte_t* pbtTx, const size_t szTx
// Retrieve the response bytes
abtRxCmd[4] = abtRxBuf[1];
szRxBufLen = sizeof(abtRxBuf);
if (SCardTransmit(pdsa->hCard,&(pdsa->ioCard),abtRxCmd,szRxCmdLen,NULL,abtRxBuf,(void*)&szRxBufLen) != SCARD_S_SUCCESS) return false;
if (SCardTransmit(pas->hCard,&(pas->ioCard),abtRxCmd,szRxCmdLen,NULL,abtRxBuf,(void*)&szRxBufLen) != SCARD_S_SUCCESS) return false;
}
#ifdef DEBUG
@ -237,20 +237,20 @@ bool acr122_transceive(const dev_spec ds, const byte_t* pbtTx, const size_t szTx
return true;
}
char* acr122_firmware(const dev_spec ds)
char* acr122_firmware(const nfc_device_spec_t nds)
{
byte_t abtGetFw[5] = { 0xFF,0x00,0x48,0x00,0x00 };
uint32_t uiResult;
dev_spec_acr122* pdsa = (dev_spec_acr122*)ds;
acr122_spec_t* pas = (acr122_spec_t*)nds;
static char abtFw[11];
size_t szFwLen = sizeof(abtFw);
memset(abtFw,0x00,szFwLen);
if (pdsa->ioCard.dwProtocol == SCARD_PROTOCOL_UNDEFINED)
if (pas->ioCard.dwProtocol == SCARD_PROTOCOL_UNDEFINED)
{
uiResult = SCardControl(pdsa->hCard,IOCTL_CCID_ESCAPE_SCARD_CTL_CODE,abtGetFw,sizeof(abtGetFw),abtFw,szFwLen,(void*)&szFwLen);
uiResult = SCardControl(pas->hCard,IOCTL_CCID_ESCAPE_SCARD_CTL_CODE,abtGetFw,sizeof(abtGetFw),abtFw,szFwLen,(void*)&szFwLen);
} else {
uiResult = SCardTransmit(pdsa->hCard,&(pdsa->ioCard),abtGetFw,sizeof(abtGetFw),NULL,(byte_t*)abtFw,(void*)&szFwLen);
uiResult = SCardTransmit(pas->hCard,&(pas->ioCard),abtGetFw,sizeof(abtGetFw),NULL,(byte_t*)abtFw,(void*)&szFwLen);
}
#ifdef DEBUG
@ -263,17 +263,17 @@ char* acr122_firmware(const dev_spec ds)
return abtFw;
}
bool acr122_led_red(const dev_spec ds, bool bOn)
bool acr122_led_red(const nfc_device_spec_t nds, bool bOn)
{
byte_t abtLed[9] = { 0xFF,0x00,0x40,0x05,0x04,0x00,0x00,0x00,0x00 };
dev_spec_acr122* pdsa = (dev_spec_acr122*)ds;
acr122_spec_t* pas = (acr122_spec_t*)nds;
byte_t abtBuf[2];
size_t szBufLen = sizeof(abtBuf);
if (pdsa->ioCard.dwProtocol == SCARD_PROTOCOL_UNDEFINED)
if (pas->ioCard.dwProtocol == SCARD_PROTOCOL_UNDEFINED)
{
return (SCardControl(pdsa->hCard,IOCTL_CCID_ESCAPE_SCARD_CTL_CODE,abtLed,sizeof(abtLed),abtBuf,szBufLen,(void*)&szBufLen) == SCARD_S_SUCCESS);
return (SCardControl(pas->hCard,IOCTL_CCID_ESCAPE_SCARD_CTL_CODE,abtLed,sizeof(abtLed),abtBuf,szBufLen,(void*)&szBufLen) == SCARD_S_SUCCESS);
} else {
return (SCardTransmit(pdsa->hCard,&(pdsa->ioCard),abtLed,sizeof(abtLed),NULL,(byte_t*)abtBuf,(void*)&szBufLen) == SCARD_S_SUCCESS);
return (SCardTransmit(pas->hCard,&(pas->ioCard),abtLed,sizeof(abtLed),NULL,(byte_t*)abtBuf,(void*)&szBufLen) == SCARD_S_SUCCESS);
}
}

View file

@ -35,11 +35,11 @@ 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 dev_spec ds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen);
bool acr122_transceive(const nfc_device_spec_t nds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen);
// Various additional features this device supports
char* acr122_firmware(const dev_spec ds);
bool acr122_led_red(const dev_spec ds, bool bOn);
char* acr122_firmware(const nfc_device_spec_t nds);
bool acr122_led_red(const nfc_device_spec_t nds, bool bOn);
#endif // _LIBNFC_DEV_ACR122_H_

View file

@ -127,8 +127,8 @@ nfc_device_t* arygon_connect(const nfc_device_desc_t* pndd)
// We have a connection
pnd = malloc(sizeof(nfc_device_t));
strcpy(pnd->acName,"ARYGON");
pnd->ct = CT_PN532;
pnd->ds = (dev_spec)sp;
pnd->nc = NC_PN532;
pnd->nds = (nfc_device_spec_t)sp;
pnd->bActive = true;
pnd->bCrc = true;
pnd->bPar = true;
@ -138,11 +138,11 @@ nfc_device_t* arygon_connect(const nfc_device_desc_t* pndd)
void arygon_disconnect(nfc_device_t* pnd)
{
uart_close((serial_port)pnd->ds);
uart_close((serial_port)pnd->nds);
free(pnd);
}
bool arygon_transceive(const dev_spec ds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen)
bool arygon_transceive(const nfc_device_spec_t nds, 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];
@ -170,7 +170,7 @@ bool arygon_transceive(const dev_spec ds, const byte_t* pbtTx, const size_t szTx
printf(" TX: ");
print_hex(abtTxBuf,szTxLen+8);
#endif
if (!uart_send((serial_port)ds,abtTxBuf,szTxLen+8)) {
if (!uart_send((serial_port)nds,abtTxBuf,szTxLen+8)) {
ERR("Unable to transmit data. (TX)");
return false;
}
@ -190,7 +190,7 @@ bool arygon_transceive(const dev_spec ds, const byte_t* pbtTx, const size_t szTx
* For more information, see Issue 23 on development site : http://code.google.com/p/libnfc/issues/detail?id=23
*/
if (!uart_receive((serial_port)ds,abtRxBuf,&szRxBufLen)) {
if (!uart_receive((serial_port)nds,abtRxBuf,&szRxBufLen)) {
ERR("Unable to receive data. (RX)");
return false;
}

View file

@ -32,7 +32,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 dev_spec ds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen);
bool arygon_transceive(const nfc_device_spec_t nds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen);
#endif // _LIBNFC_DEV_ARYGON_H_

View file

@ -44,10 +44,10 @@ typedef struct {
usb_dev_handle* pudh;
uint32_t uiEndPointIn;
uint32_t uiEndPointOut;
} dev_spec_pn531_usb;
} usb_spec_t;
// Find transfer endpoints for bulk transfers
static void get_end_points(struct usb_device *dev, dev_spec_pn531_usb* pdsp)
static void get_end_points(struct usb_device *dev, usb_spec_t* pus)
{
uint32_t uiIndex;
uint32_t uiEndPoint;
@ -68,7 +68,7 @@ static void get_end_points(struct usb_device *dev, dev_spec_pn531_usb* pdsp)
#ifdef DEBUG
printf("Bulk endpoint in : 0x%02X\n", uiEndPoint);
#endif
pdsp->uiEndPointIn = uiEndPoint;
pus->uiEndPointIn = uiEndPoint;
}
// Test if we dealing with a bulk OUT endpoint
@ -77,7 +77,7 @@ static void get_end_points(struct usb_device *dev, dev_spec_pn531_usb* pdsp)
#ifdef DEBUG
printf("Bulk endpoint in : 0x%02X\n", uiEndPoint);
#endif
pdsp->uiEndPointOut = uiEndPoint;
pus->uiEndPointOut = uiEndPoint;
}
}
}
@ -91,13 +91,13 @@ nfc_device_t* pn531_usb_connect(const nfc_device_desc_t* pndd)
struct usb_bus *bus;
struct usb_device *dev;
nfc_device_t* pnd = INVALID_DEVICE_INFO;
dev_spec_pn531_usb* pdsp;
dev_spec_pn531_usb dsp;
usb_spec_t* pus;
usb_spec_t us;
uint32_t uiDevIndex;
dsp.uiEndPointIn = 0;
dsp.uiEndPointOut = 0;
dsp.pudh = NULL;
us.uiEndPointIn = 0;
us.uiEndPointOut = 0;
us.pudh = NULL;
usb_init();
if (usb_find_busses() < 0) return INVALID_DEVICE_INFO;
@ -130,29 +130,29 @@ nfc_device_t* pn531_usb_connect(const nfc_device_desc_t* pndd)
DBG("Found PN531 device");
// Open the PN531 USB device
dsp.pudh = usb_open(dev);
us.pudh = usb_open(dev);
get_end_points(dev,&dsp);
if(usb_set_configuration(dsp.pudh,1) < 0)
get_end_points(dev,&us);
if(usb_set_configuration(us.pudh,1) < 0)
{
DBG("Set config failed");
usb_close(dsp.pudh);
usb_close(us.pudh);
return INVALID_DEVICE_INFO;
}
if(usb_claim_interface(dsp.pudh,0) < 0)
if(usb_claim_interface(us.pudh,0) < 0)
{
DBG("Can't claim interface");
usb_close(dsp.pudh);
usb_close(us.pudh);
return INVALID_DEVICE_INFO;
}
// Allocate memory for the device info and specification, fill it and return the info
pdsp = malloc(sizeof(dev_spec_pn531_usb));
*pdsp = dsp;
pus = malloc(sizeof(usb_spec_t));
*pus = us;
pnd = malloc(sizeof(nfc_device_t));
strcpy(pnd->acName,"PN531USB");
pnd->ct = CT_PN531;
pnd->ds = (dev_spec)pdsp;
pnd->nc = NC_PN531;
pnd->nds = (nfc_device_spec_t)pus;
pnd->bActive = true;
pnd->bCrc = true;
pnd->bPar = true;
@ -166,20 +166,20 @@ nfc_device_t* pn531_usb_connect(const nfc_device_desc_t* pndd)
void pn531_usb_disconnect(nfc_device_t* pnd)
{
dev_spec_pn531_usb* pdsp = (dev_spec_pn531_usb*)pnd->ds;
usb_release_interface(pdsp->pudh,0);
usb_close(pdsp->pudh);
free(pnd->ds);
usb_spec_t* pus = (usb_spec_t*)pnd->nds;
usb_release_interface(pus->pudh,0);
usb_close(pus->pudh);
free(pnd->nds);
free(pnd);
}
bool pn531_usb_transceive(const dev_spec ds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen)
bool pn531_usb_transceive(const nfc_device_spec_t nds, 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];
dev_spec_pn531_usb* pdsp = (dev_spec_pn531_usb*)ds;
usb_spec_t* pus = (usb_spec_t*)nds;
// Packet length = data length (len) + checksum (1) + end of stream marker (1)
abtTx[3] = szTxLen;
@ -203,7 +203,7 @@ bool pn531_usb_transceive(const dev_spec ds, const byte_t* pbtTx, const size_t s
print_hex(abtTx,szTxLen+7);
#endif
ret = usb_bulk_write(pdsp->pudh, pdsp->uiEndPointOut, (char*)abtTx, szTxLen+7, USB_TIMEOUT);
ret = usb_bulk_write(pus->pudh, pus->uiEndPointOut, (char*)abtTx, szTxLen+7, USB_TIMEOUT);
if( ret < 0 )
{
#ifdef DEBUG
@ -212,7 +212,7 @@ bool pn531_usb_transceive(const dev_spec ds, const byte_t* pbtTx, const size_t s
return false;
}
ret = usb_bulk_read(pdsp->pudh, pdsp->uiEndPointIn, (char*)abtRx, BUFFER_LENGTH, USB_TIMEOUT);
ret = usb_bulk_read(pus->pudh, pus->uiEndPointIn, (char*)abtRx, BUFFER_LENGTH, USB_TIMEOUT);
if( ret < 0 )
{
#ifdef DEBUG
@ -228,7 +228,7 @@ bool pn531_usb_transceive(const dev_spec ds, const byte_t* pbtTx, const size_t s
if( ret == 6 )
{
ret = usb_bulk_read(pdsp->pudh, pdsp->uiEndPointIn, (char*)abtRx, BUFFER_LENGTH, USB_TIMEOUT);
ret = usb_bulk_read(pus->pudh, pus->uiEndPointIn, (char*)abtRx, BUFFER_LENGTH, USB_TIMEOUT);
if( ret < 0 )
{
#ifdef DEBUG

View file

@ -35,7 +35,7 @@ nfc_device_t* pn531_usb_connect(const nfc_device_desc_t* pndd);
void pn531_usb_disconnect(nfc_device_t* pnd);
// Callback function used by libnfc to transmit commands to the PN53X chip
bool pn531_usb_transceive(const dev_spec ds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen);
bool pn531_usb_transceive(const nfc_device_spec_t nds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen);
#endif // _LIBNFC_DEV_PN531_H_

View file

@ -120,8 +120,8 @@ nfc_device_t* pn532_uart_connect(const nfc_device_desc_t* pndd)
// We have a connection
pnd = malloc(sizeof(nfc_device_t));
strcpy(pnd->acName,"PN532_UART");
pnd->ct = CT_PN532;
pnd->ds = (dev_spec)sp;
pnd->nc = NC_PN532;
pnd->nds = (nfc_device_spec_t)sp;
pnd->bActive = true;
pnd->bCrc = true;
pnd->bPar = true;
@ -131,11 +131,11 @@ nfc_device_t* pn532_uart_connect(const nfc_device_desc_t* pndd)
void pn532_uart_disconnect(nfc_device_t* pnd)
{
uart_close((serial_port)pnd->ds);
uart_close((serial_port)pnd->nds);
free(pnd);
}
bool pn532_uart_transceive(const dev_spec ds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen)
bool pn532_uart_transceive(const nfc_device_spec_t nds, 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];
@ -163,7 +163,7 @@ bool pn532_uart_transceive(const dev_spec ds, const byte_t* pbtTx, const size_t
printf(" TX: ");
print_hex(abtTxBuf,szTxLen+7);
#endif
if (!uart_send((serial_port)ds,abtTxBuf,szTxLen+7)) {
if (!uart_send((serial_port)nds,abtTxBuf,szTxLen+7)) {
ERR("Unable to transmit data. (TX)");
return false;
}
@ -178,7 +178,7 @@ bool pn532_uart_transceive(const dev_spec ds, const byte_t* pbtTx, const size_t
*/
delay_ms(30);
if (!uart_receive((serial_port)ds,abtRxBuf,&szRxBufLen)) {
if (!uart_receive((serial_port)nds,abtRxBuf,&szRxBufLen)) {
ERR("Unable to receive data. (RX)");
return false;
}

View file

@ -32,7 +32,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 dev_spec ds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen);
bool pn532_uart_transceive(const nfc_device_spec_t nds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen);
#endif // _LIBNFC_DEV_PN532_UART_H_

View file

@ -43,10 +43,10 @@ typedef struct {
usb_dev_handle* pudh;
uint32_t uiEndPointIn;
uint32_t uiEndPointOut;
} dev_spec_pn533_usb;
} usb_spec_t;
// Find transfer endpoints for bulk transfers
static void get_end_points(struct usb_device *dev, dev_spec_pn533_usb* pdsp)
static void get_end_points(struct usb_device *dev, usb_spec_t* pus)
{
uint32_t uiIndex;
uint32_t uiEndPoint;
@ -67,7 +67,7 @@ static void get_end_points(struct usb_device *dev, dev_spec_pn533_usb* pdsp)
#ifdef DEBUG
printf("Bulk endpoint in : 0x%02X\n", uiEndPoint);
#endif
pdsp->uiEndPointIn = uiEndPoint;
pus->uiEndPointIn = uiEndPoint;
}
// Test if we dealing with a bulk OUT endpoint
@ -76,7 +76,7 @@ static void get_end_points(struct usb_device *dev, dev_spec_pn533_usb* pdsp)
#ifdef DEBUG
printf("Bulk endpoint in : 0x%02X\n", uiEndPoint);
#endif
pdsp->uiEndPointOut = uiEndPoint;
pus->uiEndPointOut = uiEndPoint;
}
}
}
@ -88,13 +88,13 @@ nfc_device_t* pn533_usb_connect(const nfc_device_desc_t* pndd)
struct usb_bus *bus;
struct usb_device *dev;
nfc_device_t* pnd = INVALID_DEVICE_INFO;
dev_spec_pn533_usb* pdsp;
dev_spec_pn533_usb dsp;
usb_spec_t* pus;
usb_spec_t us;
uint32_t uiDevIndex;
dsp.uiEndPointIn = 0;
dsp.uiEndPointOut = 0;
dsp.pudh = NULL;
us.uiEndPointIn = 0;
us.uiEndPointOut = 0;
us.pudh = NULL;
usb_init();
if (usb_find_busses() < 0) return INVALID_DEVICE_INFO;
@ -126,29 +126,29 @@ nfc_device_t* pn533_usb_connect(const nfc_device_desc_t* pndd)
DBG("Found PN533 device");
// Open the PN533 USB device
dsp.pudh = usb_open(dev);
us.pudh = usb_open(dev);
get_end_points(dev,&dsp);
if(usb_set_configuration(dsp.pudh,1) < 0)
get_end_points(dev,&us);
if(usb_set_configuration(us.pudh,1) < 0)
{
DBG("Setting config failed");
usb_close(dsp.pudh);
usb_close(us.pudh);
return INVALID_DEVICE_INFO;
}
if(usb_claim_interface(dsp.pudh,0) < 0)
if(usb_claim_interface(us.pudh,0) < 0)
{
DBG("Can't claim interface");
usb_close(dsp.pudh);
usb_close(us.pudh);
return INVALID_DEVICE_INFO;
}
// Allocate memory for the device info and specification, fill it and return the info
pdsp = malloc(sizeof(dev_spec_pn533_usb));
*pdsp = dsp;
pus = malloc(sizeof(usb_spec_t));
*pus = us;
pnd = malloc(sizeof(nfc_device_t));
strcpy(pnd->acName,"PN533USB");
pnd->ct = CT_PN533;
pnd->ds = (dev_spec)pdsp;
pnd->nc = NC_PN533;
pnd->nds = (nfc_device_spec_t)pus;
pnd->bActive = true;
pnd->bCrc = true;
pnd->bPar = true;
@ -162,20 +162,20 @@ nfc_device_t* pn533_usb_connect(const nfc_device_desc_t* pndd)
void pn533_usb_disconnect(nfc_device_t* pnd)
{
dev_spec_pn533_usb* pdsp = (dev_spec_pn533_usb*)pnd->ds;
usb_release_interface(pdsp->pudh,0);
usb_close(pdsp->pudh);
free(pnd->ds);
usb_spec_t* pus = (usb_spec_t*)pnd->nds;
usb_release_interface(pus->pudh,0);
usb_close(pus->pudh);
free(pnd->nds);
free(pnd);
}
bool pn533_usb_transceive(const dev_spec ds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen)
bool pn533_usb_transceive(const nfc_device_spec_t nds, 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];
dev_spec_pn533_usb* pdsp = (dev_spec_pn533_usb*)ds;
usb_spec_t* pus = (usb_spec_t*)nds;
// Packet length = data length (len) + checksum (1) + end of stream marker (1)
abtTx[3] = szTxLen;
@ -199,7 +199,7 @@ bool pn533_usb_transceive(const dev_spec ds, const byte_t* pbtTx, const size_t s
print_hex(abtTx,szTxLen+7);
#endif
ret = usb_bulk_write(pdsp->pudh, pdsp->uiEndPointOut, (char*)abtTx, szTxLen+7, USB_TIMEOUT);
ret = usb_bulk_write(pus->pudh, pus->uiEndPointOut, (char*)abtTx, szTxLen+7, USB_TIMEOUT);
if( ret < 0 )
{
#ifdef DEBUG
@ -208,7 +208,7 @@ bool pn533_usb_transceive(const dev_spec ds, const byte_t* pbtTx, const size_t s
return false;
}
ret = usb_bulk_read(pdsp->pudh, pdsp->uiEndPointIn, (char*)abtRx, BUFFER_LENGTH, USB_TIMEOUT);
ret = usb_bulk_read(pus->pudh, pus->uiEndPointIn, (char*)abtRx, BUFFER_LENGTH, USB_TIMEOUT);
if( ret < 0 )
{
#ifdef DEBUG
@ -224,7 +224,7 @@ bool pn533_usb_transceive(const dev_spec ds, const byte_t* pbtTx, const size_t s
if( ret == 6 )
{
ret = usb_bulk_read(pdsp->pudh, pdsp->uiEndPointIn, (char*)abtRx, BUFFER_LENGTH, USB_TIMEOUT);
ret = usb_bulk_read(pus->pudh, pus->uiEndPointIn, (char*)abtRx, BUFFER_LENGTH, USB_TIMEOUT);
if( ret < 0 )
{
#ifdef DEBUG

View file

@ -32,7 +32,7 @@ nfc_device_t* pn533_usb_connect(const nfc_device_desc_t* pndd);
void pn533_usb_disconnect(nfc_device_t* pnd);
// Callback function used by libnfc to transmit commands to the PN53X chip
bool pn533_usb_transceive(const dev_spec ds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen);
bool pn533_usb_transceive(const nfc_device_spec_t nds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen);
#endif // _LIBNFC_DEV_PN533_H_

View file

@ -37,14 +37,14 @@
typedef uint8_t byte_t;
typedef enum {
CT_PN531 = 0x10,
CT_PN532 = 0x20,
CT_PN533 = 0x30,
} chip_type;
NC_PN531 = 0x10,
NC_PN532 = 0x20,
NC_PN533 = 0x30,
} nfc_chip_t;
struct driver_callbacks; // Prototype the callback struct
typedef void* dev_spec; // Device connection specification
typedef void* nfc_device_spec_t; // Device connection specification
#define DEVICE_NAME_LENGTH 256
/**
@ -57,9 +57,9 @@ typedef struct {
/** Device name string, including device wrapper firmware */
char acName[DEVICE_NAME_LENGTH];
/** PN53X chip type, this is useful for some "bug" work-arounds */
chip_type ct;
nfc_chip_t nc;
/** Pointer to the device connection specification */
dev_spec ds;
nfc_device_spec_t nds;
/** This represents if the PN53X device was initialized succesful */
bool bActive;
/** Is the crc automaticly added, checked and removed from the frames */
@ -98,7 +98,7 @@ struct driver_callbacks {
/** Connect callback */
nfc_device_t* (*connect)(const nfc_device_desc_t* pndd);
/** Transceive callback */
bool (*transceive)(const dev_spec ds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen);
bool (*transceive)(const nfc_device_spec_t nds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen);
/** Disconnect callback */
void (*disconnect)(nfc_device_t* pnd);
};

View file

@ -115,7 +115,7 @@ bool pn53x_transceive(const nfc_device_t* pnd, const byte_t* pbtTx, const size_t
*pszRxLen = MAX_FRAME_LEN;
// Call the tranceive callback function of the current device
if (!pnd->pdc->transceive(pnd->ds,pbtTx,szTxLen,pbtRx,pszRxLen)) return false;
if (!pnd->pdc->transceive(pnd->nds,pbtTx,szTxLen,pbtRx,pszRxLen)) return false;
// Make sure there was no failure reported by the PN53X chip (0x00 == OK)
if (pbtRx[0] != 0) return false;
@ -134,7 +134,7 @@ 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->ds,abtCmd,4,&ui8Value,&szValueLen);
pnd->pdc->transceive(pnd->nds,abtCmd,4,&ui8Value,&szValueLen);
return ui8Value;
}
@ -147,7 +147,7 @@ bool pn53x_set_reg(const nfc_device_t* pnd, uint16_t ui16Reg, uint8_t ui8SybmolM
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->ds,abtCmd,5,NULL,NULL);
return pnd->pdc->transceive(pnd->nds,abtCmd,5,NULL,NULL);
}
bool pn53x_set_parameters(const nfc_device_t* pnd, uint8_t ui8Value)
@ -157,7 +157,7 @@ bool pn53x_set_parameters(const nfc_device_t* pnd, uint8_t ui8Value)
abtCmd[2] = ui8Value;
// We can not use pn53x_transceive() because abtRx[0] gives no status info
return pnd->pdc->transceive(pnd->ds,abtCmd,3,NULL,NULL);
return pnd->pdc->transceive(pnd->nds,abtCmd,3,NULL,NULL);
}
bool pn53x_set_tx_bits(const nfc_device_t* pnd, uint8_t ui8Bits)
@ -310,7 +310,7 @@ nfc_device_t* nfc_connect(nfc_device_desc_t* pndd)
// Try to retrieve PN53x chip revision
// We can not use pn53x_transceive() because abtRx[0] gives no status info
if (!pnd->pdc->transceive(pnd->ds,pncmd_get_firmware_version,2,abtFw,&szFwLen))
if (!pnd->pdc->transceive(pnd->nds,pncmd_get_firmware_version,2,abtFw,&szFwLen))
{
// Failed to get firmware revision??, whatever...let's disconnect and clean up and return err
ERR("Failed to get firmware revision for: %s", pnd->acName);
@ -319,11 +319,11 @@ nfc_device_t* nfc_connect(nfc_device_desc_t* pndd)
}
// Add the firmware revision to the device name, PN531 gives 2 bytes info, but PN532 gives 4
switch(pnd->ct)
switch(pnd->nc)
{
case CT_PN531: sprintf(pnd->acName,"%s - PN531 v%d.%d",pnd->acName,abtFw[0],abtFw[1]); break;
case CT_PN532: sprintf(pnd->acName,"%s - PN532 v%d.%d (0x%02x)",pnd->acName,abtFw[1],abtFw[2],abtFw[3]); break;
case CT_PN533: sprintf(pnd->acName,"%s - PN533 v%d.%d (0x%02x)",pnd->acName,abtFw[1],abtFw[2],abtFw[3]); break;
case NC_PN531: sprintf(pnd->acName,"%s - PN531 v%d.%d",pnd->acName,abtFw[0],abtFw[1]); break;
case NC_PN532: sprintf(pnd->acName,"%s - PN532 v%d.%d (0x%02x)",pnd->acName,abtFw[1],abtFw[2],abtFw[3]); break;
case NC_PN533: sprintf(pnd->acName,"%s - PN533 v%d.%d (0x%02x)",pnd->acName,abtFw[1],abtFw[2],abtFw[3]); break;
}
// Reset the ending transmission bits register, it is unknown what the last tranmission used there
@ -384,7 +384,7 @@ bool nfc_configure(nfc_device_t* pnd, const dev_config_option dco, const bool bE
abtCmd[2] = RFCI_FIELD;
abtCmd[3] = (bEnable) ? 1 : 0;
// We can not use pn53x_transceive() because abtRx[0] gives no status info
if (!pnd->pdc->transceive(pnd->ds,abtCmd,4,NULL,NULL)) return false;
if (!pnd->pdc->transceive(pnd->nds,abtCmd,4,NULL,NULL)) return false;
break;
case DCO_ACTIVATE_CRYPTO1:
@ -399,7 +399,7 @@ bool nfc_configure(nfc_device_t* pnd, const dev_config_option dco, const bool bE
abtCmd[4] = (bEnable) ? 0xff : 0x00; // MxRtyPSL, default: 0x01
abtCmd[5] = (bEnable) ? 0xff : 0x00; // MxRtyPassiveActivation, default: 0xff
// We can not use pn53x_transceive() because abtRx[0] gives no status info
if (!pnd->pdc->transceive(pnd->ds,abtCmd,6,NULL,NULL)) return false;
if (!pnd->pdc->transceive(pnd->nds,abtCmd,6,NULL,NULL)) return false;
break;
case DCO_ACCEPT_INVALID_FRAMES:
@ -536,7 +536,7 @@ bool nfc_initiator_select_tag(const nfc_device_t* pnd, const init_modulation im,
// Try to find a tag, call the tranceive callback function of the current device
szRxLen = MAX_FRAME_LEN;
// We can not use pn53x_transceive() because abtRx[0] gives no status info
if (!pnd->pdc->transceive(pnd->ds,abtCmd,4+szInitLen,abtRx,&szRxLen)) return false;
if (!pnd->pdc->transceive(pnd->nds,abtCmd,4+szInitLen,abtRx,&szRxLen)) return false;
// Make sure one tag has been found, the PN53X returns 0x00 if none was available
if (abtRx[0] != 1) return false;
@ -549,7 +549,7 @@ bool nfc_initiator_select_tag(const nfc_device_t* pnd, const init_modulation im,
{
case IM_ISO14443A_106:
// Somehow they switched the lower and upper ATQA bytes around for the PN531 chipset
if (pnd->ct == CT_PN531)
if (pnd->nc == NC_PN531)
{
pti->tia.abtAtqa[0] = abtRx[3];
pti->tia.abtAtqa[1] = abtRx[2];
@ -839,7 +839,7 @@ bool nfc_target_init(const nfc_device_t* pnd, byte_t* pbtRx, size_t* pszRxBits)
// Request the initialization as a target, we can not use pn53x_transceive() because
// abtRx[0] contains the emulation mode (baudrate, 14443-4?, DEP and framing type)
szRxLen = MAX_FRAME_LEN;
if (!pnd->pdc->transceive(pnd->ds,abtCmd,39,abtRx,&szRxLen)) return false;
if (!pnd->pdc->transceive(pnd->nds,abtCmd,39,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;