Use hidapi as the default USB HID library

On Windows, the hidapi library uses the standard Microsoft USB HID
driver, while libusb requires the installation of a different driver
(WinUSB or libusbK). But installing one of the libusb drivers breaks
compatibility with other applications using hidapi (Scubapro LogTRAK and
Suunto DM5) because only one driver can be active. Switching
libdivecomputer to hidapi avoids this problem.

On Linux, the hidapi library doesn't seem to offer any advantages over
libusb. Most distributions don't even have the hidapi library installed
by default. Because there are usually two variants of the hidapi library
available on Linux (hidapi-libusb and hidapi-hidraw), the autotools
build system won't be able to detect it out-of-the-box, and will
automatically fallback to the libusb implementation.

On Mac OS X, hidapi is already the default (and also the only option).
This commit is contained in:
Jef Driesen 2017-07-21 00:48:04 +02:00
parent c9ed92d3f5
commit eea02126a4
2 changed files with 26 additions and 20 deletions

View File

@ -23,9 +23,9 @@
#include "config.h"
#endif
#if defined(HAVE_LIBUSB) && !defined(__APPLE__)
#if defined(HAVE_HIDAPI)
#define USBHID
#elif defined(HAVE_HIDAPI)
#elif defined(HAVE_LIBUSB) && !defined(__APPLE__)
#define USBHID
#endif

View File

@ -25,14 +25,20 @@
#include <stdlib.h>
#if defined(HAVE_LIBUSB) && !defined(__APPLE__)
#if defined(HAVE_HIDAPI)
#define USE_HIDAPI
#define USBHID
#elif defined(HAVE_LIBUSB) && !defined(__APPLE__)
#define USE_LIBUSB
#define USBHID
#endif
#if defined(USE_LIBUSB)
#ifdef _WIN32
#define NOGDI
#endif
#include <libusb-1.0/libusb.h>
#elif defined(HAVE_HIDAPI)
#define USBHID
#elif defined(USE_HIDAPI)
#include <hidapi/hidapi.h>
#endif
@ -44,20 +50,20 @@ struct dc_usbhid_t {
/* Library context. */
dc_context_t *context;
/* Internal state. */
#if defined(HAVE_LIBUSB) && !defined(__APPLE__)
#if defined(USE_LIBUSB)
libusb_context *ctx;
libusb_device_handle *handle;
int interface;
unsigned char endpoint_in;
unsigned char endpoint_out;
unsigned int timeout;
#elif defined(HAVE_HIDAPI)
#elif defined(USE_HIDAPI)
hid_device *handle;
int timeout;
#endif
};
#if defined(HAVE_LIBUSB) && !defined(__APPLE__)
#if defined(USE_LIBUSB)
static dc_status_t
syserror(int errcode)
{
@ -103,7 +109,7 @@ dc_usbhid_open (dc_usbhid_t **out, dc_context_t *context, unsigned int vid, unsi
// Library context.
usbhid->context = context;
#if defined(HAVE_LIBUSB) && !defined(__APPLE__)
#if defined(USE_LIBUSB)
struct libusb_device **devices = NULL;
struct libusb_config_descriptor *config = NULL;
@ -235,7 +241,7 @@ dc_usbhid_open (dc_usbhid_t **out, dc_context_t *context, unsigned int vid, unsi
libusb_free_config_descriptor (config);
libusb_free_device_list (devices, 1);
#elif defined(HAVE_HIDAPI)
#elif defined(USE_HIDAPI)
// Initialize the hidapi library.
rc = hid_init();
@ -260,7 +266,7 @@ dc_usbhid_open (dc_usbhid_t **out, dc_context_t *context, unsigned int vid, unsi
return DC_STATUS_SUCCESS;
#if defined(HAVE_LIBUSB) && !defined(__APPLE__)
#if defined(USE_LIBUSB)
error_usb_close:
libusb_close (usbhid->handle);
error_usb_free_config:
@ -269,7 +275,7 @@ error_usb_free_list:
libusb_free_device_list (devices, 1);
error_usb_exit:
libusb_exit (usbhid->ctx);
#elif defined(HAVE_HIDAPI)
#elif defined(USE_HIDAPI)
error_hid_exit:
hid_exit ();
#endif
@ -290,11 +296,11 @@ dc_usbhid_close (dc_usbhid_t *usbhid)
if (usbhid == NULL)
return DC_STATUS_SUCCESS;
#if defined(HAVE_LIBUSB) && !defined(__APPLE__)
#if defined(USE_LIBUSB)
libusb_release_interface (usbhid->handle, usbhid->interface);
libusb_close (usbhid->handle);
libusb_exit (usbhid->ctx);
#elif defined(HAVE_HIDAPI)
#elif defined(USE_HIDAPI)
hid_close(usbhid->handle);
hid_exit();
#endif
@ -315,7 +321,7 @@ dc_usbhid_set_timeout (dc_usbhid_t *usbhid, int timeout)
INFO (usbhid->context, "Timeout: value=%i", timeout);
#if defined(HAVE_LIBUSB) && !defined(__APPLE__)
#if defined(USE_LIBUSB)
if (timeout < 0) {
usbhid->timeout = 0;
} else if (timeout == 0) {
@ -323,7 +329,7 @@ dc_usbhid_set_timeout (dc_usbhid_t *usbhid, int timeout)
} else {
usbhid->timeout = timeout;
}
#elif defined(HAVE_HIDAPI)
#elif defined(USE_HIDAPI)
if (timeout < 0) {
usbhid->timeout = -1;
} else {
@ -349,7 +355,7 @@ dc_usbhid_read (dc_usbhid_t *usbhid, void *data, size_t size, size_t *actual)
goto out_invalidargs;
}
#if defined(HAVE_LIBUSB) && !defined(__APPLE__)
#if defined(USE_LIBUSB)
int rc = libusb_interrupt_transfer (usbhid->handle, usbhid->endpoint_in, data, size, &nbytes, usbhid->timeout);
if (rc != LIBUSB_SUCCESS) {
ERROR (usbhid->context, "Usb read interrupt transfer failed (%s).",
@ -357,7 +363,7 @@ dc_usbhid_read (dc_usbhid_t *usbhid, void *data, size_t size, size_t *actual)
status = syserror (rc);
goto out;
}
#elif defined(HAVE_HIDAPI)
#elif defined(USE_HIDAPI)
nbytes = hid_read_timeout(usbhid->handle, data, size, usbhid->timeout);
if (nbytes < 0) {
ERROR (usbhid->context, "Usb read interrupt transfer failed.");
@ -396,7 +402,7 @@ dc_usbhid_write (dc_usbhid_t *usbhid, const void *data, size_t size, size_t *act
goto out;
}
#if defined(HAVE_LIBUSB) && !defined(__APPLE__)
#if defined(USE_LIBUSB)
const unsigned char *buffer = (const unsigned char *) data;
size_t length = size;
@ -418,7 +424,7 @@ dc_usbhid_write (dc_usbhid_t *usbhid, const void *data, size_t size, size_t *act
if (report == 0) {
nbytes++;
}
#elif defined(HAVE_HIDAPI)
#elif defined(USE_HIDAPI)
nbytes = hid_write(usbhid->handle, data, size);
if (nbytes < 0) {
ERROR (usbhid->context, "Usb write interrupt transfer failed.");