diff --git a/libnfc/buses/uart.h b/libnfc/buses/uart.h index 8b0ea8a..8ec4978 100644 --- a/libnfc/buses/uart.h +++ b/libnfc/buses/uart.h @@ -59,16 +59,16 @@ // Try to guess what we should use. // // XXX: Some review from users cross-compiling is welcome! -#if defined(_WIN32) - #define SERIAL_STRING "COM" -//#elif defined(__APPLE__) -// TODO: find UART connection string for PN53X device on Mac OS X -// #define SERIAL_STRING "" +#if defined (_WIN32) + #define DEFAULT_SERIAL_PORTS { "COM1", "COM2", "COM3", "COM4", NULL } +#elif defined(__APPLE__) + // XXX: find UART connection string for PN53X device on Mac OS X when multiples devices are used + #define DEFAULT_SERIAL_PORTS { "/dev/tty.SLAB_USBtoUART", NULL } #elif defined (__FreeBSD__) || defined (__OpenBSD__) // XXX: Not tested - #define SERIAL_STRING "/dev/cuau" + #define DEFAULT_SERIAL_PORTS { "/dev/cuau0", "/dev/cuau1", "/dev/cuau2", "/dev/cuau3", NULL } #elif defined (__linux__) - #define SERIAL_STRING "/dev/ttyUSB" + #define DEFAULT_SERIAL_PORTS { "/dev/ttyUSB0", "/dev/ttyUSB1", "/dev/ttyUSB2", "/dev/ttyUSB3", "/dev/tty0", "/dev/tty1", "/dev/tty2", "/dev/tty3", NULL } #else #error "Can't determine serial string for your system" #endif diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 0ff9090..eeb1bf5 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -109,21 +109,13 @@ arygon_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *p *pszDeviceFound = 0; serial_port sp; - char acPort[BUFFER_LENGTH]; - int iDevice; + const char** pcPorts = UNIX_SERIAL_PORT_DEVS; + const char* pcPort; + int iDevice = 0; - // I have no idea how MAC OS X deals with multiple devices, so a quick workaround - for (iDevice=0; iDevice<DRIVERS_MAX_DEVICES; iDevice++) - { -#ifdef __APPLE__ - strncpy(acPort, SERIAL_STRING, BUFFER_LENGTH - 1); - acPort[BUFFER_LENGTH - 1] = '\0'; -#else /* __APPLE__ */ - snprintf(acPort, BUFFER_LENGTH - 1, "%s%d", SERIAL_STRING, iDevice); - acPort[BUFFER_LENGTH - 1] = '\0'; -#endif /* __APPLE__ */ - sp = uart_open(acPort); - DBG("Trying to find ARYGON device on serial port: %s at %d bauds.",acPort, SERIAL_DEFAULT_PORT_SPEED); + while( pcPort = pcPorts[i++] ) { + 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)) { @@ -135,7 +127,7 @@ arygon_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *p snprintf(pnddDevices[*pszDeviceFound].acDevice, DEVICE_NAME_LENGTH - 1, "%s (%s)", "ARYGON", acPort); pnddDevices[*pszDeviceFound].acDevice[DEVICE_NAME_LENGTH - 1] = '\0'; pnddDevices[*pszDeviceFound].pcDriver = ARYGON_DRIVER_NAME; - pnddDevices[*pszDeviceFound].pcPort = strdup(acPort); + pnddDevices[*pszDeviceFound].pcPort = strdup(pcPort); pnddDevices[*pszDeviceFound].uiSpeed = SERIAL_DEFAULT_PORT_SPEED; DBG("Device found: %s.", pnddDevices[*pszDeviceFound].acDevice); (*pszDeviceFound)++; @@ -144,8 +136,8 @@ arygon_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *p if((*pszDeviceFound) >= szDevices) break; } #ifdef DEBUG - if (sp == INVALID_SERIAL_PORT) DBG("Invalid serial port: %s",acPort); - if (sp == CLAIMED_SERIAL_PORT) DBG("Serial port already claimed: %s",acPort); + 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 */ @@ -157,19 +149,14 @@ nfc_device_t* arygon_connect(const nfc_device_desc_t* pndd) serial_port sp; nfc_device_t* pnd = NULL; - if( pndd == NULL ) { - DBG("%s", "arygon_connect() need an nfc_device_desc_t struct."); - return NULL; - } else { - 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); diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 6359bcd..a0c3833 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -83,21 +83,13 @@ pn532_uart_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_ *pszDeviceFound = 0; serial_port sp; - char acPort[BUFFER_LENGTH]; - int iDevice; - - // I have no idea how MAC OS X deals with multiple devices, so a quick workaround - for (iDevice=0; iDevice<DRIVERS_MAX_DEVICES; iDevice++) - { -#ifdef __APPLE__ - strncpy(acPort,SERIAL_STRING, BUFFER_LENGTH - 1); - acPort[BUFFER_LENGTH - 1] = '\0'; -#else /* __APPLE__ */ - snprintf(acPort,BUFFER_LENGTH - 1,"%s%d",SERIAL_STRING,iDevice); - acPort[BUFFER_LENGTH - 1] = '\0'; -#endif /* __APPLE__ */ - sp = uart_open(acPort); - DBG("Trying to find PN532 device on serial port: %s at %d bauds.",acPort, SERIAL_DEFAULT_PORT_SPEED); + const char** pcPorts = UNIX_SERIAL_PORT_DEVS; + const char* pcPort; + int iDevice = 0; + + while( pcPort = pcPorts[i++] ) { + 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)) { @@ -109,11 +101,10 @@ pn532_uart_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_ if(!pn532_uart_check_communication((nfc_device_spec_t)sp)) continue; uart_close(sp); - snprintf(pnddDevices[*pszDeviceFound].acDevice, DEVICE_NAME_LENGTH - 1, "%s (%s)", "PN532", acPort); + 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(acPort); - pnddDevices[*pszDeviceFound].pcPort[BUFFER_LENGTH] = '\0'; + pnddDevices[*pszDeviceFound].pcPort = strdup(pcPort); pnddDevices[*pszDeviceFound].uiSpeed = SERIAL_DEFAULT_PORT_SPEED; DBG("Device found: %s.", pnddDevices[*pszDeviceFound].acDevice); (*pszDeviceFound)++; @@ -135,19 +126,15 @@ 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."); - return NULL; - } else { - 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); // Check communication using "Diagnose" command, with "Comunication test" (0x00)