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:
parent
ae32bae5c7
commit
51a8a2d525
@ -85,12 +85,11 @@ QStringList RawHIDConnection::availableDevices()
|
|||||||
{
|
{
|
||||||
QStringList devices;
|
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
|
// We currently list devices by their serial number
|
||||||
foreach(USBPortInfo prt, portsList) {
|
foreach(USBPortInfo prt, portsList) {
|
||||||
devices.append(prt.serialNumber);
|
devices.append(prt.serialNumber);
|
||||||
}
|
}
|
||||||
|
|
||||||
return devices;
|
return devices;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@
|
|||||||
#ifndef USBMONITOR_H
|
#ifndef USBMONITOR_H
|
||||||
#define USBMONITOR_H
|
#define USBMONITOR_H
|
||||||
|
|
||||||
//#include "rawhid_global.h"
|
#include "rawhid_global.h"
|
||||||
|
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
|
|
||||||
@ -100,16 +100,23 @@ protected:
|
|||||||
/**
|
/**
|
||||||
* A monitoring thread which will wait for device events.
|
* A monitoring thread which will wait for device events.
|
||||||
*/
|
*/
|
||||||
class USBMonitor : public QThread
|
|
||||||
|
class RAWHID_EXPORT USBMonitor : public QThread
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
enum RunState {
|
||||||
|
Bootloader = 0x01,
|
||||||
|
Running = 0x02
|
||||||
|
};
|
||||||
|
|
||||||
|
static USBMonitor *instance();
|
||||||
|
|
||||||
USBMonitor(QObject *parent = 0);
|
USBMonitor(QObject *parent = 0);
|
||||||
// USBMonitor(int vid, int pid);
|
|
||||||
~USBMonitor();
|
~USBMonitor();
|
||||||
QList<USBPortInfo> availableDevices();
|
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)
|
#if defined (Q_OS_WIN32)
|
||||||
LRESULT onDeviceChangeWin( WPARAM wParam, LPARAM lParam );
|
LRESULT onDeviceChangeWin( WPARAM wParam, LPARAM lParam );
|
||||||
#endif
|
#endif
|
||||||
@ -140,6 +147,9 @@ private slots:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
Q_DISABLE_COPY(USBMonitor)
|
||||||
|
static USBMonitor *m_instance;
|
||||||
|
|
||||||
// Depending on the OS, we'll need different things:
|
// Depending on the OS, we'll need different things:
|
||||||
#if defined( Q_OS_MAC)
|
#if defined( Q_OS_MAC)
|
||||||
|
|
||||||
|
@ -59,11 +59,21 @@ void USBMonitor::deviceEventReceived() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
USBMonitor* USBMonitor::instance()
|
||||||
|
{
|
||||||
|
return m_instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
USBMonitor* USBMonitor::m_instance = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Initialize the udev monitor here
|
Initialize the udev monitor here
|
||||||
*/
|
*/
|
||||||
USBMonitor::USBMonitor(QObject *parent): QThread(parent) {
|
USBMonitor::USBMonitor(QObject *parent): QThread(parent) {
|
||||||
|
|
||||||
|
m_instance = this;
|
||||||
|
|
||||||
this->context = udev_new();
|
this->context = udev_new();
|
||||||
|
|
||||||
this->monitor = udev_monitor_new_from_netlink(this->context, "udev");
|
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:
|
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> allPorts = availableDevices();
|
||||||
QList<USBPortInfo> thePortsWeWant;
|
QList<USBPortInfo> thePortsWeWant;
|
||||||
|
|
||||||
foreach (USBPortInfo port, allPorts) {
|
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);
|
thePortsWeWant.append(port);
|
||||||
}
|
}
|
||||||
return thePortsWeWant;
|
return thePortsWeWant;
|
||||||
|
@ -36,6 +36,15 @@ void USBMonitor::deviceEventReceived() {
|
|||||||
qDebug() << "Device event";
|
qDebug() << "Device event";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
USBMonitor* USBMonitor::instance()
|
||||||
|
{
|
||||||
|
return m_instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
USBMonitor* USBMonitor::m_instance = 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Initialize the USB monitor here
|
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:
|
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> allPorts = availableDevices();
|
||||||
QList<USBPortInfo> thePortsWeWant;
|
QList<USBPortInfo> thePortsWeWant;
|
||||||
|
|
||||||
foreach (USBPortInfo port, allPorts) {
|
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);
|
thePortsWeWant.append(port);
|
||||||
}
|
}
|
||||||
return thePortsWeWant;
|
return thePortsWeWant;
|
||||||
|
@ -41,6 +41,14 @@ void USBMonitor::deviceEventReceived() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
USBMonitor* USBMonitor::instance()
|
||||||
|
{
|
||||||
|
return m_instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
USBMonitor* USBMonitor::m_instance = 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
USBMonitor::USBMonitor(QObject *parent): QThread(parent) {
|
USBMonitor::USBMonitor(QObject *parent): QThread(parent) {
|
||||||
HidD_GetHidGuid(&guid_hid);
|
HidD_GetHidGuid(&guid_hid);
|
||||||
@ -63,14 +71,17 @@ USBMonitor::~USBMonitor()
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
Be a bit more picky and ask only for a specific type of device:
|
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> allPorts = availableDevices();
|
||||||
QList<USBPortInfo> thePortsWeWant;
|
QList<USBPortInfo> thePortsWeWant;
|
||||||
|
|
||||||
foreach (USBPortInfo port, allPorts) {
|
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);
|
thePortsWeWant.append(port);
|
||||||
}
|
}
|
||||||
return thePortsWeWant;
|
return thePortsWeWant;
|
||||||
|
Loading…
Reference in New Issue
Block a user