From 51a8a2d52599f12bbbfa42a1a0f4ccc75ec9c6ee Mon Sep 17 00:00:00 2001 From: edouard Date: Tue, 8 Mar 2011 23:59:29 +0000 Subject: [PATCH] OP-334: Make the USBMonitor a singleton and export its symbols in the RawHID plugin, so that other plugins can use its instance(). Also implement a way to ask for devices in bootloader or running mode. git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@3012 ebee16cc-31ac-478f-84a7-5cbb03baadba --- .../src/plugins/rawhid/rawhidplugin.cpp | 3 +-- .../src/plugins/rawhid/usbmonitor.h | 20 ++++++++++++++----- .../src/plugins/rawhid/usbmonitor_linux.cpp | 17 ++++++++++++++-- .../src/plugins/rawhid/usbmonitor_mac.cpp | 16 +++++++++++++-- .../src/plugins/rawhid/usbmonitor_win.cpp | 15 ++++++++++++-- 5 files changed, 58 insertions(+), 13 deletions(-) diff --git a/ground/openpilotgcs/src/plugins/rawhid/rawhidplugin.cpp b/ground/openpilotgcs/src/plugins/rawhid/rawhidplugin.cpp index a1402a849..76729ec96 100644 --- a/ground/openpilotgcs/src/plugins/rawhid/rawhidplugin.cpp +++ b/ground/openpilotgcs/src/plugins/rawhid/rawhidplugin.cpp @@ -85,12 +85,11 @@ QStringList RawHIDConnection::availableDevices() { QStringList devices; - QList portsList = m_usbMonitor.availableDevices(USB_VID, -1, -1); + QList portsList = m_usbMonitor.availableDevices(USB_VID, -1, -1,USBMonitor::Running); // We currently list devices by their serial number foreach(USBPortInfo prt, portsList) { devices.append(prt.serialNumber); } - return devices; } diff --git a/ground/openpilotgcs/src/plugins/rawhid/usbmonitor.h b/ground/openpilotgcs/src/plugins/rawhid/usbmonitor.h index b0e2311db..7090b59fb 100644 --- a/ground/openpilotgcs/src/plugins/rawhid/usbmonitor.h +++ b/ground/openpilotgcs/src/plugins/rawhid/usbmonitor.h @@ -28,7 +28,7 @@ #ifndef USBMONITOR_H #define USBMONITOR_H -//#include "rawhid_global.h" +#include "rawhid_global.h" #include @@ -100,16 +100,23 @@ protected: /** * A monitoring thread which will wait for device events. */ -class USBMonitor : public QThread + +class RAWHID_EXPORT USBMonitor : public QThread { - Q_OBJECT + Q_OBJECT public: + enum RunState { + Bootloader = 0x01, + Running = 0x02 + }; + + static USBMonitor *instance(); + USBMonitor(QObject *parent = 0); -// USBMonitor(int vid, int pid); ~USBMonitor(); QList availableDevices(); - QList availableDevices(int vid, int pid, int bcdDevice); + QList availableDevices(int vid, int pid, int boardModel, int runState); #if defined (Q_OS_WIN32) LRESULT onDeviceChangeWin( WPARAM wParam, LPARAM lParam ); #endif @@ -140,6 +147,9 @@ private slots: private: + Q_DISABLE_COPY(USBMonitor) + static USBMonitor *m_instance; + // Depending on the OS, we'll need different things: #if defined( Q_OS_MAC) diff --git a/ground/openpilotgcs/src/plugins/rawhid/usbmonitor_linux.cpp b/ground/openpilotgcs/src/plugins/rawhid/usbmonitor_linux.cpp index c7f0f1772..12c72d930 100644 --- a/ground/openpilotgcs/src/plugins/rawhid/usbmonitor_linux.cpp +++ b/ground/openpilotgcs/src/plugins/rawhid/usbmonitor_linux.cpp @@ -59,11 +59,21 @@ void USBMonitor::deviceEventReceived() { } + +USBMonitor* USBMonitor::instance() +{ + return m_instance; +} + +USBMonitor* USBMonitor::m_instance = 0; + /** Initialize the udev monitor here */ USBMonitor::USBMonitor(QObject *parent): QThread(parent) { + m_instance = this; + this->context = udev_new(); this->monitor = udev_monitor_new_from_netlink(this->context, "udev"); @@ -120,14 +130,17 @@ QList USBMonitor::availableDevices() /** Be a bit more picky and ask only for a specific type of device: + On OpenPilot, the bcdDeviceLSB indicates the run state: bootloader or running. + bcdDeviceMSB indicates the board model. */ -QList USBMonitor::availableDevices(int vid, int pid, int bcdDevice) +QList USBMonitor::availableDevices(int vid, int pid, int bcdDeviceMSB, int bcdDeviceLSB) { QList allPorts = availableDevices(); QList thePortsWeWant; foreach (USBPortInfo port, allPorts) { - if((port.vendorID==vid || vid==-1) && (port.productID==pid || pid==-1) && (port.bcdDevice==bcdDevice || bcdDevice==-1)) + if((port.vendorID==vid || vid==-1) && (port.productID==pid || pid==-1) && ((port.bcdDevice>>8)==bcdDeviceMSB || bcdDeviceMSB==-1) && + ( (port.bcdDevice&0x00ff) ==bcdDeviceLSB || bcdDeviceLSB==-1)) thePortsWeWant.append(port); } return thePortsWeWant; diff --git a/ground/openpilotgcs/src/plugins/rawhid/usbmonitor_mac.cpp b/ground/openpilotgcs/src/plugins/rawhid/usbmonitor_mac.cpp index 78202dfea..aa77e5ad7 100644 --- a/ground/openpilotgcs/src/plugins/rawhid/usbmonitor_mac.cpp +++ b/ground/openpilotgcs/src/plugins/rawhid/usbmonitor_mac.cpp @@ -36,6 +36,15 @@ void USBMonitor::deviceEventReceived() { qDebug() << "Device event"; } +USBMonitor* USBMonitor::instance() +{ + return m_instance; +} + +USBMonitor* USBMonitor::m_instance = 0; + + + /** Initialize the USB monitor here */ @@ -64,14 +73,17 @@ QList USBMonitor::availableDevices() /** Be a bit more picky and ask only for a specific type of device: + On OpenPilot, the bcdDeviceLSB indicates the run state: bootloader or running. + bcdDeviceMSB indicates the board model. */ -QList USBMonitor::availableDevices(int vid, int pid, int bcdDevice) +QList USBMonitor::availableDevices(int vid, int pid, int bcdDeviceMSB, int bcdDeviceLSB) { QList allPorts = availableDevices(); QList thePortsWeWant; foreach (USBPortInfo port, allPorts) { - if((port.vendorID==vid || vid==-1) && (port.productID==pid || pid==-1) && (port.bcdDevice==bcdDevice || bcdDevice==-1)) + if((port.vendorID==vid || vid==-1) && (port.productID==pid || pid==-1) && ((port.bcdDevice>>8)==bcdDeviceMSB || bcdDeviceMSB==-1) && + ( (port.bcdDevice&0x00ff) ==bcdDeviceLSB || bcdDeviceLSB==-1)) thePortsWeWant.append(port); } return thePortsWeWant; diff --git a/ground/openpilotgcs/src/plugins/rawhid/usbmonitor_win.cpp b/ground/openpilotgcs/src/plugins/rawhid/usbmonitor_win.cpp index f9271ab92..4ff0d2649 100644 --- a/ground/openpilotgcs/src/plugins/rawhid/usbmonitor_win.cpp +++ b/ground/openpilotgcs/src/plugins/rawhid/usbmonitor_win.cpp @@ -41,6 +41,14 @@ void USBMonitor::deviceEventReceived() { } +USBMonitor* USBMonitor::instance() +{ + return m_instance; +} + +USBMonitor* USBMonitor::m_instance = 0; + + USBMonitor::USBMonitor(QObject *parent): QThread(parent) { HidD_GetHidGuid(&guid_hid); @@ -63,14 +71,17 @@ USBMonitor::~USBMonitor() /** Be a bit more picky and ask only for a specific type of device: + On OpenPilot, the bcdDeviceLSB indicates the run state: bootloader or running. + bcdDeviceMSB indicates the board model. */ -QList USBMonitor::availableDevices(int vid, int pid, int bcdDevice) +QList USBMonitor::availableDevices(int vid, int pid, int bcdDeviceMSB, int bcdDeviceLSB) { QList allPorts = availableDevices(); QList thePortsWeWant; foreach (USBPortInfo port, allPorts) { - if((port.vendorID==vid || vid==-1) && (port.productID==pid || pid==-1) && (port.bcdDevice==bcdDevice || bcdDevice==-1)) + if((port.vendorID==vid || vid==-1) && (port.productID==pid || pid==-1) && ((port.bcdDevice>>8)==bcdDeviceMSB || bcdDeviceMSB==-1) && + ( (port.bcdDevice&0x00ff) ==bcdDeviceLSB || bcdDeviceLSB==-1)) thePortsWeWant.append(port); } return thePortsWeWant;