mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2025-02-05 21:52:10 +01:00
OP-334 Further work on USB Monitor, incl. Win32 stub.
git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@2952 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
parent
eaea8f28cf
commit
cd3168d123
@ -98,6 +98,7 @@ struct USBPortInfo {
|
|||||||
QString friendName; ///< Friendly name.
|
QString friendName; ///< Friendly name.
|
||||||
QString physName;
|
QString physName;
|
||||||
QString enumName; ///< It seems its the only one with meaning
|
QString enumName; ///< It seems its the only one with meaning
|
||||||
|
QString serialNumber; // As a string as it can be anything, really...
|
||||||
int vendorID; ///< Vendor ID.
|
int vendorID; ///< Vendor ID.
|
||||||
int productID; ///< Product ID
|
int productID; ///< Product ID
|
||||||
};
|
};
|
||||||
|
@ -72,6 +72,8 @@ public:
|
|||||||
USBMonitor(QObject *parent = 0);
|
USBMonitor(QObject *parent = 0);
|
||||||
// USBMonitor(int vid, int pid);
|
// USBMonitor(int vid, int pid);
|
||||||
~USBMonitor();
|
~USBMonitor();
|
||||||
|
QList<USBPortInfo> availableDevices();
|
||||||
|
QList<USBPortInfo> availableDevices(int vid, int pid, int bcdDevice);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
/*!
|
/*!
|
||||||
@ -109,6 +111,7 @@ private:
|
|||||||
struct udev *context;
|
struct udev *context;
|
||||||
struct udev_monitor *monitor;
|
struct udev_monitor *monitor;
|
||||||
QSocketNotifier *monitorNotifier;
|
QSocketNotifier *monitorNotifier;
|
||||||
|
USBPortInfo makePortInfo(struct udev_device *dev);
|
||||||
#elif defined (Q_OS_WIN32)
|
#elif defined (Q_OS_WIN32)
|
||||||
//TODO
|
//TODO
|
||||||
#endif
|
#endif
|
||||||
|
@ -43,10 +43,14 @@ void USBMonitor::deviceEventReceived() {
|
|||||||
dev = udev_monitor_receive_device(this->monitor);
|
dev = udev_monitor_receive_device(this->monitor);
|
||||||
if (dev) {
|
if (dev) {
|
||||||
printf("Got Device");
|
printf("Got Device");
|
||||||
printf(" Node: %s", udev_device_get_devnode(dev));
|
QString action = QString(udev_device_get_action(dev));
|
||||||
printf(" Subsystem: %s", udev_device_get_subsystem(dev));
|
if (action == "add") {
|
||||||
printf(" Devtype: %s", udev_device_get_devtype(dev));
|
emit deviceDiscovered(makePortInfo(dev));
|
||||||
printf(" Action: %s", udev_device_get_action(dev));
|
|
||||||
|
} else if (action == "remove"){
|
||||||
|
emit deviceRemoved(makePortInfo(dev));
|
||||||
|
}
|
||||||
|
|
||||||
udev_device_unref(dev);
|
udev_device_unref(dev);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -55,7 +59,9 @@ void USBMonitor::deviceEventReceived() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Initialize the udev monitor here
|
||||||
|
*/
|
||||||
USBMonitor::USBMonitor(QObject *parent): QThread(parent) {
|
USBMonitor::USBMonitor(QObject *parent): QThread(parent) {
|
||||||
|
|
||||||
this->context = udev_new();
|
this->context = udev_new();
|
||||||
@ -77,3 +83,99 @@ USBMonitor::~USBMonitor()
|
|||||||
{
|
{
|
||||||
quit();
|
quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns a list of all currently available devices
|
||||||
|
*/
|
||||||
|
QList<USBPortInfo> USBMonitor::availableDevices()
|
||||||
|
{
|
||||||
|
QList<USBPortInfo> devicesList;
|
||||||
|
struct udev_list_entry *devices, *dev_list_entry;
|
||||||
|
struct udev_enumerate *enumerate;
|
||||||
|
struct udev_device *dev;
|
||||||
|
|
||||||
|
enumerate = udev_enumerate_new(this->context);
|
||||||
|
udev_enumerate_add_match_subsystem(enumerate,"hidwraw");
|
||||||
|
udev_enumerate_scan_devices(enumerate);
|
||||||
|
devices = udev_enumerate_get_list_entry(enumerate);
|
||||||
|
// Will use the 'native' udev functions to loop:
|
||||||
|
udev_list_entry_foreach(dev_list_entry,devices) {
|
||||||
|
const char *path;
|
||||||
|
|
||||||
|
/* Get the filename of the /sys entry for the device
|
||||||
|
and create a udev_device object (dev) representing it */
|
||||||
|
path = udev_list_entry_get_name(dev_list_entry);
|
||||||
|
dev = udev_device_new_from_syspath(this->context, path);
|
||||||
|
|
||||||
|
/* The device pointed to by dev contains information about
|
||||||
|
the hidraw device. In order to get information about the
|
||||||
|
USB device, get the parent device with the
|
||||||
|
subsystem/devtype pair of "usb"/"usb_device". This will
|
||||||
|
be several levels up the tree, but the function will find
|
||||||
|
it.*/
|
||||||
|
dev = udev_device_get_parent_with_subsystem_devtype(
|
||||||
|
dev,
|
||||||
|
"usb",
|
||||||
|
"usb_device");
|
||||||
|
if (!dev) {
|
||||||
|
printf("Unable to find parent usb device.");
|
||||||
|
return devicesList;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* From here, we can call get_sysattr_value() for each file
|
||||||
|
in the device's /sys entry. The strings passed into these
|
||||||
|
functions (idProduct, idVendor, serial, etc.) correspond
|
||||||
|
directly to the files in the directory which represents
|
||||||
|
the USB device. Note that USB strings are Unicode, UCS2
|
||||||
|
encoded, but the strings returned from
|
||||||
|
udev_device_get_sysattr_value() are UTF-8 encoded. */
|
||||||
|
printf(" VID/PID: %s %s\n",
|
||||||
|
udev_device_get_sysattr_value(dev,"idVendor"),
|
||||||
|
udev_device_get_sysattr_value(dev, "idProduct"));
|
||||||
|
printf(" %s\n %s\n",
|
||||||
|
udev_device_get_sysattr_value(dev,"manufacturer"),
|
||||||
|
udev_device_get_sysattr_value(dev,"product"));
|
||||||
|
printf(" serial: %s\n",
|
||||||
|
udev_device_get_sysattr_value(dev, "serial"));
|
||||||
|
devicesList.append(makePortInfo(dev));
|
||||||
|
udev_device_unref(dev);
|
||||||
|
}
|
||||||
|
/* free the enumerator object */
|
||||||
|
udev_enumerate_unref(enumerate);
|
||||||
|
|
||||||
|
return devicesList;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Be a bit more picky and ask only for a specific type of device:
|
||||||
|
*/
|
||||||
|
QList<USBPortInfo> USBMonitor::availableDevices(int vid, int pid, int bcdDevice)
|
||||||
|
{
|
||||||
|
QList<USBPortInfo> allPorts = availableDevices();
|
||||||
|
QList<USBPortInfo> thePortsWeWant;
|
||||||
|
|
||||||
|
foreach (USBPortInfo port, allPorts) {
|
||||||
|
thePortsWeWant.append(port);
|
||||||
|
}
|
||||||
|
|
||||||
|
return thePortsWeWant;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
USBPortInfo USBMonitor::makePortInfo(struct udev_device *dev)
|
||||||
|
{
|
||||||
|
USBPortInfo prtInfo;
|
||||||
|
|
||||||
|
printf(" Node: %s", udev_device_get_devnode(dev));
|
||||||
|
printf(" Subsystem: %s", udev_device_get_subsystem(dev));
|
||||||
|
printf(" Devtype: %s", udev_device_get_devtype(dev));
|
||||||
|
printf(" Action: %s", udev_device_get_action(dev));
|
||||||
|
|
||||||
|
prtInfo.vendorID = QString(udev_device_get_sysattr_value(dev, "idVendor")).toInt();
|
||||||
|
prtInfo.productID = QString(udev_device_get_sysattr_value(dev, "idProduct")).toInt();
|
||||||
|
prtInfo.serialNumber = QString(udev_device_get_sysattr_value(dev, "serial"));
|
||||||
|
|
||||||
|
return prtInfo;
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -48,3 +48,26 @@ USBMonitor::~USBMonitor()
|
|||||||
{
|
{
|
||||||
quit();
|
quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns a list of all currently available devices
|
||||||
|
*/
|
||||||
|
QList<USBPortInfo> USBMonitor::availableDevices()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Be a bit more picky and ask only for a specific type of device:
|
||||||
|
*/
|
||||||
|
QList<USBPortInfo> USBMonitor::availableDevices(int vid, int pid, int bcdDevice)
|
||||||
|
{
|
||||||
|
QList<USBPortInfo> allPorts = availableDevices();
|
||||||
|
QList<USBPortInfo> thePortsWeWant;
|
||||||
|
|
||||||
|
foreach (USBPortInfo port, allPorts) {
|
||||||
|
thePortsWeWant.append(port);
|
||||||
|
}
|
||||||
|
|
||||||
|
return thePortsWeWant;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user