diff --git a/configure.ac b/configure.ac index 38b6bf8..7b58421 100644 --- a/configure.ac +++ b/configure.ac @@ -84,17 +84,6 @@ fi # Handle --with-drivers option LIBNFC_ARG_WITH_DRIVERS -# Serial autoprobing support (default:no) -AC_ARG_ENABLE([serial-autoprobe],AS_HELP_STRING([--enable-serial-autoprobe],[Allow serial ports to be probed (can seriously disturb connected serial devices)]),[enable_serial_autoprobe=$enableval],[enable_serial_autoprobe="no"]) - -AC_MSG_CHECKING(for serial autoprobe flag) -AC_MSG_RESULT($enable_serial_autoprobe) - -if test x"$enable_serial_autoprobe" = "xyes" -then - CFLAGS="$CFLAGS -DSERIAL_AUTOPROBE_ENABLED" -fi - # Documentation (default: no) AC_ARG_ENABLE([doc],AS_HELP_STRING([--enable-doc],[Enable documentation generation.]),[enable_doc=$enableval],[enable_doc="no"]) diff --git a/include/nfc/nfc-types.h b/include/nfc/nfc-types.h index 514e207..acdf061 100644 --- a/include/nfc/nfc-types.h +++ b/include/nfc/nfc-types.h @@ -3,7 +3,7 @@ * * Copyright (C) 2009 Roel Verdult * Copyright (C) 2010, 2011 Romain Tartière - * Copyright (C) 2010, 2011 Romuald Conty + * Copyright (C) 2010, 2011, 2012 Romuald Conty * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the @@ -32,7 +32,10 @@ # include # include -typedef void *nfc_context; +/** + * NFC context + */ +typedef struct nfc_context nfc_context; /** * NFC device diff --git a/libnfc/drivers/acr122_usb.c b/libnfc/drivers/acr122_usb.c index a950b90..b8a9fc1 100644 --- a/libnfc/drivers/acr122_usb.c +++ b/libnfc/drivers/acr122_usb.c @@ -325,7 +325,6 @@ acr122_usb_scan(nfc_connstring connstrings[], const size_t connstrings_len) } size_t device_found = 0; - uint32_t uiBusIndex = 0; struct usb_bus *bus; for (bus = usb_get_busses(); bus; bus = bus->next) { @@ -862,6 +861,7 @@ const struct pn53x_io acr122_usb_io = { const struct nfc_driver acr122_usb_driver = { .name = ACR122_USB_DRIVER_NAME, + .scan_type = NOT_INTRUSIVE, .scan = acr122_usb_scan, .open = acr122_usb_open, .close = acr122_usb_close, diff --git a/libnfc/drivers/acr122s.c b/libnfc/drivers/acr122s.c index f46e0fe..a111608 100644 --- a/libnfc/drivers/acr122s.c +++ b/libnfc/drivers/acr122s.c @@ -450,15 +450,6 @@ acr122s_connstring_decode(const nfc_connstring connstring, struct acr122s_descri static size_t acr122s_scan(nfc_connstring connstrings[], const size_t connstrings_len) { - /** @note: Due to UART bus we can't know if its really an ACR122S without - * sending some commands. But using this way to scan devices, we can - * have serious problem with other device on this bus */ -#ifndef SERIAL_AUTOPROBE_ENABLED - (void) connstrings; - (void) connstrings_len; - log_put(LOG_CATEGORY, NFC_PRIORITY_INFO, "%s", "Serial auto-probing have been disabled at compile time. Skipping autoscan."); - return 0; -#else /* SERIAL_AUTOPROBE_ENABLED */ size_t device_found = 0; serial_port sp; char **acPorts = uart_list_ports(); @@ -521,7 +512,6 @@ acr122s_scan(nfc_connstring connstrings[], const size_t connstrings_len) } free(acPorts); return device_found; -#endif /* SERIAL_AUTOPROBE_ENABLED */ } static void @@ -703,6 +693,7 @@ const struct pn53x_io acr122s_io = { const struct nfc_driver acr122s_driver = { .name = ACR122S_DRIVER_NAME, + .scan_type = INTRUSIVE, .scan = acr122s_scan, .open = acr122s_open, .close = acr122s_close, diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 753afdb..eb92873 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -94,15 +94,6 @@ void arygon_firmware(nfc_device *pnd, char *str); static size_t arygon_scan(nfc_connstring connstrings[], const size_t connstrings_len) { - /** @note: Due to UART bus we can't know if its really an ARYGON without - * sending some commands. But using this way to scan devices, we can - * have serious problem with other device on this bus */ -#ifndef SERIAL_AUTOPROBE_ENABLED - (void) connstrings; - (void) connstrings_len; - log_put(LOG_CATEGORY, NFC_PRIORITY_INFO, "%s", "Serial auto-probing have been disabled at compile time. Skipping autoscan."); - return 0; -#else /* SERIAL_AUTOPROBE_ENABLED */ size_t device_found = 0; serial_port sp; char **acPorts = uart_list_ports(); @@ -159,7 +150,6 @@ arygon_scan(nfc_connstring connstrings[], const size_t connstrings_len) } free(acPorts); return device_found; -#endif /* SERIAL_AUTOPROBE_ENABLED */ } struct arygon_descriptor { @@ -564,6 +554,7 @@ const struct pn53x_io arygon_tama_io = { const struct nfc_driver arygon_driver = { .name = ARYGON_DRIVER_NAME, + .scan_type = INTRUSIVE, .scan = arygon_scan, .open = arygon_open, .close = arygon_close, diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 9053182..dc20f83 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -67,15 +67,6 @@ int pn532_uart_wakeup(nfc_device *pnd); static size_t pn532_uart_scan(nfc_connstring connstrings[], const size_t connstrings_len) { - /** @note: Due to UART bus we can't know if its really a pn532 without - * sending some PN53x commands. But using this way to scan devices, we can - * have serious problem with other device on this bus */ -#ifndef SERIAL_AUTOPROBE_ENABLED - (void) connstrings; - (void) connstrings_len; - log_put(LOG_CATEGORY, NFC_PRIORITY_INFO, "%s", "Serial auto-probing have been disabled at compile time. Skipping autoscan."); - return 0; -#else /* SERIAL_AUTOPROBE_ENABLED */ size_t device_found = 0; serial_port sp; char **acPorts = uart_list_ports(); @@ -136,7 +127,6 @@ pn532_uart_scan(nfc_connstring connstrings[], const size_t connstrings_len) } free(acPorts); return device_found; -#endif /* SERIAL_AUTOPROBE_ENABLED */ } struct pn532_uart_descriptor { @@ -505,6 +495,7 @@ const struct pn53x_io pn532_uart_io = { const struct nfc_driver pn532_uart_driver = { .name = PN532_UART_DRIVER_NAME, + .scan_type = INTRUSIVE, .scan = pn532_uart_scan, .open = pn532_uart_open, .close = pn532_uart_close, diff --git a/libnfc/nfc-internal.c b/libnfc/nfc-internal.c index 7693f93..f3fd64d 100644 --- a/libnfc/nfc-internal.c +++ b/libnfc/nfc-internal.c @@ -1,7 +1,7 @@ /*- * Public platform independent Near Field Communication (NFC) library * - * Copyright (C) 2011 Romuald Conty + * Copyright (C) 2011, 2012 Romuald Conty * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the @@ -25,6 +25,44 @@ #include #include "nfc-internal.h" +#include +#include + +static bool +string_as_boolean(const char* s) +{ + if ((s) && ( + (strcmp(s, "yes") == 0) || + (strcmp(s, "true") == 0) || + (strcmp(s, "1") == 0))) { + return true; + } + return false; +} + +nfc_context * +nfc_context_new(void) +{ + nfc_context *res = malloc(sizeof(*res)); + + if (!res) { + err(EXIT_FAILURE, "nfc_context_new: malloc"); + } + + // Load "intrusive scan" option + // XXX: Load this option from configuration file too ? + char *envvar = getenv("LIBNFC_INTRUSIVE_SCAN"); + res->allow_intrusive_scan = string_as_boolean(envvar); + log_put ("libnfc", NFC_PRIORITY_DEBUG, "allow_intrusive_scan is set to %s", (res->allow_intrusive_scan)?"true":"false"); + return res; +} + +void +nfc_context_free(nfc_context *context) +{ + free(context); +} + void prepare_initiator_data(const nfc_modulation nm, uint8_t **ppbtInitiatorData, size_t *pszInitiatorData) { diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index d4d9708..15503ec 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -107,8 +107,15 @@ } \ } while (0) +typedef enum { + NOT_INTRUSIVE, + INTRUSIVE, + NOT_AVAILABLE, +} scan_type_enum; + struct nfc_driver { const char *name; + const scan_type_enum scan_type; size_t (*scan)(nfc_connstring connstrings[], const size_t connstrings_len); struct nfc_device *(*open)(const nfc_connstring connstring); void (*close)(struct nfc_device *pnd); @@ -145,6 +152,18 @@ struct nfc_driver { # define DEVICE_NAME_LENGTH 256 # define DEVICE_PORT_LENGTH 64 +/** + * @struct nfc_context + * @brief NFC library context + * Struct which contains internal options, references, pointers, etc. used by library + */ +struct nfc_context { + bool allow_intrusive_scan; +}; + +nfc_context *nfc_context_new(void); +void nfc_context_free(nfc_context *context); + /** * @struct nfc_device * @brief NFC device information @@ -173,11 +192,11 @@ struct nfc_device { int last_error; }; -nfc_device *nfc_device_new(const nfc_connstring connstring); -void nfc_device_free(nfc_device *dev); +nfc_device *nfc_device_new(const nfc_connstring connstring); +void nfc_device_free(nfc_device *dev); -void iso14443_cascade_uid(const uint8_t abtUID[], const size_t szUID, uint8_t *pbtCascadedUID, size_t *pszCascadedUID); +void iso14443_cascade_uid(const uint8_t abtUID[], const size_t szUID, uint8_t *pbtCascadedUID, size_t *pszCascadedUID); -void prepare_initiator_data(const nfc_modulation nm, uint8_t **ppbtInitiatorData, size_t *pszInitiatorData); +void prepare_initiator_data(const nfc_modulation nm, uint8_t **ppbtInitiatorData, size_t *pszInitiatorData); #endif // __NFC_INTERNAL_H__ diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 5750551..2e25ddb 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -125,7 +125,7 @@ nfc_init(nfc_context *context) void nfc_exit(nfc_context *context) { - (void) context; + if(context) nfc_context_free(context); log_fini(); } @@ -251,7 +251,7 @@ nfc_close(nfc_device *pnd) } /** @ingroup dev - * @brief Probe for discoverable supported devices (ie. only available for some drivers) + * @brief Scan for discoverable supported devices (ie. only available for some drivers) * @return Returns the number of devices found. * @param context The context to operate on, or NULL for the default context. * @param connstrings array of \a nfc_connstring. @@ -265,17 +265,20 @@ nfc_list_devices(nfc_context *context, nfc_connstring connstrings[], const size_ const struct nfc_driver *ndr; const struct nfc_driver **pndr = nfc_drivers; - (void) context; + if (!context) context = nfc_context_new(); // Should we support NULL context ? + // FIXME: Load device(s) from configuration file(s) while ((ndr = *pndr)) { size_t _device_found = 0; - _device_found = ndr->scan(connstrings + (device_found), connstrings_len - (device_found)); - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%ld device(s) found using %s driver", (unsigned long) _device_found, ndr->name); - if (_device_found > 0) { - device_found += _device_found; - if (device_found == connstrings_len) - break; - } + if((ndr->scan_type == NOT_INTRUSIVE) || ((context->allow_intrusive_scan) && (ndr->scan_type == INTRUSIVE))) { + _device_found = ndr->scan(connstrings + (device_found), connstrings_len - (device_found)); + log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%ld device(s) found using %s driver", (unsigned long) _device_found, ndr->name); + if (_device_found > 0) { + device_found += _device_found; + if (device_found == connstrings_len) + break; + } + } // scan_type is INTRUSIVE but not allowed or NOT_AVAILABLE pndr++; } log_fini();