From e5ba266420c78994d181db1b2e63ed3cb9f011a8 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Thu, 3 Dec 2009 15:57:13 +0000 Subject: [PATCH] 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.) --- src/lib/drivers.h | 12 ++-- src/lib/drivers/acr122.c | 2 +- src/lib/drivers/pn532_uart.c | 122 ++++++++++++++++++++++++----------- src/lib/drivers/pn532_uart.h | 3 + 4 files changed, 96 insertions(+), 43 deletions(-) diff --git a/src/lib/drivers.h b/src/lib/drivers.h index 91eabc0..57f1fff 100644 --- a/src/lib/drivers.h +++ b/src/lib/drivers.h @@ -42,16 +42,16 @@ #define MAX_FRAME_LEN 264 const static struct driver_callbacks drivers_callbacks_list[] = { -// Driver Name Pick Device List Devices Connect Transceive Disconnect +// Driver Name Pick Device List Devices Connect Transceive Disconnect #ifdef HAVE_PCSC_LITE - { ACR122_DRIVER_NAME, acr122_pick_device, acr122_list_devices, acr122_connect, acr122_transceive, acr122_disconnect }, + { ACR122_DRIVER_NAME, acr122_pick_device, acr122_list_devices, acr122_connect, acr122_transceive, acr122_disconnect }, #endif /* HAVE_PCSC_LITE */ #ifdef HAVE_LIBUSB - { 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 }, + { 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 }, #endif /* HAVE_LIBUSB */ - { PN532_UART_DRIVER_NAME, NULL, NULL, pn532_uart_connect, pn532_uart_transceive, pn532_uart_disconnect }, - { ARYGON_DRIVER_NAME, NULL, NULL, arygon_connect, arygon_transceive, arygon_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 } }; #endif // __NFC_DRIVERS_H__ diff --git a/src/lib/drivers/acr122.c b/src/lib/drivers/acr122.c index f8c8a39..51b7e15 100644 --- a/src/lib/drivers/acr122.c +++ b/src/lib/drivers/acr122.c @@ -146,7 +146,7 @@ acr122_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *p // Test if context succeeded if (!(pscc = acr122_get_scardcontext ())) { - DBG("%s","PCSC-LITE daemon not found"); + DBG("%s","PCSC context not found"); return false; } diff --git a/src/lib/drivers/pn532_uart.c b/src/lib/drivers/pn532_uart.c index 0227aab..95d95cf 100644 --- a/src/lib/drivers/pn532_uart.c +++ b/src/lib/drivers/pn532_uart.c @@ -22,6 +22,7 @@ */ #define _XOPEN_SOURCE 500 #include +#include #include "pn532_uart.h" @@ -54,52 +55,99 @@ #define SERIAL_DEFAULT_PORT_SPEED 115200 +nfc_device_desc_t * +pn532_uart_pick_device (void) +{ + nfc_device_desc_t *pndd; + + if ((pndd = malloc (sizeof (*pndd)))) { + 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; + +#ifdef DISABLE_SERIAL_AUTOPROBE + INFO("Sorry, serial auto-probing have been disabled at compile time."); + return false; +#else /* DISABLE_SERIAL_AUTOPROBE */ + char acConnect[BUFFER_LENGTH]; + serial_port sp; + + + // I have no idea how MAC OS X deals with multiple devices, so a quick workaround + for (int iDevice=0; iDevice= szDevices) break; + } +#ifdef DEBUG + if (sp == INVALID_SERIAL_PORT) DBG("Invalid serial port: %s",acConnect); + if (sp == CLAIMED_SERIAL_PORT) DBG("Serial port already claimed: %s",acConnect); +#endif /* DEBUG */ + } +#endif + return true; +} + nfc_device_t* pn532_uart_connect(const nfc_device_desc_t* pndd) { - uint32_t uiDevNr; serial_port sp; - char acConnect[BUFFER_LENGTH]; nfc_device_t* pnd = NULL; if( pndd == NULL ) { -#ifdef DISABLE_SERIAL_AUTOPROBE - INFO("Sorry, serial auto-probing have been disabled at compile time."); - return NULL; -#else - DBG("Trying to find ARYGON device on serial port: %s# at %d bauds.",SERIAL_STRING, SERIAL_DEFAULT_PORT_SPEED); - // I have no idea how MAC OS X deals with multiple devices, so a quick workaround - for (uiDevNr=0; uiDevNrpcPort, pndd->uiSpeed); - strcpy(acConnect,pndd->pcPort); - sp = uart_open(acConnect); - if (sp == INVALID_SERIAL_PORT) ERR("Invalid serial port: %s",acConnect); - if (sp == CLAIMED_SERIAL_PORT) ERR("Serial port already claimed: %s",acConnect); + 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; uart_set_speed(sp, pndd->uiSpeed); } + /** @info PN532C106 wakeup. */ /** @todo Put this command in pn53x init process */ 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); #endif - DBG("Successfully connected to: %s",acConnect); + DBG("Successfully connected to: %s",pndd->pcPort); // We have a connection 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->nds = (nfc_device_spec_t)sp; pnd->bActive = true; diff --git a/src/lib/drivers/pn532_uart.h b/src/lib/drivers/pn532_uart.h index a49aad2..d364f10 100644 --- a/src/lib/drivers/pn532_uart.h +++ b/src/lib/drivers/pn532_uart.h @@ -29,6 +29,9 @@ #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_t* pn532_uart_connect(const nfc_device_desc_t* pndd); void pn532_uart_disconnect(nfc_device_t* pnd);