Current attempt to read data

This commit is contained in:
Eric Jiang 2011-02-13 20:38:25 -05:00
parent 7b4cde73b0
commit d862cd1e94

View File

@ -5,136 +5,152 @@
#include "scales.h" #include "scales.h"
#define DEBUG 1 #define DEBUG 1
/*
* These constants are from libhid/include/constants.h
* and usage based on libhid/src/hid_exchange.c
* also vvvoutput.txt
*/
#define HID_REPORT_GET 0x01
#define HID_RT_INPUT 0x01
#define WEIGH_REPORT_ID 0x06 // Scale Data Report ID
#define WEIGH_REPORT_SIZE 0x07
static libusb_device* find_scale(libusb_device**); static libusb_device* find_scale(libusb_device**);
int main(void) int main(void)
{ {
libusb_device **devs; libusb_device **devs;
int r; int r;
ssize_t cnt; ssize_t cnt;
libusb_device* dev; libusb_device* dev;
libusb_device_handle* handle; libusb_device_handle* handle;
r = libusb_init(NULL); r = libusb_init(NULL);
if (r < 0) if (r < 0)
return r; return r;
/* /*
* Get a list of USB devices * Get a list of USB devices
*/ */
cnt = libusb_get_device_list(NULL, &devs); cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0) if (cnt < 0)
return (int) cnt; return (int) cnt;
/* /*
* Look for a scale amongst the USB devices * Look for a scale amongst the USB devices
*/ */
dev = find_scale(devs); dev = find_scale(devs);
/* /*
* Open a handle to this found scale * Open a handle to this found scale
*/ */
libusb_open(dev, &handle); libusb_open(dev, &handle);
#ifdef __linux__ #ifdef __linux__
libusb_detach_kernel_driver(handle, 0); libusb_detach_kernel_driver(handle, 0);
#endif #endif
libusb_claim_interface(handle, 0); libusb_claim_interface(handle, 0);
/* /*
* Try to transfer data about status * Try to transfer data about status
* *
* http://www.beyondlogic.org/usbnutshell/usb6.shtml * http://www.beyondlogic.org/usbnutshell/usb6.shtml
*/ * test_libhid.c
unsigned char blah[8]; */
libusb_control_transfer( unsigned char data[WEIGH_REPORT_SIZE];
handle, unsigned int len = libusb_control_transfer(
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | handle,
LIBUSB_RECIPIENT_INTERFACE, //bmRequestType => direction: in, type: class,
//bmRequestType => direction: in, type: class, // recipient: interface
// recipient: interface LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS |
0x01, //bRequest => HID get report LIBUSB_RECIPIENT_INTERFACE,
0x0300, //wValue => feature report //bRequest => HID_REPORT_GET
0x00, //windex => interface 0 // see HID1_11.pdf 60/97 (pg 51)
blah, HID_REPORT_GET,
8, //wLength //wValue => hid report, no report ID
1000 //timeout => 1 sec 0x0100,
); 0x00, //windex => interface 0
int i; data,
for(i = 0; i < 8; i++) { WEIGH_REPORT_SIZE, //wLength
printf("%x\n", blah[i]); 10000 //timeout => 1 sec
} );
int i;
printf("Got %d bytes from control transfer:\n", len);
for(i = 0; i < WEIGH_REPORT_SIZE; i++) {
printf("%x\n", data[i]);
}
/* /*
* Free the device handle, device list and other cleanup * Free the device handle, device list and other cleanup
* tasks * tasks
*/ */
#ifdef __linux__ #ifdef __linux__
libusb_attach_kernel_driver(handle, 0); libusb_attach_kernel_driver(handle, 0);
#endif #endif
libusb_close(handle); libusb_close(handle);
libusb_free_device_list(devs, 1); libusb_free_device_list(devs, 1);
libusb_exit(NULL); libusb_exit(NULL);
return 0; return 0;
} }
static libusb_device* find_scale(libusb_device **devs) static libusb_device* find_scale(libusb_device **devs)
{ {
if(DEBUG) if(DEBUG)
libusb_set_debug(NULL, 3); libusb_set_debug(NULL, 3);
int i = 0; int i = 0;
libusb_device* dev; libusb_device* dev;
/* /*
* Loop through each usb device, and for each device, loop through the * Loop through each usb device, and for each device, loop through the
* scales list to see if it's one of our listed scales * scales list to see if it's one of our listed scales
*/ */
while ((dev = devs[i++]) != NULL) { while ((dev = devs[i++]) != NULL) {
struct libusb_device_descriptor desc; struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc); int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0) { if (r < 0) {
fprintf(stderr, "failed to get device descriptor"); fprintf(stderr, "failed to get device descriptor");
return; return;
} }
int i; int i;
for (i = 0; i < scalesc; i++) { for (i = 0; i < scalesc; i++) {
if(desc.idVendor == scales[i][0] && if(desc.idVendor == scales[i][0] &&
desc.idProduct == scales[i][1]) { desc.idProduct == scales[i][1]) {
/* /*
* Debugging data about found scale * Debugging data about found scale
*/ */
#ifdef DEBUG #ifdef DEBUG
printf("Found scale %04x:%04x (bus %d, device %d)\n", printf("Found scale %04x:%04x (bus %d, device %d)\n",
desc.idVendor, desc.idVendor,
desc.idProduct, desc.idProduct,
libusb_get_bus_number(dev), libusb_get_bus_number(dev),
libusb_get_device_address(dev)); libusb_get_device_address(dev));
printf("It has descriptors:\n\tmanufc: %d\n\tprodct: %d\n\tserial: %d\n", printf("It has descriptors:\n\tmanufc: %d\n\tprodct: %d\n\tserial: %d\n\tclass: %d\n\tsubclass: %d\n",
desc.iManufacturer, desc.iManufacturer,
desc.iProduct, desc.iProduct,
desc.iSerialNumber); desc.iSerialNumber,
desc.bDeviceClass,
desc.bDeviceSubClass);
/* /*
* A char buffer to pull string descriptors in from the device * A char buffer to pull string descriptors in from the device
*/ */
unsigned char string[256]; unsigned char string[256];
libusb_device_handle* hand; libusb_device_handle* hand;
libusb_open(dev, &hand); libusb_open(dev, &hand);
r = libusb_get_string_descriptor_ascii(hand, desc.iManufacturer, r = libusb_get_string_descriptor_ascii(hand, desc.iManufacturer,
string, 256); string, 256);
printf("Manufacturer: %s\n", string); printf("Manufacturer: %s\n", string);
libusb_close(hand); libusb_close(hand);
#endif #endif
return dev; return dev;
break; break;
} }
} }
} }
} }