Add list_devices capabilities to PN532_UART driver. Note: Theses functions exists for this driver only for convenience, please note that it can be anoying if there are others devices plugged on UART (ie. ttySx, ttyUSBx, COM, etc.)

This commit is contained in:
Romuald Conty 2009-12-03 15:57:13 +00:00
parent 86dac7a4bb
commit e5ba266420
4 changed files with 96 additions and 43 deletions

View file

@ -50,7 +50,7 @@ const static struct driver_callbacks drivers_callbacks_list[] = {
{ PN531_USB_DRIVER_NAME, NULL, NULL, pn531_usb_connect, pn531_usb_transceive, pn531_usb_disconnect }, { PN531_USB_DRIVER_NAME, NULL, NULL, pn531_usb_connect, pn531_usb_transceive, pn531_usb_disconnect },
{ PN533_USB_DRIVER_NAME, NULL, NULL, pn533_usb_connect, pn533_usb_transceive, pn533_usb_disconnect }, { PN533_USB_DRIVER_NAME, NULL, NULL, pn533_usb_connect, pn533_usb_transceive, pn533_usb_disconnect },
#endif /* HAVE_LIBUSB */ #endif /* HAVE_LIBUSB */
{ PN532_UART_DRIVER_NAME, NULL, NULL, pn532_uart_connect, pn532_uart_transceive, pn532_uart_disconnect }, { PN532_UART_DRIVER_NAME, pn532_uart_pick_device, pn532_uart_list_devices, pn532_uart_connect, pn532_uart_transceive, pn532_uart_disconnect },
{ ARYGON_DRIVER_NAME, NULL, NULL, arygon_connect, arygon_transceive, arygon_disconnect } { ARYGON_DRIVER_NAME, NULL, NULL, arygon_connect, arygon_transceive, arygon_disconnect }
}; };

View file

@ -146,7 +146,7 @@ acr122_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *p
// Test if context succeeded // Test if context succeeded
if (!(pscc = acr122_get_scardcontext ())) if (!(pscc = acr122_get_scardcontext ()))
{ {
DBG("%s","PCSC-LITE daemon not found"); DBG("%s","PCSC context not found");
return false; return false;
} }

View file

@ -22,6 +22,7 @@
*/ */
#define _XOPEN_SOURCE 500 #define _XOPEN_SOURCE 500
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include "pn532_uart.h" #include "pn532_uart.h"
@ -54,33 +55,71 @@
#define SERIAL_DEFAULT_PORT_SPEED 115200 #define SERIAL_DEFAULT_PORT_SPEED 115200
nfc_device_t* pn532_uart_connect(const nfc_device_desc_t* pndd) nfc_device_desc_t *
pn532_uart_pick_device (void)
{ {
uint32_t uiDevNr; nfc_device_desc_t *pndd;
serial_port sp;
char acConnect[BUFFER_LENGTH]; if ((pndd = malloc (sizeof (*pndd)))) {
nfc_device_t* pnd = NULL; size_t szN;
if (!pn532_uart_list_devices (pndd, 1, &szN)) {
ERR("%s", "pn532_uart_list_devices failed");
return NULL;
}
if (szN == 0) {
ERR("%s", "No device found");
return NULL;
}
}
return pndd;
}
bool
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 */
*pszDeviceFound = 0;
if( pndd == NULL ) {
#ifdef DISABLE_SERIAL_AUTOPROBE #ifdef DISABLE_SERIAL_AUTOPROBE
INFO("Sorry, serial auto-probing have been disabled at compile time."); INFO("Sorry, serial auto-probing have been disabled at compile time.");
return NULL; return false;
#else #else /* DISABLE_SERIAL_AUTOPROBE */
DBG("Trying to find ARYGON device on serial port: %s# at %d bauds.",SERIAL_STRING, SERIAL_DEFAULT_PORT_SPEED); char acConnect[BUFFER_LENGTH];
serial_port sp;
// I have no idea how MAC OS X deals with multiple devices, so a quick workaround // I have no idea how MAC OS X deals with multiple devices, so a quick workaround
for (uiDevNr=0; uiDevNr<DRIVERS_MAX_DEVICES; uiDevNr++) for (int iDevice=0; iDevice<DRIVERS_MAX_DEVICES; iDevice++)
{ {
#ifdef __APPLE__ #ifdef __APPLE__
strcpy(acConnect,SERIAL_STRING); strcpy(acConnect,SERIAL_STRING);
#else #else /* __APPLE__ */
sprintf(acConnect,"%s%d",SERIAL_STRING,uiDevNr); sprintf(acConnect,"%s%d",SERIAL_STRING,iDevice);
#endif /* __APPLE__ */ #endif /* __APPLE__ */
sp = uart_open(acConnect); sp = uart_open(acConnect);
DBG("Trying to find PN532 device on serial port: %s at %d bauds.",acConnect, SERIAL_DEFAULT_PORT_SPEED);
if ((sp != INVALID_SERIAL_PORT) && (sp != CLAIMED_SERIAL_PORT)) if ((sp != INVALID_SERIAL_PORT) && (sp != CLAIMED_SERIAL_PORT))
{ {
uart_set_speed(sp, SERIAL_DEFAULT_PORT_SPEED); // PN532_UART device found
break; uart_close(sp);
snprintf(pnddDevices[*pszDeviceFound].acDevice, BUFSIZ - 1, "%s (%s)", "PN532", acConnect);
pnddDevices[*pszDeviceFound].acDevice[BUFSIZ - 1] = '\0';
pnddDevices[*pszDeviceFound].pcDriver = PN532_UART_DRIVER_NAME;
//pnddDevices[*pszDeviceFound].pcPort = strndup(acConnect, BUFFER_LENGTH - 1);
pnddDevices[*pszDeviceFound].pcPort = strdup(acConnect);
pnddDevices[*pszDeviceFound].pcPort[BUFFER_LENGTH] = '\0';
pnddDevices[*pszDeviceFound].uiSpeed = SERIAL_DEFAULT_PORT_SPEED;
DBG("Device found: %s.", pnddDevices[*pszDeviceFound].acDevice);
(*pszDeviceFound)++;
// Test if we reach the maximum "wanted" devices
if((*pszDeviceFound) >= szDevices) break;
} }
#ifdef DEBUG #ifdef DEBUG
if (sp == INVALID_SERIAL_PORT) DBG("Invalid serial port: %s",acConnect); if (sp == INVALID_SERIAL_PORT) DBG("Invalid serial port: %s",acConnect);
@ -88,18 +127,27 @@ nfc_device_t* pn532_uart_connect(const nfc_device_desc_t* pndd)
#endif /* DEBUG */ #endif /* DEBUG */
} }
#endif #endif
// Test if we have found a device return true;
if (uiDevNr == DRIVERS_MAX_DEVICES) return NULL; }
nfc_device_t* pn532_uart_connect(const nfc_device_desc_t* pndd)
{
serial_port sp;
nfc_device_t* pnd = NULL;
if( pndd == NULL ) {
DBG("%s", "pn532_uart_connect() need an nfc_device_desc_t struct.");
} else { } else {
DBG("Connecting to: %s at %d bauds.",pndd->pcPort, pndd->uiSpeed); DBG("Connecting to: %s at %d bauds.",pndd->pcPort, pndd->uiSpeed);
strcpy(acConnect,pndd->pcPort); sp = uart_open(pndd->pcPort);
sp = uart_open(acConnect);
if (sp == INVALID_SERIAL_PORT) ERR("Invalid serial port: %s",acConnect); if (sp == INVALID_SERIAL_PORT) ERR("Invalid serial port: %s",pndd->pcPort);
if (sp == CLAIMED_SERIAL_PORT) ERR("Serial port already claimed: %s",acConnect); 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 == CLAIMED_SERIAL_PORT) || (sp == INVALID_SERIAL_PORT)) return NULL;
uart_set_speed(sp, pndd->uiSpeed); uart_set_speed(sp, pndd->uiSpeed);
} }
/** @info PN532C106 wakeup. */ /** @info PN532C106 wakeup. */
/** @todo Put this command in pn53x init process */ /** @todo Put this command in pn53x init process */
byte_t abtRxBuf[BUFFER_LENGTH]; byte_t abtRxBuf[BUFFER_LENGTH];
@ -118,11 +166,13 @@ nfc_device_t* pn532_uart_connect(const nfc_device_desc_t* pndd)
print_hex(abtRxBuf,szRxBufLen); print_hex(abtRxBuf,szRxBufLen);
#endif #endif
DBG("Successfully connected to: %s",acConnect); DBG("Successfully connected to: %s",pndd->pcPort);
// We have a connection // We have a connection
pnd = malloc(sizeof(nfc_device_t)); pnd = malloc(sizeof(nfc_device_t));
strcpy(pnd->acName,"PN532_UART"); strncpy(pnd->acName, pndd->acDevice, DEVICE_NAME_LENGTH - 1);
pnd->acName[DEVICE_NAME_LENGTH] = '\0';
pnd->nc = NC_PN532; pnd->nc = NC_PN532;
pnd->nds = (nfc_device_spec_t)sp; pnd->nds = (nfc_device_spec_t)sp;
pnd->bActive = true; pnd->bActive = true;

View file

@ -29,6 +29,9 @@
#define PN532_UART_DRIVER_NAME "PN532_UART" #define PN532_UART_DRIVER_NAME "PN532_UART"
// Functions used by developer to handle connection to this device // 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_t* pn532_uart_connect(const nfc_device_desc_t* pndd); nfc_device_t* pn532_uart_connect(const nfc_device_desc_t* pndd);
void pn532_uart_disconnect(nfc_device_t* pnd); void pn532_uart_disconnect(nfc_device_t* pnd);