Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[Solved .. kind of] Getting event when USB is attached/detached



  • Hi

    I am working on a embedded Linux device with Qt4.6.3. When a USB device (i.e. Memorystick) is attached it gets listed as /dev/sda1 and can be mounted manually. So, it basically does the hot-plug which works perfectly for keyboard and mouse, but it it not mounting drives.

    Is there a way I can get a signal in Qt on that event (and the remove event as well). I do not want it to auto-mount because I want to have control over mounting/unmounting. However, if implementing auto-mounting is the easier way, I can handle that as well.

    Thanks
    McL


  • Lifetime Qt Champion

    Hi,

    You can have a look at KDE's Solid library for that. IIRC it's now a tier1 lib which means it doesn't need the full KDE framework to be used.

    Hope it helps



  • Hi,

    I looked into it ... and it looks promising.
    However, I'm not sure if and how I get this into my Qt.

    For other addings (i.e. QextSerialPort) there was a pri that I simply could include into my pro and compile the sources together with my code.
    For QSerialPort there was a pro which I could use with qmake to build/install libraries and use these now.

    If I look at the sources of solid ... there's nothing alike ?!?
    Any further suggestion highly appreciated.


  • Lifetime Qt Champion

    Indeed you have to build it using cmake




  • Moderators

    If you have udisks running on the embedded system then you could use QDbus to get events.



  • No udisk and no Dbus ... unfortunately



  • also looked at libusb ...
    which is listing in TODO:_ notifications of hotplugged/unplugged devices_
    So, this seems not to work either :(


  • Lifetime Qt Champion

    Do you have udev running ?



  • Nope, running Busybox with mdev.
    Were you thinking about libudev ... me to ;)

    How about having QFileSystemWatcher monitoring /dev and getting an event when a sd?? is added .. could that work? ... I never used QFileWatcher before.


  • Lifetime Qt Champion

    IIRC, not all filesystems on linux can be watched, thinking of /dev and /sys if my memory serves well. However, you could also simply poll dev for known device, not really efficient but might be simpler



  • Setting a watcher on /dev works as supposed.
    Plugging a USB memory stick adds 5 entries in /dev which are removed again if the device is unplugged. The directoryChanged signal is emitted 17 times for plugging and 5 times for unplugging.
    However, since the signal does not contain the information of what exactly the change in /dev was, I would need to maintain a list of the contents of /dev to be able to compare ... /dev has about 150 entries ... doing this for every emit ... I'm not sure if this is a good idea nor do I already have a solid idea on a efficient solution for doing this.


  • Lifetime Qt Champion

    You could keep a list of /dev's content when starting to watch and then compare it to the current content when getting the directoryChanged signal. You can couple that with a QTimer to avoid a burst of your slot



  • Just did a test to debug output with this solution which could be a starting point to work something out:

    @void QTGUI_MainWindow::showModifiedDirectory(QString directory)
    {
    qDebug() << "directory changed:" << directory;
    QDir dir(directory);
    if(dir.exists("sda1")) { qDebug() << "sda1 found!"; }
    if(dir.exists("sdb1")) { qDebug() << "sdb1 found!"; }
    }
    @



  • Just like to add that this works as proposed using QFileSystemWatcher used to monitor the /dev. On the event I do some filtering using QDir … that’s it.



  • There is a "a device watcher":http://qt-project.org/forums/viewthread/9605/ library.
    I have not tried it.



  • Thanks for the hint. I have already checked this and looked at it. It's based on udev. Since my device uses busybox with mdev, I decided to not try to make this run and try my own solution instead.


Log in to reply