Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ matrix:
compiler:
- clang
before_install:
- mkdir build && cd build && wget "https://sourceforge.net/projects/libusb-win32/files/libusb-win32-releases/1.2.6.0/libusb-win32-bin-1.2.6.0.zip" && 7z x libusb-win32-bin-1.2.6.0.zip -o"$PROGRAMFILES" && mv "$PROGRAMFILES/libusb-win32-bin-1.2.6.0" "$PROGRAMFILES/libusb-win32"
- mkdir build && cd build && wget "https://github.com/libusb/libusb/releases/download/v1.0.29/libusb-1.0.29.7z" && 7z x libusb-1.0.29.7z -o"$PROGRAMFILES/libusb"
install:
choco install doxygen.install ninja
script:
Expand All @@ -19,7 +19,7 @@ matrix:
addons:
apt:
packages:
- libusb-dev
- libusb-1.0-0-dev
- doxygen
- cmake
script:
Expand All @@ -32,7 +32,7 @@ matrix:
addons:
apt:
packages:
- libusb-dev
- libusb-1.0-0-dev
- doxygen
script:
- autoreconf -vfi && mkdir build && cd build && ../configure --prefix=$HOME/.local/ && make -j2 && make install
Expand All @@ -44,7 +44,7 @@ matrix:
addons:
apt:
packages:
- libusb-dev
- libusb-1.0-0-dev
- doxygen
- cmake
script:
Expand All @@ -57,7 +57,7 @@ matrix:
addons:
apt:
packages:
- libusb-dev
- libusb-1.0-0-dev
- doxygen
script:
- autoreconf -vfi && mkdir build && cd build && ../configure --prefix=$HOME/.local/ && make -j2 && make install
Expand All @@ -67,7 +67,7 @@ matrix:
compiler:
- clang
before_install:
- brew install doxygen libusb-compat
- brew install doxygen libusb
script:
- mkdir build && cd build && cmake -DCMAKE_INSTALL_PREFIX=~/.local .. && make -j2 && make install

Expand All @@ -76,6 +76,6 @@ matrix:
compiler:
- clang
before_install:
- brew install doxygen libusb-compat m4
- brew install doxygen libusb m4
script:
- autoreconf -vfi && mkdir build && cd build && ../configure --prefix=$HOME/.local/ && make -j2 && make install
7 changes: 2 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ IF(CMAKE_COMPILER_IS_GNUCC)
SET(CMAKE_C_FLAGS "-Wall -pedantic -std=c99 ${CMAKE_C_FLAGS}")
ENDIF(CMAKE_COMPILER_IS_GNUCC)

# Workarounds for libusb in C99
ADD_DEFINITIONS(-Du_int8_t=uint8_t -Du_int16_t=uint16_t)

IF(MINGW)
IF (CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
IF(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86")
Expand Down Expand Up @@ -132,10 +129,10 @@ IF(NOT WIN32)
SET(exec_prefix ${CMAKE_INSTALL_PREFIX})
SET(PACKAGE "libnfc")
IF(LIBNFC_DRIVER_PN53X_USB)
SET(PKG_REQ ${PKG_REQ} "libusb")
SET(PKG_REQ ${PKG_REQ} "libusb-1.0")
ENDIF(LIBNFC_DRIVER_PN53X_USB)
IF(LIBNFC_DRIVER_ACR122_USB)
SET(PKG_REQ ${PKG_REQ} "libusb")
SET(PKG_REQ ${PKG_REQ} "libusb-1.0")
ENDIF(LIBNFC_DRIVER_ACR122_USB)
IF(LIBNFC_DRIVER_PCSC)
SET(PKG_REQ ${PKG_REQ} "libpcsclite")
Expand Down
35 changes: 28 additions & 7 deletions cmake/modules/FindLIBUSB.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,43 @@ ENDIF(CMAKE_SYSTEM_NAME MATCHES FreeBSD)

IF(NOT LIBUSB_FOUND)
IF(WIN32)
SET(LIBUSB_ROOT "$ENV{ProgramFiles}/libusb")
FIND_PATH(LIBUSB_INCLUDE_DIRS libusb.h "${LIBUSB_ROOT}/include" NO_SYSTEM_ENVIRONMENT_PATH)
IF(MINGW)
FIND_PATH(LIBUSB_INCLUDE_DIRS lusb0_usb.h "${CMAKE_CURRENT_BINARY_DIR}/LibUSB-Win32/include" NO_SYSTEM_ENVIRONMENT_PATH)
FIND_LIBRARY(LIBUSB_LIBRARIES NAMES libusb PATHS "${CMAKE_CURRENT_BINARY_DIR}/LibUSB-Win32/lib/gcc")
SET(LIBUSB_LIBRARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/LibUSB-Win32/bin/x86/")
IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
FIND_LIBRARY(LIBUSB_LIBRARIES NAMES usb-1.0 PATHS "${LIBUSB_ROOT}/MinGW64/static" NO_SYSTEM_ENVIRONMENT_PATH)
SET(LIBUSB_LIBRARY_DIR "${LIBUSB_ROOT}/MinGW64/dll")
ELSE()
FIND_LIBRARY(LIBUSB_LIBRARIES NAMES usb-1.0 PATHS "${LIBUSB_ROOT}/MinGW32/static" NO_SYSTEM_ENVIRONMENT_PATH)
SET(LIBUSB_LIBRARY_DIR "${LIBUSB_ROOT}/MinGW32/dll")
ENDIF()
ELSE(MINGW)
FIND_PATH(LIBUSB_INCLUDE_DIRS lusb0_usb.h "$ENV{ProgramW6432}/libusb-win32/include" NO_SYSTEM_ENVIRONMENT_PATH)
FIND_LIBRARY(LIBUSB_LIBRARIES NAMES libusb PATHS "$ENV{ProgramW6432}/libusb-win32/lib/msvc_x64")
SET(LIBUSB_LIBRARY_DIR "$ENV{ProgramW6432}/libusb-win32/bin/amd64/")
IF(MSVC_VERSION GREATER_EQUAL 1930)
SET(LIBUSB_VS_DIR "VS2022")
ELSEIF(MSVC_VERSION GREATER_EQUAL 1920)
SET(LIBUSB_VS_DIR "VS2019")
ELSEIF(MSVC_VERSION GREATER_EQUAL 1910)
SET(LIBUSB_VS_DIR "VS2017")
ELSEIF(MSVC_VERSION GREATER_EQUAL 1900)
SET(LIBUSB_VS_DIR "VS2015")
ELSE()
SET(LIBUSB_VS_DIR "VS2013")
ENDIF()
IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
FIND_LIBRARY(LIBUSB_LIBRARIES NAMES libusb-1.0 PATHS "${LIBUSB_ROOT}/${LIBUSB_VS_DIR}/MS64/dll" "${LIBUSB_ROOT}/${LIBUSB_VS_DIR}/MS64/static" NO_SYSTEM_ENVIRONMENT_PATH)
SET(LIBUSB_LIBRARY_DIR "${LIBUSB_ROOT}/${LIBUSB_VS_DIR}/MS64/dll")
ELSE()
FIND_LIBRARY(LIBUSB_LIBRARIES NAMES libusb-1.0 PATHS "${LIBUSB_ROOT}/${LIBUSB_VS_DIR}/MS32/dll" "${LIBUSB_ROOT}/${LIBUSB_VS_DIR}/MS32/static" NO_SYSTEM_ENVIRONMENT_PATH)
SET(LIBUSB_LIBRARY_DIR "${LIBUSB_ROOT}/${LIBUSB_VS_DIR}/MS32/dll")
ENDIF()
ENDIF(MINGW)
# Must fix up variable to avoid backslashes during packaging
STRING(REGEX REPLACE "\\\\" "/" LIBUSB_LIBRARY_DIR ${LIBUSB_LIBRARY_DIR})
ELSE(WIN32)
# If not under Windows we use PkgConfig
FIND_PACKAGE (PkgConfig)
IF(PKG_CONFIG_FOUND)
PKG_CHECK_MODULES(LIBUSB REQUIRED libusb)
PKG_CHECK_MODULES(LIBUSB REQUIRED libusb-1.0)
ELSE(PKG_CONFIG_FOUND)
MESSAGE(FATAL_ERROR "Could not find PkgConfig")
ENDIF(PKG_CONFIG_FOUND)
Expand Down
3 changes: 0 additions & 3 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,6 @@ CFLAGS="$CFLAGS -Wall -pedantic -Wextra"
# Defines and C flags
CFLAGS="$CFLAGS -std=c99"

# Workarounds for libusb in c99
CFLAGS="$CFLAGS -Du_int8_t=uint8_t -Du_int16_t=uint16_t"

AC_CONFIG_FILES([
Doxyfile
Makefile
Expand Down
34 changes: 13 additions & 21 deletions libnfc/buses/usbbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

/**
* @file usbbus.c
* @brief libusb 0.1 driver wrapper
* @brief libusb 1.0 driver wrapper
*/

#ifdef HAVE_CONFIG_H
Expand All @@ -41,39 +41,31 @@
#define LOG_CATEGORY "libnfc.buses.usbbus"
#define LOG_GROUP NFC_LOG_GROUP_DRIVER

int usb_prepare(void)
static libusb_context *usb_context = NULL;

libusb_context *
usb_get_context(void)
{
static bool usb_initialized = false;
if (!usb_initialized) {
int r = libusb_init(&usb_context);
if (r < 0) {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to initialize libusb (%s)", libusb_strerror(r));
return NULL;
}

#ifdef ENVVARS
char *env_log_level = getenv("LIBNFC_LOG_LEVEL");
// Set libusb debug only if asked explicitely:
// Set libusb debug only if asked explicitly:
// LIBUSB_LOG_LEVEL=12288 (= NFC_LOG_PRIORITY_DEBUG * 2 ^ NFC_LOG_GROUP_LIBUSB)
if (env_log_level && (((atoi(env_log_level) >> (NFC_LOG_GROUP_LIBUSB * 2)) & 0x00000003) >= NFC_LOG_PRIORITY_DEBUG)) {
setenv("USB_DEBUG", "255", 1);
libusb_set_option(usb_context, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_DEBUG);
}
#endif

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;
return usb_context;
}

17 changes: 3 additions & 14 deletions libnfc/buses/usbbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,17 @@

/**
* @file usbbus.h
* @brief libusb 0.1 driver header
* @brief libusb 1.0 driver header
*/

#ifndef __NFC_BUS_USB_H__
# define __NFC_BUS_USB_H__

#ifndef _WIN32
// Under POSIX system, we use libusb (>= 0.1.12)
#include <stdint.h>
#include <usb.h>
#define USB_TIMEDOUT ETIMEDOUT
#define _usb_strerror( X ) strerror(-X)
#else
// Under Windows we use libusb-win32 (>= 1.2.5)
#include <lusb0_usb.h>
#define USB_TIMEDOUT 116
#define _usb_strerror( X ) usb_strerror()
#endif
#include <libusb.h>

#include <stdbool.h>
#include <string.h>

int usb_prepare(void);
libusb_context *usb_get_context(void);

#endif // __NFC_BUS_USB_H__
Loading