Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to properly use the asynchronous libusb?
Forum Updated to NodeBB v4.3 + New Features

How to properly use the asynchronous libusb?

Scheduled Pinned Locked Moved Unsolved General and Desktop
libusbasynchronous
2 Posts 2 Posters 683 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andrew23
    wrote on 24 Jan 2021, 17:30 last edited by Andrew23
    #1

    I worked on the synchronous libusb in my Qt project with good results and now I need the asynchronous features of this library. I understood reading here, here and here that, after I've registered my callback function using the libusb_fill_control_transfer and submitted a transfer with libusb_submit_transfer , I need to "keep live" the libusb_handle_events_completed inside a while loop to get the transfer related events since the libusb doesn't have its own thread. For example you can read a code like this

    libusb_fill_control_transfer(transfer, dev, buffer, cb, &completed, 1000);
    libusb_submit_transfer(transfer);
    while (!completed) {
        libusb_handle_events_completed(ctx, &completed);
    }
    

    Now if I want read a packet that I don't know when it occurs, I think that goes against the asynchronous nature submit a read and wait in the while with libusb_handle_events_completed until the event is triggered.

    Then, do I need to create a separate thread within the libusb_handle_events_completed in an infinite while loop? Like

    // I create the thread when the application starts
    QFuture<void> usbEventHandlerPoller;
    usbEventHandlerPoller = QtConcurrent::run( usbConnection, &USBConnection::PollUSBEventHandler, &threadAbortFlag );
    
    // Here the cuncurrent thread with the infinite loop
    void USBConnection::PollUSBEventHandler( volatile bool* pThreadAbortFlag )
    {
        int completed = 0;
    
        while ( 1 )
        {
            libusb_handle_events_completed( NULL, &completed );
            if ( *pThreadAbortFlag == true )
            {
                return;
            }
        }
    }
    

    Can anyone, with experience in the asynchronous features of libusb library, give some suggestions on the right approach to handle the transfer events?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 24 Jan 2021, 19:48 last edited by
      #2

      Hi,

      I haven't used that feature but from the looks of it you should properly encapsulate your libusb code within a class. In this case likely a QThread subclass so you can implement the loop handling in the run reimplementation.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      1/2

      24 Jan 2021, 17:30

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved