OP-1900 have path_progress updated correctly for leg_remaining and error_below end...
[librepilot.git] / ground / openpilotgcs / src / plugins / uploader / op_dfu.h
blob4409bc117d694a9dca0bd1dd5c736f382a807856
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
44 Q_ENUMS(Status)
45 enum Status {
46 DFUidle, // 0
47 uploading, // 1
48 wrong_packet_received, // 2
49 too_many_packets, // 3
50 too_few_packets, // 4
51 Last_operation_Success, // 5
52 downloading, // 6
53 idle, // 7
54 Last_operation_failed, // 8
55 uploadingStarting, // 9
56 outsideDevCapabilities, // 10
57 CRC_Fail, // 11
58 failed_jump, // 12
59 abort // 13
62 enum Actions {
63 actionProgram,
64 actionProgramAndVerify,
65 actionDownload,
66 actionCompareAll,
67 actionCompareCrc,
68 actionListDevs,
69 actionStatusReq,
70 actionReset,
71 actionJump
74 enum Commands {
75 Reserved, // 0
76 Req_Capabilities, // 1
77 Rep_Capabilities, // 2
78 EnterDFU, // 3
79 JumpFW, // 4
80 Reset, // 5
81 Abort_Operation, // 6
82 Upload, // 7
83 Op_END, // 8
84 Download_Req, // 9
85 Download, // 10
86 Status_Request, // 11
87 Status_Rep, // 12
90 enum eBoardType {
91 eBoardUnkwn = 0,
92 eBoardMainbrd = 1,
93 eBoardINS,
94 eBoardPip = 3,
95 eBoardRevo = 9,
98 struct device {
99 int ID;
100 quint32 FW_CRC;
101 int BL_Version;
102 int SizeOfDesc;
103 quint32 SizeOfCode;
104 bool Readable;
105 bool Writable;
109 class DFUObject : public QThread {
110 Q_OBJECT;
112 public:
113 static quint32 CRCFromQBArray(QByteArray array, quint32 Size);
114 // DFUObject(bool debug);
115 DFUObject(bool debug, bool use_serial, QString port);
117 virtual ~DFUObject();
119 // Service commands:
120 bool enterDFU(int const &devNumber);
121 bool findDevices();
122 int JumpToApp(bool safeboot, bool erase);
123 int ResetDevice(void);
124 OP_DFU::Status StatusRequest();
125 bool EndOperation();
126 int AbortOperation(void);
127 bool ready()
129 return mready;
132 // Upload (send to device) commands
133 OP_DFU::Status UploadDescription(QVariant description);
134 bool UploadFirmware(const QString &sfile, const bool &verify, int device);
136 // Download (get from device) commands:
137 // DownloadDescription is synchronous
138 QString DownloadDescription(int const & numberOfChars);
139 QByteArray DownloadDescriptionAsBA(int const & numberOfChars);
140 // Asynchronous firmware download: initiates fw download,
141 // and a downloadFinished signal is emitted when download
142 // if finished:
143 bool DownloadFirmware(QByteArray *byteArray, int device);
145 // Comparison functions (is this needed?)
146 OP_DFU::Status CompareFirmware(const QString &sfile, const CompareType &type, int device);
148 bool SaveByteArrayToFile(QString const & file, QByteArray const &array);
151 // Variables:
152 QList<device> devices;
153 int numberOfDevices;
154 int send_delay;
155 bool use_delay;
157 // Helper functions:
158 QString StatusToString(OP_DFU::Status const & status);
159 static quint32 CRC32WideFast(quint32 Crc, quint32 Size, quint32 *Buffer);
160 OP_DFU::eBoardType GetBoardType(int boardNum);
163 signals:
164 void progressUpdated(int);
165 void downloadFinished();
166 void uploadFinished(OP_DFU::Status);
167 void operationProgress(QString status);
169 private:
170 // Generic variables:
171 bool debug;
172 bool use_serial;
173 bool mready;
174 int RWFlags;
175 qsspt *serialhandle;
176 int sendData(void *, int);
177 int receiveData(void *data, int size);
178 uint8_t sspTxBuf[MAX_PACKET_BUF_SIZE];
179 uint8_t sspRxBuf[MAX_PACKET_BUF_SIZE];
180 port *info;
183 // USB Bootloader:
184 opHID_hidapi hidHandle;
185 int setStartBit(int command)
187 return command | 0x20;
190 void CopyWords(char *source, char *destination, int count);
191 void printProgBar(int const & percent, QString const & label);
192 bool StartUpload(qint32 const &numberOfBytes, TransferTypes const & type, quint32 crc);
193 bool UploadData(qint32 const & numberOfPackets, QByteArray & data);
195 // Thread management:
196 // Same as startDownload except that we store in an external array:
197 bool StartDownloadT(QByteArray *fw, qint32 const & numberOfBytes, TransferTypes const & type);
198 OP_DFU::Status UploadFirmwareT(const QString &sfile, const bool &verify, int device);
199 QMutex mutex;
200 OP_DFU::Commands requestedOperation;
201 qint32 requestSize;
202 OP_DFU::TransferTypes requestTransferType;
203 QByteArray *requestStorage;
204 QString requestFilename;
205 bool requestVerify;
206 int requestDevice;
208 protected:
209 void run(); // Executes the upload or download operations
213 Q_DECLARE_METATYPE(OP_DFU::Status)
216 #endif // OP_DFU_H