1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-12-11 19: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,215 +9,238 @@ int main(int argc, char *argv[])
QCoreApplication a(argc, argv); QCoreApplication a(argc, argv);
if(argc>1) if(argc>1)
{ {
bool debug=false; bool verify;
OP_DFU::Actions action; bool debug=false;
QString file; OP_DFU::Actions action;
QString description; QString file;
int device=-1; QString description;
QStringList args; int device=-1;
for(int i=0;i<argc;++i) QStringList args;
{ for(int i=0;i<argc;++i)
args<<argv[i]; {
} args<<argv[i];
}
if(args.contains("-?")) if(args.contains("-?"))
{ {
cout<<"| Commands\n"; cout<<"| Commands\n";
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<<"| -dn <file> : download firmware to file (requires:-d)\n"; cout<<"| -v : verify (requires:-d)\n";
cout<<"| -dd <file> : download discription (requires:-d)\n"; cout<<"| -dn <file> : download firmware to file (requires:-d)\n";
cout<<"| -d <number Of Device : (requires: -p or -dn)\n"; cout<<"| -dd <file> : download discription (requires:-d)\n";
cout<<"| -w <description> : (requires: -p)\n"; cout<<"| -d <number Of Device : (requires: -p or -dn)\n";
cout<<"| -ca <file> : compares byte by byte current firmware with file\n"; cout<<"| -w <description> : (requires: -p)\n";
cout<<"| -ch <file> : compares hash of current firmware with file\n"; cout<<"| -ca <file> : compares byte by byte current firmware with file\n";
cout<<"| -s : requests status of device\n"; cout<<"| -ch <file> : compares hash of current firmware with file\n";
return 0; cout<<"| -s : requests status of device\n";
return 0;
} }
else if(args.contains("-dd")) else if(args.contains(DOWNDESCRIPTION))
{
action=OP_DFU::downdesc;
if(args.contains(DEVICE))
{ {
if(args.indexOf(DEVICE)+1<args.length()) action=OP_DFU::downdesc;
if(args.contains(DEVICE))
{ {
device=(args[args.indexOf(DEVICE)+1]).toInt(); if(args.indexOf(DEVICE)+1<args.length())
{
device=(args[args.indexOf(DEVICE)+1]).toInt();
}
} }
} }
} else if(args.contains(PROGRAMFW))
else if(args.contains("-p"))
{
if(args.contains("-w"))
{ {
if(args.indexOf("-w")+1<args.length()) if(args.contains(VERIFY))
{ {
description=(args[args.indexOf("-w")+1]); verify=true;
} }
else
verify=false;
if(args.contains(PROGRAMDESC))
{
if(args.indexOf(PROGRAMDESC)+1<args.length())
{
description=(args[args.indexOf(PROGRAMDESC)+1]);
}
}
action=OP_DFU::program;
if(args.contains(DEVICE))
{
if(args.indexOf(DEVICE)+1<args.length())
{
device=(args[args.indexOf(DEVICE)+1]).toInt();
} }
if(args.indexOf("-p")+1<args.length()) action=OP_DFU::program;
if(args.contains(DEVICE))
{ {
file=args[args.indexOf("-p")+1]; if(args.indexOf(DEVICE)+1<args.length())
} {
} device=(args[args.indexOf(DEVICE)+1]).toInt();
else }
{ if(args.indexOf(PROGRAMFW)+1<args.length())
cout<<("Device not specified\n"); {
return -1; file=args[args.indexOf(PROGRAMFW)+1];
} }
if(args.contains("-v"))
action=OP_DFU::programandverify;
}
else if(args.contains("-ch") || args.contains("-ca"))
{
int index;
if(args.contains("-ch"))
{
index=args.indexOf("-ch");
action=OP_DFU::comparehash;
}
else
{
index=args.indexOf("-ca");
action=OP_DFU::compareall;
}
if(args.contains(DEVICE))
{
if(args.indexOf(DEVICE)+1<args.length())
{
device=(args[args.indexOf(DEVICE)+1]).toInt();
} }
if(index+1<args.length()) else
{ {
file=args[index+1]; cout<<("Device not specified\n");
return -1;
}
if(args.contains(VERIFY))
action=OP_DFU::programandverify;
}
else if(args.contains(COMPAREHASH) || args.contains(COMPAREALL))
{
int index;
if(args.contains(COMPAREHASH))
{
index=args.indexOf(COMPAREHASH);
action=OP_DFU::comparehash;
}
else
{
index=args.indexOf(COMPAREALL);
action=OP_DFU::compareall;
}
if(args.contains(DEVICE))
{
if(args.indexOf(DEVICE)+1<args.length())
{
device=(args[args.indexOf(DEVICE)+1]).toInt();
}
if(index+1<args.length())
{
file=args[index+1];
}
}
else
{
cout<<"Device not specified";
return -1;
} }
} }
else else if(args.contains(DOWNLOAD))
{ {
cout<<"Device not specified"; int index;
return -1;
}
}
else if(args.contains(DOWNLOAD))
{
int index;
index=args.indexOf(DOWNLOAD); index=args.indexOf(DOWNLOAD);
action=OP_DFU::download; action=OP_DFU::download;
if(args.contains(DEVICE)) if(args.contains(DEVICE))
{
if(args.indexOf(DEVICE)+1<args.length())
{ {
device=(args[args.indexOf(DEVICE)+1]).toInt(); if(args.indexOf(DEVICE)+1<args.length())
}
if(index+1<args.length())
{
file=args[index+1];
}
}
else
{
cout<<"Device not specified";
return -1;
}
}
else if(args.contains("-s"))
{
action=OP_DFU::statusreq;
}
else if(args.contains("-ls"))
{
action=OP_DFU::listdevs;
}
if((file.isEmpty()|| device==-1) && action!=OP_DFU::downdesc && action!=OP_DFU::statusreq && action!=OP_DFU::listdevs)
{
cout<<"wrong parameters";
return -1;
}
// qDebug()<<"Action="<<(int)action;
// qDebug()<<"File="<<file;
// qDebug()<<"Device="<<device;
// qDebug()<<"Action="<<action;
OP_DFU dfu(debug);
if(!dfu.enterDFU(0))
{
cout<<"Could not enter DFU mode\n";
return -1;
}
if(action!=OP_DFU::statusreq)
{
dfu.findDevices();
if(action==OP_DFU::listdevs)
{
cout<<"Found "<<dfu.numberOfDevices<<"\n";
for(int x=0;x<dfu.numberOfDevices;++x)
{
cout<<"Device #"<<x+1<<"\n";
cout<<"Device ID="<<dfu.devices[x].ID<<"\n";
cout<<"Device Readable="<<dfu.devices[x].Readable<<"\n";
cout<<"Device Writable="<<dfu.devices[x].Writable<<"\n";
cout<<"Device SizeOfCode="<<dfu.devices[x].SizeOfCode<<"\n";
cout<<"Device SizeOfHash="<<dfu.devices[x].SizeOfHash<<"\n";
cout<<"Device SizeOfDesc="<<dfu.devices[x].SizeOfDesc<<"\n";
cout<<"\n";
}
return 0;
}
else if (action==OP_DFU::program)
{
if(device>dfu.numberOfDevices)
{
cout<<"Error:Invalid Device";
return -1;
}
if(!dfu.enterDFU(device))
{
cout<<"Error:Could not enter DFU mode\n";
return -1;
}
OP_DFU::Status retstatus=dfu.UploadFirmware(file.toAscii());
if(retstatus!=OP_DFU::Last_operation_Success)
{
cout<<"Upload failed with code:"<<dfu.StatusToString(retstatus).data();
return -1;
}
if(!description.isEmpty())
{
retstatus=dfu.UploadDescription(description);
if(retstatus!=OP_DFU::Last_operation_Success)
{ {
cout<<"Upload failed with code:"<<dfu.StatusToString(retstatus).toLatin1().data(); device=(args[args.indexOf(DEVICE)+1]).toInt();
return -1; }
if(index+1<args.length())
{
file=args[index+1];
} }
} }
cout<<"Uploading Succeded!\n"; else
{
cout<<"Device not specified";
return -1;
}
} }
else if (action==OP_DFU::download) else if(args.contains(STATUSREQUEST))
{ {
qint32 size=((OP_DFU::device)dfu.devices[device]).SizeOfCode; action=OP_DFU::statusreq;
}
else if(args.contains(LISTDEVICES))
{
action=OP_DFU::listdevs;
}
if((file.isEmpty()|| device==-1) && action!=OP_DFU::downdesc && action!=OP_DFU::statusreq && action!=OP_DFU::listdevs)
{
cout<<"wrong parameters";
return -1;
}
// qDebug()<<"Action="<<(int)action;
// qDebug()<<"File="<<file;
// qDebug()<<"Device="<<device;
// qDebug()<<"Action="<<action;
OP_DFU dfu(debug);
if(!dfu.enterDFU(0))
{
cout<<"Could not enter DFU mode\n";
return -1;
}
if(action!=OP_DFU::statusreq)
{
dfu.findDevices();
if(action==OP_DFU::listdevs)
{
cout<<"Found "<<dfu.numberOfDevices<<"\n";
for(int x=0;x<dfu.numberOfDevices;++x)
{
cout<<"Device #"<<x<<"\n";
cout<<"Device ID="<<dfu.devices[x].ID<<"\n";
cout<<"Device Readable="<<dfu.devices[x].Readable<<"\n";
cout<<"Device Writable="<<dfu.devices[x].Writable<<"\n";
cout<<"Device SizeOfCode="<<dfu.devices[x].SizeOfCode<<"\n";
cout<<"Device SizeOfHash="<<dfu.devices[x].SizeOfHash<<"\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";
}
return 0;
}
else if (action==OP_DFU::program)
{
if(device>dfu.numberOfDevices)
{
cout<<"Error:Invalid Device";
return -1;
}
if(!dfu.enterDFU(device))
{
cout<<"Error:Could not enter DFU mode\n";
return -1;
}
OP_DFU::Status retstatus=dfu.UploadFirmware(file.toAscii(),verify);
if(retstatus!=OP_DFU::Last_operation_Success)
{
cout<<"Upload failed with code:"<<dfu.StatusToString(retstatus).data();
return -1;
}
if(!description.isEmpty())
{
retstatus=dfu.UploadDescription(description);
if(retstatus!=OP_DFU::Last_operation_Success)
{
cout<<"Upload failed with code:"<<dfu.StatusToString(retstatus).toLatin1().data();
return -1;
}
}
cout<<"Uploading Succeded!\n";
}
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;
bool ret=dfu.SaveByteArrayToFile(file.toAscii(),dfu.StartDownload(size,OP_DFU::FW));
return ret;
}
else if(action==OP_DFU::downdesc)
{
int size=((OP_DFU::device)dfu.devices[device]).SizeOfDesc;
cout<<"Description:"<<dfu.DownloadDescription(size).toLatin1().data();
}
} }
else if(action==OP_DFU::downdesc) return 0;
{
int size=((OP_DFU::device)dfu.devices[device]).SizeOfDesc;
cout<<"Description:"<<dfu.DownloadDescription(size).toLatin1().data();
}
} }
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);
@ -234,12 +257,12 @@ OP_DFU dfu(true);
// dfu.UploadDescription(1,"jose manuel"); // dfu.UploadDescription(1,"jose manuel");
// QString str=dfu.DownloadDescription(1,12); // QString str=dfu.DownloadDescription(1,12);
// 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)
{ {
// qDebug()<<"Status="<<StatusToString(StatusRequest()); percentage=(float)(x+1)/numberOfPackets*100;
if(laspercentage!=(int)percentage)
printProgBar((int)percentage,"DOWNLOADING");
laspercentage=(int)percentage;
// 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