Clean up - also compiles for ubanto

This commit is contained in:
- 2020-09-02 23:09:12 -07:00
parent b841132264
commit 7769e7ef8c
2 changed files with 98 additions and 41 deletions

View File

@ -3,7 +3,8 @@
obj-m += hanvon.o
all:
gcc -L/usr/local/lib -lusb-1.0 hanvon-libusb.c -g
gcc hanvon-libusb.c -L/usr/local/lib -lusb-1.0 -g
archive:
tar f - --exclude=.git -C ../ -c hanvon | gzip -c9 > ../hanvon-`date +%Y%m%d`.tgz

View File

@ -73,27 +73,83 @@ void callback(struct libusb_transfer *transfer) {
printf("placeholder\n");
}
int find_device(libusb_device **list, unsigned int count) {
if (count < 0) {
return -1;
}
int found = -1;
struct libusb_device_descriptor desc;
for (unsigned int i = 0; i < count; i++) {
libusb_device *t = list[i];
libusb_get_device_descriptor(list[i], &desc);
printf( "Dev%u ID %04x:%04x\n", (i), desc.idVendor, desc.idProduct );
if (desc.idVendor == VENDOR_ID_HANVON) {
switch(desc.idProduct) {
default:
break;
case PRODUCT_ID_AM0806:
case PRODUCT_ID_AM0605:
case PRODUCT_ID_AM1107:
case PRODUCT_ID_AM1209:
case PRODUCT_ID_RL0604:
case PRODUCT_ID_RL0504:
case PRODUCT_ID_GP0806:
case PRODUCT_ID_GP0806B:
case PRODUCT_ID_GP0605:
case PRODUCT_ID_GP0605A:
case PRODUCT_ID_GP0504:
case PRODUCT_ID_NXS1513:
case PRODUCT_ID_GP0906:
case PRODUCT_ID_APPIV0906:
return i;
} // end switch
} // end if
} // end for
return found;
}
libusb_device *FindHanvon( libusb_context **context);
int HandleData( void );
int main()
{
libusb_context *context;
libusb_device *device;
libusb_device_handle *handle;
#define UNREF_DEVICE 1
#define KEEP_DEVICE_REF 0
int state = libusb_init( &context );
if( state != STATE_SUCCESS )
return state;
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;
}
device = FindHanvon(& context );
if( device == NULL )
return STATE_NOT_FOUND;
int index = find_device(devs, count);
if (index < 0) {
printf("Device not plugged in\n");
return 0;
}
state = libusb_open( device, &handle );
if( state != STATE_SUCCESS )
return state;
printf("Device #%i\n", index);
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;
}
printf("Got to this point\n");
// Wait and handle interrupts?
@ -103,8 +159,8 @@ int main()
const unsigned int LEN = 10; // wMaxPacketSize from lsusb -v
unsigned char buffer[LEN];
// assign timeout of 1s, for any action is too much as is
libusb_fill_interrupt_transfer(&tx,
handle,
libusb_fill_interrupt_transfer( &tx,
h,
ENDPOINT_ADDR,
buffer,
LEN,
@ -113,11 +169,11 @@ int main()
130); // milliseconds
state = libusb_submit_transfer(&tx);
if( state != STATE_SUCCESS )
if (state < 0 ) {
return state;
}
for(;;) {
libusb_handle_events(context);
libusb_handle_events(NULL);
}
return state;
}