1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-12-02 10:24:11 +01:00

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
This commit is contained in:
edouard 2011-03-08 23:59:29 +00:00 committed by edouard
parent ae32bae5c7
commit 51a8a2d525
5 changed files with 58 additions and 13 deletions

View File

@ -85,12 +85,11 @@ QStringList RawHIDConnection::availableDevices()
{
QStringList devices;
QList<USBPortInfo> portsList = m_usbMonitor.availableDevices(USB_VID, -1, -1);
QList<USBPortInfo> 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;
}

View File

@ -28,7 +28,7 @@
#ifndef USBMONITOR_H
#define USBMONITOR_H
//#include "rawhid_global.h"
#include "rawhid_global.h"
#include <QThread>
@ -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<USBPortInfo> availableDevices();
QList<USBPortInfo> availableDevices(int vid, int pid, int bcdDevice);
QList<USBPortInfo> 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)

View File

@ -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<USBPortInfo> 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<USBPortInfo> USBMonitor::availableDevices(int vid, int pid, int bcdDevice)
QList<USBPortInfo> USBMonitor::availableDevices(int vid, int pid, int bcdDeviceMSB, int bcdDeviceLSB)
{
QList<USBPortInfo> allPorts = availableDevices();
QList<USBPortInfo> 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;

View File

@ -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<USBPortInfo> 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<USBPortInfo> USBMonitor::availableDevices(int vid, int pid, int bcdDevice)
QList<USBPortInfo> USBMonitor::availableDevices(int vid, int pid, int bcdDeviceMSB, int bcdDeviceLSB)
{
QList<USBPortInfo> allPorts = availableDevices();
QList<USBPortInfo> 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;

View File

@ -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<USBPortInfo> USBMonitor::availableDevices(int vid, int pid, int bcdDevice)
QList<USBPortInfo> USBMonitor::availableDevices(int vid, int pid, int bcdDeviceMSB, int bcdDeviceLSB)
{
QList<USBPortInfo> allPorts = availableDevices();
QList<USBPortInfo> 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;