Moved device handling block to its own function

This commit is contained in:
- 2020-09-30 02:07:24 -07:00
parent 4dcb0d8f54
commit 26f132e866

View File

@ -19,6 +19,10 @@
#include <stdio.h> #include <stdio.h>
#include <libusb-1.0/libusb.h> #include <libusb-1.0/libusb.h>
#include "hidapi/hidapi.h"
#include <wchar.h>
#include <locale.h>
#define STATE_SUCCESS 0 #define STATE_SUCCESS 0
#define STATE_NOT_FOUND 1 #define STATE_NOT_FOUND 1
@ -65,13 +69,6 @@ struct hanvon {
char phys[32]; 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) { int find_device(libusb_device **list, unsigned int count) {
if (count < 0) { if (count < 0) {
@ -83,7 +80,7 @@ int find_device(libusb_device **list, unsigned int count) {
libusb_device *t = list[i]; libusb_device *t = list[i];
libusb_get_device_descriptor(list[i], &desc); 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) { if (desc.idVendor == VENDOR_ID_HANVON) {
switch(desc.idProduct) { switch(desc.idProduct) {
@ -110,42 +107,21 @@ int find_device(libusb_device **list, unsigned int count) {
return found; return found;
} }
void callback(struct libusb_transfer *transfer) {
unsigned char *data = transfer -> buffer;
libusb_device *FindHanvon( libusb_context **context); for(int i = 0; i < 10; i++) {
int HandleData( void ); printf("0x%x, ", data[i]);
int main()
{
#define UNREF_DEVICE 1
#define KEEP_DEVICE_REF 0
libusb_device ** devs;
int r = libusb_init(NULL);
if (r < 0) {
return r;
} }
int count = libusb_get_device_list(NULL, &devs); printf("placeholder\n");
if (count < 0) {
libusb_exit(NULL);
return count;
} }
int index = find_device(devs, count); int handle_device_lusb(libusb_device *d) {
if (index < 0) {
printf("Device not plugged in\n");
return 0;
}
printf("Device #%i\n", index);
libusb_device_handle *h; libusb_device_handle *h;
int state = libusb_open(devs[index], &h); int status = libusb_open(d, &h);
libusb_free_device_list (devs, UNREF_DEVICE);
if (state < 0 || h == NULL) { if (status < 0 || h == NULL) {
printf(" Something happened, %i.\n", state); printf("Error opening device, %i.\n", status);
libusb_exit(NULL);
return 0; return 0;
} }
@ -166,16 +142,66 @@ int main()
callback, callback,
NULL, NULL,
130); // timeout in milliseconds 130); // timeout in milliseconds
do { do {
state = libusb_submit_transfer(tx); status = libusb_submit_transfer(tx);
if (state < 0 ) { if (status < 0 ) {
return state; return status;
} }
libusb_handle_events(NULL); libusb_handle_events(NULL);
} while (1); } while (1);
return 0;
}
return state; libusb_device *FindHanvon( libusb_context **context);
int HandleData( void );
int main()
{
#define UNREF_DEVICE 1
#define KEEP_DEVICE_REF 0
libusb_device ** devs;
int r = libusb_init(NULL);
if (r < 0) {
return r;
}
int count = libusb_get_device_list(NULL, &devs);
if (count < 0) {
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);
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();
int s = handle_device_lusb(device);
libusb_exit(NULL);
return 0;
} }
@ -193,7 +219,7 @@ libusb_device *FindHanvon( libusb_context **context)
{ {
libusb_device *device = deviceList[i]; libusb_device *device = deviceList[i];
libusb_get_device_descriptor(device, &description); 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 ) if( description.idVendor == VENDOR_ID_HANVON )
{ {
switch( description.idProduct ) switch( description.idProduct )