Moved device handling block to its own function
This commit is contained in:
parent
4dcb0d8f54
commit
26f132e866
114
hanvon-libusb.c
114
hanvon-libusb.c
@ -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,7 +107,50 @@ int find_device(libusb_device **list, unsigned int count) {
|
|||||||
return found;
|
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);
|
libusb_device *FindHanvon( libusb_context **context);
|
||||||
int HandleData( void );
|
int HandleData( void );
|
||||||
@ -131,51 +171,37 @@ int main()
|
|||||||
libusb_exit(NULL);
|
libusb_exit(NULL);
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
int index = find_device(devs, count);
|
int index = find_device(devs, count);
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
printf("Device not plugged in\n");
|
printf("Device not plugged in\n");
|
||||||
|
libusb_exit(NULL);
|
||||||
return 0;
|
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?
|
int s = handle_device_lusb(device);
|
||||||
struct libusb_transfer *tx;
|
|
||||||
|
|
||||||
const int ENDPOINT_ADDR = 0x81; // bEndpointAddress from lsusb -v
|
libusb_exit(NULL);
|
||||||
const unsigned int LEN = 10; // wMaxPacketSize from lsusb -v
|
return 0;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -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 )
|
||||||
|
Loading…
Reference in New Issue
Block a user