From 92b0eb584fdaf8ae2ef8a65608b9aa86d275a31b Mon Sep 17 00:00:00 2001 From: nirenjan Date: Wed, 10 Jun 2020 17:04:24 -0700 Subject: [PATCH] Allow overriding the vendor command functionality This change allows for a test framework to override the functionality of libx52_vendor_command. By doing so, it can build tests that change the libx52_device structure, and therefore, not need to rely on libusbx52 to get the commands written. This allows for tests that can be built and run on other OSes as well, since the libusbx52 based tests cannot run on OSX and have not been tested on BSD systems. --- lib/libx52/x52_common.h | 4 ++++ lib/libx52/x52_control.c | 24 ++++++++++++++++-------- lib/libx52/x52_core.c | 4 ++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/libx52/x52_common.h b/lib/libx52/x52_common.h index 8928a9a..44e2e22 100644 --- a/lib/libx52/x52_common.h +++ b/lib/libx52/x52_common.h @@ -25,6 +25,8 @@ #define X52_MFD_LINES 3 #define X52_MFD_CLOCKS 3 +typedef int (*x52_vendor_command)(libx52_device *x52, uint16_t index, uint16_t value); + struct x52_mfd_line { uint8_t text[X52_MFD_LINE_SIZE]; uint8_t length; @@ -51,6 +53,7 @@ struct libx52_device { int timezone[X52_MFD_CLOCKS]; libx52_clock_format time_format[X52_MFD_CLOCKS]; + x52_vendor_command vendor_cmd_fn; }; /** Flag bits */ @@ -105,6 +108,7 @@ static inline uint32_t tst_bit(uint32_t *value, uint32_t bit) } int _x52_translate_libusb_error(enum libusb_error errcode); +int _x52_vendor_command(libx52_device *x52, uint16_t index, uint16_t value); uint16_t libx52_calculate_clock_offset(libx52_device *x52, libx52_clock_id clock, uint16_t h24); #endif /* !defined X52JOY_COMMON_H */ diff --git a/lib/libx52/x52_control.c b/lib/libx52/x52_control.c index e96db59..6193e23 100644 --- a/lib/libx52/x52_control.c +++ b/lib/libx52/x52_control.c @@ -66,18 +66,11 @@ int _x52_translate_libusb_error(enum libusb_error errcode) }; } -int libx52_vendor_command(libx52_device *x52, uint16_t index, uint16_t value) +int _x52_vendor_command(libx52_device *x52, uint16_t index, uint16_t value) { int j; int rc = 0; - /* It is possible for the vendor command to be called when the joystick - * is not connected. Check for this and return an appropriate error. - */ - if (!x52->hdl) { - return LIBX52_ERROR_NO_DEVICE; - } - /* Allow retry in case of failure */ for (j = 0; j < 3; j++) { rc = libusb_control_transfer(x52->hdl, @@ -89,6 +82,21 @@ int libx52_vendor_command(libx52_device *x52, uint16_t index, uint16_t value) } } +} + +int libx52_vendor_command(libx52_device *x52, uint16_t index, uint16_t value) +{ + int rc = 0; + + /* It is possible for the vendor command to be called when the joystick + * is not connected. Check for this and return an appropriate error. + */ + if (!x52->hdl || !x52->vendor_cmd_fn) { + return LIBX52_ERROR_NO_DEVICE; + } + + rc = (*(x52->vendor_cmd_fn))(x52, index, value); + /* Handle device removal */ if (rc == LIBUSB_ERROR_NO_DEVICE) { /* Physical device has likely been disconnected, disconnect the virtual diff --git a/lib/libx52/x52_core.c b/lib/libx52/x52_core.c index 7387ab1..0d345d4 100644 --- a/lib/libx52/x52_core.c +++ b/lib/libx52/x52_core.c @@ -59,6 +59,7 @@ int libx52_disconnect(libx52_device *dev) libusb_close(dev->hdl); dev->hdl = NULL; dev->flags = 0; + dev->vendor_cmd_fn = NULL; } return LIBX52_SUCCESS; @@ -100,6 +101,9 @@ int libx52_connect(libx52_device *dev) dev->hdl = hdl; + /* Use default vendor command function */ + dev->vendor_cmd_fn = _x52_vendor_command; + if (libx52_device_is_x52pro(desc.idProduct)) { set_bit(&(dev->flags), X52_FLAG_IS_PRO); }