diff --git a/libnfc/buses/Makefile.am b/libnfc/buses/Makefile.am index 2ce0720..23cf46f 100644 --- a/libnfc/buses/Makefile.am +++ b/libnfc/buses/Makefile.am @@ -3,7 +3,7 @@ AM_CPPFLAGS = $(all_includes) $(LIBNFC_CFLAGS) noinst_LTLIBRARIES = libnfcbuses.la -libnfcbuses_la_SOURCES = uart.c uart.h +libnfcbuses_la_SOURCES = uart.c uart.h usbbus.c usbbus.h libnfcbuses_la_CFLAGS = -I$(top_srcdir)/libnfc EXTRA_DIST = uart_posix.c uart_win32.c diff --git a/libnfc/buses/usbbus.c b/libnfc/buses/usbbus.c new file mode 100644 index 0000000..fcf4233 --- /dev/null +++ b/libnfc/buses/usbbus.c @@ -0,0 +1,61 @@ +/*- + * Public platform independent Near Field Communication (NFC) library + * + * Copyright (C) 2013, 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 + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see + * + */ + +/** + * @file usbbus.c + * @brief libusb 0.1 driver wrapper + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif // HAVE_CONFIG_H + +#include "usbbus.h" +#include "log.h" +#define LOG_CATEGORY "libnfc.buses.usbbus" +#define LOG_GROUP NFC_LOG_GROUP_DRIVER + +// Global flag to know if usb_init() has already been called or not +bool usb_initialized=false; + +int usb_prepare(void) { + if (usb_initialized) + return 0; + usb_init(); + usb_initialized = true; + + 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_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror(res)); + return -1; + } + // 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_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res)); + return -1; + } + return 0; +} + diff --git a/libnfc/buses/usbbus.h b/libnfc/buses/usbbus.h new file mode 100644 index 0000000..5c08137 --- /dev/null +++ b/libnfc/buses/usbbus.h @@ -0,0 +1,49 @@ +/*- + * Public platform independent Near Field Communication (NFC) library + * + * Copyright (C) 2013, 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 + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see + * + */ + +/** + * @file usbbus.h + * @brief libusb 0.1 driver header + */ + +#ifndef __NFC_BUS_USB_H__ +# define __NFC_BUS_USB_H__ + +#ifndef _WIN32 +// Under POSIX system, we use libusb (>= 0.1.12) +#include +#define USB_TIMEDOUT ETIMEDOUT +#define _usb_strerror( X ) strerror(-X) +#else +// Under Windows we use libusb-win32 (>= 1.2.5) +#include +#define USB_TIMEDOUT 116 +#define _usb_strerror( X ) usb_strerror() +#endif + +#include +#include + +// Global flag to know if usb_init() has already been called or not +extern bool usb_initialized; + +int usb_prepare(void); + +#endif // __NFC_BUS_USB_H__ diff --git a/libnfc/drivers/acr122_usb.c b/libnfc/drivers/acr122_usb.c index da14a60..5f35234 100644 --- a/libnfc/drivers/acr122_usb.c +++ b/libnfc/drivers/acr122_usb.c @@ -54,24 +54,12 @@ Thanks to d18c7db and Okko for example code #include #include #include - -#ifndef _WIN32 -// Under POSIX system, we use libusb (>= 0.1.12) -#include -#define USB_TIMEDOUT ETIMEDOUT -#define _usb_strerror( X ) strerror(-X) -#else -// Under Windows we use libusb-win32 (>= 1.2.5) -#include -#define USB_TIMEDOUT 116 -#define _usb_strerror( X ) usb_strerror() -#endif - #include #include #include "nfc-internal.h" +#include "buses/usbbus.h" #include "chips/pn53x.h" #include "chips/pn53x-internal.h" #include "drivers/acr122_usb.h" @@ -309,23 +297,8 @@ static size_t acr122_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len) { (void)context; - 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_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror(res)); - 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_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res)); - return 0; - } + usb_prepare(); size_t device_found = 0; uint32_t uiBusIndex = 0; @@ -446,23 +419,7 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring) struct usb_bus *bus; struct usb_device *dev; - 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_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror(res)); - goto free_mem; - } - // 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_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res)); - goto free_mem; - } + usb_prepare(); for (bus = usb_get_busses(); bus; bus = bus->next) { if (connstring_decode_level > 1) { @@ -483,7 +440,7 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring) // Retrieve end points acr122_usb_get_end_points(dev, &data); // Claim interface - res = usb_claim_interface(data.pudh, 0); + int res = usb_claim_interface(data.pudh, 0); if (res < 0) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to claim USB interface (%s)", _usb_strerror(res)); usb_close(data.pudh); diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 6340cb7..905a951 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -37,24 +37,12 @@ Thanks to d18c7db and Okko for example code #include #include #include - -#ifndef _WIN32 -// Under POSIX system, we use libusb (>= 0.1.12) -#include -#define USB_TIMEDOUT ETIMEDOUT -#define _usb_strerror( X ) strerror(-X) -#else -// Under Windows we use libusb-win32 (>= 1.2.5) -#include -#define USB_TIMEDOUT 116 -#define _usb_strerror( X ) usb_strerror() -#endif - #include #include #include "nfc-internal.h" +#include "buses/usbbus.h" #include "chips/pn53x.h" #include "chips/pn53x-internal.h" #include "drivers/pn53x_usb.h" @@ -186,23 +174,7 @@ static size_t pn53x_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len) { (void)context; - 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_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror(res)); - 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_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res)); - return 0; - } - + usb_prepare(); size_t device_found = 0; uint32_t uiBusIndex = 0; struct usb_bus *bus; @@ -227,7 +199,7 @@ pn53x_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const s usb_dev_handle *udev = usb_open(dev); // Set configuration - res = usb_set_configuration(udev, 1); + int res = usb_set_configuration(udev, 1); if (res < 0) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set USB configuration (%s)", _usb_strerror(res)); usb_close(udev); @@ -330,23 +302,7 @@ pn53x_usb_open(const nfc_context *context, const nfc_connstring connstring) struct usb_bus *bus; struct usb_device *dev; - 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_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror(res)); - goto free_mem; - } - // 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_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res)); - goto free_mem; - } + usb_prepare(); for (bus = usb_get_busses(); bus; bus = bus->next) { if (connstring_decode_level > 1) { @@ -365,7 +321,7 @@ pn53x_usb_open(const nfc_context *context, const nfc_connstring connstring) // Retrieve end points pn53x_usb_get_end_points(dev, &data); // Set configuration - res = usb_set_configuration(data.pudh, 1); + int res = usb_set_configuration(data.pudh, 1); if (res < 0) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set USB configuration (%s)", _usb_strerror(res)); if (EPERM == -res) {