2010-11-01 19:15:48 +01:00
/**
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* @ file devicewidget . cpp
* @ author The OpenPilot Team , http : //www.openpilot.org Copyright (C) 2010.
* @ addtogroup GCSPlugins GCS Plugins
* @ {
2011-01-08 14:22:15 +01:00
* @ addtogroup Uploader Serial and USB Uploader Plugin
2010-11-01 19:15:48 +01:00
* @ {
2011-01-08 14:22:15 +01:00
* @ brief The USB and Serial protocol uploader plugin
2010-11-01 19:15:48 +01:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful , but
* WITHOUT ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE . See the GNU General Public License
* for more details .
*
* You should have received a copy of the GNU General Public License along
* with this program ; if not , write to the Free Software Foundation , Inc . ,
* 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
*/
# include "devicewidget.h"
deviceWidget : : deviceWidget ( QWidget * parent ) :
QWidget ( parent )
{
myDevice = new Ui_deviceWidget ( ) ;
myDevice - > setupUi ( this ) ;
2010-11-26 23:49:50 +01:00
// Initialization of the Device icon display
2011-06-19 01:50:38 +02:00
myDevice - > verticalGroupBox_loaded - > setVisible ( false ) ;
2011-06-21 19:09:37 +02:00
myDevice - > groupCustom - > setVisible ( false ) ;
2013-03-06 22:01:05 +01:00
myDevice - > confirmCheckBox - > setVisible ( false ) ;
2011-06-19 01:50:38 +02:00
myDevice - > gVDevice - > setScene ( new QGraphicsScene ( this ) ) ;
2010-11-03 19:46:17 +01:00
connect ( myDevice - > retrieveButton , SIGNAL ( clicked ( ) ) , this , SLOT ( downloadFirmware ( ) ) ) ;
connect ( myDevice - > updateButton , SIGNAL ( clicked ( ) ) , this , SLOT ( uploadFirmware ( ) ) ) ;
2011-06-19 01:50:38 +02:00
connect ( myDevice - > pbLoad , SIGNAL ( clicked ( ) ) , this , SLOT ( loadFirmware ( ) ) ) ;
2013-03-06 22:01:05 +01:00
connect ( myDevice - > confirmCheckBox , SIGNAL ( stateChanged ( int ) ) , this , SLOT ( confirmCB ( int ) ) ) ;
2010-12-28 22:27:21 +01:00
QPixmap pix = QPixmap ( QString ( " :uploader/images/view-refresh.svg " ) ) ;
myDevice - > statusIcon - > setPixmap ( pix ) ;
2011-05-25 21:23:22 +02:00
2011-06-19 01:50:38 +02:00
myDevice - > lblCertified - > setText ( " " ) ;
2010-11-01 19:15:48 +01:00
}
2010-11-01 21:50:59 +01:00
2010-11-26 23:49:50 +01:00
void deviceWidget : : showEvent ( QShowEvent * event )
{
Q_UNUSED ( event )
// Thit fitInView method should only be called now, once the
// widget is shown, otherwise it cannot compute its values and
2012-07-16 14:20:11 +02:00
// the result is usually a ahrsbargraph that is way too small
myDevice - > gVDevice - > fitInView ( devicePic . rect ( ) , Qt : : KeepAspectRatio ) ;
2010-11-26 23:49:50 +01:00
}
void deviceWidget : : resizeEvent ( QResizeEvent * event )
{
Q_UNUSED ( event ) ;
2012-07-16 14:20:11 +02:00
myDevice - > gVDevice - > fitInView ( devicePic . rect ( ) , Qt : : KeepAspectRatio ) ;
2010-11-26 23:49:50 +01:00
}
2010-11-01 21:50:59 +01:00
void deviceWidget : : setDeviceID ( int devID ) {
deviceID = devID ;
}
2010-11-06 15:17:21 +01:00
void deviceWidget : : setDfu ( DFUObject * dfu )
2010-11-01 21:50:59 +01:00
{
m_dfu = dfu ;
}
/**
Fills the various fields for the device
*/
void deviceWidget : : populate ( )
{
2011-06-19 01:50:38 +02:00
2010-11-01 21:50:59 +01:00
int id = m_dfu - > devices [ deviceID ] . ID ;
2011-06-19 01:50:38 +02:00
myDevice - > lbldevID - > setText ( QString ( " Device ID: " ) + QString : : number ( id , 16 ) ) ;
2010-11-26 23:49:50 +01:00
// DeviceID tells us what sort of HW we have detected:
// display a nice icon:
2011-06-19 01:50:38 +02:00
myDevice - > gVDevice - > scene ( ) - > clear ( ) ;
2011-06-21 19:09:37 +02:00
myDevice - > lblDevName - > setText ( deviceDescriptorStruct : : idToBoardName ( id ) ) ;
2012-01-29 22:49:11 +01:00
myDevice - > lblHWRev - > setText ( QString ( tr ( " HW Revision: " ) ) + QString : : number ( id & 0x00FF , 16 ) ) ;
2011-06-21 19:09:37 +02:00
2010-11-26 23:49:50 +01:00
switch ( id ) {
2011-01-16 21:41:55 +01:00
case 0x0101 :
2012-07-16 14:20:11 +02:00
devicePic . load ( " " ) ; //TODO
break ;
case 0x0201 :
devicePic . load ( " " ) ; //TODO
2010-11-26 23:49:50 +01:00
break ;
2011-01-16 21:41:55 +01:00
case 0x0301 :
2012-07-20 13:55:37 +02:00
devicePic . load ( " :/uploader/images/pipx.png " ) ;
2011-01-15 16:54:04 +01:00
break ;
2011-01-16 21:41:55 +01:00
case 0x0401 :
2012-07-16 14:20:11 +02:00
devicePic . load ( " :/uploader/images/gcs-board-cc.png " ) ;
2011-01-15 16:54:04 +01:00
break ;
2012-05-26 16:46:29 +02:00
case 0x0402 :
2012-07-16 14:20:11 +02:00
devicePic . load ( " :/uploader/images/gcs-board-cc3d.png " ) ;
2010-11-26 23:49:50 +01:00
break ;
default :
break ;
}
2012-07-16 14:20:11 +02:00
myDevice - > gVDevice - > scene ( ) - > addPixmap ( devicePic ) ;
myDevice - > gVDevice - > setSceneRect ( devicePic . rect ( ) ) ;
myDevice - > gVDevice - > fitInView ( devicePic . rect ( ) , Qt : : KeepAspectRatio ) ;
2010-11-26 23:49:50 +01:00
2010-11-01 21:50:59 +01:00
bool r = m_dfu - > devices [ deviceID ] . Readable ;
bool w = m_dfu - > devices [ deviceID ] . Writable ;
2011-06-19 01:50:38 +02:00
2011-06-21 19:09:37 +02:00
myDevice - > lblAccess - > setText ( QString ( " Flash access: " ) + QString ( r ? " R " : " - " ) + QString ( w ? " W " : " - " ) ) ;
2011-06-19 01:50:38 +02:00
myDevice - > lblMaxCode - > setText ( QString ( " Max code size: " ) + QString : : number ( m_dfu - > devices [ deviceID ] . SizeOfCode ) ) ;
2011-06-26 12:18:00 +02:00
myDevice - > lblCRC - > setText ( QString : : number ( m_dfu - > devices [ deviceID ] . FW_CRC ) ) ;
2011-06-21 19:09:37 +02:00
myDevice - > lblBLVer - > setText ( QString ( " BL version: " ) + QString : : number ( m_dfu - > devices [ deviceID ] . BL_Version ) ) ;
2010-11-01 21:50:59 +01:00
int size = ( ( OP_DFU : : device ) m_dfu - > devices [ deviceID ] ) . SizeOfDesc ;
m_dfu - > enterDFU ( deviceID ) ;
2011-05-08 11:36:55 +02:00
QByteArray desc = m_dfu - > DownloadDescriptionAsBA ( size ) ;
2011-06-19 01:50:38 +02:00
if ( ! populateBoardStructuredDescription ( desc ) ) {
2011-06-19 01:56:35 +02:00
//TODO
2011-05-08 11:36:55 +02:00
// desc was not a structured description
QString str = m_dfu - > DownloadDescription ( size ) ;
2011-06-21 19:09:37 +02:00
myDevice - > lblDescription - > setText ( QString ( " Firmware custom description: " ) + str . left ( str . indexOf ( QChar ( 255 ) ) ) ) ;
2011-06-19 01:50:38 +02:00
QPixmap pix = QPixmap ( QString ( " :uploader/images/warning.svg " ) ) ;
myDevice - > lblCertified - > setPixmap ( pix ) ;
myDevice - > lblCertified - > setToolTip ( tr ( " Custom Firmware Build " ) ) ;
myDevice - > lblBuildDate - > setText ( " Warning: development firmware " ) ;
myDevice - > lblGitTag - > setText ( " " ) ;
2011-06-21 19:09:37 +02:00
myDevice - > lblBrdName - > setText ( " " ) ;
2011-05-08 11:36:55 +02:00
}
2010-12-28 22:27:21 +01:00
status ( " Ready... " , STATUSICON_INFO ) ;
2010-11-03 19:46:17 +01:00
}
2010-11-03 22:41:32 +01:00
/**
Freezes the contents of the widget so that a user cannot
try to modify the contents
*/
void deviceWidget : : freeze ( )
{
2013-02-21 01:34:17 +01:00
updateButtons ( false ) ;
}
void deviceWidget : : updateButtons ( bool enabled )
{
if ( ! enabled ) {
myDevice - > description - > setEnabled ( false ) ;
myDevice - > pbLoad - > setEnabled ( false ) ;
2013-03-06 22:01:05 +01:00
myDevice - > confirmCheckBox - > setEnabled ( false ) ;
2013-02-21 01:34:17 +01:00
myDevice - > updateButton - > setEnabled ( false ) ;
myDevice - > retrieveButton - > setEnabled ( false ) ;
}
else {
myDevice - > description - > setEnabled ( true ) ;
2013-03-06 22:01:05 +01:00
// Load button (i.e. choose file) is always enabled
2013-02-21 01:34:17 +01:00
myDevice - > pbLoad - > setEnabled ( true ) ;
2013-03-06 22:01:05 +01:00
myDevice - > confirmCheckBox - > setEnabled ( true ) ;
// Update/Upload button is enabled if the "I know what I'm doing!" check box is checked
myDevice - > updateButton - > setEnabled ( myDevice - > confirmCheckBox - > checkState ( ) = = Qt : : Checked ) ;
2013-02-21 01:34:17 +01:00
// Retreive/Download button is always enabled
myDevice - > retrieveButton - > setEnabled ( true ) ;
}
2010-11-03 22:41:32 +01:00
}
2011-05-08 11:36:55 +02:00
/**
Populates the widget field with the description in case
it is structured properly
*/
2011-06-19 01:50:38 +02:00
bool deviceWidget : : populateBoardStructuredDescription ( QByteArray desc )
2011-05-08 11:36:55 +02:00
{
2012-09-17 18:08:15 +02:00
if ( UAVObjectUtilManager : : descriptionToStructure ( desc , onBoardDescription ) )
2011-06-19 01:56:35 +02:00
{
2012-01-15 14:36:52 +01:00
myDevice - > lblGitTag - > setText ( onBoardDescription . gitHash ) ;
myDevice - > lblBuildDate - > setText ( onBoardDescription . gitDate . insert ( 4 , " - " ) . insert ( 7 , " - " ) ) ;
2012-10-22 22:44:01 +02:00
if ( onBoardDescription . gitTag . startsWith ( " RELEASE " , Qt : : CaseSensitive ) )
2011-06-19 01:56:35 +02:00
{
2012-10-26 21:58:19 +02:00
myDevice - > lblDescription - > setText ( onBoardDescription . gitTag ) ;
2011-06-19 01:56:35 +02:00
QPixmap pix = QPixmap ( QString ( " :uploader/images/application-certificate.svg " ) ) ;
myDevice - > lblCertified - > setPixmap ( pix ) ;
2011-08-14 13:28:42 +02:00
myDevice - > lblCertified - > setToolTip ( tr ( " Tagged officially released firmware build " ) ) ;
2011-06-19 01:56:35 +02:00
}
else
{
2012-01-15 14:36:52 +01:00
myDevice - > lblDescription - > setText ( onBoardDescription . gitTag ) ;
2011-06-19 01:56:35 +02:00
QPixmap pix = QPixmap ( QString ( " :uploader/images/warning.svg " ) ) ;
myDevice - > lblCertified - > setPixmap ( pix ) ;
2011-08-14 13:28:42 +02:00
myDevice - > lblCertified - > setToolTip ( tr ( " Untagged or custom firmware build " ) ) ;
2011-06-19 01:56:35 +02:00
}
2012-05-26 16:46:29 +02:00
myDevice - > lblBrdName - > setText ( deviceDescriptorStruct : : idToBoardName ( onBoardDescription . boardType < < 8 | onBoardDescription . boardRevision ) ) ;
2011-06-19 01:56:35 +02:00
return true ;
}
return false ;
2011-05-08 11:36:55 +02:00
}
2011-06-19 01:50:38 +02:00
bool deviceWidget : : populateLoadedStructuredDescription ( QByteArray desc )
{
2012-09-17 18:08:15 +02:00
if ( UAVObjectUtilManager : : descriptionToStructure ( desc , LoadedDescription ) )
2011-06-19 01:56:35 +02:00
{
2012-01-15 14:36:52 +01:00
myDevice - > lblGitTagL - > setText ( LoadedDescription . gitHash ) ;
myDevice - > lblBuildDateL - > setText ( LoadedDescription . gitDate . insert ( 4 , " - " ) . insert ( 7 , " - " ) ) ;
2012-10-22 22:44:01 +02:00
if ( LoadedDescription . gitTag . startsWith ( " RELEASE " , Qt : : CaseSensitive ) )
2011-06-19 01:56:35 +02:00
{
2012-01-15 14:36:52 +01:00
myDevice - > lblDescritpionL - > setText ( LoadedDescription . gitTag ) ;
myDevice - > description - > setText ( LoadedDescription . gitTag ) ;
2011-06-19 01:56:35 +02:00
QPixmap pix = QPixmap ( QString ( " :uploader/images/application-certificate.svg " ) ) ;
myDevice - > lblCertifiedL - > setPixmap ( pix ) ;
2011-08-14 13:28:42 +02:00
myDevice - > lblCertifiedL - > setToolTip ( tr ( " Tagged officially released firmware build " ) ) ;
2011-06-19 01:56:35 +02:00
}
else
{
2012-01-15 14:36:52 +01:00
myDevice - > lblDescritpionL - > setText ( LoadedDescription . gitTag ) ;
myDevice - > description - > setText ( LoadedDescription . gitTag ) ;
2011-06-19 01:56:35 +02:00
QPixmap pix = QPixmap ( QString ( " :uploader/images/warning.svg " ) ) ;
myDevice - > lblCertifiedL - > setPixmap ( pix ) ;
2011-08-14 13:28:42 +02:00
myDevice - > lblCertifiedL - > setToolTip ( tr ( " Untagged or custom firmware build " ) ) ;
2011-06-19 01:56:35 +02:00
}
2012-05-26 16:46:29 +02:00
myDevice - > lblBrdNameL - > setText ( deviceDescriptorStruct : : idToBoardName ( LoadedDescription . boardType < < 8 | LoadedDescription . boardRevision ) ) ;
2011-06-19 01:56:35 +02:00
return true ;
}
return false ;
2011-05-08 11:36:55 +02:00
2011-06-19 01:50:38 +02:00
}
2010-12-28 22:27:21 +01:00
/**
Updates status message for messages coming from DFU
*/
void deviceWidget : : dfuStatus ( QString str )
{
status ( str , STATUSICON_RUNNING ) ;
}
2011-06-19 01:50:38 +02:00
void deviceWidget : : confirmCB ( int value )
{
2013-02-21 01:34:17 +01:00
updateButtons ( true ) ;
2011-06-19 01:50:38 +02:00
}
2010-11-03 22:41:32 +01:00
/**
Updates status message
*/
2010-12-28 22:27:21 +01:00
void deviceWidget : : status ( QString str , StatusIcon ic )
2010-11-03 22:41:32 +01:00
{
2010-12-28 22:27:21 +01:00
QPixmap px ;
2010-11-03 22:41:32 +01:00
myDevice - > statusLabel - > setText ( str ) ;
2010-12-28 22:27:21 +01:00
switch ( ic ) {
case STATUSICON_RUNNING :
px . load ( QString ( " :/uploader/images/system-run.svg " ) ) ;
break ;
case STATUSICON_OK :
px . load ( QString ( " :/uploader/images/dialog-apply.svg " ) ) ;
break ;
case STATUSICON_FAIL :
px . load ( QString ( " :/uploader/images/process-stop.svg " ) ) ;
break ;
default :
px . load ( QString ( " :/uploader/images/gtk-info.svg " ) ) ;
}
myDevice - > statusIcon - > setPixmap ( px ) ;
2010-11-03 22:41:32 +01:00
}
2011-06-19 01:50:38 +02:00
void deviceWidget : : loadFirmware ( )
2010-11-03 19:46:17 +01:00
{
2011-06-21 19:09:37 +02:00
myDevice - > verticalGroupBox_loaded - > setVisible ( false ) ;
myDevice - > groupCustom - > setVisible ( false ) ;
2011-06-19 01:50:38 +02:00
filename = setOpenFileName ( ) ;
2013-03-06 22:01:05 +01:00
myDevice - > confirmCheckBox - > setVisible ( false ) ;
myDevice - > confirmCheckBox - > setChecked ( false ) ;
2011-06-19 01:50:38 +02:00
if ( filename . isEmpty ( ) ) {
status ( " Empty filename " , STATUSICON_FAIL ) ;
return ;
}
QFile file ( filename ) ;
if ( ! file . open ( QIODevice : : ReadOnly ) ) {
status ( " Can't open file " , STATUSICON_FAIL ) ;
return ;
}
loadedFW = file . readAll ( ) ;
2013-03-06 22:01:05 +01:00
2011-06-19 01:50:38 +02:00
QByteArray desc = loadedFW . right ( 100 ) ;
QPixmap px ;
2013-03-06 22:01:05 +01:00
if ( loadedFW . length ( ) > m_dfu - > devices [ deviceID ] . SizeOfCode ) {
2011-08-23 12:25:28 +02:00
myDevice - > lblCRCL - > setText ( tr ( " Can't calculate, file too big for device " ) ) ;
2013-03-06 22:01:05 +01:00
}
else {
2011-08-23 12:25:28 +02:00
myDevice - > lblCRCL - > setText ( QString : : number ( DFUObject : : CRCFromQBArray ( loadedFW , m_dfu - > devices [ deviceID ] . SizeOfCode ) ) ) ;
2013-03-06 22:01:05 +01:00
}
2011-06-26 12:18:00 +02:00
//myDevice->lblFirmwareSizeL->setText(QString("Firmware size: ")+QVariant(loadedFW.length()).toString()+ QString(" bytes"));
2011-06-19 01:50:38 +02:00
if ( populateLoadedStructuredDescription ( desc ) )
{
2013-03-06 22:01:05 +01:00
myDevice - > confirmCheckBox - > setChecked ( true ) ;
2011-06-19 01:50:38 +02:00
myDevice - > verticalGroupBox_loaded - > setVisible ( true ) ;
2011-06-21 19:09:37 +02:00
myDevice - > groupCustom - > setVisible ( false ) ;
2013-03-06 22:01:05 +01:00
if ( myDevice - > lblCRC - > text ( ) = = myDevice - > lblCRCL - > text ( ) ) {
myDevice - > statusLabel - > setText ( tr ( " The board has the same firmware as loaded. No need to update. " ) ) ;
2011-06-19 01:50:38 +02:00
px . load ( QString ( " :/uploader/images/warning.svg " ) ) ;
2013-03-06 22:01:05 +01:00
} else if ( myDevice - > lblDevName - > text ( ) ! = myDevice - > lblBrdNameL - > text ( ) ) {
2011-08-14 13:28:42 +02:00
myDevice - > statusLabel - > setText ( tr ( " WARNING: the loaded firmware is for different hardware. Do not update! " ) ) ;
px . load ( QString ( " :/uploader/images/error.svg " ) ) ;
2013-03-06 22:01:05 +01:00
} else if ( QDateTime : : fromString ( onBoardDescription . gitDate ) > QDateTime : : fromString ( LoadedDescription . gitDate ) ) {
2011-08-14 13:28:42 +02:00
myDevice - > statusLabel - > setText ( tr ( " The board has newer firmware than loaded. Are you sure you want to update? " ) ) ;
2011-06-19 01:50:38 +02:00
px . load ( QString ( " :/uploader/images/warning.svg " ) ) ;
2013-03-06 22:01:05 +01:00
} else if ( ! LoadedDescription . gitTag . startsWith ( " RELEASE " , Qt : : CaseSensitive ) ) {
myDevice - > statusLabel - > setText ( tr ( " The loaded firmware is untagged or custom build. Update only if it was received from a trusted source (official website or your own build). " ) ) ;
2011-06-19 01:50:38 +02:00
px . load ( QString ( " :/uploader/images/warning.svg " ) ) ;
2013-03-06 22:01:05 +01:00
} else {
myDevice - > statusLabel - > setText ( tr ( " This is the tagged officially released OpenPilot firmware. " ) ) ;
2011-06-19 01:50:38 +02:00
px . load ( QString ( " :/uploader/images/gtk-info.svg " ) ) ;
}
2013-03-06 22:01:05 +01:00
} else {
myDevice - > statusLabel - > setText ( tr ( " WARNING: the loaded firmware was not packaged with the OpenPilot format. Do not update unless you know what you are doing. " ) ) ;
2011-08-14 13:28:42 +02:00
px . load ( QString ( " :/uploader/images/error.svg " ) ) ;
2013-03-06 22:01:05 +01:00
myDevice - > confirmCheckBox - > setChecked ( false ) ;
myDevice - > confirmCheckBox - > setVisible ( true ) ;
2011-06-19 01:50:38 +02:00
myDevice - > verticalGroupBox_loaded - > setVisible ( false ) ;
2011-06-21 19:09:37 +02:00
myDevice - > groupCustom - > setVisible ( true ) ;
2011-06-19 01:50:38 +02:00
}
myDevice - > statusIcon - > setPixmap ( px ) ;
2010-11-03 19:46:17 +01:00
}
/**
Sends a firmware to the device
*/
void deviceWidget : : uploadFirmware ( )
{
2013-02-21 01:34:17 +01:00
// clear progress bar now
// this avoids displaying an error message and the progress at 100% at the same time
setProgress ( 0 ) ;
updateButtons ( false ) ;
2010-11-03 19:46:17 +01:00
if ( ! m_dfu - > devices [ deviceID ] . Writable ) {
2010-12-28 22:27:21 +01:00
status ( " Device not writable! " , STATUSICON_FAIL ) ;
2013-02-21 01:34:17 +01:00
updateButtons ( true ) ;
2010-11-03 19:46:17 +01:00
return ;
}
2010-11-26 11:44:05 +01:00
bool verify = false ;
2010-12-14 00:44:31 +01:00
/* TODO: does not work properly on current Bootloader!
2010-11-26 11:44:05 +01:00
if ( m_dfu - > devices [ deviceID ] . Readable )
verify = true ;
2010-12-14 00:44:31 +01:00
*/
2010-11-26 11:44:05 +01:00
2011-06-19 01:50:38 +02:00
QByteArray desc = loadedFW . right ( 100 ) ;
if ( desc . startsWith ( " OpFw " ) ) {
2011-05-08 11:36:55 +02:00
descriptionArray = desc ;
2011-05-08 21:22:00 +02:00
// Now do sanity checking:
// - Check whether board type matches firmware:
int board = m_dfu - > devices [ deviceID ] . ID ;
int firmwareBoard = ( ( desc . at ( 12 ) & 0xff ) < < 8 ) + ( desc . at ( 13 ) & 0xff ) ;
2012-03-22 06:28:23 +01:00
if ( ( board = = 0x401 & & firmwareBoard = = 0x402 ) | |
2012-10-02 16:37:55 +02:00
( board = = 0x901 & & firmwareBoard = = 0x902 ) | | // L3GD20 revo supports Revolution firmware
( board = = 0x902 & & firmwareBoard = = 0x903 ) ) // RevoMini1 supporetd by RevoMini2 firmware
{
2012-01-24 23:58:56 +01:00
// These firmwares are designed to be backwards compatible
} else if ( firmwareBoard ! = board ) {
2011-05-08 21:22:00 +02:00
status ( " Error: firmware does not match board " , STATUSICON_FAIL ) ;
2013-02-21 01:34:17 +01:00
updateButtons ( true ) ;
2011-05-08 21:22:00 +02:00
return ;
}
// Check the firmware embedded in the file:
QByteArray firmwareHash = desc . mid ( 40 , 20 ) ;
2011-06-19 01:50:38 +02:00
QByteArray fileHash = QCryptographicHash : : hash ( loadedFW . left ( loadedFW . length ( ) - 100 ) , QCryptographicHash : : Sha1 ) ;
2011-05-08 21:22:00 +02:00
if ( firmwareHash ! = fileHash ) {
status ( " Error: firmware file corrupt " , STATUSICON_FAIL ) ;
2013-02-21 01:34:17 +01:00
updateButtons ( true ) ;
2011-05-08 21:22:00 +02:00
return ;
}
2011-05-08 00:57:59 +02:00
} else {
2011-05-08 21:22:00 +02:00
// The firmware is not packaged, just upload the text in the description field
// if it is there.
2011-05-08 11:36:55 +02:00
descriptionArray . clear ( ) ;
2011-05-08 00:57:59 +02:00
}
2010-12-28 22:27:21 +01:00
status ( " Starting firmware upload " , STATUSICON_RUNNING ) ;
2013-02-21 01:34:17 +01:00
emit uploadStarted ( ) ;
2010-11-22 23:31:59 +01:00
// We don't know which device was used previously, so we
// are cautious and reenter DFU for this deviceID:
if ( ! m_dfu - > enterDFU ( deviceID ) )
{
2012-05-07 16:11:51 +02:00
emit uploadEnded ( false ) ;
2013-02-21 01:34:17 +01:00
status ( " Error:Could not enter DFU mode " , STATUSICON_FAIL ) ;
updateButtons ( true ) ;
2010-11-22 23:31:59 +01:00
return ;
}
OP_DFU : : Status ret = m_dfu - > StatusRequest ( ) ;
qDebug ( ) < < m_dfu - > StatusToString ( ret ) ;
m_dfu - > AbortOperation ( ) ; // Necessary, otherwise I get random failures.
2010-11-03 22:41:32 +01:00
connect ( m_dfu , SIGNAL ( progressUpdated ( int ) ) , this , SLOT ( setProgress ( int ) ) ) ;
2010-12-28 22:27:21 +01:00
connect ( m_dfu , SIGNAL ( operationProgress ( QString ) ) , this , SLOT ( dfuStatus ( QString ) ) ) ;
2010-11-06 15:17:21 +01:00
connect ( m_dfu , SIGNAL ( uploadFinished ( OP_DFU : : Status ) ) , this , SLOT ( uploadFinished ( OP_DFU : : Status ) ) ) ;
2013-02-21 01:34:17 +01:00
bool retstatus = m_dfu - > UploadFirmware ( filename , verify , deviceID ) ;
if ( ! retstatus ) {
2012-05-07 16:11:51 +02:00
emit uploadEnded ( false ) ;
2013-02-21 01:34:17 +01:00
status ( " Could not start upload! " , STATUSICON_FAIL ) ;
updateButtons ( true ) ;
2010-11-03 22:41:32 +01:00
return ;
}
2013-02-21 01:34:17 +01:00
2010-12-28 22:27:21 +01:00
status ( " Uploading, please wait... " , STATUSICON_RUNNING ) ;
2010-11-03 19:46:17 +01:00
}
/**
Retrieves the firmware from the device
*/
void deviceWidget : : downloadFirmware ( )
{
2013-02-21 01:34:17 +01:00
// clear progress bar now
// this avoids displaying an error message and the progress at 100% at the same time
setProgress ( 0 ) ;
updateButtons ( false ) ;
2010-11-03 19:46:17 +01:00
if ( ! m_dfu - > devices [ deviceID ] . Readable ) {
myDevice - > statusLabel - > setText ( QString ( " Device not readable! " ) ) ;
2013-02-21 01:34:17 +01:00
status ( " Device not readable! " , STATUSICON_FAIL ) ;
updateButtons ( true ) ;
2010-11-03 19:46:17 +01:00
return ;
}
2010-11-01 21:50:59 +01:00
2011-04-29 22:50:58 +02:00
filename = setSaveFileName ( ) ;
2010-11-06 15:17:21 +01:00
if ( filename . isEmpty ( ) ) {
2010-12-28 22:27:21 +01:00
status ( " Empty filename " , STATUSICON_FAIL ) ;
2013-02-21 01:34:17 +01:00
updateButtons ( true ) ;
2010-11-06 15:17:21 +01:00
return ;
}
2013-02-21 01:34:17 +01:00
status ( " Starting firmware download " , STATUSICON_RUNNING ) ;
emit downloadStarted ( ) ;
2010-11-06 15:17:21 +01:00
connect ( m_dfu , SIGNAL ( progressUpdated ( int ) ) , this , SLOT ( setProgress ( int ) ) ) ;
connect ( m_dfu , SIGNAL ( downloadFinished ( ) ) , this , SLOT ( downloadFinished ( ) ) ) ;
2013-02-21 01:34:17 +01:00
2010-11-06 15:17:21 +01:00
downloadedFirmware . clear ( ) ; // Empty the byte array
bool ret = m_dfu - > DownloadFirmware ( & downloadedFirmware , deviceID ) ;
2013-02-21 01:34:17 +01:00
if ( ! ret ) {
emit downloadEnded ( false ) ;
2010-12-28 22:27:21 +01:00
status ( " Could not start download! " , STATUSICON_FAIL ) ;
2013-02-21 01:34:17 +01:00
updateButtons ( true ) ;
2010-11-06 15:17:21 +01:00
return ;
}
2013-02-21 01:34:17 +01:00
status ( " Downloading, please wait... " , STATUSICON_RUNNING ) ;
return ;
2010-11-06 15:17:21 +01:00
}
/**
Callback for the firmware download result
*/
void deviceWidget : : downloadFinished ( )
{
disconnect ( m_dfu , SIGNAL ( downloadFinished ( ) ) , this , SLOT ( downloadFinished ( ) ) ) ;
2010-11-27 14:44:16 +01:00
disconnect ( m_dfu , SIGNAL ( progressUpdated ( int ) ) , this , SLOT ( setProgress ( int ) ) ) ;
2013-02-21 01:34:17 +01:00
2010-11-06 15:17:21 +01:00
// Now save the result (use the utility function from OP_DFU)
m_dfu - > SaveByteArrayToFile ( filename , downloadedFirmware ) ;
2013-02-21 01:34:17 +01:00
emit downloadEnded ( true ) ;
status ( " Download successful " , STATUSICON_OK ) ;
updateButtons ( true ) ;
2010-11-01 21:50:59 +01:00
}
2010-11-03 22:41:32 +01:00
2010-11-06 15:17:21 +01:00
/**
Callback for the firmware upload result
*/
void deviceWidget : : uploadFinished ( OP_DFU : : Status retstatus )
{
disconnect ( m_dfu , SIGNAL ( uploadFinished ( OP_DFU : : Status ) ) , this , SLOT ( uploadFinished ( OP_DFU : : Status ) ) ) ;
2010-11-27 14:44:16 +01:00
disconnect ( m_dfu , SIGNAL ( progressUpdated ( int ) ) , this , SLOT ( setProgress ( int ) ) ) ;
2011-01-08 14:22:15 +01:00
disconnect ( m_dfu , SIGNAL ( operationProgress ( QString ) ) , this , SLOT ( dfuStatus ( QString ) ) ) ;
2013-02-21 01:34:17 +01:00
if ( retstatus ! = OP_DFU : : Last_operation_Success ) {
2012-05-07 16:11:51 +02:00
emit uploadEnded ( false ) ;
2013-02-21 01:34:17 +01:00
status ( QString ( " Upload failed with code: " ) + m_dfu - > StatusToString ( retstatus ) . toLatin1 ( ) . data ( ) , STATUSICON_FAIL ) ;
updateButtons ( true ) ;
2010-11-06 15:17:21 +01:00
return ;
} else
2011-06-19 01:56:35 +02:00
if ( ! descriptionArray . isEmpty ( ) ) {
// We have a structured array to save
status ( QString ( " Updating description " ) , STATUSICON_RUNNING ) ;
repaint ( ) ; // Make sure the text above shows right away
retstatus = m_dfu - > UploadDescription ( descriptionArray ) ;
2013-02-21 01:34:17 +01:00
if ( retstatus ! = OP_DFU : : Last_operation_Success ) {
2012-05-07 16:11:51 +02:00
emit uploadEnded ( false ) ;
2013-02-21 01:34:17 +01:00
status ( QString ( " Upload failed with code: " ) + m_dfu - > StatusToString ( retstatus ) . toLatin1 ( ) . data ( ) , STATUSICON_FAIL ) ;
updateButtons ( true ) ;
2011-06-19 01:56:35 +02:00
return ;
}
} else if ( ! myDevice - > description - > text ( ) . isEmpty ( ) ) {
// Fallback: we save the description field:
status ( QString ( " Updating description " ) , STATUSICON_RUNNING ) ;
repaint ( ) ; // Make sure the text above shows right away
retstatus = m_dfu - > UploadDescription ( myDevice - > description - > text ( ) ) ;
2013-02-21 01:34:17 +01:00
if ( retstatus ! = OP_DFU : : Last_operation_Success ) {
2012-05-07 16:11:51 +02:00
emit uploadEnded ( false ) ;
2013-02-21 01:34:17 +01:00
status ( QString ( " Upload failed with code: " ) + m_dfu - > StatusToString ( retstatus ) . toLatin1 ( ) . data ( ) , STATUSICON_FAIL ) ;
updateButtons ( true ) ;
2011-06-19 01:56:35 +02:00
return ;
}
2010-11-06 15:17:21 +01:00
}
2013-02-21 01:34:17 +01:00
2011-06-21 19:09:37 +02:00
populate ( ) ;
2013-02-21 01:34:17 +01:00
2012-05-07 16:11:51 +02:00
emit uploadEnded ( true ) ;
2010-12-28 22:27:21 +01:00
status ( " Upload successful " , STATUSICON_OK ) ;
2013-02-21 01:34:17 +01:00
updateButtons ( true ) ;
2010-11-06 15:17:21 +01:00
}
2010-11-03 22:41:32 +01:00
/**
Slot to update the progress bar
*/
void deviceWidget : : setProgress ( int percent )
{
myDevice - > progressBar - > setValue ( percent ) ;
}
/**
2012-04-22 04:03:43 +02:00
* Opens an open file dialog .
*/
2010-11-03 22:41:32 +01:00
QString deviceWidget : : setOpenFileName ( )
{
QFileDialog : : Options options ;
QString selectedFilter ;
2012-04-22 04:03:43 +02:00
QString fwDirectoryStr ;
QDir fwDirectory ;
//Format filename for file chooser
# ifdef Q_OS_WIN
fwDirectoryStr = QCoreApplication : : applicationDirPath ( ) ;
fwDirectory = QDir ( fwDirectoryStr ) ;
fwDirectory . cdUp ( ) ;
fwDirectory . cd ( " firmware " ) ;
fwDirectoryStr = fwDirectory . absolutePath ( ) ;
# elif defined Q_OS_LINUX
fwDirectoryStr = QCoreApplication : : applicationDirPath ( ) ;
fwDirectory = QDir ( fwDirectoryStr ) ;
fwDirectory . cd ( " ../../.. " ) ;
fwDirectoryStr = fwDirectory . absolutePath ( ) ;
fwDirectoryStr = fwDirectoryStr + " /fw_ " + myDevice - > lblBrdName - > text ( ) . toLower ( ) + " /fw_ " + myDevice - > lblBrdName - > text ( ) . toLower ( ) + " .opfw " ;
# elif defined Q_OS_MAC
fwDirectoryStr = QCoreApplication : : applicationDirPath ( ) ;
fwDirectory = QDir ( fwDirectoryStr ) ;
fwDirectory . cd ( " ../../../../../.. " ) ;
fwDirectoryStr = fwDirectory . absolutePath ( ) ;
fwDirectoryStr = fwDirectoryStr + " /fw_ " + myDevice - > lblBrdName - > text ( ) . toLower ( ) + " /fw_ " + myDevice - > lblBrdName - > text ( ) . toLower ( ) + " .opfw " ;
# endif
2010-11-03 22:41:32 +01:00
QString fileName = QFileDialog : : getOpenFileName ( this ,
tr ( " Select firmware file " ) ,
2012-04-22 04:03:43 +02:00
fwDirectoryStr ,
2011-08-14 13:28:42 +02:00
tr ( " Firmware Files (*.opfw *.bin) " ) ,
2010-11-03 22:41:32 +01:00
& selectedFilter ,
options ) ;
return fileName ;
}
2012-04-22 04:03:43 +02:00
/**
* Set the save file name
*/
2011-04-29 22:50:58 +02:00
QString deviceWidget : : setSaveFileName ( )
{
QFileDialog : : Options options ;
QString selectedFilter ;
QString fileName = QFileDialog : : getSaveFileName ( this ,
tr ( " Select firmware file " ) ,
" " ,
tr ( " Firmware Files (*.bin) " ) ,
& selectedFilter ,
options ) ;
return fileName ;
}