Skip to content

Commit bf0802d

Browse files
committed
Port to libusb-1.0
Use the now widely available libusb-1.0 library to handle USB receivers. Co-Authored-By: Claude Sonnet 4.6
1 parent 3fa0751 commit bf0802d

9 files changed

Lines changed: 438 additions & 421 deletions

File tree

.travis.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ matrix:
66
compiler:
77
- clang
88
before_install:
9-
- 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"
9+
- 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"
1010
install:
1111
choco install doxygen.install ninja
1212
script:
@@ -19,7 +19,7 @@ matrix:
1919
addons:
2020
apt:
2121
packages:
22-
- libusb-dev
22+
- libusb-1.0-0-dev
2323
- doxygen
2424
- cmake
2525
script:
@@ -32,7 +32,7 @@ matrix:
3232
addons:
3333
apt:
3434
packages:
35-
- libusb-dev
35+
- libusb-1.0-0-dev
3636
- doxygen
3737
script:
3838
- autoreconf -vfi && mkdir build && cd build && ../configure --prefix=$HOME/.local/ && make -j2 && make install
@@ -44,7 +44,7 @@ matrix:
4444
addons:
4545
apt:
4646
packages:
47-
- libusb-dev
47+
- libusb-1.0-0-dev
4848
- doxygen
4949
- cmake
5050
script:
@@ -57,7 +57,7 @@ matrix:
5757
addons:
5858
apt:
5959
packages:
60-
- libusb-dev
60+
- libusb-1.0-0-dev
6161
- doxygen
6262
script:
6363
- autoreconf -vfi && mkdir build && cd build && ../configure --prefix=$HOME/.local/ && make -j2 && make install
@@ -67,7 +67,7 @@ matrix:
6767
compiler:
6868
- clang
6969
before_install:
70-
- brew install doxygen libusb-compat
70+
- brew install doxygen libusb
7171
script:
7272
- mkdir build && cd build && cmake -DCMAKE_INSTALL_PREFIX=~/.local .. && make -j2 && make install
7373

@@ -76,6 +76,6 @@ matrix:
7676
compiler:
7777
- clang
7878
before_install:
79-
- brew install doxygen libusb-compat m4
79+
- brew install doxygen libusb m4
8080
script:
8181
- autoreconf -vfi && mkdir build && cd build && ../configure --prefix=$HOME/.local/ && make -j2 && make install

CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,6 @@ IF(CMAKE_COMPILER_IS_GNUCC)
9999
SET(CMAKE_C_FLAGS "-Wall -pedantic -std=c99 ${CMAKE_C_FLAGS}")
100100
ENDIF(CMAKE_COMPILER_IS_GNUCC)
101101

102-
# Workarounds for libusb in C99
103-
ADD_DEFINITIONS(-Du_int8_t=uint8_t -Du_int16_t=uint16_t)
104-
105102
IF(MINGW)
106103
IF (CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
107104
IF(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86")
@@ -132,10 +129,10 @@ IF(NOT WIN32)
132129
SET(exec_prefix ${CMAKE_INSTALL_PREFIX})
133130
SET(PACKAGE "libnfc")
134131
IF(LIBNFC_DRIVER_PN53X_USB)
135-
SET(PKG_REQ ${PKG_REQ} "libusb")
132+
SET(PKG_REQ ${PKG_REQ} "libusb-1.0")
136133
ENDIF(LIBNFC_DRIVER_PN53X_USB)
137134
IF(LIBNFC_DRIVER_ACR122_USB)
138-
SET(PKG_REQ ${PKG_REQ} "libusb")
135+
SET(PKG_REQ ${PKG_REQ} "libusb-1.0")
139136
ENDIF(LIBNFC_DRIVER_ACR122_USB)
140137
IF(LIBNFC_DRIVER_PCSC)
141138
SET(PKG_REQ ${PKG_REQ} "libpcsclite")

cmake/modules/FindLIBUSB.cmake

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,43 @@ ENDIF(CMAKE_SYSTEM_NAME MATCHES FreeBSD)
2626

2727
IF(NOT LIBUSB_FOUND)
2828
IF(WIN32)
29+
SET(LIBUSB_ROOT "$ENV{ProgramFiles}/libusb")
30+
FIND_PATH(LIBUSB_INCLUDE_DIRS libusb.h "${LIBUSB_ROOT}/include" NO_SYSTEM_ENVIRONMENT_PATH)
2931
IF(MINGW)
30-
FIND_PATH(LIBUSB_INCLUDE_DIRS lusb0_usb.h "${CMAKE_CURRENT_BINARY_DIR}/LibUSB-Win32/include" NO_SYSTEM_ENVIRONMENT_PATH)
31-
FIND_LIBRARY(LIBUSB_LIBRARIES NAMES libusb PATHS "${CMAKE_CURRENT_BINARY_DIR}/LibUSB-Win32/lib/gcc")
32-
SET(LIBUSB_LIBRARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/LibUSB-Win32/bin/x86/")
32+
IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
33+
FIND_LIBRARY(LIBUSB_LIBRARIES NAMES usb-1.0 PATHS "${LIBUSB_ROOT}/MinGW64/static" NO_SYSTEM_ENVIRONMENT_PATH)
34+
SET(LIBUSB_LIBRARY_DIR "${LIBUSB_ROOT}/MinGW64/dll")
35+
ELSE()
36+
FIND_LIBRARY(LIBUSB_LIBRARIES NAMES usb-1.0 PATHS "${LIBUSB_ROOT}/MinGW32/static" NO_SYSTEM_ENVIRONMENT_PATH)
37+
SET(LIBUSB_LIBRARY_DIR "${LIBUSB_ROOT}/MinGW32/dll")
38+
ENDIF()
3339
ELSE(MINGW)
34-
FIND_PATH(LIBUSB_INCLUDE_DIRS lusb0_usb.h "$ENV{ProgramW6432}/libusb-win32/include" NO_SYSTEM_ENVIRONMENT_PATH)
35-
FIND_LIBRARY(LIBUSB_LIBRARIES NAMES libusb PATHS "$ENV{ProgramW6432}/libusb-win32/lib/msvc_x64")
36-
SET(LIBUSB_LIBRARY_DIR "$ENV{ProgramW6432}/libusb-win32/bin/amd64/")
40+
IF(MSVC_VERSION GREATER_EQUAL 1930)
41+
SET(LIBUSB_VS_DIR "VS2022")
42+
ELSEIF(MSVC_VERSION GREATER_EQUAL 1920)
43+
SET(LIBUSB_VS_DIR "VS2019")
44+
ELSEIF(MSVC_VERSION GREATER_EQUAL 1910)
45+
SET(LIBUSB_VS_DIR "VS2017")
46+
ELSEIF(MSVC_VERSION GREATER_EQUAL 1900)
47+
SET(LIBUSB_VS_DIR "VS2015")
48+
ELSE()
49+
SET(LIBUSB_VS_DIR "VS2013")
50+
ENDIF()
51+
IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
52+
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)
53+
SET(LIBUSB_LIBRARY_DIR "${LIBUSB_ROOT}/${LIBUSB_VS_DIR}/MS64/dll")
54+
ELSE()
55+
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)
56+
SET(LIBUSB_LIBRARY_DIR "${LIBUSB_ROOT}/${LIBUSB_VS_DIR}/MS32/dll")
57+
ENDIF()
3758
ENDIF(MINGW)
3859
# Must fix up variable to avoid backslashes during packaging
3960
STRING(REGEX REPLACE "\\\\" "/" LIBUSB_LIBRARY_DIR ${LIBUSB_LIBRARY_DIR})
4061
ELSE(WIN32)
4162
# If not under Windows we use PkgConfig
4263
FIND_PACKAGE (PkgConfig)
4364
IF(PKG_CONFIG_FOUND)
44-
PKG_CHECK_MODULES(LIBUSB REQUIRED libusb)
65+
PKG_CHECK_MODULES(LIBUSB REQUIRED libusb-1.0)
4566
ELSE(PKG_CONFIG_FOUND)
4667
MESSAGE(FATAL_ERROR "Could not find PkgConfig")
4768
ENDIF(PKG_CONFIG_FOUND)

configure.ac

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,6 @@ CFLAGS="$CFLAGS -Wall -pedantic -Wextra"
194194
# Defines and C flags
195195
CFLAGS="$CFLAGS -std=c99"
196196

197-
# Workarounds for libusb in c99
198-
CFLAGS="$CFLAGS -Du_int8_t=uint8_t -Du_int16_t=uint16_t"
199-
200197
AC_CONFIG_FILES([
201198
Doxyfile
202199
Makefile

libnfc/buses/usbbus.c

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
/**
2929
* @file usbbus.c
30-
* @brief libusb 0.1 driver wrapper
30+
* @brief libusb 1.0 driver wrapper
3131
*/
3232

3333
#ifdef HAVE_CONFIG_H
@@ -41,39 +41,31 @@
4141
#define LOG_CATEGORY "libnfc.buses.usbbus"
4242
#define LOG_GROUP NFC_LOG_GROUP_DRIVER
4343

44-
int usb_prepare(void)
44+
static libusb_context *usb_context = NULL;
45+
46+
libusb_context *
47+
usb_get_context(void)
4548
{
4649
static bool usb_initialized = false;
4750
if (!usb_initialized) {
51+
int r = libusb_init(&usb_context);
52+
if (r < 0) {
53+
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to initialize libusb (%s)", libusb_strerror(r));
54+
return NULL;
55+
}
4856

4957
#ifdef ENVVARS
5058
char *env_log_level = getenv("LIBNFC_LOG_LEVEL");
51-
// Set libusb debug only if asked explicitely:
59+
// Set libusb debug only if asked explicitly:
5260
// LIBUSB_LOG_LEVEL=12288 (= NFC_LOG_PRIORITY_DEBUG * 2 ^ NFC_LOG_GROUP_LIBUSB)
5361
if (env_log_level && (((atoi(env_log_level) >> (NFC_LOG_GROUP_LIBUSB * 2)) & 0x00000003) >= NFC_LOG_PRIORITY_DEBUG)) {
54-
setenv("USB_DEBUG", "255", 1);
62+
libusb_set_option(usb_context, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_DEBUG);
5563
}
5664
#endif
5765

58-
usb_init();
5966
usb_initialized = true;
6067
}
6168

62-
int res;
63-
// usb_find_busses will find all of the busses on the system. Returns the
64-
// number of changes since previous call to this function (total of new
65-
// busses and busses removed).
66-
if ((res = usb_find_busses()) < 0) {
67-
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror(res));
68-
return -1;
69-
}
70-
// usb_find_devices will find all of the devices on each bus. This should be
71-
// called after usb_find_busses. Returns the number of changes since the
72-
// previous call to this function (total of new device and devices removed).
73-
if ((res = usb_find_devices()) < 0) {
74-
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res));
75-
return -1;
76-
}
77-
return 0;
69+
return usb_context;
7870
}
7971

libnfc/buses/usbbus.h

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,17 @@
2727

2828
/**
2929
* @file usbbus.h
30-
* @brief libusb 0.1 driver header
30+
* @brief libusb 1.0 driver header
3131
*/
3232

3333
#ifndef __NFC_BUS_USB_H__
3434
# define __NFC_BUS_USB_H__
3535

36-
#ifndef _WIN32
37-
// Under POSIX system, we use libusb (>= 0.1.12)
38-
#include <stdint.h>
39-
#include <usb.h>
40-
#define USB_TIMEDOUT ETIMEDOUT
41-
#define _usb_strerror( X ) strerror(-X)
42-
#else
43-
// Under Windows we use libusb-win32 (>= 1.2.5)
44-
#include <lusb0_usb.h>
45-
#define USB_TIMEDOUT 116
46-
#define _usb_strerror( X ) usb_strerror()
47-
#endif
36+
#include <libusb.h>
4837

4938
#include <stdbool.h>
5039
#include <string.h>
5140

52-
int usb_prepare(void);
41+
libusb_context *usb_get_context(void);
5342

5443
#endif // __NFC_BUS_USB_H__

0 commit comments

Comments
 (0)