LP-311 Remove basic/advanced stabilization tab auto-switch (autotune/txpid lock issues)
[librepilot.git] / ground / gcs / src / experimental / USB_UPLOAD_TOOL / op_dfu.h
blob4da8185938e955d1bd1be018551249ae0c643301
1 #ifndef OP_DFU_H
2 #define OP_DFU_H
4 #include <QByteArray>
5 // #include <ophid/inc/ophid_hidapi.h>
6 // #include <ophid/inc/ophid_usbmon.h>
7 // #include <ophid/inc/ophid_usbsignal.h>
8 #include <QDebug>
9 #include <QFile>
10 #include <QThread>
11 #include <QMutex>
12 #include <QMutexLocker>
13 #include <QMetaType>
14 #include <QCryptographicHash>
15 #include <QList>
16 #include <QVariant>
17 #include <iostream>
18 #include "delay.h"
19 #include <QtSerialPort/QSerialPort>
20 #include <QtSerialPort/QSerialPortInfo>
21 #include <QTime>
22 #include <QTimer>
23 #include "SSP/qssp.h"
24 #include "SSP/port.h"
25 #include "SSP/qsspt.h"
27 using namespace std;
28 #define BUF_LEN 64
30 #define MAX_PACKET_DATA_LEN 255
31 #define MAX_PACKET_BUF_SIZE (1 + 1 + MAX_PACKET_DATA_LEN + 2)
33 namespace OP_DFU {
34 enum TransferTypes {
35 FW,
36 Descript
39 enum CompareType {
40 crccompare,
41 bytetobytecompare
43 // Command Line Options
44 #define DOWNLOAD "-dn" // done
45 #define DEVICE "-d" // done
46 #define DOWNDESCRIPTION "-dd" // done
47 #define PROGRAMFW "-p" // done
48 #define PROGRAMDESC "-w" // done
49 #define VERIFY "-v" // done
50 #define COMPARECRC "-cc"
51 #define COMPAREALL "-ca"
52 #define STATUSREQUEST "-s" // done
53 #define LISTDEVICES "-ls" // done
54 #define RESET "-r"
55 #define JUMP "-j"
56 #define USE_SERIAL "-t"
57 #define NO_COUNTDOWN "-i"
59 Q_ENUMS(Status)
60 enum Status {
61 DFUidle, // 0
62 uploading, // 1
63 wrong_packet_received, // 2
64 too_many_packets, // 3
65 too_few_packets, // 4
66 Last_operation_Success, // 5
67 downloading, // 6
68 idle, // 7
69 Last_operation_failed, // 8
70 uploadingStarting, // 9
71 outsideDevCapabilities, // 10
72 CRC_Fail, // 11
73 failed_jump, // 12
74 abort // 13
77 enum Actions {
78 actionProgram,
79 actionProgramAndVerify,
80 actionDownload,
81 actionCompareAll,
82 actionCompareCrc,
83 actionListDevs,
84 actionStatusReq,
85 actionReset,
86 actionJump
89 enum Commands {
90 Reserved, // 0
91 Req_Capabilities, // 1
92 Rep_Capabilities, // 2
93 EnterDFU, // 3
94 JumpFW, // 4
95 Reset, // 5
96 Abort_Operation, // 6
97 Upload, // 7
98 Op_END, // 8
99 Download_Req, // 9
100 Download, // 10
101 Status_Request, // 11
102 Status_Rep, // 12
105 enum eBoardType {
106 eBoardUnkwn = 0,
107 eBoardMainbrd = 1,
108 eBoardINS,
109 eBoardPip = 3,
110 eBoardCC = 4,
111 eBoardRevo = 9,
114 struct device {
115 int ID;
116 quint32 FW_CRC;
117 int BL_Version;
118 int SizeOfDesc;
119 quint32 SizeOfCode;
120 bool Readable;
121 bool Writable;
125 class DFUObject : public QThread {
126 Q_OBJECT;
128 public:
129 static quint32 CRCFromQBArray(QByteArray array, quint32 Size);
130 // DFUObject(bool debug);
131 DFUObject(bool debug, bool use_serial, QString port);
133 virtual ~DFUObject();
135 // Service commands:
136 bool enterDFU(int const &devNumber);
137 bool findDevices();
138 int JumpToApp(bool safeboot, bool erase);
139 int ResetDevice(void);
140 void sendReset(void);
141 OP_DFU::Status StatusRequest();
142 bool EndOperation();
143 int AbortOperation(void);
144 bool ready()
146 return mready;
149 // Upload (send to device) commands
150 OP_DFU::Status UploadDescription(QVariant description);
151 bool UploadFirmware(const QString &sfile, const bool &verify, int device);
153 // Download (get from device) commands:
154 // DownloadDescription is synchronous
155 QString DownloadDescription(int const & numberOfChars);
156 QByteArray DownloadDescriptionAsBA(int const & numberOfChars);
157 // Asynchronous firmware download: initiates fw download,
158 // and a downloadFinished signal is emitted when download
159 // if finished:
160 bool DownloadFirmware(QByteArray *byteArray, int device);
162 // Comparison functions (is this needed?)
163 OP_DFU::Status CompareFirmware(const QString &sfile, const CompareType &type, int device);
165 bool SaveByteArrayToFile(QString const & file, QByteArray const &array);
168 // Variables:
169 QList<device> devices;
170 int numberOfDevices;
171 int send_delay;
172 bool use_delay;
174 // Helper functions:
175 QString StatusToString(OP_DFU::Status const & status);
176 static quint32 CRC32WideFast(quint32 Crc, quint32 Size, quint32 *Buffer);
177 OP_DFU::eBoardType GetBoardType(int boardNum);
180 signals:
181 void progressUpdated(int);
182 void downloadFinished();
183 void uploadFinished(OP_DFU::Status);
184 void operationProgress(QString status);
186 private:
187 // Generic variables:
188 bool debug;
189 bool use_serial;
190 bool mready;
191 int RWFlags;
192 qsspt *serialhandle;
193 int sendData(void *, int);
194 int receiveData(void *data, int size);
195 uint8_t sspTxBuf[MAX_PACKET_BUF_SIZE];
196 uint8_t sspRxBuf[MAX_PACKET_BUF_SIZE];
197 port *info;
200 // USB Bootloader:
201 // opHID_hidapi hidHandle;
202 int setStartBit(int command)
204 return command | 0x20;
207 void CopyWords(char *source, char *destination, int count);
208 void printProgBar(int const & percent, QString const & label);
209 bool StartUpload(qint32 const &numberOfBytes, TransferTypes const & type, quint32 crc);
210 bool UploadData(qint32 const & numberOfPackets, QByteArray & data);
212 // Thread management:
213 // Same as startDownload except that we store in an external array:
214 bool StartDownloadT(QByteArray *fw, qint32 const & numberOfBytes, TransferTypes const & type);
215 OP_DFU::Status UploadFirmwareT(const QString &sfile, const bool &verify, int device);
216 QMutex mutex;
217 OP_DFU::Commands requestedOperation;
218 qint32 requestSize;
219 OP_DFU::TransferTypes requestTransferType;
220 QByteArray *requestStorage;
221 QString requestFilename;
222 bool requestVerify;
223 int requestDevice;
225 protected:
226 void run(); // Executes the upload or download operations
230 Q_DECLARE_METATYPE(OP_DFU::Status)
233 #endif // OP_DFU_H