From 6836e1d54292d05635aa7acee272e004f5af898d Mon Sep 17 00:00:00 2001 From: "Thomas \"Cakeisalie5\" Touhey" Date: Sun, 6 May 2018 19:08:28 +0200 Subject: [PATCH] Added usb bus and device address filtering --- Makefile.vars | 3 ++- include/libcasio/builtin.h | 6 ++++-- include/libcasio/link.h | 3 ++- include/libcasio/stream.h | 18 ++++++++++++------ lib/link/open/usb.c | 7 +++++-- lib/stream/builtin/libusb/open.c | 16 ++++++++++++++-- lib/stream/open/usb.c | 12 ++++++++++-- src/p7/main.c | 2 +- src/p7screen/main.c | 2 +- 9 files changed, 51 insertions(+), 18 deletions(-) diff --git a/Makefile.vars b/Makefile.vars index 8f85fc1..1ffced2 100755 --- a/Makefile.vars +++ b/Makefile.vars @@ -219,7 +219,8 @@ define get-util-source U_SRC_$1 := $(patsubst $(U_SRCDIR)/$1/%,%,$(wildcard $(U_SRCDIR)/$1/*.c \ $(U_SRCDIR)/$1/**/*.c)) U_INC_$1 := $(patsubst $(U_INCDIR)/$1/%,%,$(wildcard $(U_SRCDIR)/$1/*.h \ - $(U_SRCDIR)/$1/**/*.h)) + $(U_SRCDIR)/$1/**/*.h)) \ + $(if $(filter libcasio,$(U_DEPS_$1)),$(L_INCp:%=$(L_INCDIR)/%)) U_CFLAGS_$1 := $(CFLAGS) $(foreach dep,$(U_DEPS_$1),$(DEP_$(dep)_CFLAGS)) \ -D BIN="\"$1$(if $(FOR_WINDOWS),.exe)\"" \ diff --git a/include/libcasio/builtin.h b/include/libcasio/builtin.h index 11c6b3d..eb8bf1e 100644 --- a/include/libcasio/builtin.h +++ b/include/libcasio/builtin.h @@ -61,7 +61,8 @@ CASIO_EXTERN int CASIO_EXPORT casio_open_stream_fd # ifndef LIBCASIO_DISABLED_LIBUSB CASIO_EXTERN int CASIO_EXPORT casio_openusb_libusb - OF((casio_stream_t **casio__stream)); + OF((casio_stream_t **casio__stream, + int casio__bus, int casio__address)); # endif /* Make a stream using the Microsoft Windows API. */ @@ -69,7 +70,8 @@ CASIO_EXTERN int CASIO_EXPORT casio_openusb_libusb # if defined(_WIN16) || defined(_WIN32) || defined(_WIN64) \ || defined(__WINDOWS__) CASIO_EXTERN int CASIO_EXPORT casio_openusb_windows - OF((casio_stream_t **casio__stream)); + OF((casio_stream_t **casio__stream, + int casio__bus, int casio__address)); CASIO_EXTERN int CASIO_EXPORT casio_opencom_windows OF((casio_stream_t **casio__stream, const char *casio__path)); # else diff --git a/include/libcasio/link.h b/include/libcasio/link.h index 002c347..7ff7e5f 100644 --- a/include/libcasio/link.h +++ b/include/libcasio/link.h @@ -134,7 +134,8 @@ CASIO_BEGIN_DECLS /* Cross-platform initialization. */ CASIO_EXTERN int CASIO_EXPORT casio_open_usb - OF((casio_link_t **casio__h, unsigned long casio__flags)); + OF((casio_link_t **casio__h, unsigned long casio__flags, + int casio__bus, int casio__address)); CASIO_EXTERN int CASIO_EXPORT casio_open_com OF((casio_link_t **casio__h, unsigned long casio__flags, const char *casio__path, diff --git a/include/libcasio/stream.h b/include/libcasio/stream.h index 4abaebb..257c474 100644 --- a/include/libcasio/stream.h +++ b/include/libcasio/stream.h @@ -445,21 +445,27 @@ CASIO_EXTERN int CASIO_EXPORT casio_add_default_comlist typedef int casio_opencomstream_t OF((casio_stream_t **casio__stream, const char *casio__path)); +CASIO_EXTERN int CASIO_EXPORT casio_add_default_com_stream + OF((casio_opencomstream_t *casio__function)); CASIO_EXTERN int CASIO_EXPORT casio_open_com_stream OF((casio_stream_t **casio__stream, const char *casio__path)); -CASIO_EXTERN int CASIO_EXPORT casio_add_default_com_stream - OF((casio_opencomstream_t *casio__function)); -/* USB stream opening. */ +/* USB stream opening. + * The `bus` argument is set to -1 if we ought to find the first appropriate + * argument. + * The `address` argument is set to -1 if we ought to find any device on + * the given bus. */ typedef int CASIO_EXPORT casio_openusbstream_t - OF((casio_stream_t **casio__stream)); + OF((casio_stream_t **casio__stream, + int casio__bus, int casio__address)); -CASIO_EXTERN int CASIO_EXPORT casio_open_usb_stream - OF((casio_stream_t **casio__stream)); CASIO_EXTERN int CASIO_EXPORT casio_add_default_usb_stream OF((casio_openusbstream_t *casio__function)); +CASIO_EXTERN int CASIO_EXPORT casio_open_usb_stream + OF((casio_stream_t **casio__stream, + int casio__bus, int casio__address)); CASIO_END_DECLS CASIO_END_NAMESPACE diff --git a/lib/link/open/usb.c b/lib/link/open/usb.c index 9b0a935..8c04ca4 100644 --- a/lib/link/open/usb.c +++ b/lib/link/open/usb.c @@ -24,10 +24,13 @@ * * @arg handle the link handle to make. * @arg flags the link flags. + * @arg bus the bus number. + * @arg address the device number. * @return the error code (0 if ok). */ -int CASIO_EXPORT casio_open_usb(casio_link_t **handle, unsigned long flags) +int CASIO_EXPORT casio_open_usb(casio_link_t **handle, unsigned long flags, + int bus, int address) { int err, failed = 0, tries = 3; casio_stream_t *stream, *ns; @@ -42,7 +45,7 @@ int CASIO_EXPORT casio_open_usb(casio_link_t **handle, unsigned long flags) casio_sleep(1000); } - err = casio_open_usb_stream(&stream); + err = casio_open_usb_stream(&stream, bus, address); if (err == casio_error_op) return (casio_error_nocalc); if (!err) diff --git a/lib/stream/builtin/libusb/open.c b/lib/stream/builtin/libusb/open.c index 3eeead1..fccd755 100644 --- a/lib/stream/builtin/libusb/open.c +++ b/lib/stream/builtin/libusb/open.c @@ -35,11 +35,13 @@ CASIO_LOCAL const casio_streamfuncs_t casio_libusb_callbacks = { * Initialize a stream with USB device using libusb. * * @arg handle the handle to create. - * @arg flags the flags. + * @arg bus the bus number (-1 if both bus and address aren't set). + * @arg address the address on the bus (-1 if any address). * @return the error code (0 if you're a knoop). */ -int CASIO_EXPORT casio_openusb_libusb(casio_stream_t **stream) +int CASIO_EXPORT casio_openusb_libusb(casio_stream_t **stream, + int bus, int address) { int err = 0, uerr, id, device_count; libusb_context *context = NULL; @@ -69,6 +71,16 @@ int CASIO_EXPORT casio_openusb_libusb(casio_stream_t **stream) for (id = 0; id < device_count; id++) { struct libusb_device_descriptor descriptor; + /* Check the position on the bus. */ + + if (bus >= 0) { + if (libusb_get_bus_number(device_list[id]) != bus) + continue; + if (address >= 0 + && libusb_get_device_address(device_list[id]) != address) + continue; + } + /* Get the device descriptor. */ if (libusb_get_device_descriptor(device_list[id], &descriptor)) diff --git a/lib/stream/open/usb.c b/lib/stream/open/usb.c index f9089dc..7bbd688 100644 --- a/lib/stream/open/usb.c +++ b/lib/stream/open/usb.c @@ -62,17 +62,25 @@ int CASIO_EXPORT casio_add_default_usb_stream(casio_openusbstream_t *function) * Open the USB stream. * * @arg stream the stream to make. + * @arg bus the USB bus on which to find the device. + * -1 if any device on any bus (and address is not read). + * @arg address the address on the bus of the device. + * -1 if any device on the bus. * @return the error code (0 if ok). */ -int CASIO_EXPORT casio_open_usb_stream(casio_stream_t **stream) +int CASIO_EXPORT casio_open_usb_stream(casio_stream_t **stream, + int bus, int address) { int err; struct corresp *c = openusbs; + if (bus < -1 || bus > 255 || address < -1 || address > 255) + return (casio_error_op); if (!c->_valid) return (casio_error_op); + for (; c->_valid; c++) { - err = (*c->_openusb)(stream); + err = (*c->_openusb)(stream, bus, address); if (!err) return (0); diff --git a/src/p7/main.c b/src/p7/main.c index 43d8e52..bbb57fd 100644 --- a/src/p7/main.c +++ b/src/p7/main.c @@ -212,7 +212,7 @@ int main(int ac, char **av) if (args.com) err = casio_open_com(&handle, args.initflags, args.com, args.use); else - err = casio_open_usb(&handle, args.initflags); + err = casio_open_usb(&handle, args.initflags, -1, -1); if (err) { switch (err) { case casio_error_nocalc: diff --git a/src/p7screen/main.c b/src/p7screen/main.c index 327871d..13bd075 100644 --- a/src/p7screen/main.c +++ b/src/p7screen/main.c @@ -211,7 +211,7 @@ int main(int ac, char **av) /* Make the libcasio link handle. */ - if ((err = casio_open_usb(&handle, 0))) { + if ((err = casio_open_usb(&handle, 0, -1, -1))) { switch (err) { case casio_error_nocalc: fprintf(stderr, error_noconnexion);