Fix transfer error handling

This commit is contained in:
Alexander Ponyatykh committed 2019-11-18 02:50:24 +03:00
1 parent 80dac3d2d6
commit bd4495ec01
2 files changed
+89 -63

No files matched your search

+1 -1
View File
@@ -3,7 +3,7 @@ libg15 (1.3.0~svn324-2) unstable; urgency=medium
* Migrate from legacy libusb-0.1 to libusb-1.0 (closes: bug#810409, * Migrate from legacy libusb-0.1 to libusb-1.0 (closes: bug#810409,
bug#611649). bug#611649).
-- Alexander Ponyatykh <lazyranma@gmail.com> Sun, 17 Nov 2019 03:14:55 +0300 -- Alexander Ponyatykh <lazyranma@gmail.com> Sun, 17 Nov 2019 14:29:55 +0300
libg15 (1.3.0~svn324-1) unstable; urgency=medium libg15 (1.3.0~svn324-1) unstable; urgency=medium
@@ -4,12 +4,14 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
--- ---
configure.in | 5 +- configure.in | 5 +-
libg15.c | 476 +++++++++++++++++++++++++++++------------------------------ libg15.c | 504 +++++++++++++++++++++++++++++------------------------------
2 files changed, 234 insertions(+), 247 deletions(-) 2 files changed, 249 insertions(+), 260 deletions(-)
diff --git a/configure.in b/configure.in
index 074d98e..b533d2c 100644
--- a/configure.in --- a/configure.in
+++ b/configure.in +++ b/configure.in
@@ -31,7 +31,8 @@ @@ -31,7 +31,8 @@ case $host_os in
esac esac
# Checks for libraries. # Checks for libraries.
@@ -19,7 +21,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
dnl Some versions of libusb do not honour timeout and block. Suns' version is one of them. dnl Some versions of libusb do not honour timeout and block. Suns' version is one of them.
if test "x$libusb_blocks" == "xtrue"; then if test "x$libusb_blocks" == "xtrue"; then
@@ -41,7 +42,7 @@ @@ -41,7 +42,7 @@ fi
# Checks for header files. # Checks for header files.
AC_HEADER_STDC AC_HEADER_STDC
AC_CHECK_HEADERS([string.h]) AC_CHECK_HEADERS([string.h])
@@ -28,6 +30,8 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
# Checks for typedefs, structures, and compiler characteristics. # Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST AC_C_CONST
diff --git a/libg15.c b/libg15.c
index ba1c94b..ba01ae0 100644
--- a/libg15.c --- a/libg15.c
+++ b/libg15.c +++ b/libg15.c
@@ -24,20 +24,21 @@ @@ -24,20 +24,21 @@
@@ -55,7 +59,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
/* to add a new device, simply create a new DEVICE() in this list */ /* to add a new device, simply create a new DEVICE() in this list */
/* Fields are: "Name",VendorID,ProductID,Capabilities */ /* Fields are: "Name",VendorID,ProductID,Capabilities */
@@ -67,7 +68,9 @@ @@ -67,7 +68,9 @@ int g15DeviceCapabilities() {
void libg15Debug(int option) { void libg15Debug(int option) {
libg15_debugging_enabled = option; libg15_debugging_enabled = option;
@@ -66,7 +70,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
} }
/* debugging wrapper */ /* debugging wrapper */
@@ -86,191 +89,207 @@ @@ -86,191 +89,207 @@ static int g15_log (FILE *fd, unsigned int level, const char *fmt, ...) {
/* return number of connected and supported devices */ /* return number of connected and supported devices */
int g15NumberOfConnectedDevices() { int g15NumberOfConnectedDevices() {
@@ -110,13 +114,13 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
static int initLibUsb() static int initLibUsb()
{ {
- usb_init(); - usb_init();
+ int errcode = libusb_init(&libg15_libusb_context); -
- /** - /**
- * usb_find_busses and usb_find_devices both report the number of devices - * usb_find_busses and usb_find_devices both report the number of devices
- * / busses added / removed since the last call. since this is the first - * / busses added / removed since the last call. since this is the first
- * call we have to return values != 0 or else we didnt find anything */ - * call we have to return values != 0 or else we didnt find anything */
- + int errcode = libusb_init(&libg15_libusb_context);
- if (!usb_find_busses()) - if (!usb_find_busses())
+ if (errcode != 0) + if (errcode != 0)
+ { + {
@@ -148,17 +152,17 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
+ int ret; + int ret;
+ +
+ ssize_t cnt = libusb_get_device_list(libg15_libusb_context, &list); + ssize_t cnt = libusb_get_device_list(libg15_libusb_context, &list);
+
- for (bus = usb_busses; bus; bus = bus->next)
+ if (cnt < 0) + if (cnt < 0)
{ + {
- for (dev = bus->devices; dev; dev = dev->next)
+ g15_log(stderr, G15_LOG_INFO, "Unable to list USB devices: %s\n", libusb_strerror(cnt)); + g15_log(stderr, G15_LOG_INFO, "Unable to list USB devices: %s\n", libusb_strerror(cnt));
+ return 0; + return 0;
+ } + }
+
- for (bus = usb_busses; bus; bus = bus->next)
+ for (ssize_t devno = 0; devno < cnt; devno++) + for (ssize_t devno = 0; devno < cnt; devno++)
+ { {
- for (dev = bus->devices; dev; dev = dev->next)
+ libusb_get_device_descriptor(list[devno], &desc); + libusb_get_device_descriptor(list[devno], &desc);
+ if (desc.idVendor == handled_device.vendorid && desc.idProduct == handled_device.productid) + if (desc.idVendor == handled_device.vendorid && desc.idProduct == handled_device.productid)
{ {
@@ -187,40 +191,40 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
+ break; + break;
+ } + }
+ } + }
+
- usleep(50*1000);
+ if (!dev) { + if (!dev) {
+ libusb_free_device_list(list, 1); + libusb_free_device_list(list, 1);
+ return 0; + return 0;
+ } + }
+
+ found_devicetype = device_index;
+
+ g15_log(stderr,G15_LOG_INFO,"Found %s, trying to open it\n",handled_device.name);
- usleep(50*1000);
+ ret = libusb_open(dev, &devh);
+ libusb_free_device_list(list, 1);
- g15_log(stderr, G15_LOG_INFO, "Device has %i possible configurations\n",dev->descriptor.bNumConfigurations); - g15_log(stderr, G15_LOG_INFO, "Device has %i possible configurations\n",dev->descriptor.bNumConfigurations);
+ found_devicetype = device_index;
- /* if device is shared with another driver, such as the Z-10 speakers sharing with alsa, we have to disable some calls */
- if(g15DeviceCapabilities() & G15_DEVICE_IS_SHARED)
- shared_device = 1;
+ g15_log(stderr,G15_LOG_INFO,"Found %s, trying to open it\n",handled_device.name);
+
+ ret = libusb_open(dev, &devh);
+ libusb_free_device_list(list, 1);
+
+ if (ret || !devh) + if (ret || !devh)
+ { + {
+ g15_log(stderr,G15_LOG_INFO, "Error, could not open the keyboard: %s\n", libusb_strerror(ret)); + g15_log(stderr,G15_LOG_INFO, "Error, could not open the keyboard: %s\n", libusb_strerror(ret));
+ return 0; + return 0;
+ } + }
+
+ usleep(50*1000);
+
+ g15_log(stderr, G15_LOG_INFO, "Device has %i possible configurations\n", desc.bNumConfigurations);
- /* if device is shared with another driver, such as the Z-10 speakers sharing with alsa, we have to disable some calls */
- if(g15DeviceCapabilities() & G15_DEVICE_IS_SHARED)
- shared_device = 1;
+ /* if device is shared with another driver, such as the Z-10 speakers sharing with alsa, we have to disable some calls */
+ if(g15DeviceCapabilities() & G15_DEVICE_IS_SHARED)
+ shared_device = 1;
- for (j = 0; j<dev->descriptor.bNumConfigurations;j++){ - for (j = 0; j<dev->descriptor.bNumConfigurations;j++){
- struct usb_config_descriptor *cfg = &dev->config[j]; - struct usb_config_descriptor *cfg = &dev->config[j];
+ usleep(50*1000);
+
+ g15_log(stderr, G15_LOG_INFO, "Device has %i possible configurations\n", desc.bNumConfigurations);
+
+ /* if device is shared with another driver, such as the Z-10 speakers sharing with alsa, we have to disable some calls */
+ if(g15DeviceCapabilities() & G15_DEVICE_IS_SHARED)
+ shared_device = 1;
+
+ for (int j = 0; j < desc.bNumConfigurations; j++) + for (int j = 0; j < desc.bNumConfigurations; j++)
+ { + {
+ struct libusb_config_descriptor *cfg; + struct libusb_config_descriptor *cfg;
@@ -232,16 +236,16 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
+ { + {
+ if (i==G510_STANDARD_KEYBOARD_INTERFACE) continue; + if (i==G510_STANDARD_KEYBOARD_INTERFACE) continue;
+ } + }
+
- for (i=0;i<cfg->bNumInterfaces; i++){
- if (g15DeviceCapabilities()&G15_DEVICE_G510){
- if (i==G510_STANDARD_KEYBOARD_INTERFACE) continue;
+ const struct libusb_interface *ifp = &cfg->interface[i]; + const struct libusb_interface *ifp = &cfg->interface[i];
+ /* if endpoints are already known, finish up */ + /* if endpoints are already known, finish up */
+ if (g15_keys_endpoint && g15_lcd_endpoint) + if (g15_keys_endpoint && g15_lcd_endpoint)
+ break; + break;
+ g15_log(stderr, G15_LOG_INFO, "Device has %i Alternate Settings\n", ifp->num_altsetting); + g15_log(stderr, G15_LOG_INFO, "Device has %i Alternate Settings\n", ifp->num_altsetting);
+
- for (i=0;i<cfg->bNumInterfaces; i++){
- if (g15DeviceCapabilities()&G15_DEVICE_G510){
- if (i==G510_STANDARD_KEYBOARD_INTERFACE) continue;
+ for (int k=0; k < ifp->num_altsetting; k++) + for (int k=0; k < ifp->num_altsetting; k++)
+ { + {
+ const struct libusb_interface_descriptor *as = &ifp->altsetting[k]; + const struct libusb_interface_descriptor *as = &ifp->altsetting[k];
@@ -418,7 +422,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
int i; int i;
for (i=0; g15_devices[i].name !=NULL ;i++){ for (i=0; g15_devices[i].name !=NULL ;i++){
g15_log(stderr,G15_LOG_INFO,"Trying to find %s\n",g15_devices[i].name); g15_log(stderr,G15_LOG_INFO,"Trying to find %s\n",g15_devices[i].name);
@@ -288,19 +307,13 @@ @@ -288,19 +307,13 @@ static int closeKeyboardDevice()
{ {
int retval = G15_NO_ERROR; int retval = G15_NO_ERROR;
@@ -443,7 +447,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
keyboard_device=0; keyboard_device=0;
return retval; return retval;
} }
@@ -311,9 +324,6 @@ @@ -311,9 +324,6 @@ int re_initLibG15()
{ {
closeKeyboardDevice(); closeKeyboardDevice();
@@ -453,7 +457,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
keyboard_device = findAndOpenG15(); keyboard_device = findAndOpenG15();
if (!keyboard_device) if (!keyboard_device)
return G15_ERROR_OPENING_USB_DEVICE; return G15_ERROR_OPENING_USB_DEVICE;
@@ -330,10 +340,6 @@ @@ -330,10 +340,6 @@ int initLibG15()
g15_log(stderr,G15_LOG_INFO,"%s\n",PACKAGE_STRING); g15_log(stderr,G15_LOG_INFO,"%s\n",PACKAGE_STRING);
@@ -464,7 +468,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
g15NumberOfConnectedDevices(); g15NumberOfConnectedDevices();
keyboard_device = findAndOpenG15(); keyboard_device = findAndOpenG15();
@@ -347,7 +353,9 @@ @@ -347,7 +353,9 @@ int initLibG15()
int exitLibG15() int exitLibG15()
{ {
pthread_mutex_destroy(&libusb_mutex); pthread_mutex_destroy(&libusb_mutex);
@@ -475,7 +479,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
} }
static void dumpPixmapIntoLCDFormat(unsigned char *lcd_buffer, unsigned char const *data) static void dumpPixmapIntoLCDFormat(unsigned char *lcd_buffer, unsigned char const *data)
@@ -428,36 +436,6 @@ @@ -428,36 +436,6 @@ static void dumpPixmapIntoLCDFormat(unsigned char *lcd_buffer, unsigned char con
} }
} }
@@ -512,7 +516,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
int writePixmapToLCD(unsigned char const *data) int writePixmapToLCD(unsigned char const *data)
{ {
int ret = 0; int ret = 0;
@@ -474,43 +452,27 @@ @@ -474,43 +452,27 @@ int writePixmapToLCD(unsigned char const *data)
/* the keyboard needs this magic byte */ /* the keyboard needs this magic byte */
lcd_buffer[0] = 0x03; lcd_buffer[0] = 0x03;
@@ -574,7 +578,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
return 0; return 0;
} }
@@ -535,7 +497,9 @@ @@ -535,7 +497,9 @@ int setLCDContrast(unsigned int level)
usb_data[3] = 18; usb_data[3] = 18;
} }
pthread_mutex_lock(&libusb_mutex); pthread_mutex_lock(&libusb_mutex);
@@ -585,7 +589,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
pthread_mutex_unlock(&libusb_mutex); pthread_mutex_unlock(&libusb_mutex);
return retval; return retval;
} }
@@ -553,7 +517,9 @@ @@ -553,7 +517,9 @@ int setLEDs(unsigned int leds)
return G15_ERROR_UNSUPPORTED; return G15_ERROR_UNSUPPORTED;
pthread_mutex_lock(&libusb_mutex); pthread_mutex_lock(&libusb_mutex);
@@ -596,7 +600,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
pthread_mutex_unlock(&libusb_mutex); pthread_mutex_unlock(&libusb_mutex);
return retval; return retval;
} }
@@ -578,7 +544,9 @@ @@ -578,7 +544,9 @@ int setLCDBrightness(unsigned int level)
usb_data[2] = 0x00; usb_data[2] = 0x00;
} }
pthread_mutex_lock(&libusb_mutex); pthread_mutex_lock(&libusb_mutex);
@@ -607,7 +611,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
pthread_mutex_unlock(&libusb_mutex); pthread_mutex_unlock(&libusb_mutex);
return retval; return retval;
} }
@@ -604,7 +572,9 @@ @@ -604,7 +572,9 @@ int setKBBrightness(unsigned int level)
usb_data[2] = 0x0; usb_data[2] = 0x0;
} }
pthread_mutex_lock(&libusb_mutex); pthread_mutex_lock(&libusb_mutex);
@@ -618,7 +622,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
pthread_mutex_unlock(&libusb_mutex); pthread_mutex_unlock(&libusb_mutex);
return retval; return retval;
} }
@@ -619,7 +589,9 @@ @@ -619,7 +589,9 @@ int setG510LEDColor(unsigned char r, unsigned char g, unsigned char b)
usb_data[3] = b; usb_data[3] = b;
pthread_mutex_lock(&libusb_mutex); pthread_mutex_lock(&libusb_mutex);
@@ -629,7 +633,7 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
pthread_mutex_unlock(&libusb_mutex); pthread_mutex_unlock(&libusb_mutex);
return retval; return retval;
} }
@@ -1033,18 +1005,30 @@ @@ -1033,38 +1005,54 @@ static void processKeyEvent4Byte(unsigned int *pressed_keys, unsigned char *buff
int getPressedKeys(unsigned int *pressed_keys, unsigned int timeout) int getPressedKeys(unsigned int *pressed_keys, unsigned int timeout)
{ {
unsigned char buffer[G15_KEY_READ_LENGTH]; unsigned char buffer[G15_KEY_READ_LENGTH];
@@ -661,26 +665,48 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
+ //no break + //no break
+ default: + default:
+ g15_log(stderr,G15_LOG_INFO,"USB error on keybaord read: %s.\n", libusb_strerror(ret)); + g15_log(stderr,G15_LOG_INFO,"USB error on keybaord read: %s.\n", libusb_strerror(ret));
+ return G15_ERROR_READING_USB_DEVICE;
+ } + }
+ +
+ if (transferred > 0 && buffer[0] == 1) { + if (transferred > 0 && buffer[0] == 1) {
return G15_ERROR_TRY_AGAIN; return G15_ERROR_TRY_AGAIN;
} }
@@ -1054,7 +1038,7 @@ caps = g15DeviceCapabilities();
return G15_NO_ERROR; - if((caps & G15_DEVICE_G13) && buffer[0]==0x25){
- processKeyEventG13(pressed_keys, buffer);
- return G15_NO_ERROR;
+ if ((caps & G15_DEVICE_G13) && buffer[0]==0x25)
+ {
+ processKeyEventG13(pressed_keys, buffer);
+ return G15_NO_ERROR;
} }
- switch(ret) { - switch(ret) {
+ switch(transferred) { - case 4:
case 4: - processKeyEvent4Byte(pressed_keys, buffer);
processKeyEvent4Byte(pressed_keys, buffer); - return G15_NO_ERROR;
return G15_NO_ERROR; - case 5:
@@ -1065,6 +1049,6 @@ - processKeyEvent5Byte(pressed_keys, buffer);
processKeyEvent9Byte(pressed_keys, buffer); - return G15_NO_ERROR;
return G15_NO_ERROR; - case 9:
default: - processKeyEvent9Byte(pressed_keys, buffer);
- return G15_NO_ERROR;
- default:
- return handle_usb_errors("Keyboard Read", ret); /* allow the app to deal with errors */ - return handle_usb_errors("Keyboard Read", ret); /* allow the app to deal with errors */
+ switch (transferred)
+ {
+ case 4:
+ processKeyEvent4Byte(pressed_keys, buffer);
+ return G15_NO_ERROR;
+ case 5:
+ processKeyEvent5Byte(pressed_keys, buffer);
+ return G15_NO_ERROR;
+ case 9:
+ processKeyEvent9Byte(pressed_keys, buffer);
+ return G15_NO_ERROR;
+ default:
+ g15_log(stderr, G15_LOG_INFO,"USB error on keybaord read: unsupported packet size.\n");
+ return G15_ERROR_READING_USB_DEVICE; + return G15_ERROR_READING_USB_DEVICE;
} }
} }