From 822cbfbc8171d0ad4d62dc6bc2f3e4a2203c596b Mon Sep 17 00:00:00 2001 From: Edouard Lafargue Date: Sun, 25 Sep 2011 15:26:12 +0200 Subject: [PATCH] Fix MacOS halt issues by using the USBMonitor to detect device re-insertion:wq: --- .../src/plugins/rawhid/usbmonitor_mac.cpp | 5 ++- .../src/plugins/uploader/op_dfu.cpp | 37 +++++++++---------- .../src/plugins/uploader/op_dfu.h | 1 + .../plugins/uploader/uploadergadgetwidget.cpp | 4 +- 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/ground/openpilotgcs/src/plugins/rawhid/usbmonitor_mac.cpp b/ground/openpilotgcs/src/plugins/rawhid/usbmonitor_mac.cpp index e094a9a68..51fd6cce7 100644 --- a/ground/openpilotgcs/src/plugins/rawhid/usbmonitor_mac.cpp +++ b/ground/openpilotgcs/src/plugins/rawhid/usbmonitor_mac.cpp @@ -117,7 +117,7 @@ void USBMonitor::removeDevice(IOHIDDeviceRef dev) { */ void USBMonitor::detach_callback(void *context, IOReturn r, void *hid_mgr, IOHIDDeviceRef dev) { - + qDebug() << "USBMonitor: Device detached event"; instance()->removeDevice(dev); } @@ -136,6 +136,8 @@ void USBMonitor::attach_callback(void *context, IOReturn r, void *hid_mgr, IOHID deviceInfo.dev_handle = dev; + qDebug() << "USBMonitor: Device attached event"; + // Populate the device info structure got_properties &= HID_GetIntProperty(dev, CFSTR( kIOHIDVendorIDKey ), &deviceInfo.vendorID); got_properties &= HID_GetIntProperty(dev, CFSTR( kIOHIDProductIDKey ), &deviceInfo.productID); @@ -149,6 +151,7 @@ void USBMonitor::attach_callback(void *context, IOReturn r, void *hid_mgr, IOHID // Currently only enumerating objects that have the complete list of properties if(got_properties) { + qDebug() << "USBMonitor: Adding device"; instance()->addDevice(deviceInfo); } } diff --git a/ground/openpilotgcs/src/plugins/uploader/op_dfu.cpp b/ground/openpilotgcs/src/plugins/uploader/op_dfu.cpp index 8a9045233..edd5dfb0b 100644 --- a/ground/openpilotgcs/src/plugins/uploader/op_dfu.cpp +++ b/ground/openpilotgcs/src/plugins/uploader/op_dfu.cpp @@ -80,29 +80,26 @@ DFUObject::DFUObject(bool _debug,bool _use_serial,QString portname): } else { - send_delay=10; - use_delay=true; -// int numDevices=0; QList devices; - int count=0; - while((devices.length()==0) && count < 10) - { - if (debug) - qDebug() << "."; - delay::msleep(500); - // processEvents enables XP to process the system - // plug/unplug events, otherwise it will not process - // those events before the end of the call! - QApplication::processEvents(); - devices = USBMonitor::instance()->availableDevices(0x20a0,-1,-1,USBMonitor::Bootloader); - count++; - } - if (devices.length()==1) { + devices = USBMonitor::instance()->availableDevices(0x20a0,-1,-1,USBMonitor::Bootloader); + if (devices.length()==1) { hidHandle.open(1,devices.first().vendorID,devices.first().productID,0,0); } else { - qDebug() << devices.length() << " device(s) detected, don't know what to do!"; - mready = false; - } + // Wait for the board to appear on the USB bus: + QEventLoop m_eventloop; + connect(USBMonitor::instance(), SIGNAL(deviceDiscovered(USBPortInfo)),&m_eventloop, SLOT(quit())); + QTimer::singleShot(5000,&m_eventloop, SLOT(quit())); + m_eventloop.exec(); + devices = USBMonitor::instance()->availableDevices(0x20a0,-1,-1,USBMonitor::Bootloader); + if (devices.length()==1) { + delay::msleep(2000); // Let the USB Subsystem settle (especially important on Mac!) + hidHandle.open(1,devices.first().vendorID,devices.first().productID,0,0); + } + else { + qDebug() << devices.length() << " device(s) detected, don't know what to do!"; + mready = false; + } + } } } diff --git a/ground/openpilotgcs/src/plugins/uploader/op_dfu.h b/ground/openpilotgcs/src/plugins/uploader/op_dfu.h index 420e0a3ea..1fc92637c 100644 --- a/ground/openpilotgcs/src/plugins/uploader/op_dfu.h +++ b/ground/openpilotgcs/src/plugins/uploader/op_dfu.h @@ -17,6 +17,7 @@ #include "delay.h" #include #include +#include #include "SSP/qssp.h" #include "SSP/port.h" #include "SSP/qsspt.h" diff --git a/ground/openpilotgcs/src/plugins/uploader/uploadergadgetwidget.cpp b/ground/openpilotgcs/src/plugins/uploader/uploadergadgetwidget.cpp index 397533c2b..883955a5b 100755 --- a/ground/openpilotgcs/src/plugins/uploader/uploadergadgetwidget.cpp +++ b/ground/openpilotgcs/src/plugins/uploader/uploadergadgetwidget.cpp @@ -235,9 +235,7 @@ void UploaderGadgetWidget::goToBootloader(UAVObject* callerObj, bool success) currentStep = IAP_STATE_BOOTLOADER; // Tell the mainboard to get into bootloader state: - log("Detecting devices, please wait 5 seconds..."); - this->repaint(); - delay::msleep(5100); // Required to let the board(s) settle + log("Detecting devices, please wait a few seconds..."); if (!dfu) { if (dlj.startsWith("USB")) dfu = new DFUObject(DFU_DEBUG, false, QString());