Hi,
I'm new to linux development and I try to send a few bytes over USB to the JB media light manager (I think it doesn't matter which device it is).
My programm crashes when it comes to usb_set_configuration().
If I commit this method out (//) and let the programm invoke directly the usb_claim_interface() method it crashes, too.
I'm developing with Eclipse and I'm using the GCC C++ Compiler under Ubuntu 10.10 32bit.
lsusb tells me following:
...
Bus 005 Device 004: ID 16c0:0a32 VOTI
...
That's the device because disconnecting it makes the line disappear.
Can anyone help ?
Code:
#include <usb.h>
#include <stdio.h>
#include <iostream>
/* the device's vendor and product id */
#define MY_VID 0x16c0
#define MY_PID 0x0a32
/* the device's endpoints */
#define EP_IN 0x81
#define EP_OUT 0x01
#define BUF_SIZE 8
using namespace std;
struct usb_device *device;
usb_dev_handle *open_dev(void);
usb_dev_handle *open_dev(void)
{
struct usb_bus *bus;
for(bus = usb_get_busses(); bus; bus = bus->next)
{
for(device = bus->devices; device; device = device->next)
{
if(device->descriptor.idVendor == MY_VID
&& device->descriptor.idProduct == MY_PID)
{
return usb_open(device);
}
}
}
return NULL;
}
int main(void)
{
usb_dev_handle *dev_handl = NULL; /* the device handle */
char tmp[BUF_SIZE] = {0x05, 0x22, 0x01, 0x06, 0x00, 0x00, 0x00, 0x00};
usb_init(); /* initialize the library */
usb_find_busses(); /* find all busses */
usb_find_devices(); /* find all connected devices */
if(!(dev_handl = open_dev()))
{
printf("error: device not found!\n");
return 0;
}
usb_config_descriptor *config = device->config;
usb_interface *interface = config->interface;
usb_interface_descriptor *altsetting = interface->altsetting;
if(usb_set_configuration(dev_handl, config->iConfiguration) < 0)
{
printf("error: setting config failed\n");
usb_close(dev_handl);
return 0;
}
if(usb_claim_interface(dev_handl, altsetting->bInterfaceNumber) < 0)
{
printf("error: claiming interface failed\n");
usb_close(dev_handl);
return 0;
}
if(usb_bulk_write(dev_handl, EP_OUT, tmp, sizeof(tmp), 5000)
!= sizeof(tmp))
{
printf("error: bulk write failed\n");
}
if(usb_bulk_read(dev_handl, EP_IN, tmp, sizeof(tmp), 5000)
!= sizeof(tmp))
{
printf("error: bulk read failed\n");
}
usb_release_interface(dev_handl, 0);
usb_close(dev_handl);
return 0;
}