From b5aa91fd6236391e24edd90b7f48caa88ebfa2c8 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Sun, 21 Oct 2012 14:11:17 +0000 Subject: [PATCH] Use a more appropriated term regarding device detection: from probe to scan - Rename nfc-probe to nfc-scan-device - Rename internal drivers function prototypes: _probe to _scan - Revamp _scan function prototype: it now returns device found count --- libnfc/drivers/acr122_pcsc.c | 39 ++++++++++-------------- libnfc/drivers/acr122_usb.c | 24 +++++++-------- libnfc/drivers/acr122s.c | 24 +++++++-------- libnfc/drivers/arygon.c | 24 +++++++-------- libnfc/drivers/pn532_uart.c | 24 +++++++-------- libnfc/drivers/pn53x_usb.c | 26 ++++++++-------- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 24 +++++++-------- utils/Makefile.am | 6 ++-- utils/{nfc-probe.c => nfc-scan-device.c} | 2 +- 10 files changed, 90 insertions(+), 105 deletions(-) rename utils/{nfc-probe.c => nfc-scan-device.c} (99%) diff --git a/libnfc/drivers/acr122_pcsc.c b/libnfc/drivers/acr122_pcsc.c index c4e45f7..bbd9935 100644 --- a/libnfc/drivers/acr122_pcsc.c +++ b/libnfc/drivers/acr122_pcsc.c @@ -135,15 +135,14 @@ acr122_pcsc_free_scardcontext(void) /** * @brief List opened devices * - * Probe PCSC to find NFC capable hardware. + * Probe PCSC to find ACR122 devices (ACR122U and Touchatag/Tikitag). * - * @param pnddDevices Array of nfc_device_desc_t previously allocated by the caller. - * @param szDevices size of the pnddDevices array. - * @param pszDeviceFound number of devices found. - * @return true if succeeded, false otherwise. + * @param connstring array of nfc_connstring where found device's connection strings will be stored. + * @param connstrings_len size of connstrings array. + * @return number of devices found. */ -static bool -acr122_pcsc_probe(nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound) +static size_t +acr122_pcsc_scan(nfc_connstring connstrings[], const size_t connstrings_len) { size_t szPos = 0; char acDeviceNames[256 + 64 * PCSC_MAX_DEVICES]; @@ -155,22 +154,18 @@ acr122_pcsc_probe(nfc_connstring connstrings[], size_t connstrings_len, size_t * // Clear the reader list memset(acDeviceNames, '\0', szDeviceNamesLen); - *pszDeviceFound = 0; - // Test if context succeeded if (!(pscc = acr122_pcsc_get_scardcontext())) { log_put(LOG_CATEGORY, NFC_PRIORITY_WARN, "%s", "PCSC context not found (make sure PCSC daemon is running)."); - return false; + return 0; } // Retrieve the string array of all available pcsc readers DWORD dwDeviceNamesLen = szDeviceNamesLen; if (SCardListReaders(*pscc, NULL, acDeviceNames, &dwDeviceNamesLen) != SCARD_S_SUCCESS) - return false; - - while ((acDeviceNames[szPos] != '\0') && ((*pszDeviceFound) < connstrings_len)) { - - // DBG("- %s (pos=%ld)", acDeviceNames + szPos, (unsigned long) szPos); + return 0; + size_t device_found = 0; + while ((acDeviceNames[szPos] != '\0') && (device_found < connstrings_len)) { bSupported = false; for (i = 0; supported_devices[i] && !bSupported; i++) { int l = strlen(supported_devices[i]); @@ -179,8 +174,8 @@ acr122_pcsc_probe(nfc_connstring connstrings[], size_t connstrings_len, size_t * if (bSupported) { // Supported ACR122 device found - snprintf(connstrings[*pszDeviceFound], sizeof(nfc_connstring), "%s:%s", ACR122_PCSC_DRIVER_NAME, acDeviceNames + szPos); - (*pszDeviceFound)++; + snprintf(connstrings[device_found], sizeof(nfc_connstring), "%s:%s", ACR122_PCSC_DRIVER_NAME, acDeviceNames + szPos); + device_found++; } else { log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "PCSC device [%s] is not NFC capable or not supported by libnfc.", acDeviceNames + szPos); } @@ -190,7 +185,7 @@ acr122_pcsc_probe(nfc_connstring connstrings[], size_t connstrings_len, size_t * } acr122_pcsc_free_scardcontext(); - return true; + return device_found; } struct acr122_pcsc_descriptor { @@ -248,8 +243,7 @@ acr122_pcsc_open(const nfc_connstring connstring) nfc_connstring fullconnstring; if (connstring_decode_level == 1) { // Device was not specified, take the first one we can find - size_t szDeviceFound; - acr122_pcsc_probe(&fullconnstring, 1, &szDeviceFound); + size_t szDeviceFound = acr122_pcsc_scan(&fullconnstring, 1); if (szDeviceFound < 1) return NULL; connstring_decode_level = acr122_pcsc_connstring_decode(fullconnstring, &ndd); @@ -265,8 +259,7 @@ acr122_pcsc_open(const nfc_connstring connstring) if (sscanf(ndd.pcsc_device_name, "%lu", &index) != 1) return NULL; nfc_connstring *ncs = malloc(sizeof(nfc_connstring) * (index + 1)); - size_t szDeviceFound; - acr122_pcsc_probe(ncs, index + 1, &szDeviceFound); + size_t szDeviceFound = acr122_pcsc_scan(ncs, index + 1); if (szDeviceFound < index + 1) return NULL; strncpy(fullconnstring, ncs[index], sizeof(nfc_connstring)); @@ -491,7 +484,7 @@ const struct pn53x_io acr122_pcsc_io = { const struct nfc_driver acr122_pcsc_driver = { .name = ACR122_PCSC_DRIVER_NAME, - .probe = acr122_pcsc_probe, + .scan = acr122_pcsc_scan, .open = acr122_pcsc_open, .close = acr122_pcsc_close, .strerror = pn53x_strerror, diff --git a/libnfc/drivers/acr122_usb.c b/libnfc/drivers/acr122_usb.c index 525768f..a950b90 100644 --- a/libnfc/drivers/acr122_usb.c +++ b/libnfc/drivers/acr122_usb.c @@ -303,8 +303,8 @@ acr122_usb_get_end_points(struct usb_device *dev, struct acr122_usb_data *data) } } -static bool -acr122_usb_probe(nfc_connstring connstrings[], size_t connstrings_len, size_t *device_found) +static size_t +acr122_usb_scan(nfc_connstring connstrings[], const size_t connstrings_len) { usb_init(); @@ -314,17 +314,17 @@ acr122_usb_probe(nfc_connstring connstrings[], size_t connstrings_len, size_t *d // busses and busses removed). if ((res = usb_find_busses() < 0)) { log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror(res)); - return false; + return 0; } // 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 ((res = usb_find_devices() < 0)) { log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res)); - return false; + return 0; } - *device_found = 0; + size_t device_found = 0; uint32_t uiBusIndex = 0; struct usb_bus *bus; @@ -349,21 +349,21 @@ acr122_usb_probe(nfc_connstring connstrings[], size_t connstrings_len, size_t *d usb_dev_handle *udev = usb_open(dev); // Set configuration - // acr122_usb_get_usb_device_name (dev, udev, pnddDevices[*device_found].acDevice, sizeof (pnddDevices[*device_found].acDevice)); + // acr122_usb_get_usb_device_name (dev, udev, pnddDevices[device_found].acDevice, sizeof (pnddDevices[device_found].acDevice)); log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "device found: Bus %s Device %s Name %s", bus->dirname, dev->filename, acr122_usb_supported_devices[n].name); usb_close(udev); - snprintf(connstrings[*device_found], sizeof(nfc_connstring), "%s:%s:%s", ACR122_USB_DRIVER_NAME, bus->dirname, dev->filename); - (*device_found)++; + snprintf(connstrings[device_found], sizeof(nfc_connstring), "%s:%s:%s", ACR122_USB_DRIVER_NAME, bus->dirname, dev->filename); + device_found++; // Test if we reach the maximum "wanted" devices - if ((*device_found) == connstrings_len) { - return true; + if (device_found == connstrings_len) { + return device_found; } } } } } - return true; + return device_found; } struct acr122_usb_descriptor { @@ -862,7 +862,7 @@ const struct pn53x_io acr122_usb_io = { const struct nfc_driver acr122_usb_driver = { .name = ACR122_USB_DRIVER_NAME, - .probe = acr122_usb_probe, + .scan = acr122_usb_scan, .open = acr122_usb_open, .close = acr122_usb_close, .strerror = pn53x_strerror, diff --git a/libnfc/drivers/acr122s.c b/libnfc/drivers/acr122s.c index 0fc2484..f46e0fe 100644 --- a/libnfc/drivers/acr122s.c +++ b/libnfc/drivers/acr122s.c @@ -447,21 +447,19 @@ acr122s_connstring_decode(const nfc_connstring connstring, struct acr122s_descri return 3; } -static bool -acr122s_probe(nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound) +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 probe devices, we can + * 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; - *pszDeviceFound = 0; - log_put(LOG_CATEGORY, NFC_PRIORITY_INFO, "%s", "Serial auto-probing have been disabled at compile time. Skipping autoprobe."); - return false; + 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 */ - *pszDeviceFound = 0; - + size_t device_found = 0; serial_port sp; char **acPorts = uart_list_ports(); const char *acPort; @@ -509,11 +507,11 @@ acr122s_probe(nfc_connstring connstrings[], size_t connstrings_len, size_t *pszD continue; // ACR122S reader is found - memcpy(connstrings[*pszDeviceFound], connstring, sizeof(nfc_connstring)); - (*pszDeviceFound)++; + memcpy(connstrings[device_found], connstring, sizeof(nfc_connstring)); + device_found++; // Test if we reach the maximum "wanted" devices - if (*pszDeviceFound >= connstrings_len) + if (device_found >= connstrings_len) break; } } @@ -522,8 +520,8 @@ acr122s_probe(nfc_connstring connstrings[], size_t connstrings_len, size_t *pszD free((void *)acPort); } free(acPorts); + return device_found; #endif /* SERIAL_AUTOPROBE_ENABLED */ - return true; } static void @@ -705,7 +703,7 @@ const struct pn53x_io acr122s_io = { const struct nfc_driver acr122s_driver = { .name = ACR122S_DRIVER_NAME, - .probe = acr122s_probe, + .scan = acr122s_scan, .open = acr122s_open, .close = acr122s_close, .strerror = pn53x_strerror, diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 812836a..753afdb 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -91,21 +91,19 @@ static const uint8_t arygon_error_unknown_mode[] = "FF060000\x0d\x0a"; int arygon_reset_tama(nfc_device *pnd); void arygon_firmware(nfc_device *pnd, char *str); -static bool -arygon_probe(nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound) +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 probe devices, we can + * 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; - *pszDeviceFound = 0; - log_put(LOG_CATEGORY, NFC_PRIORITY_INFO, "%s", "Serial auto-probing have been disabled at compile time. Skipping autoprobe."); - return false; + 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 */ - *pszDeviceFound = 0; - + size_t device_found = 0; serial_port sp; char **acPorts = uart_list_ports(); const char *acPort; @@ -147,11 +145,11 @@ arygon_probe(nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDe } // ARYGON reader is found - memcpy(connstrings[*pszDeviceFound], connstring, sizeof(nfc_connstring)); - (*pszDeviceFound)++; + memcpy(connstrings[device_found], connstring, sizeof(nfc_connstring)); + device_found++; // Test if we reach the maximum "wanted" devices - if ((*pszDeviceFound) >= connstrings_len) + if (device_found >= connstrings_len) break; } } @@ -160,8 +158,8 @@ arygon_probe(nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDe free((void *)acPort); } free(acPorts); + return device_found; #endif /* SERIAL_AUTOPROBE_ENABLED */ - return true; } struct arygon_descriptor { @@ -566,7 +564,7 @@ const struct pn53x_io arygon_tama_io = { const struct nfc_driver arygon_driver = { .name = ARYGON_DRIVER_NAME, - .probe = arygon_probe, + .scan = arygon_scan, .open = arygon_open, .close = arygon_close, .strerror = pn53x_strerror, diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index cc0dbd8..9053182 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -64,21 +64,19 @@ int pn532_uart_wakeup(nfc_device *pnd); #define DRIVER_DATA(pnd) ((struct pn532_uart_data*)(pnd->driver_data)) -static bool -pn532_uart_probe(nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound) +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 probe devices, we can + * 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; - *pszDeviceFound = 0; - log_put(LOG_CATEGORY, NFC_PRIORITY_INFO, "%s", "Serial auto-probing have been disabled at compile time. Skipping autoprobe."); - return false; + 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 */ - *pszDeviceFound = 0; - + size_t device_found = 0; serial_port sp; char **acPorts = uart_list_ports(); const char *acPort; @@ -124,11 +122,11 @@ pn532_uart_probe(nfc_connstring connstrings[], size_t connstrings_len, size_t *p continue; } - memcpy(connstrings[*pszDeviceFound], connstring, sizeof(nfc_connstring)); - (*pszDeviceFound)++; + memcpy(connstrings[device_found], connstring, sizeof(nfc_connstring)); + device_found++; // Test if we reach the maximum "wanted" devices - if ((*pszDeviceFound) >= connstrings_len) + if (device_found >= connstrings_len) break; } } @@ -137,8 +135,8 @@ pn532_uart_probe(nfc_connstring connstrings[], size_t connstrings_len, size_t *p free((void *)acPort); } free(acPorts); + return device_found; #endif /* SERIAL_AUTOPROBE_ENABLED */ - return true; } struct pn532_uart_descriptor { @@ -507,7 +505,7 @@ const struct pn53x_io pn532_uart_io = { const struct nfc_driver pn532_uart_driver = { .name = PN532_UART_DRIVER_NAME, - .probe = pn532_uart_probe, + .scan = pn532_uart_scan, .open = pn532_uart_open, .close = pn532_uart_close, .strerror = pn53x_strerror, diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 053d31c..23db81f 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -181,29 +181,27 @@ pn53x_usb_get_end_points(struct usb_device *dev, struct pn53x_usb_data *data) } } -static bool -pn53x_usb_probe(nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound) +static size_t +pn53x_usb_scan(nfc_connstring connstrings[], const size_t connstrings_len) { usb_init(); - int res; // 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 ((res = usb_find_busses() < 0)) { log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror(res)); - return false; + return 0; } // 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 ((res = usb_find_devices() < 0)) { log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res)); - return false; + return 0; } - *pszDeviceFound = 0; - + size_t device_found = 0; uint32_t uiBusIndex = 0; struct usb_bus *bus; for (bus = usb_get_busses(); bus; bus = bus->next) { @@ -235,21 +233,21 @@ pn53x_usb_probe(nfc_connstring connstrings[], size_t connstrings_len, size_t *ps continue; } - // pn53x_usb_get_usb_device_name (dev, udev, pnddDevices[*pszDeviceFound].acDevice, sizeof (pnddDevices[*pszDeviceFound].acDevice)); + // pn53x_usb_get_usb_device_name (dev, udev, pnddDevices[device_found].acDevice, sizeof (pnddDevices[device_found].acDevice)); log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "device found: Bus %s Device %s", bus->dirname, dev->filename); usb_close(udev); - snprintf(connstrings[*pszDeviceFound], sizeof(nfc_connstring), "%s:%s:%s", PN53X_USB_DRIVER_NAME, bus->dirname, dev->filename); - (*pszDeviceFound)++; + snprintf(connstrings[device_found], sizeof(nfc_connstring), "%s:%s:%s", PN53X_USB_DRIVER_NAME, bus->dirname, dev->filename); + device_found++; // Test if we reach the maximum "wanted" devices - if ((*pszDeviceFound) == connstrings_len) { - return true; + if (device_found == connstrings_len) { + return device_found; } } } } } - return true; + return device_found; } struct pn53x_usb_descriptor { @@ -761,7 +759,7 @@ const struct pn53x_io pn53x_usb_io = { const struct nfc_driver pn53x_usb_driver = { .name = PN53X_USB_DRIVER_NAME, - .probe = pn53x_usb_probe, + .scan = pn53x_usb_scan, .open = pn53x_usb_open, .close = pn53x_usb_close, .strerror = pn53x_strerror, diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 77b5981..d4d9708 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -109,7 +109,7 @@ struct nfc_driver { const char *name; - bool (*probe)(nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound); + 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); const char *(*strerror)(const struct nfc_device *pnd); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index e511b59..5750551 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -150,8 +150,8 @@ nfc_get_default_device(nfc_connstring *connstring) if (NULL == env_default_connstring) { // LIBNFC_DEFAULT_DEVICE is not set, we fallback on probing for the first available device nfc_connstring listed_cs[1]; - size_t szDeviceFound = nfc_list_devices(NULL, listed_cs, 1); - if (szDeviceFound) { + size_t device_found = nfc_list_devices(NULL, listed_cs, 1); + if (device_found) { strncpy(*connstring, listed_cs[0], sizeof(nfc_connstring)); } else { return false; @@ -255,31 +255,31 @@ nfc_close(nfc_device *pnd) * @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. - * @param szDevices size of the \a connstrings array. + * @param connstrings_len size of the \a connstrings array. * */ size_t -nfc_list_devices(nfc_context *context, nfc_connstring connstrings[] , size_t szDevices) +nfc_list_devices(nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len) { - size_t szN; - size_t szDeviceFound = 0; + size_t device_found = 0; const struct nfc_driver *ndr; const struct nfc_driver **pndr = nfc_drivers; (void) context; while ((ndr = *pndr)) { - szN = 0; - if (ndr->probe(connstrings + (szDeviceFound), szDevices - (szDeviceFound), &szN)) { - szDeviceFound += szN; - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%ld device(s) found using %s driver", (unsigned long) szN, ndr->name); - if (szDeviceFound == szDevices) + 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; } pndr++; } log_fini(); - return szDeviceFound; + return device_found; } /** @ingroup properties diff --git a/utils/Makefile.am b/utils/Makefile.am index a86e6ed..5758b51 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -3,7 +3,7 @@ bin_PROGRAMS = \ nfc-list \ nfc-mfclassic \ nfc-mfultralight \ - nfc-probe \ + nfc-scan-device \ nfc-read-forum-tag3 \ nfc-relay-picc @@ -30,8 +30,8 @@ nfc_mfclassic_LDADD = $(top_builddir)/libnfc/libnfc.la \ nfc_mfultralight_SOURCES = nfc-mfultralight.c mifare.c mifare.h nfc_mfultralight_LDADD = $(top_builddir)/libnfc/libnfc.la -nfc_probe_SOURCES = nfc-probe.c -nfc_probe_LDADD = $(top_builddir)/libnfc/libnfc.la \ +nfc_scan_device_SOURCES = nfc-scan-device.c +nfc_scan_device_LDADD = $(top_builddir)/libnfc/libnfc.la \ libnfcutils.la nfc_read_forum_tag3_SOURCES = nfc-read-forum-tag3.c diff --git a/utils/nfc-probe.c b/utils/nfc-scan-device.c similarity index 99% rename from utils/nfc-probe.c rename to utils/nfc-scan-device.c index 1c9bbc3..9a0eed0 100644 --- a/utils/nfc-probe.c +++ b/utils/nfc-scan-device.c @@ -30,7 +30,7 @@ */ /** - * @file nfc-probe.c + * @file nfc-scan-device.c * @brief Lists each available NFC device */