Reputation: 95
I pulse a USB camera with a 5v pulse and it takes a picture. Pulsing the camera then sends a USB signal back to a raspberry pi. I'm writing a program to collect the images sent over USB. Below is my code for the function to begin taking input from the camera after it is triggered.
void opendevice()
{
libusb_device_handle* dev;
struct libusb_device_descriptor* desc;
int r;
int err;
int transfered;
libusb_init(NULL);
dev = libusb_open_device_with_vid_pid( NULL, 0x2a0b, 0x00f8);
if (dev == NULL)
{
printf("device not found\n");
}
if(libusb_kernel_driver_active(dev, 0) == 1)
{
printf("Driver active\n");
if(libusb_detach_kernel_driver(dev, 0) == 0)
{
printf("Kernel Driver Detached\n");
}
}
libusb_set_configuration(dev,1);
err = libusb_claim_interface(dev, 0);
if(err != 0)
{
printf("cannot claim interface\n");
}
unsigned char usb_data[4];
int size = sizeof(unsigned int) *1280 *960;
unsigned *data;
data = (unsigned int *)malloc(size);
r = libusb_bulk_transfer(dev, LIBUSB_ENDPOINT_IN | 0x83, usb_data, sizeof(data), &transfered, 0);
if(r == 0)
{
printf("data has been transfered\n");
}
else{
printf("error code %d\n", r);
printf("bytes transfered %d\n", transfered);
}
}
I locate the device detach check to see if the kernel is using it then if it is I detach it. After detaching it I claim the interface then wait for a transfer to happen inside of bulk transfer. However I never receive data from the transfer. Even when unplugging the camera from the usb port r returns value -1 and my transfer is of size 0. Can anyone tell me what I am missing here?
Let me know if you need any more information.
Upvotes: 3
Views: 1803
Reputation: 7711
Your code has a buffer overflow:
unsigned char usb_data[4]; // <---
int size = sizeof(unsigned int) *1280 *960;
unsigned *data; // <-- not used!
data = (unsigned int *)malloc(size);
r = libusb_bulk_transfer(dev, LIBUSB_ENDPOINT_IN | 0x83, usb_data //<
,sizeof(data), &transfered, 0)
You probably wanted to use "data" as receiving buffer in the libusb_bulk_transfer()
call, but you actually used usb_data
which is only 4 bytes long.
Upvotes: 2