From 85d506b037083ddbc561b42bb5be1bed2fb230b6 Mon Sep 17 00:00:00 2001 From: Marc Schink Date: Mon, 2 Jan 2017 15:46:11 +0100 Subject: [PATCH] Make libusb optional Foolishly, 'interface' is a reserved keyword on Windows according to MSDN: https://msdn.microsoft.com/en-us/library/2kb28261.aspx Rename all 'interface' identifiers in the source code to avoid possible conflicts. This is necessary because if libusb is not available, the keyword is not undefined (#undef) by libusb.h any longer. Signed-off-by: Marc Schink --- configure.ac | 13 +++++++++++- libjaylink.pc.in | 2 +- libjaylink/Makefile.am | 12 +++++++---- libjaylink/core.c | 17 ++++++++++++++- libjaylink/device.c | 32 +++++++++++++++++++--------- libjaylink/discovery.c | 5 +++++ libjaylink/discovery_tcp.c | 4 ++-- libjaylink/discovery_usb.c | 4 ++-- libjaylink/libjaylink-internal.h | 15 +++++++++++++- libjaylink/transport.c | 45 +++++++++++++++++++++++++++------------- 10 files changed, 113 insertions(+), 36 deletions(-) diff --git a/configure.ac b/configure.ac index 6462415..400d6db 100644 --- a/configure.ac +++ b/configure.ac @@ -50,7 +50,17 @@ PKG_PROG_PKG_CONFIG # Checks for libraries. # Check for libusb-1.0 which is always needed. -PKG_CHECK_MODULES([libusb], [libusb-1.0 >= 1.0.9]) +PKG_CHECK_MODULES([libusb], [libusb-1.0 >= 1.0.9], + [HAVE_LIBUSB=yes], [HAVE_LIBUSB=no]) + +AS_IF([test "x$HAVE_LIBUSB" = "xyes"], + [AC_DEFINE([HAVE_LIBUSB], [1], [Define to 1 if libusb is available.])]) + +AS_IF([test "x$HAVE_LIBUSB" = "xyes"], + [JAYLINK_PKG_LIBS="libusb-1.0"]) + +AM_CONDITIONAL([HAVE_LIBUSB], + [test "x$HAVE_LIBUSB" = "xyes"]) # Checks for header files. @@ -93,6 +103,7 @@ AS_CASE([$host_os], [mingw*], [JAYLINK_LIBS="$JAYLINK_LIBS -lws2_32"]) AC_SUBST([JAYLINK_CFLAGS]) AC_SUBST([JAYLINK_LDFLAGS]) AC_SUBST([JAYLINK_LIBS]) +AC_SUBST([JAYLINK_PKG_LIBS]) AC_CONFIG_FILES([Makefile]) AC_CONFIG_FILES([libjaylink/Makefile]) diff --git a/libjaylink.pc.in b/libjaylink.pc.in index 2b156ab..a5efd3a 100644 --- a/libjaylink.pc.in +++ b/libjaylink.pc.in @@ -6,6 +6,6 @@ includedir=@includedir@ Name: libjaylink Description: Library to access J-Link devices Version: @VERSION@ -Requires.private: libusb-1.0 +Requires.private: @JAYLINK_PKG_LIBS@ Libs: -L${libdir} -ljaylink Cflags: -I${includedir} diff --git a/libjaylink/Makefile.am b/libjaylink/Makefile.am index 3ed3c74..62c5489 100644 --- a/libjaylink/Makefile.am +++ b/libjaylink/Makefile.am @@ -33,7 +33,6 @@ libjaylink_la_SOURCES = \ device.c \ discovery.c \ discovery_tcp.c \ - discovery_usb.c \ emucom.c \ error.c \ fileio.c \ @@ -47,12 +46,17 @@ libjaylink_la_SOURCES = \ target.c \ transport.c \ transport_tcp.c \ - transport_usb.c \ util.c \ version.c -libjaylink_la_CFLAGS = $(JAYLINK_CFLAGS) $(libusb_CFLAGS) +libjaylink_la_CFLAGS = $(JAYLINK_CFLAGS) libjaylink_la_LDFLAGS = $(JAYLINK_LDFLAGS) -no-undefined -libjaylink_la_LIBADD = $(JAYLINK_LIBS) $(libusb_LIBS) +libjaylink_la_LIBADD = $(JAYLINK_LIBS) + +if HAVE_LIBUSB +libjaylink_la_SOURCES += discovery_usb.c transport_usb.c +libjaylink_la_CFLAGS += $(libusb_CFLAGS) +libjaylink_la_LIBADD += $(libusb_LIBS) +endif noinst_HEADERS = libjaylink-internal.h diff --git a/libjaylink/core.c b/libjaylink/core.c index 9f0d8b1..e2d1e8f 100644 --- a/libjaylink/core.c +++ b/libjaylink/core.c @@ -17,12 +17,18 @@ * along with this program. If not, see . */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include #include #ifdef _WIN32 #include #endif +#ifdef HAVE_LIBUSB #include +#endif #include "libjaylink.h" #include "libjaylink-internal.h" @@ -98,22 +104,28 @@ JAYLINK_API int jaylink_init(struct jaylink_context **ctx) if (!context) return JAYLINK_ERR_MALLOC; +#ifdef HAVE_LIBUSB if (libusb_init(&context->usb_ctx) != LIBUSB_SUCCESS) { free(context); return JAYLINK_ERR; } +#endif #ifdef _WIN32 ret = WSAStartup(MAKEWORD(2, 2), &wsa_data); if (ret != 0) { +#ifdef HAVE_LIBUSB libusb_exit(context->usb_ctx); +#endif free(context); return JAYLINK_ERR; } if (LOBYTE(wsa_data.wVersion) != 2 || HIBYTE(wsa_data.wVersion) != 2) { +#ifdef HAVE_LIBUSB libusb_exit(context->usb_ctx); +#endif free(context); return JAYLINK_ERR; } @@ -170,11 +182,12 @@ JAYLINK_API int jaylink_exit(struct jaylink_context *ctx) list_free(ctx->discovered_devs); list_free(ctx->devs); +#ifdef HAVE_LIBUSB libusb_exit(ctx->usb_ctx); +#endif #ifdef _WIN32 WSACleanup(); #endif - free(ctx); return JAYLINK_OK; @@ -193,8 +206,10 @@ JAYLINK_API int jaylink_exit(struct jaylink_context *ctx) JAYLINK_API bool jaylink_library_has_cap(enum jaylink_capability cap) { switch (cap) { +#ifdef HAVE_LIBUSB case JAYLINK_CAP_HIF_USB: return true; +#endif default: return false; } diff --git a/libjaylink/device.c b/libjaylink/device.c index 5b79b43..a3bddf6 100644 --- a/libjaylink/device.c +++ b/libjaylink/device.c @@ -17,6 +17,10 @@ * along with this program. If not, see . */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include #include #include @@ -27,7 +31,9 @@ #include #include #endif +#ifdef HAVE_LIBUSB #include +#endif #include "libjaylink.h" #include "libjaylink-internal.h" @@ -206,7 +212,7 @@ JAYLINK_API int jaylink_device_get_host_interface( if (!dev || !iface) return JAYLINK_ERR_ARG; - *iface = dev->interface; + *iface = dev->iface; return JAYLINK_OK; } @@ -267,12 +273,16 @@ JAYLINK_API int jaylink_device_get_usb_address( if (!dev || !address) return JAYLINK_ERR_ARG; - if (dev->interface != JAYLINK_HIF_USB) + if (dev->iface != JAYLINK_HIF_USB) return JAYLINK_ERR_NOT_SUPPORTED; +#ifdef HAVE_LIBUSB *address = dev->usb_address; return JAYLINK_OK; +#else + return JAYLINK_ERR_NOT_SUPPORTED; +#endif } /** @@ -295,7 +305,7 @@ JAYLINK_API int jaylink_device_get_ipv4_address( if (!dev || !address) return JAYLINK_ERR_ARG; - if (dev->interface != JAYLINK_HIF_TCP) + if (dev->iface != JAYLINK_HIF_TCP) return JAYLINK_ERR_NOT_SUPPORTED; memcpy(address, dev->ipv4_address, sizeof(dev->ipv4_address)); @@ -325,7 +335,7 @@ JAYLINK_API int jaylink_device_get_mac_address( if (!dev || !address) return JAYLINK_ERR_ARG; - if (dev->interface != JAYLINK_HIF_TCP) + if (dev->iface != JAYLINK_HIF_TCP) return JAYLINK_ERR_NOT_SUPPORTED; if (!dev->has_mac_address) @@ -361,7 +371,7 @@ JAYLINK_API int jaylink_device_get_hardware_version( if (!dev || !version) return JAYLINK_ERR_ARG; - if (dev->interface != JAYLINK_HIF_TCP) + if (dev->iface != JAYLINK_HIF_TCP) return JAYLINK_ERR_NOT_SUPPORTED; if (!dev->has_hw_version) @@ -394,7 +404,7 @@ JAYLINK_API int jaylink_device_get_product_name( if (!dev || !name) return JAYLINK_ERR_ARG; - if (dev->interface != JAYLINK_HIF_TCP) + if (dev->iface != JAYLINK_HIF_TCP) return JAYLINK_ERR_NOT_SUPPORTED; if (!dev->has_product_name) @@ -427,7 +437,7 @@ JAYLINK_API int jaylink_device_get_nickname(const struct jaylink_device *dev, if (!dev || !nickname) return JAYLINK_ERR_ARG; - if (dev->interface != JAYLINK_HIF_TCP) + if (dev->iface != JAYLINK_HIF_TCP) return JAYLINK_ERR_NOT_SUPPORTED; if (!dev->has_nickname) @@ -478,19 +488,21 @@ JAYLINK_API void jaylink_unref_device(struct jaylink_device *dev) ctx = dev->ctx; ctx->devs = list_remove(dev->ctx->devs, dev); - if (dev->interface == JAYLINK_HIF_USB) { + if (dev->iface == JAYLINK_HIF_USB) { +#ifdef HAVE_LIBUSB log_dbg(ctx, "Device destroyed (bus:address = " "%03u:%03u).", libusb_get_bus_number(dev->usb_dev), libusb_get_device_address(dev->usb_dev)); libusb_unref_device(dev->usb_dev); - } else if (dev->interface == JAYLINK_HIF_TCP) { +#endif + } else if (dev->iface == JAYLINK_HIF_TCP) { log_dbg(ctx, "Device destroyed (IPv4 address = %s).", dev->ipv4_address); } else { log_err(ctx, "BUG: Invalid host interface: %u.", - dev->interface); + dev->iface); } free(dev); diff --git a/libjaylink/discovery.c b/libjaylink/discovery.c index 7b4808b..1ac96e7 100644 --- a/libjaylink/discovery.c +++ b/libjaylink/discovery.c @@ -19,6 +19,9 @@ #include +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #include "libjaylink.h" #include "libjaylink-internal.h" @@ -79,6 +82,7 @@ JAYLINK_API int jaylink_discovery_scan(struct jaylink_context *ctx, clear_discovery_list(ctx); +#ifdef HAVE_LIBUSB if (ifaces & JAYLINK_HIF_USB) { ret = discovery_usb_scan(ctx); @@ -87,6 +91,7 @@ JAYLINK_API int jaylink_discovery_scan(struct jaylink_context *ctx, return ret; } } +#endif if (ifaces & JAYLINK_HIF_TCP) { ret = discovery_tcp_scan(ctx); diff --git a/libjaylink/discovery_tcp.c b/libjaylink/discovery_tcp.c index 25e7732..555e121 100644 --- a/libjaylink/discovery_tcp.c +++ b/libjaylink/discovery_tcp.c @@ -61,7 +61,7 @@ static bool compare_devices(const void *a, const void *b) dev = a; new_dev = b; - if (dev->interface != JAYLINK_HIF_TCP) + if (dev->iface != JAYLINK_HIF_TCP) return false; if (memcmp(dev->ipv4_address, new_dev->ipv4_address, @@ -195,7 +195,7 @@ static struct jaylink_device *probe_device(struct jaylink_context *ctx, return NULL; } - dev->interface = JAYLINK_HIF_TCP; + dev->iface = JAYLINK_HIF_TCP; dev->serial_number = tmp.serial_number; dev->valid_serial_number = tmp.valid_serial_number; diff --git a/libjaylink/discovery_usb.c b/libjaylink/discovery_usb.c index 409ca75..48d5322 100644 --- a/libjaylink/discovery_usb.c +++ b/libjaylink/discovery_usb.c @@ -102,7 +102,7 @@ static bool compare_devices(const void *a, const void *b) dev = a; usb_dev = b; - if (dev->interface != JAYLINK_HIF_USB) + if (dev->iface != JAYLINK_HIF_USB) return false; if (dev->usb_dev == usb_dev) @@ -232,7 +232,7 @@ static struct jaylink_device *probe_device(struct jaylink_context *ctx, return NULL; } - dev->interface = JAYLINK_HIF_USB; + dev->iface = JAYLINK_HIF_USB; dev->usb_dev = libusb_ref_device(usb_dev); dev->usb_address = usb_address; dev->serial_number = serial_number; diff --git a/libjaylink/libjaylink-internal.h b/libjaylink/libjaylink-internal.h index e48d192..c5b128b 100644 --- a/libjaylink/libjaylink-internal.h +++ b/libjaylink/libjaylink-internal.h @@ -31,7 +31,14 @@ #include #include #endif + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_LIBUSB #include +#endif #include "libjaylink.h" @@ -52,8 +59,10 @@ #define MIN(a, b) (((a) < (b)) ? (a) : (b)) struct jaylink_context { +#ifdef HAVE_LIBUSB /** libusb context. */ struct libusb_context *usb_ctx; +#endif /** * List of allocated device instances. * @@ -78,7 +87,7 @@ struct jaylink_device { /** Number of references held on this device instance. */ size_t ref_count; /** Host interface. */ - enum jaylink_host_interface interface; + enum jaylink_host_interface iface; /** * Serial number of the device. * @@ -88,10 +97,12 @@ struct jaylink_device { uint32_t serial_number; /** Indicates whether the serial number is valid. */ bool valid_serial_number; +#ifdef HAVE_LIBUSB /** libusb device instance. */ struct libusb_device *usb_dev; /** USB address of the device. */ uint8_t usb_address; +#endif /** * IPv4 address. * @@ -169,6 +180,7 @@ struct jaylink_device_handle { * write operations only. */ size_t write_pos; +#ifdef HAVE_LIBUSB /** libusb device handle. */ struct libusb_device_handle *usb_devh; /** USB interface number of the device. */ @@ -177,6 +189,7 @@ struct jaylink_device_handle { uint8_t endpoint_in; /** USB interface OUT endpoint of the device. */ uint8_t endpoint_out; +#endif /** * Socket descriptor. * diff --git a/libjaylink/transport.c b/libjaylink/transport.c index 11837a4..0c276b3 100644 --- a/libjaylink/transport.c +++ b/libjaylink/transport.c @@ -22,6 +22,9 @@ #include #include +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #include "libjaylink.h" #include "libjaylink-internal.h" @@ -47,16 +50,18 @@ JAYLINK_PRIV int transport_open(struct jaylink_device_handle *devh) { int ret; - switch (devh->dev->interface) { + switch (devh->dev->iface) { +#ifdef HAVE_LIBUSB case JAYLINK_HIF_USB: ret = transport_usb_open(devh); break; +#endif case JAYLINK_HIF_TCP: ret = transport_tcp_open(devh); break; default: log_err(devh->dev->ctx, "BUG: Invalid host interface: %u.", - devh->dev->interface); + devh->dev->iface); return JAYLINK_ERR; } @@ -78,16 +83,18 @@ JAYLINK_PRIV int transport_close(struct jaylink_device_handle *devh) { int ret; - switch (devh->dev->interface) { + switch (devh->dev->iface) { +#ifdef HAVE_LIBUSB case JAYLINK_HIF_USB: ret = transport_usb_close(devh); break; +#endif case JAYLINK_HIF_TCP: ret = transport_tcp_close(devh); break; default: log_err(devh->dev->ctx, "BUG: Invalid host interface: %u.", - devh->dev->interface); + devh->dev->iface); return JAYLINK_ERR; } @@ -114,16 +121,18 @@ JAYLINK_PRIV int transport_start_write(struct jaylink_device_handle *devh, { int ret; - switch (devh->dev->interface) { + switch (devh->dev->iface) { +#ifdef HAVE_LIBUSB case JAYLINK_HIF_USB: ret = transport_usb_start_write(devh, length, has_command); break; +#endif case JAYLINK_HIF_TCP: ret = transport_tcp_start_write(devh, length, has_command); break; default: log_err(devh->dev->ctx, "BUG: Invalid host interface: %u.", - devh->dev->interface); + devh->dev->iface); return JAYLINK_ERR; } @@ -148,16 +157,18 @@ JAYLINK_PRIV int transport_start_read(struct jaylink_device_handle *devh, { int ret; - switch (devh->dev->interface) { + switch (devh->dev->iface) { +#ifdef HAVE_LIBUSB case JAYLINK_HIF_USB: ret = transport_usb_start_read(devh, length); break; +#endif case JAYLINK_HIF_TCP: ret = transport_tcp_start_read(devh, length); break; default: log_err(devh->dev->ctx, "BUG: Invalid host interface: %u.", - devh->dev->interface); + devh->dev->iface); return JAYLINK_ERR; } @@ -189,18 +200,20 @@ JAYLINK_PRIV int transport_start_write_read(struct jaylink_device_handle *devh, { int ret; - switch (devh->dev->interface) { + switch (devh->dev->iface) { +#ifdef HAVE_LIBUSB case JAYLINK_HIF_USB: ret = transport_usb_start_write_read(devh, write_length, read_length, has_command); break; +#endif case JAYLINK_HIF_TCP: ret = transport_tcp_start_write_read(devh, write_length, read_length, has_command); break; default: log_err(devh->dev->ctx, "BUG: Invalid host interface: %u.", - devh->dev->interface); + devh->dev->iface); return JAYLINK_ERR; } @@ -235,16 +248,18 @@ JAYLINK_PRIV int transport_write(struct jaylink_device_handle *devh, { int ret; - switch (devh->dev->interface) { + switch (devh->dev->iface) { +#ifdef HAVE_LIBUSB case JAYLINK_HIF_USB: ret = transport_usb_write(devh, buffer, length); break; +#endif case JAYLINK_HIF_TCP: ret = transport_tcp_write(devh, buffer, length); break; default: log_err(devh->dev->ctx, "BUG: Invalid host interface: %u.", - devh->dev->interface); + devh->dev->iface); return JAYLINK_ERR; } @@ -275,16 +290,18 @@ JAYLINK_PRIV int transport_read(struct jaylink_device_handle *devh, { int ret; - switch (devh->dev->interface) { + switch (devh->dev->iface) { +#ifdef HAVE_LIBUSB case JAYLINK_HIF_USB: ret = transport_usb_read(devh, buffer, length); break; +#endif case JAYLINK_HIF_TCP: ret = transport_tcp_read(devh, buffer, length); break; default: log_err(devh->dev->ctx, "BUG: Invalid host interface: %u.", - devh->dev->interface); + devh->dev->iface); return JAYLINK_ERR; } -- 2.11.4.GIT