1 year ago
#242135
Amulya N Rao
Getting Libusb_Error_IO error while performing libusb_bulk_transfer DataIN ubuntu OS reading data from usb device
Thanks in advance. I am having 2 USB devices (master and slave). I am able to write data from master to slave but not able to read data from slave to master USB device I am getting "LIBUSB_ERROR_IO" Error. The code has complete root access. The below is the code and output. The same code works fine in RHEL OS but not in Ubuntu. Code is in c++, tool Qt creator.
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <libusb-1.0/libusb.h>
#define EP_DATA_IN 0x81
#define EP_DATA_OUT 0x01
#define DEVICE_CONFIGURATION 0
#define MASTER 0x00f4
#define SLAVE 0x00f2
void writeButtonClicked()
{
int rc;
libusb_context *ctx = NULL;
libusb_device_handle *dev_handle;
QString writeData = "Slave writing !!!";
int actual = writeData.length();
unsigned char *sendData;
sendData = (unsigned char *)malloc(actual);
memcpy(sendData, writeData.toLocal8Bit(), actual);
rc = libusb_init(&ctx);
if(rc < 0) {
std::cout << "Init Error " << rc << std::endl;
}
libusb_set_debug(ctx, 6);
dev_handle = libusb_open_device_with_vid_pid(ctx, 0x04b4, SLAVE); // Give SLAVE or MASTER to perform write operation
if (!dev_handle) {
fprintf(stderr, "Error finding USB device\n");
}
if(libusb_kernel_driver_active(dev_handle, DEVICE_CONFIGURATION) == 1) {
std::cout << "Kernel Driver Active" << std::endl;
if(libusb_detach_kernel_driver(dev_handle, DEVICE_CONFIGURATION) == 0)
std::cout << "Kernel Driver Detached!" << std::endl;
}
rc = libusb_claim_interface(dev_handle, DEVICE_CONFIGURATION);
if(rc != 0) {
std::cout << "Cannot Claim Interface" << std::endl;
rc = libusb_set_interface_alt_setting(dev_handle, DEVICE_CONFIGURATION, 1);
if(rc!=0)
{
std::cout <<"Cannot configure alt setting "<<std::endl;
}
else
{
std::cout <<"configured alt setting "<<std::endl;
}
}
std::cout << "Data->" << sendData << "<-" << std::endl;
std::cout << "Writing Data..." << std::endl;
std::cout << "Trying endpoint " << EP_DATA_OUT << "." << std::endl;
rc = libusb_bulk_transfer(dev_handle, EP_DATA_OUT, sendData, sizeof(sendData), &actual, 1000);
if(rc == 0)
{
std::cout << "Writing Successful!" << std::endl;
}
else
{
std::cout << "Write Error! Rc: " << rc << " Actual transfered bytes: " << actual << "." << std::endl;
std::cout << "Error code means: " << libusb_error_name(rc) << std::endl;
}
rc = libusb_release_interface(dev_handle, 0);
if(rc!=0) {
std::cout << "Cannot Release Interface" << std::endl;
}
if (dev_handle)
libusb_close(dev_handle);
libusb_exit(ctx);
}
void readButtonClicked()
{
int rc;
libusb_context *ctx = NULL;
libusb_device_handle *dev_handle;
int actual = 0;
unsigned char *readData = new unsigned char[512];
rc = libusb_init(&ctx);
if(rc < 0) {
std::cout << "Init Error " << rc << std::endl;
}
libusb_set_debug(ctx, 6);
dev_handle = libusb_open_device_with_vid_pid(ctx, 0x04b4, MASTER); // Give SLAVE or MASTER to perform Read operation
if (!dev_handle) {
fprintf(stderr, "Error finding USB device\n");
}
if(libusb_kernel_driver_active(dev_handle, DEVICE_CONFIGURATION) == 1) {
std::cout << "Kernel Driver Active" << std::endl;
if(libusb_detach_kernel_driver(dev_handle, DEVICE_CONFIGURATION) == 0)
std::cout << "Kernel Driver Detached!" << std::endl;
}
rc = libusb_claim_interface(dev_handle, DEVICE_CONFIGURATION);
if(rc != 0) {
std::cout << "Cannot Claim Interface" << std::endl;
rc = libusb_set_interface_alt_setting(dev_handle, DEVICE_CONFIGURATION, 1);
if(rc!=0)
{
std::cout <<"Cannot configure alt setting "<<std::endl;
}
else
{
std::cout <<"configured alt setting "<<std::endl;
}
}
std::cout << "Trying endpoint " << EP_DATA_IN << "." << std::endl;
rc = libusb_bulk_transfer(dev_handle, EP_DATA_IN, readData, sizeof(readData), &actual, 1000);
if(rc >=0)
{
std::cout << "Read Success! Rc: " << libusb_error_name(rc) << " Actual transfered bytes: " << actual << "." << std::endl;
}
else
{
std::cout << "Read Error! Rc: " << rc << " Actual transfered bytes: " << actual << "." << std::endl;
std::cout << "Error code means: " << libusb_error_name(rc) << std::endl;
}
rc = libusb_release_interface(dev_handle, 0);
if(rc!=0) {
std::cout << "Cannot Release Interface" << std::endl;
}
if (dev_handle)
libusb_close(dev_handle);
libusb_exit(ctx);
}
//Output Slave writing Master reading
libusb: debug [libusb_get_device_list]
libusb: debug [discovered_devs_append] need to increase capacity
libusb: debug [libusb_get_device_descriptor]
libusb: debug [libusb_get_device_descriptor]
libusb: debug [libusb_get_device_descriptor]
libusb: debug [libusb_open] open 2.16
libusb: debug [usbi_add_pollfd] add fd 26 events 4
libusb: debug [libusb_kernel_driver_active] interface 0
libusb: debug [libusb_claim_interface] interface 0
Data->Slave writing !!!<-
Writing Data...
Trying endpoint 1.
Writing Successful!
libusb: debug [libusb_alloc_transfer] transfer 0x563230a5b760
libusb: debug [libusb_submit_transfer] transfer 0x563230a5b760
libusb: debug [add_to_flying_list] arm timerfd for timeout in 1000ms (first in line)
libusb: debug [submit_bulk_transfer] need 1 urbs for new transfer with length 8
libusb: debug [libusb_handle_events_timeout_completed] doing our own event handling
libusb: debug [handle_events] poll fds modified, reallocating
libusb: debug [handle_events] poll() 3 fds with timeout in 60000ms
libusb: debug [handle_events] poll() returned 1
libusb: debug [reap_for_handle] urb type=3 status=0 transferred=8
libusb: debug [handle_bulk_completion] handling completion status 0 of bulk urb 1/1
libusb: debug [handle_bulk_completion] last URB in transfer --> complete!
libusb: debug [disarm_timerfd]
libusb: debug [usbi_handle_transfer_completion] transfer 0x563230a5b760 has callback 0x7f2537dfbb30
libusb: debug [sync_transfer_cb] actual_length=8
libusb: debug [libusb_free_transfer] transfer 0x563230a5b760
libusb: debug [libusb_release_interface] interface 0
libusb: debug [libusb_close]
libusb: debug [usbi_remove_pollfd] remove fd 26
libusb: debug [libusb_exit]
libusb: debug [libusb_exit] destroying default context
libusb: debug [libusb_get_device_list]
libusb: debug [discovered_devs_append] need to increase capacity
libusb: debug [libusb_get_device_descriptor]
libusb: debug [libusb_get_device_descriptor]
libusb: debug [libusb_open] open 2.17
libusb: debug [usbi_add_pollfd] add fd 26 events 4
libusb: debug [libusb_kernel_driver_active] interface 0
libusb: debug [libusb_claim_interface] interface 0
Trying endpoint 129.
libusb: debug [libusb_alloc_transfer] transfer 0x563230a35450
libusb: debug [libusb_submit_transfer] transfer 0x563230a35450
libusb: debug [add_to_flying_list] arm timerfd for timeout in 1000ms (first in line)
libusb: debug [submit_bulk_transfer] need 1 urbs for new transfer with length 8
libusb: debug [libusb_handle_events_timeout_completed] doing our own event handling
libusb: debug [handle_events] poll fds modified, reallocating
libusb: debug [handle_events] poll() 3 fds with timeout in 60000ms
Master Read Error! Rc: -1 Actual transfered bytes: 0.
Error code means: LIBUSB_ERROR_IO
libusb: debug [handle_events] poll() returned 1
libusb: debug [reap_for_handle] urb type=3 status=-71 transferred=0
libusb: debug [handle_bulk_completion] handling completion status -71 of bulk urb 1/1
libusb: debug [handle_bulk_completion] low level error -71
libusb: debug [disarm_timerfd]
libusb: debug [usbi_handle_transfer_completion] transfer 0x563230a35450 has callback 0x7f2537dfbb30
libusb: debug [sync_transfer_cb] actual_length=0
libusb: debug [libusb_free_transfer] transfer 0x563230a35450
libusb: debug [libusb_release_interface] interface 0
libusb: debug [libusb_close]
libusb: debug [usbi_remove_pollfd] remove fd 26
libusb: debug [libusb_exit]
libusb: debug [libusb_exit] destroying default context
Thanks, Amulya
c++
qt
ubuntu
libusb
libusb-1.0
0 Answers
Your Answer