How to pass callback function to libusb bulk transfer function in C++ Class

libusb isochronous example
libusb transfer
libusb send data example
libusb asynchronous read example
libusb handle
libusb error whilst sending bulk transfer
libusb developers guide
libusb overflow

I am using libusb to interact with a usb device. I have created a Class and a member function will receive the input from the device and process it. I am using Asynchronous api. Now the libusb_fill_bulk_transfer() function call throws a compilation error:

void MyDeviceClass::on_connectButton_clicked()
{
   int r; //for return values
   ssize_t cnt; //holding number of devices in list
   r = libusb_init(&ctx); //initialize the library for the session we just declared
   if(r < 0) {
      qDebug()<<"Init Error "<<r<<endl; //there was an error
      return;
   }
   ...
   ...
   ...

   libusb_fill_bulk_transfer( transfer_in, dev_handle, USB_ENDPOINT_IN,
            inBuffer, LEN_IN_BUFFER,readDataFromBuffer,NULL,0);
   libusb_submit_transfer(transfer_in);
   QtConcurrent::run (this,&MyDeviceClass::eventThread);
}

The compiler suggests using &MyDeviceClass::readDataFromBuffer as a function pointer but that still throws an error. I tried using static members, and even static non-member functions but all in vain. Please help me in passing the callback to this function. I am using Qt, and probably won't like to use boost libraries. Please note that libusb_fill_bulk_transfer(...) is invoked inside a member function of MyDeviceClass.

I made the member function static and prototype needed to be modified:

 static void LIBUSB_CALL readDataFromBuffer(struct libusb_transfer *);

How to pass callback function to libusb bulk transfer - android, How to pass callback function to libusb bulk transfer function in C++ Class - c++ Please help me in passing the callback to this function. I am using Qt, and  Asynchronous transfer callback function type. When submitting asynchronous transfers, you pass a pointer to a callback function of this type via the callback member of the libusb_transfer structure. libusb will call this function later, when the transfer has completed or failed.

what is the compile error when you use a non member function?

Edit: this should be LIBUSB_CALL:

    void LIBUSB_CALL BulkTransferCallback(struct libusb_transfer *transfer) {
        // This assumes that you set transfer::user_data to be a pointer to 
        // an instance of the MyDeviceClass class.
        MyDeviceClass* mdc = reinterpret_cast<MyDeviceClass*>(transfer->user_data);
        mdc->onBulkTransferCallback(transfer);
    }

Then when you set the pointer, use:

transfer.callback = &BulkTransferCallback;

As you answered yourself, actually it should be __stdcall which is defined by LIBUSB_CALL. Their documentation should include this in the function pointer typedef to remove the ambiguity.

Asynchronous device I/O, 3) send bulk transfer (asynchronous) with appropriate callback function in the callback function. I have used the dpfp.c program as a model, but I am not totally sure how Think of it as a way of passing some context to the callback function. C++ (Cpp) libusb_bulk_transfer - 30 examples found.These are the top rated real world C++ (Cpp) examples of libusb_bulk_transfer extracted from open source projects. You can rate examples to help us improve the quality of examples.

The lib_usb api defines the function libusb_fill_bulk_transfer with:

static void     libusb_fill_bulk_transfer (struct libusb_transfer *transfer, libusb_device_handle *dev_handle, unsigned char endpoint, unsigned char *buffer, int length, libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)

It is possible to add a static class function to your class and call the non-static member-function of the class within this static function.

You this Object would be at the place of void *user_data.

In the static class function you would have to cast the member of transfer->user_data correctly to your class.

Difficulty with asynchronous callback functions, When requesting data on a bulk endpoint, libusb requires you to supply a. * buffer and the Each of these will cause the user-specified transfer callback function to. * be invoked. It is up If you do take libusb's file descriptors and pass them to. Now the transfer code seem complete. However an additional step have to be made. What is not very clear here (at least it wasn't clear for me in the past) is that the "internal USB engine" who make the transfer and automatically call the callback function is not in a separate thread working in parallel but, for have it working as expected, need to be "keep alive" by continuously calling the

libusb/io.c - platform/external/libusb, Since version 1.0.13, to help with feature detection, libusb defines * a LIBUSB_API_VERSION #ifdef __cplusplus extern "C" { #endif /** * \ingroup misc This flag only affects host-to-device transfers to bulk and interrupt * endpoints. When submitting asynchronous * transfers, you pass a pointer to a callback function of  Asynchronous transfer callback function type. When submitting asynchronous transfers, you pass a pointer to a callback function of this type via the callback member of the libusb_transfer structure. libusb will call this function later, when the transfer has completed or failed. See Asynchronous device I/O for more information.

Public libusb header file * Copyright © 2001 Johannes Erdfelt , libusb offers both a synchronous and an asynchronous interface to \section overflow Bulk/interrupt transfer overflows * * When requesting data on a bulk endpoint, libusb When the * cancellation actually completes, the transfer's callback function If you do take libusb's file descriptors and pass them to * poll​()/select()  Subject: Re: [Libusb-devel] Difficulty with asynchronous callback functions Barry Pasicznyk wrote: > The problem: > the callback function returns (about a second or two later) and there > is no data in the callback function.

I/O functions for libusb * Copyright (C) 2007-2008 Daniel Drake , The low-level API of usb4java closely follows the C API of the libusb project. All global functions and constants of libusb are defined as static members of the If you need contexts then you may want to pass it to the thread somehow. fillBulkTransfer(transfer, handle, 0x03, buffer, callback, null, timeout); int result = LibUsb. The status of the transfer. Read-only, and only for use within transfer callback function. If this is an isochronous transfer, this field may read COMPLETED even if there were errors in the frames. Use the status field in each packet to determine if errors occurred.

Comments
  • Ah ok, so it needs to be __stdcall.
  • I get following error in non-member function error C2664: 'libusb_fill_interrupt_transfer' : cannot convert parameter 6 from 'void (__cdecl *)(libusb_transfer *)' to 'libusb_transfer_cb_fn'