1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-03-15 07:29:15 +01:00

*[mac only] definition usb devices made ​​in an additional thread

This commit is contained in:
Voha 2013-05-24 21:53:35 +03:00
parent ccc7e4a0ab
commit 3b7c6eaa5f
4 changed files with 75 additions and 36 deletions

View File

@ -137,6 +137,9 @@ signals:
\param info The device that has been discovered.
*/
void deviceDiscovered( const USBPortInfo & info );
#ifdef __APPLE__
void deviceDiscovered();
#endif // __APPLE__
/*!
A device has been disconnected from the system.
@ -145,6 +148,9 @@ signals:
\param info The device that was disconnected.
*/
void deviceRemoved( const USBPortInfo & info );
#ifdef __APPLE__
void deviceRemoved();
#endif // __APPLE__
private slots:
/**
@ -198,6 +204,13 @@ private:
#endif
#endif
#ifdef __APPLE__
protected:
bool m_terminate;
void run();
#endif // __APPLE__
};
#endif // OPHID_USBMON_H

View File

@ -49,8 +49,13 @@ RawHIDConnection::RawHIDConnection()
m_usbMonitor = USBMonitor::instance();
connect(m_usbMonitor, SIGNAL(deviceDiscovered(USBPortInfo)), this, SLOT(onDeviceConnected()));
connect(m_usbMonitor, SIGNAL(deviceRemoved(USBPortInfo)), this, SLOT(onDeviceDisconnected()));
#ifndef __APPLE__
connect(m_usbMonitor, SIGNAL(deviceDiscovered(USBPortInfo)), this, SLOT(onDeviceConnected()));
connect(m_usbMonitor, SIGNAL(deviceRemoved(USBPortInfo)), this, SLOT(onDeviceDisconnected()));
#else
connect(m_usbMonitor, SIGNAL(deviceDiscovered()), this, SLOT(onDeviceConnected()));
connect(m_usbMonitor, SIGNAL(deviceRemoved()), this, SLOT(onDeviceDisconnected()));
#endif
}

View File

@ -35,6 +35,8 @@
#include <QDebug>
#include "opHID_const.h"
#define OP_LOOPMODE_NAME_MAC "Open_Pilot_Loop_Mode"
#define printf qDebug
// ! Local helper functions
@ -46,45 +48,25 @@ static bool HID_GetStrProperty(IOHIDDeviceRef dev, CFStringRef property, QString
*/
USBMonitor::USBMonitor(QObject *parent) : QThread(parent)
{
hid_manager = NULL;
IOReturn ret;
m_instance = this;
listMutex = new QMutex();
knowndevices.clear();
hid_manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
if (hid_manager == NULL || CFGetTypeID(hid_manager) != IOHIDManagerGetTypeID()) {
if (hid_manager) {
CFRelease(hid_manager);
}
Q_ASSERT(0);
}
// No matching filter
IOHIDManagerSetDeviceMatching(hid_manager, NULL);
// set up a callbacks for device attach & detach
IOHIDManagerScheduleWithRunLoop(hid_manager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
IOHIDManagerRegisterDeviceMatchingCallback(hid_manager, attach_callback, NULL);
IOHIDManagerRegisterDeviceRemovalCallback(hid_manager, detach_callback, NULL);
ret = IOHIDManagerOpen(hid_manager, kIOHIDOptionsTypeNone);
if (ret != kIOReturnSuccess) {
IOHIDManagerUnscheduleFromRunLoop(hid_manager,
CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFRelease(hid_manager);
return;
}
m_instance = this;
hid_manager = NULL;
listMutex = new QMutex();
knowndevices.clear();
m_terminate = false;
start();
}
USBMonitor::~USBMonitor()
{
m_terminate = true;
// if(hid_manager != NULL)
// IOHIDManagerUnscheduleFromRunLoop(hid_manager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
quit();
// quit();
while (hid_manager != 0) {
this->sleep(10);
}
}
void USBMonitor::deviceEventReceived()
@ -108,6 +90,7 @@ void USBMonitor::removeDevice(IOHIDDeviceRef dev)
QMutexLocker locker(listMutex);
knowndevices.removeAt(i);
emit deviceRemoved(port);
emit deviceRemoved();
return;
}
}
@ -130,8 +113,9 @@ void USBMonitor::addDevice(USBPortInfo info)
{
QMutexLocker locker(listMutex);
knowndevices.append(info);
emit deviceDiscovered(info);
knowndevices.append(info);
emit deviceDiscovered(info);
emit deviceDiscovered();
}
void USBMonitor::attach_callback(void *context, IOReturn r, void *hid_mgr, IOHIDDeviceRef dev)
@ -201,6 +185,42 @@ QList<USBPortInfo> USBMonitor::availableDevices(int vid, int pid, int bcdDeviceM
return thePortsWeWant;
}
void USBMonitor::run()
{
IOReturn ret;
hid_manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
if (hid_manager == NULL || CFGetTypeID(hid_manager) != IOHIDManagerGetTypeID()) {
if (hid_manager) {
CFRelease(hid_manager);
}
assert(0);
}
// No matching filter
IOHIDManagerSetDeviceMatching(hid_manager, NULL);
CFRunLoopRef loop = CFRunLoopGetCurrent();
// set up a callbacks for device attach & detach
IOHIDManagerScheduleWithRunLoop(hid_manager, loop, kCFRunLoopDefaultMode);
IOHIDManagerRegisterDeviceMatchingCallback(hid_manager, attach_callback, this);
IOHIDManagerRegisterDeviceRemovalCallback(hid_manager, detach_callback, this);
ret = IOHIDManagerOpen(hid_manager, kIOHIDOptionsTypeNone);
if (ret != kIOReturnSuccess) {
IOHIDManagerUnscheduleFromRunLoop(hid_manager, loop, kCFRunLoopDefaultMode);
CFRelease(hid_manager);
return;
}
while(!m_terminate) {
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1, false);
}
IOHIDManagerUnscheduleFromRunLoop(hid_manager, loop, kCFRunLoopDefaultMode);
CFRelease(hid_manager);
hid_manager = NULL;
}
/**
* @brief Helper function get get a HID integer property
* @param[in] dev Device reference

View File

@ -68,7 +68,8 @@ USBSignalFilter::USBSignalFilter(int vid, int pid, int boardModel, int runState)
connect(USBMonitor::instance(),
SIGNAL(deviceDiscovered(USBPortInfo)),
this,
SLOT(m_deviceDiscovered(USBPortInfo)));
SLOT(m_deviceDiscovered(USBPortInfo)),
Qt::QueuedConnection);
}