Skip to content

Commit

Permalink
Merge pull request #1244 from jimklimov/fightwarn-libusb-1.0+0.1-reba…
Browse files Browse the repository at this point in the history
…sed-part1
  • Loading branch information
jimklimov committed Dec 25, 2021
2 parents f3cf4ed + abb01ed commit 2c30c70
Show file tree
Hide file tree
Showing 22 changed files with 834 additions and 670 deletions.
70 changes: 24 additions & 46 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,13 @@ AC_ARG_WITH(all,

dnl ----------------------------------------------------------------------
dnl declare a number of --with-FEATURE options. Do this early, so that
dnl they are listed near the top by "./configure --help"
dnl they are listed near the top by "./configure --help"; however,
dnl note that options with further investigation methods are listed
dnl a bit below to be grouped with their additional with/enable help.

NUT_ARG_WITH([dev], [build and install the development files], [no])
NUT_ARG_WITH([serial], [build and install serial drivers], [yes])
NUT_ARG_WITH([usb], [build and install USB drivers], [auto])
NUT_ARG_WITH([avahi], [build and install Avahi support], [auto])

dnl The NUT legacy option was --with-doc; however to simplify configuration
dnl in some common packaging frameworks, we also allow --with-docs as
dnl a second-class citizen (if both are set, the old option name wins).
Expand All @@ -401,6 +402,7 @@ dnl of the "make distcheck", but it was reduced to "man=auto" so that
dnl the usual builds can pass by default on systems without asciidoc.
NUT_ARG_WITH([docs], [build and install documentation (alias to --with-doc)], [man=auto])
NUT_ARG_WITH([doc], [build and install documentation (see docs/configure.txt for many variants of the option)], [${nut_with_docs}])

NUT_ARG_ENABLE([warnings], [enable warning presets that were picked as useful in maintainership and CI practice (variants include gcc-minimal, gcc-medium, gcc-hard, clang-minimal, clang-medium, clang-hard, all; auto-choosers: hard, medium, minimal, yes=auto='gcc or clang or all at hardcoded default difficulty')], [legacy])
NUT_ARG_ENABLE([Werror], [fail the build if compiler emits any warnings (treat them as errors)], [no])

Expand All @@ -411,15 +413,23 @@ dnl These checks are performed unconditionally, even if the corresponding
dnl --with-* options are not given. This is because we cannot predict
dnl what will be in the --with-drivers argument.

NUT_CHECK_LIBNETSNMP
NUT_ARG_WITH([usb], [build and install USB drivers, optionally require build with specified version of libusb library and API: (auto|libusb-0.1|libusb-1.0)], [auto])
nut_usb_lib=""
NUT_CHECK_LIBUSB

NUT_ARG_WITH([snmp], [build and install SNMP drivers], [auto])
NUT_CHECK_LIBNETSNMP

NUT_ARG_WITH([neon], [build and install neon based XML/HTTP driver], [auto])
NUT_CHECK_LIBNEON

NUT_ARG_WITH([powerman], [build and install Powerman PDU client driver], [auto])
NUT_CHECK_LIBPOWERMAN

NUT_ARG_WITH([modbus], [build and install modbus drivers], [auto])
NUT_CHECK_LIBMODBUS

NUT_ARG_WITH([avahi], [build and install Avahi support], [auto])
NUT_CHECK_LIBAVAHI

dnl ----------------------------------------------------------------------
Expand Down Expand Up @@ -505,52 +515,15 @@ NUT_REPORT_FEATURE([build serial drivers], [${nut_with_serial}], [],
[WITH_SERIAL], [Define to enable serial support])

dnl ----------------------------------------------------------------------
dnl checks related to --with-usb
dnl checks related to --with-usb are in its m4 file and NUT_CHECK_LIBUSB() called above

nut_usb_lib=""

if test "${nut_with_usb}" != "no"; then
if test "${nut_with_usb}" = "libusb-1.0" -o "${nut_with_usb}" = "libusb-0.1"; then
NUT_CHECK_LIBUSB([${nut_with_usb}])
dnl Explicitly choosing a library implies 'yes' (i.e. fail if not found), not 'auto'.
nut_with_usb=yes
else
NUT_CHECK_LIBUSB
fi
dnl Any other value not implying 'yes' is treated as 'auto'.
if test "${nut_with_usb}" = "yes" -a "${nut_have_libusb}" != "yes"; then
AC_MSG_ERROR([USB drivers requested, but libusb not found.])
fi
nut_with_usb="${nut_have_libusb}"
fi

AM_CONDITIONAL(WITH_LIBUSB_0_1, test "${nut_usb_lib}" = "(libusb-0.1)")
dnl Note: there is no libusb-config script (and variable) for libusb-1.0
AM_CONDITIONAL(WITH_LIBUSB_1_0, test "${nut_usb_lib}" = "(libusb-1.0)")
AM_CONDITIONAL(WITH_LIBUSB_0_1, test "${nut_usb_lib}" = "(libusb-0.1)" -o "${nut_usb_lib}" = "(libusb-0.1-config)")

NUT_REPORT_FEATURE([build USB drivers], [${nut_with_usb}], [${nut_usb_lib}],
[WITH_USB], [Define to enable USB support])


dnl ----------------------------------------------------------------------
dnl additional USB-related checks

dnl Solaris 10/11 USB handling (need librt and libusb runtime path)
dnl HPUX, since v11, needs an explicit activation of pthreads
case ${target_os} in
solaris2.1* )
AC_MSG_CHECKING([for Solaris 10 / 11 specific configuration for usb drivers])
AC_SEARCH_LIBS(nanosleep, rt)
LIBUSB_LIBS="-R/usr/sfw/lib ${LIBUSB_LIBS}"
dnl FIXME: Sun's libusb doesn't support timeout (so blocks notification)
dnl and need to call libusb close upon reconnection
AC_DEFINE(SUN_LIBUSB, 1, [Define to 1 for Sun version of the libusb.])
SUN_LIBUSB=1
AC_MSG_RESULT([${LIBUSB_LIBS}])
;;
hpux11*)
CFLAGS="${CFLAGS} -lpthread"
;;
esac

dnl ----------------------------------------------------------------------
dnl checks related to --with-neon

Expand Down Expand Up @@ -2290,6 +2263,11 @@ dnl # signatures differ between dependency releases; sometimes they just
dnl # happened too hard to unravel cleanly and add more warnings.
dnl # This exclusion may be removed after common warnings are solved,
dnl # to allow progress on rectifying these cases next.
dnl # -Wno-incompatible-pointer-types-discards-qualifiers -- our code often
dnl # defines (char*) as the type for struct fields and method arguments,
dnl # but initializes/passes (char[]) variables or fixed strings.
dnl # This makes at least clang-3.4 quite upset and noisy (seems newer
dnl # versions care less about this situation).
AS_CASE(["${nut_enable_warnings}"],
[all], [
CFLAGS="${CFLAGS} -Wall"
Expand All @@ -2300,7 +2278,7 @@ AS_CASE(["${nut_enable_warnings}"],
CXXFLAGS="${CXXFLAGS} -ferror-limit=0 -Wall -Wextra -Weverything -Wno-unused-macros -Wno-reserved-id-macro -Wno-padded -Wno-documentation -Wno-cast-qual -Wno-c++98-compat-pedantic -Wno-c++98-compat"
],
[clang-medium], [
CFLAGS="${CFLAGS} -ferror-limit=0 -Wall -Wextra -Weverything -Wno-unused-macros -Wno-reserved-id-macro -Wno-padded -Wno-documentation -Wno-cast-qual -pedantic -Wno-float-conversion -Wno-double-promotion -Wno-implicit-float-conversion"
CFLAGS="${CFLAGS} -ferror-limit=0 -Wall -Wextra -Weverything -Wno-unused-macros -Wno-reserved-id-macro -Wno-padded -Wno-documentation -Wno-cast-qual -pedantic -Wno-float-conversion -Wno-double-promotion -Wno-implicit-float-conversion -Wno-incompatible-pointer-types-discards-qualifiers"
CXXFLAGS="${CXXFLAGS} -ferror-limit=0 -Wall -Wextra -Weverything -Wno-unused-macros -Wno-reserved-id-macro -Wno-padded -Wno-documentation -Wno-cast-qual -Wno-c++98-compat-pedantic -Wno-c++98-compat"
],
[clang-minimal], [
Expand Down
13 changes: 8 additions & 5 deletions docs/configure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ Build and install the serial drivers (default: yes)
--with-usb

Build and install the USB drivers (default: auto-detect)
Note that you need to install the libusb development package or files, and that
both libusb 0.1 and 1.0 are supported. In case both are available, libusb 1.0
takes precedence, and will be used. It is however possible to override this
default choice by explicitly calling '--with-usb=libusb-0.1' or
'--with-usb=libusb-1.0'.
Note that you need to install the libusb development package or files,
and that both libusb 0.1 and 1.0 are supported. In case both are
available, libusb 1.0 takes precedence, and will be used by default.
It is however possible to override this default choice by explicitly
calling `--with-usb=libusb-0.1` or `--with-usb=libusb-1.0`.
If you do specify the version to use (or `yes` for auto-detection),
this option would fail if requested (or any) libusb version was not
found. The default `auto` value would not fail in such case.

--with-snmp

Expand Down
2 changes: 1 addition & 1 deletion drivers/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ riello_usb_LDADD = $(LDADD_DRIVERS) $(LIBUSB_LIBS) -lm
# HID-over-serial
mge_shut_SOURCES = usbhid-ups.c libshut.c libhid.c hidparser.c mge-hid.c
# per-target CFLAGS are necessary here
mge_shut_CFLAGS = $(AM_CFLAGS) -DSHUT_MODE
mge_shut_CFLAGS = $(AM_CFLAGS) -DSHUT_MODE=1
mge_shut_LDADD = $(LDADD) -lm

# SNMP
Expand Down
47 changes: 7 additions & 40 deletions drivers/bcmxcp_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@
#include <sys/file.h>
#include <sys/types.h>
#include <unistd.h>
/* libusb header file */
#ifdef WITH_LIBUSB_1_0
#include <libusb.h>
#endif
#ifdef WITH_LIBUSB_0_1
#include <usb.h>
#endif

#define SUBDRIVER_NAME "USB communication subdriver"
#define SUBDRIVER_VERSION "0.26"
Expand All @@ -42,35 +35,6 @@ upsdrv_info_t comm_upsdrv_info = {

static USBDevice_t curDevice;

#ifdef WITH_LIBUSB_1_0
/* Simply remap libusb functions/structures from 0.1 to 1.0 */
/* Structures */
#define usb_dev_handle libusb_device_handle
/* defines */
#define USB_DT_STRING LIBUSB_DT_STRING
#define USB_ENDPOINT_OUT LIBUSB_ENDPOINT_OUT
#define USB_REQ_SET_DESCRIPTOR LIBUSB_REQUEST_SET_DESCRIPTOR
#define USB_CLASS_PER_INTERFACE LIBUSB_CLASS_PER_INTERFACE
/* Functions */
#define usb_control_msg libusb_control_transfer
static inline int usb_interrupt_read(usb_dev_handle *dev, int ep,
char *bytes, int size, int timeout)
{
int ret = libusb_interrupt_transfer(dev, ep, (unsigned char *) bytes,
size, &size, timeout);
/* In case of success, return the operation size, as done with libusb 0.1 */
return (ret == LIBUSB_SUCCESS)?size:ret;
}

#define usb_claim_interface libusb_claim_interface
#define usb_release_interface libusb_release_interface
#define usb_reset libusb_reset_device
#define usb_clear_halt libusb_clear_halt
#define nut_usb_strerror(a) libusb_strerror(a)
#else
#define nut_usb_strerror(a) usb_strerror()
#endif /* #ifdef WITH_LIBUSB_1_0 */

/* USB functions */
usb_dev_handle *nutusb_open(const char *port);
int nutusb_close(usb_dev_handle *dev_h, const char *port);
Expand All @@ -79,10 +43,12 @@ void nutusb_comm_fail(const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 1, 2)));
void nutusb_comm_good(void);
/* function pointer, set depending on which device is used */
/* FIXME? Use usb_ctrl_* typedefs*/
static int (*usb_set_descriptor)(usb_dev_handle *udev, unsigned char type,
unsigned char index, void *buf, size_t size);

/* usb_set_descriptor() for Powerware devices */
/* FIXME? Use usb_ctrl_* typedefs*/
static int usb_set_powerware(usb_dev_handle *udev, unsigned char type, unsigned char index, void *buf, size_t size)
{
assert (size < INT_MAX);
Expand All @@ -96,6 +62,7 @@ static void *powerware_ups(USBDevice_t *device) {
}

/* usb_set_descriptor() for Phoenixtec devices */
/* FIXME? Use usb_ctrl_* typedefs*/
static int usb_set_phoenixtec(usb_dev_handle *udev, unsigned char type, unsigned char index, void *buf, size_t size)
{
NUT_UNUSED_VARIABLE(index);
Expand Down Expand Up @@ -210,7 +177,7 @@ ssize_t get_answer(unsigned char *data, unsigned char command)
if (need_data > 0) {
res = usb_interrupt_read(upsdev,
0x81,
(char *) buf + bytes_read,
(usb_ctrl_charbuf) buf + bytes_read,
128,
(int)(XCP_USB_TIMEOUT - elapsed_time));

Expand Down Expand Up @@ -428,7 +395,7 @@ static void nutusb_open_error(const char *port)
/* FIXME: this part of the opening can go into common... */
static usb_dev_handle *open_powerware_usb(void)
{
#ifdef WITH_LIBUSB_1_0
#if WITH_LIBUSB_1_0
libusb_device **devlist;
ssize_t devcount = 0;
libusb_device_handle *udev;
Expand Down Expand Up @@ -517,7 +484,7 @@ usb_dev_handle *nutusb_open(const char *port)
upsdebugx(1, "entering nutusb_open()");

/* Initialize Libusb */
#ifdef WITH_LIBUSB_1_0
#if WITH_LIBUSB_1_0
if (libusb_init(NULL) < 0) {
libusb_exit(NULL);
fatal_with_errno(EXIT_FAILURE, "Failed to init libusb 1.0");
Expand Down Expand Up @@ -597,7 +564,7 @@ int nutusb_close(usb_dev_handle *dev_h, const char *port)
if (dev_h)
{
usb_release_interface(dev_h, 0);
#ifdef WITH_LIBUSB_1_0
#if WITH_LIBUSB_1_0
libusb_close(dev_h);
libusb_exit(NULL);
#else
Expand Down

0 comments on commit 2c30c70

Please sign in to comment.