From 26f132e8665b092f48c0a86b8d0eca2c2825e536 Mon Sep 17 00:00:00 2001 From: - <-> Date: Wed, 30 Sep 2020 02:07:24 -0700 Subject: [PATCH] Moved device handling block to its own function --- hanvon-libusb.c | 114 +++++++++++++++++++++++++++++------------------- 1 file changed, 70 insertions(+), 44 deletions(-) diff --git a/hanvon-libusb.c b/hanvon-libusb.c index ec84956..bcb271c 100644 --- a/hanvon-libusb.c +++ b/hanvon-libusb.c @@ -19,6 +19,10 @@ #include #include +#include "hidapi/hidapi.h" +#include +#include + #define STATE_SUCCESS 0 #define STATE_NOT_FOUND 1 @@ -65,13 +69,6 @@ struct hanvon { char phys[32]; }; -void callback(struct libusb_transfer *transfer) { - int i = 0; - for(; i < 10; i++) { - printf("0x%x, ", transfer -> buffer[i]); - } - printf("placeholder\n"); -} int find_device(libusb_device **list, unsigned int count) { if (count < 0) { @@ -83,7 +80,7 @@ int find_device(libusb_device **list, unsigned int count) { libusb_device *t = list[i]; libusb_get_device_descriptor(list[i], &desc); - printf( "Dev%u ID %04x:%04x\n", (i), desc.idVendor, desc.idProduct ); +// printf( "Dev%u ID %04x:%04x\n", (i), desc.idVendor, desc.idProduct ); if (desc.idVendor == VENDOR_ID_HANVON) { switch(desc.idProduct) { @@ -110,7 +107,50 @@ int find_device(libusb_device **list, unsigned int count) { return found; } +void callback(struct libusb_transfer *transfer) { + unsigned char *data = transfer -> buffer; + for(int i = 0; i < 10; i++) { + printf("0x%x, ", data[i]); + } + printf("placeholder\n"); +} +int handle_device_lusb(libusb_device *d) { + libusb_device_handle *h; + int status = libusb_open(d, &h); + + if (status < 0 || h == NULL) { + printf("Error opening device, %i.\n", status); + libusb_exit(NULL); + return 0; + } + + // Wait and handle interrupts? + struct libusb_transfer *tx; + + const int ENDPOINT_ADDR = 0x81; // bEndpointAddress from lsusb -v + const unsigned int LEN = 10; // wMaxPacketSize from lsusb -v + unsigned char buffer[LEN]; + + // Allocate memory for transfer, configure, then submit + tx = libusb_alloc_transfer(0); + libusb_fill_interrupt_transfer( tx, + h, + ENDPOINT_ADDR, + buffer, + LEN, + callback, + NULL, + 130); // timeout in milliseconds + do { + status = libusb_submit_transfer(tx); + if (status < 0 ) { + return status; + } + libusb_handle_events(NULL); + } while (1); + return 0; +} libusb_device *FindHanvon( libusb_context **context); int HandleData( void ); @@ -131,51 +171,37 @@ int main() libusb_exit(NULL); return count; } - int index = find_device(devs, count); if (index < 0) { printf("Device not plugged in\n"); + libusb_exit(NULL); return 0; } + libusb_device *device = devs[index]; + libusb_free_device_list (devs, UNREF_DEVICE); - printf("Device #%i\n", index); + int res = hid_init(); + hid_device *handle = hid_open(VENDOR_ID_HANVON, PRODUCT_ID_GP0504, NULL); + if (handle != NULL) { + // wchar_t wstr[80]; + // setlocale(LC_CTYPE, ""); + unsigned char buf[10]; + res = hid_read(handle, buf, 10); + for (unsigned int i = 0; i < 10; i++) { + printf("0x%x, ", buf[i]); + } + } else { + printf("hidapi: Could not open device\n"); + } + hid_close(handle); + res = hid_exit(); - libusb_device_handle *h; - int state = libusb_open(devs[index], &h); - libusb_free_device_list (devs, UNREF_DEVICE); - if (state < 0 || h == NULL) { - printf(" Something happened, %i.\n", state); - return 0; - } - // Wait and handle interrupts? - struct libusb_transfer *tx; + int s = handle_device_lusb(device); - const int ENDPOINT_ADDR = 0x81; // bEndpointAddress from lsusb -v - const unsigned int LEN = 10; // wMaxPacketSize from lsusb -v - unsigned char buffer[LEN]; - - // Allocate memory for transfer, configure, then submit - tx = libusb_alloc_transfer(0); - libusb_fill_interrupt_transfer( tx, - h, - ENDPOINT_ADDR, - buffer, - LEN, - callback, - NULL, - 130); // timeout in milliseconds - - do { - state = libusb_submit_transfer(tx); - if (state < 0 ) { - return state; - } - libusb_handle_events(NULL); - } while (1); - - return state; + libusb_exit(NULL); + return 0; } @@ -193,7 +219,7 @@ libusb_device *FindHanvon( libusb_context **context) { libusb_device *device = deviceList[i]; libusb_get_device_descriptor(device, &description); - printf( "Dev%u ID %04x:%04x\n", (i + 1), description.idVendor, description.idProduct ); + //printf( "Dev%u ID %04x:%04x\n", (i + 1), description.idVendor, description.idProduct ); if( description.idVendor == VENDOR_ID_HANVON ) { switch( description.idProduct )