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 obj-m += hanvon.o
all: 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: archive:
tar f - --exclude=.git -C ../ -c hanvon | gzip -c9 > ../hanvon-`date +%Y%m%d`.tgz 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"); 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); libusb_device *FindHanvon( libusb_context **context);
int HandleData( void ); int HandleData( void );
int main() int main()
{ {
libusb_context *context; #define UNREF_DEVICE 1
libusb_device *device; #define KEEP_DEVICE_REF 0
libusb_device_handle *handle;
int state = libusb_init( &context ); libusb_device ** devs;
if( state != STATE_SUCCESS ) int r = libusb_init(NULL);
return state; if (r < 0) {
return r;
}
int count = libusb_get_device_list(NULL, &devs);
if (count < 0) {
libusb_exit(NULL);
return count;
}
device = FindHanvon(& context ); int index = find_device(devs, count);
if( device == NULL ) if (index < 0) {
return STATE_NOT_FOUND; printf("Device not plugged in\n");
return 0;
}
state = libusb_open( device, &handle ); printf("Device #%i\n", index);
if( state != STATE_SUCCESS )
return state; 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? // Wait and handle interrupts?
@ -104,7 +160,7 @@ int main()
unsigned char buffer[LEN]; unsigned char buffer[LEN];
// assign timeout of 1s, for any action is too much as is // assign timeout of 1s, for any action is too much as is
libusb_fill_interrupt_transfer( &tx, libusb_fill_interrupt_transfer( &tx,
handle, h,
ENDPOINT_ADDR, ENDPOINT_ADDR,
buffer, buffer,
LEN, LEN,
@ -113,11 +169,11 @@ int main()
130); // milliseconds 130); // milliseconds
state = libusb_submit_transfer(&tx); state = libusb_submit_transfer(&tx);
if( state != STATE_SUCCESS ) if (state < 0 ) {
return state; return state;
}
for(;;) { for(;;) {
libusb_handle_events(context); libusb_handle_events(NULL);
} }
return state; return state;
} }