mirror of https://github.com/nirenjan/libx52.git
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.pull/22/head
parent
2cb3474861
commit
92b0eb584f
|
@ -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 */
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue