From 5556ed15b91d47e0335cd4a8b411129f19cddebd Mon Sep 17 00:00:00 2001 From: PT_Dreamer Date: Fri, 5 Oct 2012 19:55:43 +0100 Subject: [PATCH] GCS-Auto update function Quick and dirty autoupdate function. The uploader plugin is in need of a major refactoring to separate the logic from the UI. This will be on my TODO list. --- .../plugins/uploader/uploadergadgetwidget.cpp | 107 ++++++++++++++++++ .../plugins/uploader/uploadergadgetwidget.h | 7 ++ 2 files changed, 114 insertions(+) diff --git a/ground/openpilotgcs/src/plugins/uploader/uploadergadgetwidget.cpp b/ground/openpilotgcs/src/plugins/uploader/uploadergadgetwidget.cpp index 06efb17b1..8656b45ea 100755 --- a/ground/openpilotgcs/src/plugins/uploader/uploadergadgetwidget.cpp +++ b/ground/openpilotgcs/src/plugins/uploader/uploadergadgetwidget.cpp @@ -432,6 +432,102 @@ void UploaderGadgetWidget::commonSystemBoot(bool safeboot) delete dfu; // Frees up the USB/Serial port too dfu = NULL; } +bool UploaderGadgetWidget::autoUpdate() +{ + Core::ConnectionManager *cm = Core::ICore::instance()->connectionManager(); + cm->disconnectDevice(); + cm->suspendPolling(); + if (dfu) { + delete dfu; + dfu = NULL; + } + if(USBMonitor::instance()->availableDevices(0x20a0,-1,-1,-1).length()>0) + { + emit autoUpdateSignal(WAITING_DISCONNECT,QVariant()); + if(QMessageBox::warning(this,tr("OpenPilot Uploader"),tr("Please disconnect all openpilot boards"),QMessageBox::Ok,QMessageBox::Cancel)==QMessageBox::Cancel) + { + return false; + } + } + m_timer = new QTimer(this); + connect(m_timer, SIGNAL(timeout()), this, SLOT(performAuto())); + m_timer->start(1000); + connect(USBMonitor::instance(), SIGNAL(deviceDiscovered(USBPortInfo)),&m_eventloop, SLOT(quit())); + m_eventloop.exec(); + if(!m_timer->isActive()) + { + m_timer->stop(); + QMessageBox::warning(this,tr("Openpilot Uploader"),tr("No board connection was detected!")); + return false; + } + m_timer->stop(); + dfu = new DFUObject(DFU_DEBUG, false, QString()); + dfu->AbortOperation(); + emit autoUpdateSignal(JUMP_TO_BL,QVariant()); + if(!dfu->enterDFU(0)) + { + delete dfu; + dfu = NULL; + cm->resumePolling(); + return false; + } + if(!dfu->findDevices() || (dfu->numberOfDevices != 1)) + { + delete dfu; + dfu = NULL; + cm->resumePolling(); + return false; + } + if (dfu->numberOfDevices > 5) { + delete dfu; + dfu = NULL; + cm->resumePolling(); + return false; + } + QString filename; + emit autoUpdateSignal(LOADING_FW,QVariant()); + switch (dfu->devices[0].ID) + { + case 0x401: + filename="fw_coptercontrol.opfw"; + break; + case 0x402: + filename="fw_coptercontrol.opfw"; + break; + default: + break; + } + filename=":/firmware/firmware/"+filename; + QByteArray firmware; + QFile file(filename); + if (!file.open(QIODevice::ReadOnly)) { + return false; + } + firmware = file.readAll(); + QEventLoop loop; + connect(dfu, SIGNAL(progressUpdated(int)), this, SLOT(autoUpdateProgress(int))); + connect(dfu, SIGNAL(uploadFinished(OP_DFU::Status)), &loop, SLOT(quit())); + emit autoUpdateSignal(UPLOADING_FW,QVariant()); + if(!dfu->enterDFU(0)) + { + return false; + } + dfu->AbortOperation(); + if(!dfu->UploadFirmware(filename,false,0)) + return false; + loop.exec(); + QByteArray desc = firmware.right(100); + emit autoUpdateSignal(UPLOADING_FW,QVariant()); + if(dfu->UploadDescription(desc)!= OP_DFU::Last_operation_Success) + return false; + systemBoot(); + return true; +} + +void UploaderGadgetWidget::autoUpdateProgress(int value) +{ + emit autoUpdateSignal(UPLOADING_FW,value); +} /** Attempt a guided procedure to put both boards in BL mode when @@ -556,6 +652,17 @@ void UploaderGadgetWidget::perform() } m_progress->setValue(m_progress->value()+1); } +void UploaderGadgetWidget::performAuto() +{ + ++autoUpdateConnectTimeout; + emit autoUpdateSignal(WAITING_CONNECT,autoUpdateConnectTimeout); + if(autoUpdateConnectTimeout==19) + { + m_timer->stop(); + m_eventloop.exit(); + } + +} void UploaderGadgetWidget::cancel() { m_timer->stop(); diff --git a/ground/openpilotgcs/src/plugins/uploader/uploadergadgetwidget.h b/ground/openpilotgcs/src/plugins/uploader/uploadergadgetwidget.h index 5f5706381..3c6f8414c 100755 --- a/ground/openpilotgcs/src/plugins/uploader/uploadergadgetwidget.h +++ b/ground/openpilotgcs/src/plugins/uploader/uploadergadgetwidget.h @@ -69,6 +69,7 @@ public: UploaderGadgetWidget(QWidget *parent = 0); ~UploaderGadgetWidget(); typedef enum { IAP_STATE_READY, IAP_STATE_STEP_1, IAP_STATE_STEP_2, IAP_STEP_RESET, IAP_STATE_BOOTLOADER} IAPStep; + typedef enum { WAITING_DISCONNECT, WAITING_CONNECT, JUMP_TO_BL, LOADING_FW, UPLOADING_FW, UPLOADING_DESC, BOOTING, SUCCESS, FAILURE} AutoUpdateStep; void log(QString str); public slots: @@ -76,6 +77,10 @@ public slots: void onAutopilotDisconnect(); void populate(); void openHelp(); + bool autoUpdate(); + void autoUpdateProgress(int); +signals: + void autoUpdateSignal(AutoUpdateStep,QVariant); private: Ui_UploaderWidget *m_config; DFUObject *dfu; @@ -89,6 +94,7 @@ private: QEventLoop m_eventloop; QErrorMessage * msg; void connectSignalSlot(QWidget * widget); + int autoUpdateConnectTimeout; private slots: void onPhisicalHWConnect(); void versionMatchCheck(); @@ -102,6 +108,7 @@ private slots: void systemRescue(); void getSerialPorts(); void perform(); + void performAuto(); void cancel(); void uploadStarted(); void uploadEnded(bool succeed);