Indent whole code using make indent. (Fixes issue 84).

This commit is contained in:
Romuald Conty
2010-09-07 17:51:03 +00:00
parent f93b4939f4
commit 18cc86a613
42 changed files with 2613 additions and 2479 deletions
+120 -117
View File
@@ -23,7 +23,7 @@
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
# include "config.h"
#endif // HAVE_CONFIG_H
#include <stdio.h>
@@ -38,7 +38,7 @@
#include <winscard.h>
#ifdef __APPLE__
#include <wintypes.h>
# include <wintypes.h>
#endif
#include <nfc/nfc.h>
@@ -50,7 +50,7 @@
#define SCARD_OPERATION_ERROR 0x63
#ifndef SCARD_PROTOCOL_UNDEFINED
#define SCARD_PROTOCOL_UNDEFINED SCARD_PROTOCOL_UNSET
# define SCARD_PROTOCOL_UNDEFINED SCARD_PROTOCOL_UNSET
#endif
#define FIRMWARE_TEXT "ACR122U" // Tested on: ACR122U101(ACS), ACR122U102(Tikitag), ACR122U203(ACS)
@@ -74,12 +74,12 @@ typedef struct {
static SCARDCONTEXT _SCardContext;
static int _iSCardContextRefCount = 0;
SCARDCONTEXT*
acr122_get_scardcontext(void)
SCARDCONTEXT *
acr122_get_scardcontext (void)
{
if ( _iSCardContextRefCount == 0 )
{
if (SCardEstablishContext(SCARD_SCOPE_USER,NULL,NULL,&_SCardContext) != SCARD_S_SUCCESS) return NULL;
if (_iSCardContextRefCount == 0) {
if (SCardEstablishContext (SCARD_SCOPE_USER, NULL, NULL, &_SCardContext) != SCARD_S_SUCCESS)
return NULL;
}
_iSCardContextRefCount++;
@@ -87,14 +87,12 @@ acr122_get_scardcontext(void)
}
void
acr122_free_scardcontext(void)
acr122_free_scardcontext (void)
{
if (_iSCardContextRefCount)
{
if (_iSCardContextRefCount) {
_iSCardContextRefCount--;
if (!_iSCardContextRefCount)
{
SCardReleaseContext(_SCardContext);
if (!_iSCardContextRefCount) {
SCardReleaseContext (_SCardContext);
}
}
}
@@ -106,15 +104,15 @@ acr122_pick_device (void)
nfc_device_desc_t *pndd;
if ((pndd = malloc (sizeof (*pndd)))) {
size_t szN;
size_t szN;
if (!acr122_list_devices (pndd, 1, &szN)) {
DBG("%s", "acr122_list_devices failed");
DBG ("%s", "acr122_list_devices failed");
return NULL;
}
if (szN == 0) {
DBG("%s", "No device found");
DBG ("%s", "No device found");
return NULL;
}
}
@@ -133,30 +131,29 @@ acr122_pick_device (void)
* @return true if succeeded, false otherwise.
*/
bool
acr122_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *pszDeviceFound)
acr122_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound)
{
size_t szPos = 0;
char acDeviceNames[256+64*DRIVERS_MAX_DEVICES];
size_t szDeviceNamesLen = sizeof(acDeviceNames);
size_t szPos = 0;
char acDeviceNames[256 + 64 * DRIVERS_MAX_DEVICES];
size_t szDeviceNamesLen = sizeof (acDeviceNames);
uint32_t uiBusIndex = 0;
SCARDCONTEXT *pscc;
bool bSupported;
int i;
bool bSupported;
int i;
// Clear the reader list
memset(acDeviceNames, '\0', szDeviceNamesLen);
memset (acDeviceNames, '\0', szDeviceNamesLen);
*pszDeviceFound = 0;
// Test if context succeeded
if (!(pscc = acr122_get_scardcontext ()))
{
DBG("%s","PCSC context not found");
if (!(pscc = acr122_get_scardcontext ())) {
DBG ("%s", "PCSC context not found");
return false;
}
// Retrieve the string array of all available pcsc readers
if (SCardListReaders(*pscc,NULL,acDeviceNames,(void*)&szDeviceNamesLen) != SCARD_S_SUCCESS) return false;
if (SCardListReaders (*pscc, NULL, acDeviceNames, (void *) &szDeviceNamesLen) != SCARD_S_SUCCESS)
return false;
// DBG("%s", "PCSC reports following device(s):");
@@ -167,22 +164,19 @@ acr122_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *p
bSupported = false;
for (i = 0; supported_devices[i] && !bSupported; i++) {
int l = strlen(supported_devices[i]);
bSupported = 0 == strncmp(supported_devices[i], acDeviceNames + szPos, l);
int l = strlen (supported_devices[i]);
bSupported = 0 == strncmp (supported_devices[i], acDeviceNames + szPos, l);
}
if (bSupported)
{
if (bSupported) {
// Supported ACR122 device found
strncpy(pnddDevices[*pszDeviceFound].acDevice, acDeviceNames + szPos, DEVICE_NAME_LENGTH - 1);
strncpy (pnddDevices[*pszDeviceFound].acDevice, acDeviceNames + szPos, DEVICE_NAME_LENGTH - 1);
pnddDevices[*pszDeviceFound].acDevice[DEVICE_NAME_LENGTH - 1] = '\0';
pnddDevices[*pszDeviceFound].pcDriver = ACR122_DRIVER_NAME;
pnddDevices[*pszDeviceFound].uiBusIndex = uiBusIndex;
(*pszDeviceFound)++;
}
else
{
DBG("PCSC device [%s] is not NFC capable or not supported by libnfc.", acDeviceNames + szPos);
} else {
DBG ("PCSC device [%s] is not NFC capable or not supported by libnfc.", acDeviceNames + szPos);
}
// Find next device name position
@@ -190,52 +184,54 @@ acr122_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *p
}
acr122_free_scardcontext ();
if(*pszDeviceFound)
if (*pszDeviceFound)
return true;
return false;
}
nfc_device_t* acr122_connect(const nfc_device_desc_t* pndd)
nfc_device_t *
acr122_connect (const nfc_device_desc_t * pndd)
{
nfc_device_t* pnd = NULL;
nfc_device_t *pnd = NULL;
acr122_spec_t as;
acr122_spec_t* pas;
char* pcFirmware;
acr122_spec_t *pas;
char *pcFirmware;
SCARDCONTEXT *pscc;
DBG("Attempt to connect to %s",pndd->acDevice);
DBG ("Attempt to connect to %s", pndd->acDevice);
// Test if context succeeded
if (!(pscc = acr122_get_scardcontext ())) return NULL;
if (!(pscc = acr122_get_scardcontext ()))
return NULL;
// Test if we were able to connect to the "emulator" card
if (SCardConnect(*pscc,pndd->acDevice,SCARD_SHARE_EXCLUSIVE,SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,&(as.hCard),(void*)&(as.ioCard.dwProtocol)) != SCARD_S_SUCCESS)
{
if (SCardConnect
(*pscc, pndd->acDevice, 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(*pscc,pndd->acDevice,SCARD_SHARE_DIRECT,0,&(as.hCard),(void*)&(as.ioCard.dwProtocol)) != SCARD_S_SUCCESS)
{
if (SCardConnect (*pscc, pndd->acDevice, SCARD_SHARE_DIRECT, 0, &(as.hCard), (void *) &(as.ioCard.dwProtocol)) !=
SCARD_S_SUCCESS) {
// We can not connect to this device.
DBG("%s","PCSC connect failed");
DBG ("%s", "PCSC connect failed");
return NULL;
}
}
// Configure I/O settings for card communication
as.ioCard.cbPciLength = sizeof(SCARD_IO_REQUEST);
as.ioCard.cbPciLength = sizeof (SCARD_IO_REQUEST);
// Retrieve the current firmware version
pcFirmware = acr122_firmware((nfc_device_t*)&as);
if (strstr(pcFirmware,FIRMWARE_TEXT) != NULL)
{
pcFirmware = acr122_firmware ((nfc_device_t *) & as);
if (strstr (pcFirmware, FIRMWARE_TEXT) != NULL) {
// Allocate memory and store the device specification
pas = malloc(sizeof(acr122_spec_t));
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,pndd->acDevice);
strcpy(pnd->acName + strlen(pnd->acName)," / ");
strcpy(pnd->acName + strlen(pnd->acName),pcFirmware);
pnd = malloc (sizeof (nfc_device_t));
strcpy (pnd->acName, pndd->acDevice);
strcpy (pnd->acName + strlen (pnd->acName), " / ");
strcpy (pnd->acName + strlen (pnd->acName), pcFirmware);
pnd->nc = NC_PN532;
pnd->nds = (nfc_device_spec_t)pas;
pnd->nds = (nfc_device_spec_t) pas;
pnd->bActive = true;
pnd->bCrc = true;
pnd->bPar = true;
@@ -246,23 +242,25 @@ nfc_device_t* acr122_connect(const nfc_device_desc_t* pndd)
return NULL;
}
void acr122_disconnect(nfc_device_t* pnd)
void
acr122_disconnect (nfc_device_t * pnd)
{
acr122_spec_t* pas = (acr122_spec_t*)pnd->nds;
SCardDisconnect(pas->hCard,SCARD_LEAVE_CARD);
acr122_spec_t *pas = (acr122_spec_t *) pnd->nds;
SCardDisconnect (pas->hCard, SCARD_LEAVE_CARD);
acr122_free_scardcontext ();
free(pas);
free(pnd);
free (pas);
free (pnd);
}
bool acr122_transceive(nfc_device_t* pnd, 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*)pnd->nds;
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 *) pnd->nds;
// FIXME: Should be handled by the library.
// Make sure the command does not overflow the send buffer
@@ -270,111 +268,116 @@ bool acr122_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTx
pnd->iLastError = DEIO;
return false;
}
// Store the length of the command we are going to send
abtTxBuf[4] = szTxLen;
// Prepare and transmit the send buffer
memcpy(abtTxBuf+5,pbtTx,szTxLen);
szRxBufLen = sizeof(abtRxBuf);
memcpy (abtTxBuf + 5, pbtTx, szTxLen);
szRxBufLen = sizeof (abtRxBuf);
#ifdef DEBUG
PRINT_HEX("TX", abtTxBuf,szTxLen+5);
PRINT_HEX ("TX", abtTxBuf, szTxLen + 5);
#endif
if (pas->ioCard.dwProtocol == SCARD_PROTOCOL_UNDEFINED)
{
if (SCardControl(pas->hCard,IOCTL_CCID_ESCAPE_SCARD_CTL_CODE,abtTxBuf,szTxLen+5,abtRxBuf,szRxBufLen,(void*)&szRxBufLen) != SCARD_S_SUCCESS) {
if (pas->ioCard.dwProtocol == SCARD_PROTOCOL_UNDEFINED) {
if (SCardControl
(pas->hCard, IOCTL_CCID_ESCAPE_SCARD_CTL_CODE, abtTxBuf, szTxLen + 5, abtRxBuf, szRxBufLen,
(void *) &szRxBufLen) != SCARD_S_SUCCESS) {
pnd->iLastError = DEIO;
return false;
}
} else {
if (SCardTransmit(pas->hCard,&(pas->ioCard),abtTxBuf,szTxLen+5,NULL,abtRxBuf,(void*)&szRxBufLen) != SCARD_S_SUCCESS) {
if (SCardTransmit (pas->hCard, &(pas->ioCard), abtTxBuf, szTxLen + 5, NULL, abtRxBuf, (void *) &szRxBufLen) !=
SCARD_S_SUCCESS) {
pnd->iLastError = DEIO;
return false;
}
}
if (pas->ioCard.dwProtocol == SCARD_PROTOCOL_T0)
{
if (pas->ioCard.dwProtocol == SCARD_PROTOCOL_T0) {
// Make sure we received the byte-count we expected
if (szRxBufLen != 2) {
pnd->iLastError = DEIO;
return false;
}
// Check if the operation was successful, so an answer is available
if (*abtRxBuf == SCARD_OPERATION_ERROR) {
pnd->iLastError = DEISERRFRAME;
return false;
}
// Retrieve the response bytes
abtRxCmd[4] = abtRxBuf[1];
szRxBufLen = sizeof(abtRxBuf);
if (SCardTransmit(pas->hCard,&(pas->ioCard),abtRxCmd,szRxCmdLen,NULL,abtRxBuf,(void*)&szRxBufLen) != SCARD_S_SUCCESS) {
szRxBufLen = sizeof (abtRxBuf);
if (SCardTransmit (pas->hCard, &(pas->ioCard), abtRxCmd, szRxCmdLen, NULL, abtRxBuf, (void *) &szRxBufLen) !=
SCARD_S_SUCCESS) {
pnd->iLastError = DEIO;
return false;
}
}
#ifdef DEBUG
PRINT_HEX("RX", abtRxBuf,szRxBufLen);
PRINT_HEX ("RX", abtRxBuf, szRxBufLen);
#endif
// When the answer should be ignored, just return a succesful result
if (pbtRx == NULL || pszRxLen == NULL) return true;
if (pbtRx == NULL || pszRxLen == NULL)
return true;
// Make sure we have an emulated answer that fits the return buffer
if (szRxBufLen < 4 || (szRxBufLen-4) > *pszRxLen) {
if (szRxBufLen < 4 || (szRxBufLen - 4) > *pszRxLen) {
pnd->iLastError = DEIO;
return false;
}
// Wipe out the 4 APDU emulation bytes: D5 4B .. .. .. 90 00
*pszRxLen = ((size_t)szRxBufLen)-4;
memcpy(pbtRx,abtRxBuf+2,*pszRxLen);
*pszRxLen = ((size_t) szRxBufLen) - 4;
memcpy (pbtRx, abtRxBuf + 2, *pszRxLen);
// Transmission went successful
return true;
}
char* acr122_firmware(const nfc_device_spec_t nds)
char *
acr122_firmware (const nfc_device_spec_t nds)
{
byte_t abtGetFw[5] = { 0xFF,0x00,0x48,0x00,0x00 };
byte_t abtGetFw[5] = { 0xFF, 0x00, 0x48, 0x00, 0x00 };
uint32_t uiResult;
acr122_spec_t* pas = (acr122_spec_t*)nds;
acr122_spec_t *pas = (acr122_spec_t *) nds;
static char abtFw[11];
size_t szFwLen = sizeof(abtFw);
memset(abtFw,0x00,szFwLen);
if (pas->ioCard.dwProtocol == SCARD_PROTOCOL_UNDEFINED)
{
uiResult = SCardControl(pas->hCard,IOCTL_CCID_ESCAPE_SCARD_CTL_CODE,abtGetFw,sizeof(abtGetFw),abtFw,szFwLen,(void*)&szFwLen);
size_t szFwLen = sizeof (abtFw);
memset (abtFw, 0x00, szFwLen);
if (pas->ioCard.dwProtocol == SCARD_PROTOCOL_UNDEFINED) {
uiResult =
SCardControl (pas->hCard, IOCTL_CCID_ESCAPE_SCARD_CTL_CODE, abtGetFw, sizeof (abtGetFw), abtFw, szFwLen,
(void *) &szFwLen);
} else {
uiResult = SCardTransmit(pas->hCard,&(pas->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
if (uiResult != SCARD_S_SUCCESS)
{
printf("No ACR122 firmware received, Error: %08x\n",uiResult);
#ifdef DEBUG
if (uiResult != SCARD_S_SUCCESS) {
printf ("No ACR122 firmware received, Error: %08x\n", uiResult);
}
#endif
#endif
return abtFw;
}
bool acr122_led_red(const nfc_device_spec_t nds, 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 };
acr122_spec_t* pas = (acr122_spec_t*)nds;
byte_t abtBuf[2];
size_t szBufLen = sizeof(abtBuf);
(void)bOn;
if (pas->ioCard.dwProtocol == SCARD_PROTOCOL_UNDEFINED)
{
return (SCardControl(pas->hCard,IOCTL_CCID_ESCAPE_SCARD_CTL_CODE,abtLed,sizeof(abtLed),abtBuf,szBufLen,(void*)&szBufLen) == SCARD_S_SUCCESS);
byte_t abtLed[9] = { 0xFF, 0x00, 0x40, 0x05, 0x04, 0x00, 0x00, 0x00, 0x00 };
acr122_spec_t *pas = (acr122_spec_t *) nds;
byte_t abtBuf[2];
size_t szBufLen = sizeof (abtBuf);
(void) bOn;
if (pas->ioCard.dwProtocol == SCARD_PROTOCOL_UNDEFINED) {
return (SCardControl
(pas->hCard, IOCTL_CCID_ESCAPE_SCARD_CTL_CODE, abtLed, sizeof (abtLed), abtBuf, szBufLen,
(void *) &szBufLen) == SCARD_S_SUCCESS);
} else {
return (SCardTransmit(pas->hCard,&(pas->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);
}
}
+13 -13
View File
@@ -22,29 +22,29 @@
*/
#ifndef __NFC_DRIVER_ACR122_H__
#define __NFC_DRIVER_ACR122_H__
# define __NFC_DRIVER_ACR122_H__
#include <stdint.h>
#include <stdbool.h>
# include <stdint.h>
# include <stdbool.h>
#include <nfc/nfc-types.h>
# include <nfc/nfc-types.h>
#define ACR122_DRIVER_NAME "ACR122"
# define ACR122_DRIVER_NAME "ACR122"
nfc_device_desc_t* acr122_pick_device(void);
bool acr122_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *pszDeviceFound);
nfc_device_desc_t *acr122_pick_device (void);
bool acr122_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound);
// Functions used by developer to handle connection to this device
nfc_device_t* acr122_connect(const nfc_device_desc_t* pndd);
void acr122_disconnect(nfc_device_t* pnd);
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(nfc_device_t* pnd, 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);
bool acr122_led_red(const nfc_device_spec_t nds, bool bOn);
char *acr122_firmware (const nfc_device_spec_t nds);
bool acr122_led_red (const nfc_device_spec_t nds, bool bOn);
#endif // ! __NFC_DRIVER_ACR122_H__
+106 -93
View File
@@ -26,7 +26,7 @@
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
# include "config.h"
#endif // HAVE_CONFIG_H
#include "../drivers.h"
@@ -69,7 +69,7 @@
#define SERIAL_DEFAULT_PORT_SPEED 9600
bool arygon_check_communication(const nfc_device_spec_t nds);
bool arygon_check_communication (const nfc_device_spec_t nds);
/**
* @note ARYGON-ADRA (PN531): ???,n,8,1
@@ -84,15 +84,15 @@ arygon_pick_device (void)
nfc_device_desc_t *pndd;
if ((pndd = malloc (sizeof (*pndd)))) {
size_t szN;
size_t szN;
if (!arygon_list_devices (pndd, 1, &szN)) {
DBG("%s", "arygon_list_devices failed");
DBG ("%s", "arygon_list_devices failed");
return NULL;
}
if (szN == 0) {
DBG("%s", "No device found");
DBG ("%s", "No device found");
return NULL;
}
}
@@ -101,79 +101,87 @@ arygon_pick_device (void)
}
bool
arygon_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *pszDeviceFound)
arygon_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound)
{
/** @note: Due to UART bus we can't know if its really a pn532 without
* sending some PN53x commands. But using this way to probe devices, we can
* have serious problem with other device on this bus */
#ifndef SERIAL_AUTOPROBE_ENABLED
(void)pnddDevices;
(void)szDevices;
(void) pnddDevices;
(void) szDevices;
*pszDeviceFound = 0;
DBG("%s", "Serial auto-probing have been disabled at compile time. Skipping autoprobe.");
DBG ("%s", "Serial auto-probing have been disabled at compile time. Skipping autoprobe.");
return false;
#else /* SERIAL_AUTOPROBE_ENABLED */
*pszDeviceFound = 0;
serial_port sp;
const char* pcPorts[] = DEFAULT_SERIAL_PORTS;
const char* pcPort;
int iDevice = 0;
const char *pcPorts[] = DEFAULT_SERIAL_PORTS;
const char *pcPort;
int iDevice = 0;
while( (pcPort = pcPorts[iDevice++]) ) {
sp = uart_open(pcPort);
DBG("Trying to find ARYGON device on serial port: %s at %d bauds.", pcPort, SERIAL_DEFAULT_PORT_SPEED);
while ((pcPort = pcPorts[iDevice++])) {
sp = uart_open (pcPort);
DBG ("Trying to find ARYGON device on serial port: %s at %d bauds.", pcPort, SERIAL_DEFAULT_PORT_SPEED);
if ((sp != INVALID_SERIAL_PORT) && (sp != CLAIMED_SERIAL_PORT))
{
uart_set_speed(sp, SERIAL_DEFAULT_PORT_SPEED);
if(!arygon_check_communication((nfc_device_spec_t)sp)) continue;
uart_close(sp);
if ((sp != INVALID_SERIAL_PORT) && (sp != CLAIMED_SERIAL_PORT)) {
uart_set_speed (sp, SERIAL_DEFAULT_PORT_SPEED);
if (!arygon_check_communication ((nfc_device_spec_t) sp))
continue;
uart_close (sp);
// ARYGON reader is found
snprintf(pnddDevices[*pszDeviceFound].acDevice, DEVICE_NAME_LENGTH - 1, "%s (%s)", "ARYGON", pcPort);
snprintf (pnddDevices[*pszDeviceFound].acDevice, DEVICE_NAME_LENGTH - 1, "%s (%s)", "ARYGON", pcPort);
pnddDevices[*pszDeviceFound].acDevice[DEVICE_NAME_LENGTH - 1] = '\0';
pnddDevices[*pszDeviceFound].pcDriver = ARYGON_DRIVER_NAME;
pnddDevices[*pszDeviceFound].pcPort = strdup(pcPort);
pnddDevices[*pszDeviceFound].pcPort = strdup (pcPort);
pnddDevices[*pszDeviceFound].uiSpeed = SERIAL_DEFAULT_PORT_SPEED;
DBG("Device found: %s.", pnddDevices[*pszDeviceFound].acDevice);
DBG ("Device found: %s.", pnddDevices[*pszDeviceFound].acDevice);
(*pszDeviceFound)++;
// Test if we reach the maximum "wanted" devices
if((*pszDeviceFound) >= szDevices) break;
if ((*pszDeviceFound) >= szDevices)
break;
}
#ifdef DEBUG
if (sp == INVALID_SERIAL_PORT) DBG("Invalid serial port: %s", pcPort);
if (sp == CLAIMED_SERIAL_PORT) DBG("Serial port already claimed: %s", pcPort);
#endif /* DEBUG */
# ifdef DEBUG
if (sp == INVALID_SERIAL_PORT)
DBG ("Invalid serial port: %s", pcPort);
if (sp == CLAIMED_SERIAL_PORT)
DBG ("Serial port already claimed: %s", pcPort);
# endif
/* DEBUG */
}
#endif /* SERIAL_AUTOPROBE_ENABLED */
return true;
}
nfc_device_t* arygon_connect(const nfc_device_desc_t* pndd)
nfc_device_t *
arygon_connect (const nfc_device_desc_t * pndd)
{
serial_port sp;
nfc_device_t* pnd = NULL;
nfc_device_t *pnd = NULL;
DBG("Attempt to connect to: %s at %d bauds.",pndd->pcPort, pndd->uiSpeed);
sp = uart_open(pndd->pcPort);
DBG ("Attempt to connect to: %s at %d bauds.", pndd->pcPort, pndd->uiSpeed);
sp = uart_open (pndd->pcPort);
if (sp == INVALID_SERIAL_PORT) ERR("Invalid serial port: %s",pndd->pcPort);
if (sp == CLAIMED_SERIAL_PORT) ERR("Serial port already claimed: %s",pndd->pcPort);
if ((sp == CLAIMED_SERIAL_PORT) || (sp == INVALID_SERIAL_PORT)) return NULL;
if (sp == INVALID_SERIAL_PORT)
ERR ("Invalid serial port: %s", pndd->pcPort);
if (sp == CLAIMED_SERIAL_PORT)
ERR ("Serial port already claimed: %s", pndd->pcPort);
if ((sp == CLAIMED_SERIAL_PORT) || (sp == INVALID_SERIAL_PORT))
return NULL;
uart_set_speed(sp, pndd->uiSpeed);
uart_set_speed (sp, pndd->uiSpeed);
DBG("Successfully connected to: %s",pndd->pcPort);
DBG ("Successfully connected to: %s", pndd->pcPort);
// We have a connection
pnd = malloc(sizeof(nfc_device_t));
strncpy(pnd->acName, pndd->acDevice, DEVICE_NAME_LENGTH - 1);
pnd = malloc (sizeof (nfc_device_t));
strncpy (pnd->acName, pndd->acDevice, DEVICE_NAME_LENGTH - 1);
pnd->acName[DEVICE_NAME_LENGTH - 1] = '\0';
pnd->nc = NC_PN532;
pnd->nds = (nfc_device_spec_t)sp;
pnd->nds = (nfc_device_spec_t) sp;
pnd->bActive = true;
pnd->bCrc = true;
pnd->bPar = true;
@@ -181,19 +189,21 @@ nfc_device_t* arygon_connect(const nfc_device_desc_t* pndd)
return pnd;
}
void arygon_disconnect(nfc_device_t* pnd)
void
arygon_disconnect (nfc_device_t * pnd)
{
uart_close((serial_port)pnd->nds);
free(pnd);
uart_close ((serial_port) pnd->nds);
free (pnd);
}
bool arygon_transceive(nfc_device_t* pnd, 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];
size_t szRxBufLen = BUFFER_LENGTH;
size_t szPos;
int res;
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];
size_t szRxBufLen = BUFFER_LENGTH;
size_t szPos;
int res;
// TODO: Move this one level up for libnfc-1.6
uint8_t ack_frame[] = { 0x00, 0x00, 0xff, 0x00, 0xff, 0x00 };
@@ -202,57 +212,55 @@ bool arygon_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTx
// Packet length checksum
abtTxBuf[5] = BUFFER_LENGTH - abtTxBuf[4];
// Copy the PN53X command into the packet buffer
memmove(abtTxBuf+6,pbtTx,szTxLen);
memmove (abtTxBuf + 6, pbtTx, szTxLen);
// Calculate data payload checksum
abtTxBuf[szTxLen+6] = 0;
for(szPos=0; szPos < szTxLen; szPos++) {
abtTxBuf[szTxLen+6] -= abtTxBuf[szPos+6];
abtTxBuf[szTxLen + 6] = 0;
for (szPos = 0; szPos < szTxLen; szPos++) {
abtTxBuf[szTxLen + 6] -= abtTxBuf[szPos + 6];
}
// End of stream marker
abtTxBuf[szTxLen+7] = 0;
abtTxBuf[szTxLen + 7] = 0;
#ifdef DEBUG
PRINT_HEX("TX", abtTxBuf,szTxLen+8);
PRINT_HEX ("TX", abtTxBuf, szTxLen + 8);
#endif
res = uart_send((serial_port)pnd->nds,abtTxBuf,szTxLen+8);
res = uart_send ((serial_port) pnd->nds, abtTxBuf, szTxLen + 8);
if (res != 0) {
ERR("%s", "Unable to transmit data. (TX)");
ERR ("%s", "Unable to transmit data. (TX)");
pnd->iLastError = res;
return false;
}
#ifdef DEBUG
bzero(abtRxBuf, sizeof(abtRxBuf));
bzero (abtRxBuf, sizeof (abtRxBuf));
#endif
res = uart_receive((serial_port)pnd->nds,abtRxBuf,&szRxBufLen);
res = uart_receive ((serial_port) pnd->nds, abtRxBuf, &szRxBufLen);
if (res != 0) {
ERR("%s", "Unable to receive data. (RX)");
ERR ("%s", "Unable to receive data. (RX)");
pnd->iLastError = res;
return false;
}
#ifdef DEBUG
PRINT_HEX("RX", abtRxBuf,szRxBufLen);
PRINT_HEX ("RX", abtRxBuf, szRxBufLen);
#endif
// WARN: UART is a per byte reception, so you usually receive ACK and next frame the same time
if (!pn53x_transceive_check_ack_frame_callback(pnd, abtRxBuf, szRxBufLen))
if (!pn53x_transceive_check_ack_frame_callback (pnd, abtRxBuf, szRxBufLen))
return false;
szRxBufLen -= sizeof(ack_frame);
memmove(abtRxBuf, abtRxBuf+sizeof(ack_frame), szRxBufLen);
szRxBufLen -= sizeof (ack_frame);
memmove (abtRxBuf, abtRxBuf + sizeof (ack_frame), szRxBufLen);
if (szRxBufLen == 0) {
szRxBufLen = BUFFER_LENGTH;
do {
delay_ms(10);
res = uart_receive((serial_port)pnd->nds,abtRxBuf,&szRxBufLen);
} while (res != 0 );
#ifdef DEBUG
PRINT_HEX("RX", abtRxBuf,szRxBufLen);
#endif
delay_ms (10);
res = uart_receive ((serial_port) pnd->nds, abtRxBuf, &szRxBufLen);
} while (res != 0);
#ifdef DEBUG
PRINT_HEX ("RX", abtRxBuf, szRxBufLen);
#endif
}
/*
@@ -266,56 +274,61 @@ bool arygon_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t szTx
return false;
}
*/
if (!pn53x_transceive_check_error_frame_callback(pnd, abtRxBuf, szRxBufLen))
if (!pn53x_transceive_check_error_frame_callback (pnd, abtRxBuf, szRxBufLen))
return false;
// When the answer should be ignored, just return a successful result
if(pbtRx == NULL || pszRxLen == NULL) return true;
if (pbtRx == NULL || pszRxLen == NULL)
return true;
// Only succeed when the result is at least 00 00 FF xx Fx Dx xx .. .. .. xx 00 (x = variable)
if(szRxBufLen < 9) return false;
if (szRxBufLen < 9)
return false;
// Remove the preceding and appending bytes 00 00 ff 00 ff 00 00 00 FF xx Fx .. .. .. xx 00 (x = variable)
*pszRxLen = szRxBufLen - 9;
memcpy(pbtRx, abtRxBuf+7, *pszRxLen);
memcpy (pbtRx, abtRxBuf + 7, *pszRxLen);
return true;
}
//TODO Use tranceive function instead of raw uart send/receive for communication check.
bool
arygon_check_communication(const nfc_device_spec_t nds)
arygon_check_communication (const nfc_device_spec_t nds)
{
byte_t abtRx[BUFFER_LENGTH];
size_t szRxLen;
const byte_t attempted_result[] = { 0x00,0x00,0xff,0x00,0xff,0x00,0x00,0x00,0xff,0x09,0xf7,0xD5,0x01,0x00,'l','i','b','n','f','c',0xbc,0x00};
int res;
byte_t abtRx[BUFFER_LENGTH];
size_t szRxLen;
const byte_t attempted_result[] =
{ 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x09, 0xf7, 0xD5, 0x01, 0x00, 'l', 'i', 'b', 'n', 'f', 'c',
0xbc, 0x00 };
int res;
/** To be sure that PN532 is alive, we have put a "Diagnose" command to execute a "Communication Line Test" */
const byte_t pncmd_communication_test[] = { DEV_ARYGON_PROTOCOL_TAMA, 0x00,0x00,0xff,0x09,0xf7,0xd4,0x00,0x00,'l','i','b','n','f','c',0xbe,0x00 };
const byte_t pncmd_communication_test[] =
{ DEV_ARYGON_PROTOCOL_TAMA, 0x00, 0x00, 0xff, 0x09, 0xf7, 0xd4, 0x00, 0x00, 'l', 'i', 'b', 'n', 'f', 'c', 0xbe,
0x00 };
#ifdef DEBUG
PRINT_HEX("TX", pncmd_communication_test,sizeof(pncmd_communication_test));
PRINT_HEX ("TX", pncmd_communication_test, sizeof (pncmd_communication_test));
#endif
res = uart_send((serial_port)nds, pncmd_communication_test, sizeof(pncmd_communication_test));
res = uart_send ((serial_port) nds, pncmd_communication_test, sizeof (pncmd_communication_test));
if (res != 0) {
ERR("%s", "Unable to transmit data. (TX)");
ERR ("%s", "Unable to transmit data. (TX)");
return false;
}
res = uart_receive((serial_port)nds,abtRx,&szRxLen);
res = uart_receive ((serial_port) nds, abtRx, &szRxLen);
if (res != 0) {
ERR("%s", "Unable to receive data. (RX)");
ERR ("%s", "Unable to receive data. (RX)");
return false;
}
#ifdef DEBUG
PRINT_HEX("RX", abtRx,szRxLen);
PRINT_HEX ("RX", abtRx, szRxLen);
#endif
if(0 != memcmp(abtRx,attempted_result,sizeof(attempted_result))) {
DBG("%s", "Communication test failed, result doesn't match to attempted one.");
if (0 != memcmp (abtRx, attempted_result, sizeof (attempted_result))) {
DBG ("%s", "Communication test failed, result doesn't match to attempted one.");
return false;
}
return true;
}
+9 -9
View File
@@ -22,21 +22,21 @@
*/
#ifndef __NFC_DRIVER_ARYGON_H__
#define __NFC_DRIVER_ARYGON_H__
# define __NFC_DRIVER_ARYGON_H__
#include <nfc/nfc-types.h>
# include <nfc/nfc-types.h>
#define ARYGON_DRIVER_NAME "ARYGON"
# define ARYGON_DRIVER_NAME "ARYGON"
// Functions used by developer to handle connection to this device
nfc_device_desc_t * arygon_pick_device (void);
bool arygon_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *pszDeviceFound);
nfc_device_desc_t *arygon_pick_device (void);
bool arygon_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound);
nfc_device_t* arygon_connect(const nfc_device_desc_t* pndd);
void arygon_disconnect(nfc_device_t* pnd);
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(nfc_device_t* pnd, 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__
+19 -17
View File
@@ -27,7 +27,7 @@ Thanks to d18c7db and Okko for example code
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
# include "config.h"
#endif // HAVE_CONFIG_H
#include <stdlib.h>
@@ -35,39 +35,41 @@ Thanks to d18c7db and Okko for example code
#include "../drivers.h"
#include <nfc/nfc-messages.h>
nfc_device_desc_t * pn531_usb_pick_device (void)
nfc_device_desc_t *
pn531_usb_pick_device (void)
{
nfc_device_desc_t *pndd;
if ((pndd = malloc (sizeof (*pndd))))
{
size_t szN;
if ((pndd = malloc (sizeof (*pndd)))) {
size_t szN;
if (!pn531_usb_list_devices (pndd, 1, &szN))
{
DBG("%s", "pn531_usb_list_devices failed");
if (!pn531_usb_list_devices (pndd, 1, &szN)) {
DBG ("%s", "pn531_usb_list_devices failed");
return NULL;
}
if (szN == 0)
{
DBG("%s", "No device found");
if (szN == 0) {
DBG ("%s", "No device found");
return NULL;
}
}
return pndd;
}
bool pn531_usb_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *pszDeviceFound)
bool
pn531_usb_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound)
{
// array of {vendor,product} pairs for USB devices
usb_candidate_t candidates[]= {{0x04CC,0x0531},{0x054c,0x0193}};
usb_candidate_t candidates[] = { {0x04CC, 0x0531}
, {0x054c, 0x0193}
};
return pn53x_usb_list_devices(&pnddDevices[0], szDevices, pszDeviceFound, &candidates[0], sizeof(candidates) / sizeof(usb_candidate_t),PN531_USB_DRIVER_NAME);
return pn53x_usb_list_devices (&pnddDevices[0], szDevices, pszDeviceFound, &candidates[0],
sizeof (candidates) / sizeof (usb_candidate_t), PN531_USB_DRIVER_NAME);
}
nfc_device_t* pn531_usb_connect(const nfc_device_desc_t* pndd)
nfc_device_t *
pn531_usb_connect (const nfc_device_desc_t * pndd)
{
return pn53x_usb_connect(pndd, pndd->acDevice, NC_PN531);
return pn53x_usb_connect (pndd, pndd->acDevice, NC_PN531);
}
+5 -6
View File
@@ -22,14 +22,13 @@
*/
#ifndef __NFC_DRIVER_PN531_USB_H__
#define __NFC_DRIVER_PN531_USB_H__
# define __NFC_DRIVER_PN531_USB_H__
#define PN531_USB_DRIVER_NAME "PN531_USB"
# define PN531_USB_DRIVER_NAME "PN531_USB"
// Functions used by developer to handle connection to this device
nfc_device_t* pn531_usb_connect(const nfc_device_desc_t* pndd);
bool pn531_usb_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *pszDeviceFound);
nfc_device_desc_t * pn531_usb_pick_device (void);
nfc_device_t *pn531_usb_connect (const nfc_device_desc_t * pndd);
bool pn531_usb_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound);
nfc_device_desc_t *pn531_usb_pick_device (void);
#endif // ! __NFC_DRIVER_PN531_USB_H__
+125 -115
View File
@@ -23,7 +23,7 @@
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
# include "config.h"
#endif // HAVE_CONFIG_H
#include "../drivers.h"
@@ -43,8 +43,8 @@
#define SERIAL_DEFAULT_PORT_SPEED 115200
void pn532_uart_wakeup(const nfc_device_spec_t nds);
bool pn532_uart_check_communication(const nfc_device_spec_t nds, bool* success);
void pn532_uart_wakeup (const nfc_device_spec_t nds);
bool pn532_uart_check_communication (const nfc_device_spec_t nds, bool * success);
nfc_device_desc_t *
pn532_uart_pick_device (void)
@@ -52,15 +52,15 @@ pn532_uart_pick_device (void)
nfc_device_desc_t *pndd;
if ((pndd = malloc (sizeof (*pndd)))) {
size_t szN;
size_t szN;
if (!pn532_uart_list_devices (pndd, 1, &szN)) {
DBG("%s", "pn532_uart_list_devices failed");
DBG ("%s", "pn532_uart_list_devices failed");
return NULL;
}
if (szN == 0) {
DBG("%s", "No device found");
DBG ("%s", "No device found");
return NULL;
}
}
@@ -69,95 +69,102 @@ pn532_uart_pick_device (void)
}
bool
pn532_uart_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *pszDeviceFound)
pn532_uart_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound)
{
/** @note: Due to UART bus we can't know if its really a pn532 without
* sending some PN53x commands. But using this way to probe devices, we can
* have serious problem with other device on this bus */
#ifndef SERIAL_AUTOPROBE_ENABLED
(void)pnddDevices;
(void)szDevices;
(void) pnddDevices;
(void) szDevices;
*pszDeviceFound = 0;
DBG("%s", "Serial auto-probing have been disabled at compile time. Skipping autoprobe.");
DBG ("%s", "Serial auto-probing have been disabled at compile time. Skipping autoprobe.");
return false;
#else /* SERIAL_AUTOPROBE_ENABLED */
*pszDeviceFound = 0;
serial_port sp;
const char* pcPorts[] = DEFAULT_SERIAL_PORTS;
const char* pcPort;
int iDevice = 0;
while( (pcPort = pcPorts[iDevice++]) ) {
sp = uart_open(pcPort);
DBG("Trying to find PN532 device on serial port: %s at %d bauds.", pcPort, SERIAL_DEFAULT_PORT_SPEED);
const char *pcPorts[] = DEFAULT_SERIAL_PORTS;
const char *pcPort;
int iDevice = 0;
if ((sp != INVALID_SERIAL_PORT) && (sp != CLAIMED_SERIAL_PORT))
{
bool bComOk;
while ((pcPort = pcPorts[iDevice++])) {
sp = uart_open (pcPort);
DBG ("Trying to find PN532 device on serial port: %s at %d bauds.", pcPort, SERIAL_DEFAULT_PORT_SPEED);
if ((sp != INVALID_SERIAL_PORT) && (sp != CLAIMED_SERIAL_PORT)) {
bool bComOk;
// Serial port claimed but we need to check if a PN532_UART is connected.
uart_set_speed(sp, SERIAL_DEFAULT_PORT_SPEED);
uart_set_speed (sp, SERIAL_DEFAULT_PORT_SPEED);
// PN532 could be powered down, we need to wake it up before line testing.
pn532_uart_wakeup((nfc_device_spec_t)sp);
pn532_uart_wakeup ((nfc_device_spec_t) sp);
// Check communication using "Diagnose" command, with "Comunication test" (0x00)
if(!pn532_uart_check_communication((nfc_device_spec_t)sp, &bComOk))
if (!pn532_uart_check_communication ((nfc_device_spec_t) sp, &bComOk))
return false;
if (!bComOk)
continue;
uart_close(sp);
uart_close (sp);
snprintf(pnddDevices[*pszDeviceFound].acDevice, DEVICE_NAME_LENGTH - 1, "%s (%s)", "PN532", pcPort);
snprintf (pnddDevices[*pszDeviceFound].acDevice, DEVICE_NAME_LENGTH - 1, "%s (%s)", "PN532", pcPort);
pnddDevices[*pszDeviceFound].acDevice[DEVICE_NAME_LENGTH - 1] = '\0';
pnddDevices[*pszDeviceFound].pcDriver = PN532_UART_DRIVER_NAME;
pnddDevices[*pszDeviceFound].pcPort = strdup(pcPort);
pnddDevices[*pszDeviceFound].pcPort = strdup (pcPort);
pnddDevices[*pszDeviceFound].uiSpeed = SERIAL_DEFAULT_PORT_SPEED;
DBG("Device found: %s.", pnddDevices[*pszDeviceFound].acDevice);
DBG ("Device found: %s.", pnddDevices[*pszDeviceFound].acDevice);
(*pszDeviceFound)++;
// Test if we reach the maximum "wanted" devices
if((*pszDeviceFound) >= szDevices) break;
if ((*pszDeviceFound) >= szDevices)
break;
}
#ifdef DEBUG
if (sp == INVALID_SERIAL_PORT) DBG("Invalid serial port: %s", pcPort);
if (sp == CLAIMED_SERIAL_PORT) DBG("Serial port already claimed: %s", pcPort);
#endif /* DEBUG */
# ifdef DEBUG
if (sp == INVALID_SERIAL_PORT)
DBG ("Invalid serial port: %s", pcPort);
if (sp == CLAIMED_SERIAL_PORT)
DBG ("Serial port already claimed: %s", pcPort);
# endif
/* DEBUG */
}
#endif /* SERIAL_AUTOPROBE_ENABLED */
return true;
}
nfc_device_t* pn532_uart_connect(const nfc_device_desc_t* pndd)
nfc_device_t *
pn532_uart_connect (const nfc_device_desc_t * pndd)
{
serial_port sp;
nfc_device_t* pnd = NULL;
bool bComOk;
nfc_device_t *pnd = NULL;
bool bComOk;
DBG("Attempt to connect to: %s at %d bauds.",pndd->pcPort, pndd->uiSpeed);
sp = uart_open(pndd->pcPort);
DBG ("Attempt to connect to: %s at %d bauds.", pndd->pcPort, pndd->uiSpeed);
sp = uart_open (pndd->pcPort);
if (sp == INVALID_SERIAL_PORT) ERR("Invalid serial port: %s",pndd->pcPort);
if (sp == CLAIMED_SERIAL_PORT) ERR("Serial port already claimed: %s",pndd->pcPort);
if ((sp == CLAIMED_SERIAL_PORT) || (sp == INVALID_SERIAL_PORT)) return NULL;
if (sp == INVALID_SERIAL_PORT)
ERR ("Invalid serial port: %s", pndd->pcPort);
if (sp == CLAIMED_SERIAL_PORT)
ERR ("Serial port already claimed: %s", pndd->pcPort);
if ((sp == CLAIMED_SERIAL_PORT) || (sp == INVALID_SERIAL_PORT))
return NULL;
uart_set_speed (sp, pndd->uiSpeed);
uart_set_speed(sp, pndd->uiSpeed);
// PN532 could be powered down, we need to wake it up before line testing.
pn532_uart_wakeup((nfc_device_spec_t)sp);
pn532_uart_wakeup ((nfc_device_spec_t) sp);
// Check communication using "Diagnose" command, with "Comunication test" (0x00)
if(!pn532_uart_check_communication((nfc_device_spec_t)sp, &bComOk))
return NULL;
if (!pn532_uart_check_communication ((nfc_device_spec_t) sp, &bComOk))
return NULL;
if (!bComOk)
return NULL;
return NULL;
DBG("Successfully connected to: %s",pndd->pcPort);
DBG ("Successfully connected to: %s", pndd->pcPort);
// We have a connection
pnd = malloc(sizeof(nfc_device_t));
strncpy(pnd->acName, pndd->acDevice, DEVICE_NAME_LENGTH - 1);
pnd = malloc (sizeof (nfc_device_t));
strncpy (pnd->acName, pndd->acDevice, DEVICE_NAME_LENGTH - 1);
pnd->acName[DEVICE_NAME_LENGTH - 1] = '\0';
pnd->nc = NC_PN532;
pnd->nds = (nfc_device_spec_t)sp;
pnd->nds = (nfc_device_spec_t) sp;
pnd->bActive = true;
pnd->bCrc = true;
pnd->bPar = true;
@@ -165,19 +172,22 @@ nfc_device_t* pn532_uart_connect(const nfc_device_desc_t* pndd)
return pnd;
}
void pn532_uart_disconnect(nfc_device_t* pnd)
void
pn532_uart_disconnect (nfc_device_t * pnd)
{
uart_close((serial_port)pnd->nds);
free(pnd);
uart_close ((serial_port) pnd->nds);
free (pnd);
}
bool pn532_uart_transceive(nfc_device_t* pnd, 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;
int res;
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;
int res;
// TODO: Move this one level up for libnfc-1.6
uint8_t ack_frame[] = { 0x00, 0x00, 0xff, 0x00, 0xff, 0x00 };
@@ -186,63 +196,60 @@ bool pn532_uart_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t
// Packet length checksum
abtTxBuf[4] = BUFFER_LENGTH - abtTxBuf[3];
// Copy the PN53X command into the packet buffer
memmove(abtTxBuf+5,pbtTx,szTxLen);
memmove (abtTxBuf + 5, pbtTx, szTxLen);
// Calculate data payload checksum
abtTxBuf[szTxLen+5] = 0;
for(szPos=0; szPos < szTxLen; szPos++)
{
abtTxBuf[szTxLen+5] -= abtTxBuf[szPos+5];
abtTxBuf[szTxLen + 5] = 0;
for (szPos = 0; szPos < szTxLen; szPos++) {
abtTxBuf[szTxLen + 5] -= abtTxBuf[szPos + 5];
}
// End of stream marker
abtTxBuf[szTxLen+6] = 0;
abtTxBuf[szTxLen + 6] = 0;
#ifdef DEBUG
PRINT_HEX("TX", abtTxBuf,szTxLen+7);
PRINT_HEX ("TX", abtTxBuf, szTxLen + 7);
#endif
res = uart_send((serial_port)pnd->nds,abtTxBuf,szTxLen+7);
if(res != 0) {
ERR("%s", "Unable to transmit data. (TX)");
pnd->iLastError = res;
return false;
}
res = uart_receive((serial_port)pnd->nds,abtRxBuf,&szRxBufLen);
res = uart_send ((serial_port) pnd->nds, abtTxBuf, szTxLen + 7);
if (res != 0) {
ERR("%s", "Unable to receive data. (RX)");
ERR ("%s", "Unable to transmit data. (TX)");
pnd->iLastError = res;
return false;
}
res = uart_receive ((serial_port) pnd->nds, abtRxBuf, &szRxBufLen);
if (res != 0) {
ERR ("%s", "Unable to receive data. (RX)");
pnd->iLastError = res;
return false;
}
#ifdef DEBUG
PRINT_HEX("RX", abtRxBuf,szRxBufLen);
PRINT_HEX ("RX", abtRxBuf, szRxBufLen);
#endif
// WARN: UART is a per byte reception, so you usually receive ACK and next frame the same time
if (!pn53x_transceive_check_ack_frame_callback(pnd, abtRxBuf, szRxBufLen))
if (!pn53x_transceive_check_ack_frame_callback (pnd, abtRxBuf, szRxBufLen))
return false;
szRxBufLen -= sizeof(ack_frame);
memmove(abtRxBuf, abtRxBuf+sizeof(ack_frame), szRxBufLen);
szRxBufLen -= sizeof (ack_frame);
memmove (abtRxBuf, abtRxBuf + sizeof (ack_frame), szRxBufLen);
if (szRxBufLen == 0) {
szRxBufLen = BUFFER_LENGTH;
do {
delay_ms(10);
res = uart_receive((serial_port)pnd->nds,abtRxBuf,&szRxBufLen);
} while (res != 0 );
delay_ms (10);
res = uart_receive ((serial_port) pnd->nds, abtRxBuf, &szRxBufLen);
} while (res != 0);
#ifdef DEBUG
PRINT_HEX("RX", abtRxBuf,szRxBufLen);
PRINT_HEX ("RX", abtRxBuf, szRxBufLen);
#endif
}
#ifdef DEBUG
PRINT_HEX("TX", ack_frame,6);
PRINT_HEX ("TX", ack_frame, 6);
#endif
res = uart_send((serial_port)pnd->nds,ack_frame,6);
res = uart_send ((serial_port) pnd->nds, ack_frame, 6);
if (res != 0) {
ERR("%s", "Unable to transmit data. (TX)");
ERR ("%s", "Unable to transmit data. (TX)");
pnd->iLastError = res;
return false;
}
@@ -251,76 +258,79 @@ bool pn532_uart_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t
return false;
// When the answer should be ignored, just return a successful result
if(pbtRx == NULL || pszRxLen == NULL) return true;
if (pbtRx == NULL || pszRxLen == NULL)
return true;
// Only succeed when the result is at least 00 00 FF xx Fx Dx xx .. .. .. xx 00 (x = variable)
if(szRxBufLen < 9) {
if (szRxBufLen < 9) {
pnd->iLastError = DEINVAL;
return false;
}
// Remove the preceding and appending bytes 00 00 ff 00 ff 00 00 00 FF xx Fx .. .. .. xx 00 (x = variable)
*pszRxLen = szRxBufLen - 9;
memcpy(pbtRx, abtRxBuf+7, *pszRxLen);
memcpy (pbtRx, abtRxBuf + 7, *pszRxLen);
return true;
}
void
pn532_uart_wakeup(const nfc_device_spec_t nds)
pn532_uart_wakeup (const nfc_device_spec_t nds)
{
byte_t abtRx[BUFFER_LENGTH];
size_t szRxLen;
byte_t abtRx[BUFFER_LENGTH];
size_t szRxLen;
/** PN532C106 wakeup. */
/** High Speed Unit (HSU) wake up consist to send 0x55 and wait a "long" delay for PN532 being wakeup. */
/** After the preamble we request the PN532C106 chip to switch to "normal" mode (SAM is not used) */
const byte_t pncmd_pn532c106_wakeup_preamble[] = { 0x55,0x55,0x00,0x00,0x00,0x00,0x00,0xff,0x03,0xfd,0xd4,0x14,0x01,0x17,0x00,0x00,0xff,0x03,0xfd,0xd4,0x14,0x01,0x17,0x00 };
const byte_t pncmd_pn532c106_wakeup_preamble[] =
{ 0x55, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x03, 0xfd, 0xd4, 0x14, 0x01, 0x17, 0x00, 0x00, 0xff, 0x03, 0xfd,
0xd4, 0x14, 0x01, 0x17, 0x00 };
#ifdef DEBUG
PRINT_HEX("TX", pncmd_pn532c106_wakeup_preamble,sizeof(pncmd_pn532c106_wakeup_preamble));
PRINT_HEX ("TX", pncmd_pn532c106_wakeup_preamble, sizeof (pncmd_pn532c106_wakeup_preamble));
#endif
uart_send((serial_port)nds, pncmd_pn532c106_wakeup_preamble, sizeof(pncmd_pn532c106_wakeup_preamble));
if(0 == uart_receive((serial_port)nds,abtRx,&szRxLen)) {
uart_send ((serial_port) nds, pncmd_pn532c106_wakeup_preamble, sizeof (pncmd_pn532c106_wakeup_preamble));
if (0 == uart_receive ((serial_port) nds, abtRx, &szRxLen)) {
#ifdef DEBUG
PRINT_HEX("RX", abtRx,szRxLen);
PRINT_HEX ("RX", abtRx, szRxLen);
#endif
}
}
bool
pn532_uart_check_communication(const nfc_device_spec_t nds, bool* success)
pn532_uart_check_communication (const nfc_device_spec_t nds, bool * success)
{
byte_t abtRx[BUFFER_LENGTH];
size_t szRxLen;
const byte_t attempted_result[] = { 0x00,0x00,0xff,0x00,0xff,0x00,0x00,0x00,0xff,0x09,0xf7,0xD5,0x01,0x00,'l','i','b','n','f','c',0xbc,0x00};
int res;
byte_t abtRx[BUFFER_LENGTH];
size_t szRxLen;
const byte_t attempted_result[] =
{ 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x09, 0xf7, 0xD5, 0x01, 0x00, 'l', 'i', 'b', 'n', 'f', 'c',
0xbc, 0x00 };
int res;
/** To be sure that PN532 is alive, we have put a "Diagnose" command to execute a "Communication Line Test" */
const byte_t pncmd_communication_test[] = { 0x00,0x00,0xff,0x09,0xf7,0xd4,0x00,0x00,'l','i','b','n','f','c',0xbe,0x00 };
const byte_t pncmd_communication_test[] =
{ 0x00, 0x00, 0xff, 0x09, 0xf7, 0xd4, 0x00, 0x00, 'l', 'i', 'b', 'n', 'f', 'c', 0xbe, 0x00 };
*success = false;
#ifdef DEBUG
PRINT_HEX("TX", pncmd_communication_test,sizeof(pncmd_communication_test));
PRINT_HEX ("TX", pncmd_communication_test, sizeof (pncmd_communication_test));
#endif
res = uart_send((serial_port)nds, pncmd_communication_test, sizeof(pncmd_communication_test));
res = uart_send ((serial_port) nds, pncmd_communication_test, sizeof (pncmd_communication_test));
if (res != 0) {
ERR("%s", "Unable to transmit data. (TX)");
ERR ("%s", "Unable to transmit data. (TX)");
return false;
}
res = uart_receive((serial_port)nds,abtRx,&szRxLen);
res = uart_receive ((serial_port) nds, abtRx, &szRxLen);
if (res != 0) {
ERR("%s", "Unable to receive data. (RX)");
ERR ("%s", "Unable to receive data. (RX)");
return false;
}
#ifdef DEBUG
PRINT_HEX("RX", abtRx,szRxLen);
PRINT_HEX ("RX", abtRx, szRxLen);
#endif
if(0 == memcmp(abtRx,attempted_result,sizeof(attempted_result)))
if (0 == memcmp (abtRx, attempted_result, sizeof (attempted_result)))
*success = true;
return true;
}
+9 -9
View File
@@ -22,21 +22,21 @@
*/
#ifndef __NFC_DRIVER_PN532_UART_H__
#define __NFC_DRIVER_PN532_UART_H__
# define __NFC_DRIVER_PN532_UART_H__
#include <nfc/nfc-types.h>
# include <nfc/nfc-types.h>
#define PN532_UART_DRIVER_NAME "PN532_UART"
# define PN532_UART_DRIVER_NAME "PN532_UART"
// Functions used by developer to handle connection to this device
nfc_device_desc_t * pn532_uart_pick_device (void);
bool pn532_uart_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *pszDeviceFound);
nfc_device_desc_t *pn532_uart_pick_device (void);
bool pn532_uart_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound);
nfc_device_t* pn532_uart_connect(const nfc_device_desc_t* pndd);
void pn532_uart_disconnect(nfc_device_t* pnd);
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(nfc_device_t* pnd, 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__
+16 -11
View File
@@ -27,7 +27,7 @@ Thanks to d18c7db and Okko for example code
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
# include "config.h"
#endif // HAVE_CONFIG_H
#include <stdlib.h>
@@ -35,20 +35,21 @@ Thanks to d18c7db and Okko for example code
#include "../drivers.h"
#include <nfc/nfc-messages.h>
nfc_device_desc_t * pn533_usb_pick_device (void)
nfc_device_desc_t *
pn533_usb_pick_device (void)
{
nfc_device_desc_t *pndd;
if ((pndd = malloc (sizeof (*pndd)))) {
size_t szN;
size_t szN;
if (!pn533_usb_list_devices (pndd, 1, &szN)) {
DBG("%s", "pn533_usb_list_devices failed");
DBG ("%s", "pn533_usb_list_devices failed");
return NULL;
}
if (szN == 0) {
DBG("%s", "No device found");
DBG ("%s", "No device found");
return NULL;
}
}
@@ -56,16 +57,20 @@ nfc_device_desc_t * pn533_usb_pick_device (void)
return pndd;
}
bool pn533_usb_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *pszDeviceFound)
bool
pn533_usb_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound)
{
// array of {vendor,product} pairs for USB devices
usb_candidate_t candidates[]= {{0x04CC,0x2533},{0x04E6,0x5591}};
usb_candidate_t candidates[] = { {0x04CC, 0x2533}
, {0x04E6, 0x5591}
};
return pn53x_usb_list_devices(&pnddDevices[0], szDevices, pszDeviceFound, &candidates[0], sizeof(candidates) / sizeof(usb_candidate_t),PN533_USB_DRIVER_NAME);
return pn53x_usb_list_devices (&pnddDevices[0], szDevices, pszDeviceFound, &candidates[0],
sizeof (candidates) / sizeof (usb_candidate_t), PN533_USB_DRIVER_NAME);
}
nfc_device_t* pn533_usb_connect(const nfc_device_desc_t* pndd)
nfc_device_t *
pn533_usb_connect (const nfc_device_desc_t * pndd)
{
return pn53x_usb_connect(pndd, pndd->acDevice, NC_PN533);
return pn53x_usb_connect (pndd, pndd->acDevice, NC_PN533);
}
+5 -6
View File
@@ -22,14 +22,13 @@
*/
#ifndef __NFC_DRIVER_PN533_USB_H__
#define __NFC_DRIVER_PN533_USB_H__
# define __NFC_DRIVER_PN533_USB_H__
#define PN533_USB_DRIVER_NAME "PN533_USB"
# define PN533_USB_DRIVER_NAME "PN533_USB"
// Functions used by developer to handle connection to this device
nfc_device_t* pn533_usb_connect(const nfc_device_desc_t* pndd);
bool pn533_usb_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *pszDeviceFound);
nfc_device_desc_t * pn533_usb_pick_device (void);
nfc_device_t *pn533_usb_connect (const nfc_device_desc_t * pndd);
bool pn533_usb_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound);
nfc_device_desc_t *pn533_usb_pick_device (void);
#endif // ! __NFC_DRIVER_PN533_USB_H__
+113 -127
View File
@@ -24,7 +24,7 @@
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
# include "config.h"
#endif // HAVE_CONFIG_H
/*
@@ -46,113 +46,109 @@ Thanks to d18c7db and Okko for example code
#define USB_TIMEOUT 30000
// Find transfer endpoints for bulk transfers
void get_end_points(struct usb_device *dev, usb_spec_t* pus)
void
get_end_points (struct usb_device *dev, usb_spec_t * pus)
{
uint32_t uiIndex;
uint32_t uiEndPoint;
struct usb_interface_descriptor* puid = dev->config->interface->altsetting;
struct usb_interface_descriptor *puid = dev->config->interface->altsetting;
// 3 Endpoints maximum: Interrupt In, Bulk In, Bulk Out
for(uiIndex = 0; uiIndex < puid->bNumEndpoints; uiIndex++)
{
for (uiIndex = 0; uiIndex < puid->bNumEndpoints; uiIndex++) {
// Only accept bulk transfer endpoints (ignore interrupt endpoints)
if(puid->endpoint[uiIndex].bmAttributes != USB_ENDPOINT_TYPE_BULK) continue;
if (puid->endpoint[uiIndex].bmAttributes != USB_ENDPOINT_TYPE_BULK)
continue;
// Copy the endpoint to a local var, makes it more readable code
uiEndPoint = puid->endpoint[uiIndex].bEndpointAddress;
// Test if we dealing with a bulk IN endpoint
if((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_IN)
{
if ((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_IN) {
pus->uiEndPointIn = uiEndPoint;
}
// Test if we dealing with a bulk OUT endpoint
if((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_OUT)
{
if ((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_OUT) {
pus->uiEndPointOut = uiEndPoint;
}
}
}
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)
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)
{
int ret, i;
int ret,
i;
struct usb_bus *bus;
struct usb_device *dev;
usb_dev_handle *udev;
uint32_t uiBusIndex = 0;
char string[256];
char string[256];
string[0]= '\0';
usb_init();
string[0] = '\0';
usb_init ();
// usb_find_busses will find all of the busses on the system. Returns the number of changes since previous call to this function (total of new busses and busses removed).
if ((ret= usb_find_busses() < 0)) return false;
if ((ret = usb_find_busses () < 0))
return false;
// usb_find_devices will find all of the devices on each bus. This should be called after usb_find_busses. Returns the number of changes since the previous call to this function (total of new device and devices removed).
if ((ret= usb_find_devices() < 0)) return false;
if ((ret = usb_find_devices () < 0))
return false;
*pszDeviceFound = 0;
for (bus = usb_get_busses(); bus; bus = bus->next)
{
for (dev = bus->devices; dev; dev = dev->next, uiBusIndex++)
{
for(i = 0; i < num_candidates; ++i)
{
for (bus = usb_get_busses (); bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next, uiBusIndex++) {
for (i = 0; i < num_candidates; ++i) {
// DBG("Checking device %04x:%04x (%04x:%04x)",dev->descriptor.idVendor,dev->descriptor.idProduct,candidates[i].idVendor,candidates[i].idProduct);
if (candidates[i].idVendor==dev->descriptor.idVendor && candidates[i].idProduct==dev->descriptor.idProduct)
{
if (candidates[i].idVendor == dev->descriptor.idVendor && candidates[i].idProduct == dev->descriptor.idProduct) {
// Make sure there are 2 endpoints available
// with libusb-win32 we got some null pointers so be robust before looking at endpoints:
if (dev->config == NULL || dev->config->interface == NULL || dev->config->interface->altsetting == NULL)
{
if (dev->config == NULL || dev->config->interface == NULL || dev->config->interface->altsetting == NULL) {
// Nope, we maybe want the next one, let's try to find another
continue;
}
if (dev->config->interface->altsetting->bNumEndpoints < 2)
{
if (dev->config->interface->altsetting->bNumEndpoints < 2) {
// Nope, we maybe want the next one, let's try to find another
continue;
}
if (dev->descriptor.iManufacturer || dev->descriptor.iProduct)
{
udev = usb_open(dev);
if(udev)
{
usb_get_string_simple(udev, dev->descriptor.iManufacturer, string, sizeof(string));
if(strlen(string) > 0)
strcpy(string + strlen(string)," / ");
usb_get_string_simple(udev, dev->descriptor.iProduct, string + strlen(string), sizeof(string) - strlen(string));
if (dev->descriptor.iManufacturer || dev->descriptor.iProduct) {
udev = usb_open (dev);
if (udev) {
usb_get_string_simple (udev, dev->descriptor.iManufacturer, string, sizeof (string));
if (strlen (string) > 0)
strcpy (string + strlen (string), " / ");
usb_get_string_simple (udev, dev->descriptor.iProduct, string + strlen (string),
sizeof (string) - strlen (string));
}
usb_close(udev);
usb_close (udev);
}
if(strlen(string) == 0)
strcpy(pnddDevices[*pszDeviceFound].acDevice, target_name);
if (strlen (string) == 0)
strcpy (pnddDevices[*pszDeviceFound].acDevice, target_name);
else
strcpy(pnddDevices[*pszDeviceFound].acDevice, string);
strcpy (pnddDevices[*pszDeviceFound].acDevice, string);
pnddDevices[*pszDeviceFound].pcDriver = target_name;
pnddDevices[*pszDeviceFound].uiBusIndex = uiBusIndex;
(*pszDeviceFound)++;
// Test if we reach the maximum "wanted" devices
if((*pszDeviceFound) == szDevices)
{
if ((*pszDeviceFound) == szDevices) {
return true;
}
}
}
}
}
if(*pszDeviceFound)
if (*pszDeviceFound)
return true;
return false;
}
nfc_device_t* pn53x_usb_connect(const nfc_device_desc_t* pndd,const char * target_name, int target_chip)
nfc_device_t *
pn53x_usb_connect (const nfc_device_desc_t * pndd, const char *target_name, int target_chip)
{
nfc_device_t* pnd = NULL;
usb_spec_t* pus;
nfc_device_t *pnd = NULL;
usb_spec_t *pus;
usb_spec_t us;
struct usb_bus *bus;
struct usb_device *dev;
@@ -162,44 +158,39 @@ nfc_device_t* pn53x_usb_connect(const nfc_device_desc_t* pndd,const char * targe
us.uiEndPointOut = 0;
us.pudh = NULL;
DBG("Attempt to connect to %s device", target_name);
usb_init();
DBG ("Attempt to connect to %s device", target_name);
usb_init ();
uiBusIndex= pndd->uiBusIndex;
uiBusIndex = pndd->uiBusIndex;
for (bus = usb_get_busses(); bus; bus = bus->next)
{
for (dev = bus->devices; dev; dev = dev->next, uiBusIndex--)
{
for (bus = usb_get_busses (); bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next, uiBusIndex--) {
// DBG("Checking device %04x:%04x",dev->descriptor.idVendor,dev->descriptor.idProduct);
if(uiBusIndex == 0)
{
if (uiBusIndex == 0) {
// Open the USB device
us.pudh = usb_open(dev);
us.pudh = usb_open (dev);
get_end_points(dev,&us);
if(usb_set_configuration(us.pudh,1) < 0)
{
DBG("%s", "Setting config failed");
usb_close(us.pudh);
get_end_points (dev, &us);
if (usb_set_configuration (us.pudh, 1) < 0) {
DBG ("%s", "Setting config failed");
usb_close (us.pudh);
// we failed to use the specified device
return NULL;
}
if(usb_claim_interface(us.pudh,0) < 0)
{
DBG("%s", "Can't claim interface");
usb_close(us.pudh);
if (usb_claim_interface (us.pudh, 0) < 0) {
DBG ("%s", "Can't claim interface");
usb_close (us.pudh);
// we failed to use the specified device
return NULL;
}
// Allocate memory for the device info and specification, fill it and return the info
pus = malloc(sizeof(usb_spec_t));
pus = malloc (sizeof (usb_spec_t));
*pus = us;
pnd = malloc(sizeof(nfc_device_t));
strcpy(pnd->acName,target_name);
pnd = malloc (sizeof (nfc_device_t));
strcpy (pnd->acName, target_name);
pnd->nc = target_chip;
pnd->nds = (nfc_device_spec_t)pus;
pnd->nds = (nfc_device_spec_t) pus;
pnd->bActive = true;
pnd->bCrc = true;
pnd->bPar = true;
@@ -209,38 +200,40 @@ nfc_device_t* pn53x_usb_connect(const nfc_device_desc_t* pndd,const char * targe
}
}
// We ran out of devices before the index required
DBG("%s","Device index not found!");
DBG ("%s", "Device index not found!");
return NULL;
}
void pn53x_usb_disconnect(nfc_device_t* pnd)
void
pn53x_usb_disconnect (nfc_device_t * pnd)
{
usb_spec_t* pus = (usb_spec_t*)pnd->nds;
int ret;
usb_spec_t *pus = (usb_spec_t *) pnd->nds;
int ret;
if((ret = usb_release_interface(pus->pudh,0)) < 0) {
ERR("usb_release_interface failed (%i)",ret);
if ((ret = usb_release_interface (pus->pudh, 0)) < 0) {
ERR ("usb_release_interface failed (%i)", ret);
}
if((ret = usb_close(pus->pudh)) < 0) {
ERR("usb_close failed (%i)",ret);
if ((ret = usb_close (pus->pudh)) < 0) {
ERR ("usb_close failed (%i)", ret);
}
/*
if((ret = usb_reset(pus->pudh)) < 0) {
ERR("usb_reset failed (%i, if errno: %s)",ret, strerror(-ret));
}
*/
free(pnd->nds);
free(pnd);
free (pnd->nds);
free (pnd);
}
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_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*)pnd->nds;
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 *) pnd->nds;
// TODO: Move this one level up for libnfc-1.6
uint8_t ack_frame[] = { 0x00, 0x00, 0xff, 0x00, 0xff, 0x00 };
@@ -249,88 +242,81 @@ bool pn53x_usb_transceive(nfc_device_t* pnd, const byte_t* pbtTx, const size_t s
// Packet length checksum
abtTx[4] = 0x0100 - abtTx[3];
// Copy the PN53X command into the packet abtTx
memmove(abtTx+5,pbtTx,szTxLen);
memmove (abtTx + 5, pbtTx, szTxLen);
// Calculate data payload checksum
abtTx[szTxLen+5] = 0;
for(uiPos=0; uiPos < szTxLen; uiPos++)
{
abtTx[szTxLen+5] -= abtTx[uiPos+5];
abtTx[szTxLen + 5] = 0;
for (uiPos = 0; uiPos < szTxLen; uiPos++) {
abtTx[szTxLen + 5] -= abtTx[uiPos + 5];
}
// End of stream marker
abtTx[szTxLen+6] = 0;
abtTx[szTxLen + 6] = 0;
#ifdef DEBUG
PRINT_HEX("TX", abtTx,szTxLen+7);
PRINT_HEX ("TX", abtTx, szTxLen + 7);
#endif
ret = usb_bulk_write(pus->pudh, pus->uiEndPointOut, (char*)abtTx, szTxLen+7, USB_TIMEOUT);
if( ret < 0 )
{
DBG("usb_bulk_write failed with error %d", ret);
ret = usb_bulk_write (pus->pudh, pus->uiEndPointOut, (char *) abtTx, szTxLen + 7, USB_TIMEOUT);
if (ret < 0) {
DBG ("usb_bulk_write failed with error %d", ret);
pnd->iLastError = DEIO;
return false;
}
ret = usb_bulk_read(pus->pudh, pus->uiEndPointIn, (char*)abtRx, BUFFER_LENGTH, USB_TIMEOUT);
if( ret < 0 )
{
DBG( "usb_bulk_read failed with error %d", ret);
ret = usb_bulk_read (pus->pudh, pus->uiEndPointIn, (char *) abtRx, BUFFER_LENGTH, USB_TIMEOUT);
if (ret < 0) {
DBG ("usb_bulk_read failed with error %d", ret);
pnd->iLastError = DEIO;
return false;
}
#ifdef DEBUG
PRINT_HEX("RX", abtRx,ret);
PRINT_HEX ("RX", abtRx, ret);
#endif
if (!pn53x_transceive_check_ack_frame_callback (pnd, abtRx, ret))
return false;
ret = usb_bulk_read(pus->pudh, pus->uiEndPointIn, (char*)abtRx, BUFFER_LENGTH, USB_TIMEOUT);
if( ret < 0 )
{
DBG("usb_bulk_read failed with error %d", ret);
ret = usb_bulk_read (pus->pudh, pus->uiEndPointIn, (char *) abtRx, BUFFER_LENGTH, USB_TIMEOUT);
if (ret < 0) {
DBG ("usb_bulk_read failed with error %d", ret);
pnd->iLastError = DEIO;
return false;
}
return false;
}
#ifdef DEBUG
PRINT_HEX("RX", abtRx,ret);
PRINT_HEX ("RX", abtRx, ret);
#endif
#ifdef DEBUG
PRINT_HEX("TX", ack_frame,6);
PRINT_HEX ("TX", ack_frame, 6);
#endif
usb_bulk_write(pus->pudh, pus->uiEndPointOut, (char *)ack_frame, 6, USB_TIMEOUT);
usb_bulk_write (pus->pudh, pus->uiEndPointOut, (char *) ack_frame, 6, USB_TIMEOUT);
if (!pn53x_transceive_check_error_frame_callback (pnd, abtRx, ret))
return false;
// When the answer should be ignored, just return a succesful result
if(pbtRx == NULL || pszRxLen == NULL) return true;
if (pbtRx == NULL || pszRxLen == NULL)
return true;
// Only succeed when the result is at least 00 00 FF xx Fx Dx xx .. .. .. xx 00 (x = variable)
if(ret < 9)
{
DBG("%s","No data");
if (ret < 9) {
DBG ("%s", "No data");
pnd->iLastError = DEINVAL;
return false;
}
// Remove the preceding and appending bytes 00 00 FF xx Fx .. .. .. xx 00 (x = variable)
*pszRxLen = ret - 7 - 2;
// Get register: nuke extra byte (awful hack)
if ((abtRx[5]==0xd5) && (abtRx[6]==0x07) && (*pszRxLen==2)) {
// DBG("awful hack: abtRx[7]=%02x, abtRx[8]=%02x, we only keep abtRx[8]=%02x", abtRx[7], abtRx[8], abtRx[8]);
*pszRxLen = (*pszRxLen) - 1;
memcpy( pbtRx, abtRx + 8, *pszRxLen);
return true;
if ((abtRx[5] == 0xd5) && (abtRx[6] == 0x07) && (*pszRxLen == 2)) {
// DBG("awful hack: abtRx[7]=%02x, abtRx[8]=%02x, we only keep abtRx[8]=%02x", abtRx[7], abtRx[8], abtRx[8]);
*pszRxLen = (*pszRxLen) - 1;
memcpy (pbtRx, abtRx + 8, *pszRxLen);
return true;
}
memcpy( pbtRx, abtRx + 7, *pszRxLen);
memcpy (pbtRx, abtRx + 7, *pszRxLen);
return true;
}
+8 -6
View File
@@ -24,7 +24,7 @@
#include <usb.h>
typedef struct {
usb_dev_handle* pudh;
usb_dev_handle *pudh;
uint32_t uiEndPointIn;
uint32_t uiEndPointOut;
} usb_spec_t;
@@ -34,8 +34,10 @@ typedef struct {
uint16_t idProduct;
} usb_candidate_t;
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(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);
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 (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);