1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-12-03 11:24:10 +01:00

OP-21/Flight Bootloader PC APP- More command line parameters handled.

git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@1531 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
zedamota 2010-09-05 00:18:49 +00:00 committed by zedamota
parent 492227c916
commit 70715ba0e2
4 changed files with 291 additions and 196 deletions

View File

@ -9,6 +9,7 @@ int main(int argc, char *argv[])
QCoreApplication a(argc, argv); QCoreApplication a(argc, argv);
if(argc>1) if(argc>1)
{ {
bool verify;
bool debug=false; bool debug=false;
OP_DFU::Actions action; OP_DFU::Actions action;
QString file; QString file;
@ -26,6 +27,7 @@ int main(int argc, char *argv[])
cout<<"| \n"; cout<<"| \n";
cout<<"| -ls : lists available devices\n"; cout<<"| -ls : lists available devices\n";
cout<<"| -p <file> : program hw (requires:-d - optionals:-v,-w)\n"; cout<<"| -p <file> : program hw (requires:-d - optionals:-v,-w)\n";
cout<<"| -v : verify (requires:-d)\n";
cout<<"| -dn <file> : download firmware to file (requires:-d)\n"; cout<<"| -dn <file> : download firmware to file (requires:-d)\n";
cout<<"| -dd <file> : download discription (requires:-d)\n"; cout<<"| -dd <file> : download discription (requires:-d)\n";
cout<<"| -d <number Of Device : (requires: -p or -dn)\n"; cout<<"| -d <number Of Device : (requires: -p or -dn)\n";
@ -37,7 +39,7 @@ int main(int argc, char *argv[])
} }
else if(args.contains("-dd")) else if(args.contains(DOWNDESCRIPTION))
{ {
action=OP_DFU::downdesc; action=OP_DFU::downdesc;
if(args.contains(DEVICE)) if(args.contains(DEVICE))
@ -48,13 +50,19 @@ int main(int argc, char *argv[])
} }
} }
} }
else if(args.contains("-p")) else if(args.contains(PROGRAMFW))
{ {
if(args.contains("-w")) if(args.contains(VERIFY))
{ {
if(args.indexOf("-w")+1<args.length()) verify=true;
}
else
verify=false;
if(args.contains(PROGRAMDESC))
{ {
description=(args[args.indexOf("-w")+1]); if(args.indexOf(PROGRAMDESC)+1<args.length())
{
description=(args[args.indexOf(PROGRAMDESC)+1]);
} }
} }
@ -65,9 +73,9 @@ int main(int argc, char *argv[])
{ {
device=(args[args.indexOf(DEVICE)+1]).toInt(); device=(args[args.indexOf(DEVICE)+1]).toInt();
} }
if(args.indexOf("-p")+1<args.length()) if(args.indexOf(PROGRAMFW)+1<args.length())
{ {
file=args[args.indexOf("-p")+1]; file=args[args.indexOf(PROGRAMFW)+1];
} }
} }
else else
@ -75,20 +83,20 @@ int main(int argc, char *argv[])
cout<<("Device not specified\n"); cout<<("Device not specified\n");
return -1; return -1;
} }
if(args.contains("-v")) if(args.contains(VERIFY))
action=OP_DFU::programandverify; action=OP_DFU::programandverify;
} }
else if(args.contains("-ch") || args.contains("-ca")) else if(args.contains(COMPAREHASH) || args.contains(COMPAREALL))
{ {
int index; int index;
if(args.contains("-ch")) if(args.contains(COMPAREHASH))
{ {
index=args.indexOf("-ch"); index=args.indexOf(COMPAREHASH);
action=OP_DFU::comparehash; action=OP_DFU::comparehash;
} }
else else
{ {
index=args.indexOf("-ca"); index=args.indexOf(COMPAREALL);
action=OP_DFU::compareall; action=OP_DFU::compareall;
} }
if(args.contains(DEVICE)) if(args.contains(DEVICE))
@ -131,11 +139,11 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
} }
else if(args.contains("-s")) else if(args.contains(STATUSREQUEST))
{ {
action=OP_DFU::statusreq; action=OP_DFU::statusreq;
} }
else if(args.contains("-ls")) else if(args.contains(LISTDEVICES))
{ {
action=OP_DFU::listdevs; action=OP_DFU::listdevs;
} }
@ -145,7 +153,7 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
// qDebug()<<"Action="<<(int)action; // qDebug()<<"Action="<<(int)action;
// qDebug()<<"File="<<file; // qDebug()<<"File="<<file;
// qDebug()<<"Device="<<device; // qDebug()<<"Device="<<device;
// qDebug()<<"Action="<<action; // qDebug()<<"Action="<<action;
@ -163,13 +171,15 @@ int main(int argc, char *argv[])
cout<<"Found "<<dfu.numberOfDevices<<"\n"; cout<<"Found "<<dfu.numberOfDevices<<"\n";
for(int x=0;x<dfu.numberOfDevices;++x) for(int x=0;x<dfu.numberOfDevices;++x)
{ {
cout<<"Device #"<<x+1<<"\n"; cout<<"Device #"<<x<<"\n";
cout<<"Device ID="<<dfu.devices[x].ID<<"\n"; cout<<"Device ID="<<dfu.devices[x].ID<<"\n";
cout<<"Device Readable="<<dfu.devices[x].Readable<<"\n"; cout<<"Device Readable="<<dfu.devices[x].Readable<<"\n";
cout<<"Device Writable="<<dfu.devices[x].Writable<<"\n"; cout<<"Device Writable="<<dfu.devices[x].Writable<<"\n";
cout<<"Device SizeOfCode="<<dfu.devices[x].SizeOfCode<<"\n"; cout<<"Device SizeOfCode="<<dfu.devices[x].SizeOfCode<<"\n";
cout<<"Device SizeOfHash="<<dfu.devices[x].SizeOfHash<<"\n"; cout<<"Device SizeOfHash="<<dfu.devices[x].SizeOfHash<<"\n";
cout<<"Device SizeOfDesc="<<dfu.devices[x].SizeOfDesc<<"\n"; cout<<"Device SizeOfDesc="<<dfu.devices[x].SizeOfDesc<<"\n";
int size=((OP_DFU::device)dfu.devices[x]).SizeOfDesc;
cout<<"Description:"<<dfu.DownloadDescription(size).toLatin1().data()<<"\n";
cout<<"\n"; cout<<"\n";
} }
return 0; return 0;
@ -186,7 +196,7 @@ int main(int argc, char *argv[])
cout<<"Error:Could not enter DFU mode\n"; cout<<"Error:Could not enter DFU mode\n";
return -1; return -1;
} }
OP_DFU::Status retstatus=dfu.UploadFirmware(file.toAscii()); OP_DFU::Status retstatus=dfu.UploadFirmware(file.toAscii(),verify);
if(retstatus!=OP_DFU::Last_operation_Success) if(retstatus!=OP_DFU::Last_operation_Success)
{ {
cout<<"Upload failed with code:"<<dfu.StatusToString(retstatus).data(); cout<<"Upload failed with code:"<<dfu.StatusToString(retstatus).data();
@ -205,7 +215,19 @@ int main(int argc, char *argv[])
} }
else if (action==OP_DFU::download) else if (action==OP_DFU::download)
{ {
if(device>dfu.numberOfDevices)
{
cout<<"Error:Invalid Device";
return -1;
}
if(!dfu.enterDFU(device))
{
cout<<"Error:Could not enter DFU mode\n";
return -1;
}
qint32 size=((OP_DFU::device)dfu.devices[device]).SizeOfCode; qint32 size=((OP_DFU::device)dfu.devices[device]).SizeOfCode;
bool ret=dfu.SaveByteArrayToFile(file.toAscii(),dfu.StartDownload(size,OP_DFU::FW));
return ret;
} }
else if(action==OP_DFU::downdesc) else if(action==OP_DFU::downdesc)
@ -213,11 +235,12 @@ int main(int argc, char *argv[])
int size=((OP_DFU::device)dfu.devices[device]).SizeOfDesc; int size=((OP_DFU::device)dfu.devices[device]).SizeOfDesc;
cout<<"Description:"<<dfu.DownloadDescription(size).toLatin1().data(); cout<<"Description:"<<dfu.DownloadDescription(size).toLatin1().data();
} }
} }
return 0; return 0;
} }
OP_DFU dfu(true); OP_DFU dfu(true);
dfu.enterDFU(0); dfu.enterDFU(0);
// dfu.enterDFU(1); // dfu.enterDFU(1);
// dfu.StartUpload(4,OP_DFU::Descript); // dfu.StartUpload(4,OP_DFU::Descript);
@ -236,10 +259,10 @@ OP_DFU dfu(true);
// dfu.JumpToApp(); // dfu.JumpToApp();
// dfu.findDevices(); // dfu.findDevices();
// dfu.UploadFirmware(filename.toAscii()); dfu.UploadFirmware("c:/openpilot.bin",true);
// dfu.UploadDescription("josemanuel"); // dfu.UploadDescription("josemanuel");
QString str=dfu.DownloadDescription(12); // QString str=dfu.DownloadDescription(12);
// dfu.JumpToApp(); // dfu.JumpToApp();
qDebug()<<"Description="<<str; // qDebug()<<"Description="<<str;
return a.exec(); return a.exec();
} }

View File

@ -5,8 +5,8 @@
OP_DFU::OP_DFU(bool _debug): debug(_debug) OP_DFU::OP_DFU(bool _debug): debug(_debug)
{ {
QWaitCondition sleep; QWaitCondition sleep;
QMutex mutex; QMutex mutex;
int numDevices=0; int numDevices=0;
cout<<"Please connect device now\n"; cout<<"Please connect device now\n";
int count=0; int count=0;
@ -28,6 +28,21 @@ QMutex mutex;
if(debug) if(debug)
qDebug() << numDevices << " device(s) opened"; qDebug() << numDevices << " device(s) opened";
} }
bool OP_DFU::SaveByteArrayToFile(QString sfile, const QByteArray &array)
{
QFile file(sfile);
//QFile file("in.txt");
if (!file.open(QIODevice::WriteOnly))
{
if(debug)
qDebug()<<"Cant open file";
return false;
}
file.write(array);
file.close();
return true;
}
bool OP_DFU::enterDFU(int devNumber) bool OP_DFU::enterDFU(int devNumber)
{ {
char buf[BUF_LEN]; char buf[BUF_LEN];
@ -104,8 +119,14 @@ bool OP_DFU::UploadData(qint32 numberOfBytes, QByteArray data)
buf[0] =0x02;//reportID buf[0] =0x02;//reportID
buf[1] = OP_DFU::Upload;//DFU Command buf[1] = OP_DFU::Upload;//DFU Command
int packetsize; int packetsize;
float percentage;
int laspercentage;
for(qint32 packetcount=0;packetcount<numberOfPackets;++packetcount) for(qint32 packetcount=0;packetcount<numberOfPackets;++packetcount)
{ {
percentage=(float)(packetcount+1)/numberOfPackets*100;
if(laspercentage!=(int)percentage)
printProgBar((int)percentage,"UPLOADING");
laspercentage=(int)percentage;
if(packetcount==numberOfPackets) if(packetcount==numberOfPackets)
packetsize=lastPacketCount; packetsize=lastPacketCount;
else else
@ -133,6 +154,7 @@ bool OP_DFU::UploadData(qint32 numberOfBytes, QByteArray data)
} }
// qDebug() << "UPLOAD:"<<"Data="<<(int)buf[6]<<(int)buf[7]<<(int)buf[8]<<(int)buf[9]<<";"<<result << " bytes sent"; // qDebug() << "UPLOAD:"<<"Data="<<(int)buf[6]<<(int)buf[7]<<(int)buf[8]<<(int)buf[9]<<";"<<result << " bytes sent";
} }
return true; return true;
} }
@ -189,10 +211,10 @@ QByteArray OP_DFU::StartDownload(qint32 numberOfBytes, TransferTypes type)
char buf[BUF_LEN]; char buf[BUF_LEN];
buf[0] =0x02;//reportID buf[0] =0x02;//reportID
buf[1] = OP_DFU::Download_Req;//DFU Command buf[1] = OP_DFU::Download_Req;//DFU Command
buf[2] = (char)numberOfPackets>>24;//DFU Count buf[2] = numberOfPackets>>24;//DFU Count
buf[3] = (char)numberOfPackets>>16;//DFU Count buf[3] = numberOfPackets>>16;//DFU Count
buf[4] = (char)numberOfPackets>>8;//DFU Count buf[4] = numberOfPackets>>8;//DFU Count
buf[5] = (char)numberOfPackets;//DFU Count buf[5] = numberOfPackets;//DFU Count
buf[6] = (int)type;//DFU Data0 buf[6] = (int)type;//DFU Data0
buf[7] = lastPacketCount;//DFU Data1 buf[7] = lastPacketCount;//DFU Data1
buf[8] = 1;//DFU Data2 buf[8] = 1;//DFU Data2
@ -200,13 +222,21 @@ QByteArray OP_DFU::StartDownload(qint32 numberOfBytes, TransferTypes type)
int result = hidHandle.send(0,buf, BUF_LEN, 500); int result = hidHandle.send(0,buf, BUF_LEN, 500);
if(debug) if(debug)
qDebug() << "StartDownload:"<<result << " bytes sent"; qDebug() << "StartDownload:"<<numberOfPackets<<"packets"<<" Last Packet Size="<<lastPacketCount<<" "<<result << " bytes sent";
float percentage;
int laspercentage;
for(qint32 x=0;x<numberOfPackets;++x) for(qint32 x=0;x<numberOfPackets;++x)
{ {
percentage=(float)(x+1)/numberOfPackets*100;
if(laspercentage!=(int)percentage)
printProgBar((int)percentage,"DOWNLOADING");
laspercentage=(int)percentage;
// qDebug()<<"Status="<<StatusToString(StatusRequest()); // qDebug()<<"Status="<<StatusToString(StatusRequest());
result = hidHandle.receive(0,buf,BUF_LEN,5000); result = hidHandle.receive(0,buf,BUF_LEN,5000);
if(debug) if(debug)
qDebug() << result << " bytes received"<<" Count="<<(int)buf[2]<<";"<<(int)buf[3]<<";"<<(int)buf[4]<<";"<<(int)buf[5]<<" Data="<<(int)buf[6]<<";"<<(int)buf[7]<<";"<<(int)buf[8]<<";"<<(int)buf[9]; qDebug() << result << " bytes received"<<" Count="<<x<<"-"<<(int)buf[2]<<";"<<(int)buf[3]<<";"<<(int)buf[4]<<";"<<(int)buf[5]<<" Data="<<(int)buf[6]<<";"<<(int)buf[7]<<";"<<(int)buf[8]<<";"<<(int)buf[9];
int size; int size;
if(x==numberOfPackets-1) if(x==numberOfPackets-1)
size=lastPacketCount*4; size=lastPacketCount*4;
@ -375,7 +405,7 @@ bool OP_DFU::EndOperation()
return true; return true;
return false; return false;
} }
OP_DFU::Status OP_DFU::UploadFirmware(const QString &sfile) OP_DFU::Status OP_DFU::UploadFirmware(const QString &sfile, const bool &verify)
{ {
cout<<"Starting Firmware Uploading...\n"; cout<<"Starting Firmware Uploading...\n";
QFile file(sfile); QFile file(sfile);
@ -423,6 +453,17 @@ OP_DFU::Status OP_DFU::UploadFirmware(const QString &sfile)
{ {
cout<<"Firmware Uploading succeeded...going to upload hash\n"; cout<<"Firmware Uploading succeeded...going to upload hash\n";
} }
if(verify)
{
if(arr==StartDownload(arr.length(),OP_DFU::FW))
cout<<"Verify:PASSED\n";
else
{
cout<<"Verify:FAILED\n";
return OP_DFU::abort;
}
}
else else
{ {
return ret; return ret;
@ -497,3 +538,20 @@ QString OP_DFU::StatusToString(OP_DFU::Status status)
} }
} }
void OP_DFU::printProgBar( int percent,QString const& label){
std::string bar;
for(int i = 0; i < 50; i++){
if( i < (percent/2)){
bar.replace(i,1,"=");
}else if( i == (percent/2)){
bar.replace(i,1,">");
}else{
bar.replace(i,1," ");
}
}
std::cout<< "\r"<<label.toLatin1().data()<< "[" << bar << "] ";
std::cout.width( 3 );
std::cout<< percent << "% " << std::flush;
}

View File

@ -10,8 +10,20 @@
#include <iostream> #include <iostream>
using namespace std; using namespace std;
#define BUF_LEN 64 #define BUF_LEN 64
#define DOWNLOAD "-dn"
#define DEVICE "-d" //Command Line Options
#define DOWNLOAD "-dn" //done
#define DEVICE "-d" //done
#define DOWNDESCRIPTION "-dd" //done
#define PROGRAMFW "-p" //done
#define PROGRAMDESC "-w" //done
#define VERIFY "-v"
#define COMPAREHASH "-ch"
#define COMPAREALL "-ca"
#define STATUSREQUEST "-s" //done
#define LISTDEVICES "-ls" //done
class OP_DFU class OP_DFU
{ {
public: public:
@ -81,11 +93,13 @@ public:
bool StartUpload(qint32 numberOfBytes, TransferTypes type); bool StartUpload(qint32 numberOfBytes, TransferTypes type);
bool UploadData(qint32 numberOfPackets,QByteArray data); bool UploadData(qint32 numberOfPackets,QByteArray data);
Status UploadDescription(QString description); Status UploadDescription(QString description);
Status UploadFirmware(const QString &sfile); Status UploadFirmware(const QString &sfile, const bool &verify);
Status StatusRequest(); Status StatusRequest();
bool EndOperation(); bool EndOperation();
void printProgBar( int percent,QString const& label);
QString DownloadDescription(int numberOfChars); QString DownloadDescription(int numberOfChars);
QByteArray StartDownload(qint32 numberOfBytes, TransferTypes type); QByteArray StartDownload(qint32 numberOfBytes, TransferTypes type);
bool SaveByteArrayToFile(QString file,QByteArray const &array);
void CopyWords(char * source, char* destination, int count); void CopyWords(char * source, char* destination, int count);
// QByteArray DownloadData(int devNumber,int numberOfPackets); // QByteArray DownloadData(int devNumber,int numberOfPackets);
OP_DFU(bool debug); OP_DFU(bool debug);

View File

@ -8,7 +8,7 @@ QT += core
QT -= gui QT -= gui
TARGET = HIDTest TARGET = OPUploadTool
CONFIG += console CONFIG += console
CONFIG -= app_bundle CONFIG -= app_bundle