dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
rawhidplugin.cpp
Go to the documentation of this file.
1 
12 /*
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  * for more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with this program; if not, see <http://www.gnu.org/licenses/>
25  */
26 
27 #include "rawhidplugin.h"
28 #include "rawhid.h"
29 
30 #include <coreplugin/icore.h>
31 #include <QtCore/QtPlugin>
32 #include <QtCore/QMutexLocker>
33 #include <QDebug>
34 
35 #include "rawhid_const.h"
36 #include "usbsignalfilter.h"
37 
38 // **********************************************************************
39 
41 {
42  RawHidHandle = NULL;
43  enablePolling = true;
44 
45  m_usbMonitor = USBMonitor::instance();
46 
47  m_signalFilter = new USBSignalFilter(-1, -1, -1, USBMonitor::Running);
48  connect(m_signalFilter, &USBSignalFilter::deviceDiscovered, this,
50  connect(m_signalFilter, &USBSignalFilter::deviceRemoved, this,
52 }
53 
55 {
56  if (RawHidHandle)
57  if (RawHidHandle->isOpen())
58  RawHidHandle->close();
59 
60  delete m_signalFilter;
61 }
62 
67 {
68  emit availableDevChanged(this);
69 }
70 
75 {
76  RAW_HID_QXTLOG_DEBUG("onDeviceDisconnected()");
77  if (enablePolling)
78  emit availableDevChanged(this);
79 }
80 
86 {
87  QList<Core::IDevice *> devices;
88  QList<USBPortInfo> portsList;
89 
90  // Loop for all vendorIDs known by the board manager
92  QList<int> brdVID = brdMgr->getKnownVendorIDs();
93  foreach (int vendorID, brdVID) {
94  RAW_HID_QXTLOG_DEBUG("[rawhidplugin] VendorID type known: ", vendorID);
95  portsList = m_usbMonitor->availableDevices(vendorID, -1, -1, USBMonitor::Running);
96  // We currently list devices by their serial number
97  USBDevice *dev = new USBDevice();
98  foreach (USBPortInfo prt, portsList) {
99  dev->setName(prt.serialNumber);
100  dev->setDisplayName(prt.product);
101  dev->setVendorID(prt.vendorID);
102  dev->setProductID(prt.productID);
103  dev->setPath(prt.path);
104  devices.append(dev);
105  }
106  }
107  return devices;
108 }
109 
111 {
112  if (RawHidHandle)
113  closeDevice(deviceName->getName());
114 
115  // We know this device is (should be?) a USB device:
116  USBDevice *usbDev = dynamic_cast<USBDevice *>(deviceName);
117  Q_ASSERT(usbDev);
118 
119  RawHidHandle = new RawHID(usbDev);
120  return RawHidHandle;
121 }
122 
123 void RawHIDConnection::closeDevice(const QString &deviceName)
124 {
125  Q_UNUSED(deviceName);
126  if (RawHidHandle) {
127  RAW_HID_QXTLOG_DEBUG("Closing the device here");
128  RawHidHandle->close();
129  delete RawHidHandle;
130  RawHidHandle = NULL;
131  }
132 }
133 
135 {
136  return QString("Raw HID USB");
137 }
138 
140 {
141  return QString("USB");
142 }
143 
148 {
149  enablePolling = false;
150 }
151 
156 {
157  enablePolling = true;
158 }
159 
160 // **********************************************************************
161 
162 RawHIDPlugin::RawHIDPlugin()
163 {
164  m_usbMonitor = NULL;
165  hidConnection = NULL;
166 }
167 
168 RawHIDPlugin::~RawHIDPlugin()
169 {
170  if (m_usbMonitor)
171  delete m_usbMonitor;
172 }
173 
174 void RawHIDPlugin::extensionsInitialized()
175 {
176  hidConnection = new RawHIDConnection();
177  addAutoReleasedObject(hidConnection);
178 }
179 
180 bool RawHIDPlugin::initialize(const QStringList &arguments, QString *errorString)
181 {
182  Q_UNUSED(arguments);
183  Q_UNUSED(errorString);
184 
185  // We have to create the USB Monitor here:
186  m_usbMonitor = new USBMonitor(this);
187 
188  return true;
189 }
void availableDevChanged(IConnection *)
int productID
Product ID.
Definition: usbmonitor.h:51
QString getName() const
Definition: idevice.h:49
void onDeviceDisconnected()
QList< int > getKnownVendorIDs()
getKnownVendorIDs Get all USB VendorIDs known by the board manager. This can be used by any plugin wh...
void setName(QString theName)
Definition: idevice.h:50
virtual void closeDevice(const QString &deviceName)
void setProductID(int pid)
Definition: usbdevice.h:41
virtual void close()
Definition: rawhid.cpp:269
void setVendorID(int vid)
Definition: usbdevice.h:40
QString serialNumber
Definition: usbmonitor.h:44
virtual void suspendPolling()
QList< USBPortInfo > availableDevices()
Definition: usbmonitor.cpp:142
Definition: rawhid.h:134
void setDisplayName(QString dn)
Definition: idevice.h:52
static ICore * instance()
Definition: coreimpl.cpp:46
void setPath(QString p)
Definition: usbdevice.h:42
void onDeviceConnected()
virtual ~RawHIDConnection()
virtual BoardManager * boardManager() const =0
void deviceRemoved()
static USBMonitor * instance()
Definition: usbmonitor.cpp:176
QString product
Definition: usbmonitor.h:46
virtual QString connectionName()
virtual QIODevice * openDevice(Core::IDevice *deviceName)
virtual QString shortName()
virtual void resumePolling()
virtual QList< Core::IDevice * > availableDevices()
void addAutoReleasedObject(QObject *obj)
Definition: iplugin.cpp:306
void deviceDiscovered()
QString path
Opaque OS-specific path.
Definition: usbmonitor.h:48
int vendorID
Vendor ID.
Definition: usbmonitor.h:50