What is the interface I have to inspect for detecting connected USB?

853 views Asked by At

I'm trying to detect that a USB device was plugged or removed from within a qt programm via the method: http://doc.qt.io/qt-4.8/qdbusconnection.html#connect

My current code of the corresponding class looks like this:

#include "usbhandler.h"
#include <QDebug>

USBHandler::USBHandler()
{
    QDBusConnection *bus;
    bool success;

    bus = new QDBusConnection("DeviceAdded");

    if (bus == NULL)
    {
        qDebug() << "Allocation Error";
        return;
    }

    if (!QDBusConnection::systemBus().isConnected())
    {
        qDebug() << "Cannot connect to system bus";
    }

    success = QDBusConnection::systemBus().connect(
                "org.freedesktop.UDisks",
                "/org/freedesktop/UDisks",
                "org.freedesktop.UDisks",
                "DeviceAdded",
            this, SLOT(deviceAdded(QDBusObjectPath)));

    if (success != true)
    {
        qDebug() << "Unsuccesfully connected!";
        delete bus;
        return;
    }
}

void USBHandler::deviceAdded(QDBusObjectPath dev)
{
  qDebug() << "device added!"<<dev.path();
}

When I'm running solid-hardware listen I get when plugging / unplugging, the following notifications:

Device Added:
udi = '/org/freedesktop/UDisks2/block_devices/sdb'
"/org/freedesktop/UDisks2/block_devices/sdb1" has new interfaces:     ("org.freedesktop.UDisks2.Block", "org.freedesktop.UDisks2.Filesystem", "org.freedesktop.UDisks2.Partition") 

Device Added:
udi = '/org/freedesktop/UDisks2/block_devices/sdb1'
"/org/freedesktop/UDisks2/block_devices/sdb1" lost interfaces: ("org.freedesktop.UDisks2.Partition", "org.freedesktop.UDisks2.Filesystem", "org.freedesktop.UDisks2.Block") 

Device Removed:
udi = '/org/freedesktop/UDisks2/block_devices/sdb1'
"/org/freedesktop/UDisks2/block_devices/sdb" lost interfaces: ("org.freedesktop.UDisks2.PartitionTable", "org.freedesktop.UDisks2.Block") 

Device Removed:
udi = '/org/freedesktop/UDisks2/block_devices/sdb'

So I was sure that the system notifys the USB stickAnd I'm now allready playing around for hours with the for the usb named interfaces but I can't get any success for my apllication reacting on pluggining/unplugging the stick.

So what Am I doing wrong? What should be the interface and name paramter of connect() be like? And can you explain me what exactly they do?

1

There are 1 answers

5
Christophe Vu-Brugier On
Device Added:
udi = '/org/freedesktop/UDisks2/block_devices/sdb'
"/org/freedesktop/UDisks2/block_devices/sdb1" has new interfaces:

I am not sure it is your problem, but the udi contains UDisks2, not UDisks. I suggest you adjust your call to connect(). Something like this:

success = QDBusConnection::systemBus().connect(
            "org.freedesktop.UDisks",
            "/org/freedesktop/UDisks2",
            "org.freedesktop.UDisks2",
            [...]);