Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / ground / gcs / src / experimental / USB_UPLOAD_TOOL / dfu.h
blob8c8c8e50187bf30875c67bfd81bad8b61571c0e2
1 /**
2 ******************************************************************************
4 * @file dfu.h
5 * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2017.
6 * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
7 * @addtogroup GCSPlugins GCS Plugins
8 * @{
9 * @addtogroup Uploader Uploader Plugin
10 * @{
11 * @brief The uploader plugin
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #ifndef DFU_H
29 #define DFU_H
31 #include <QByteArray>
32 #include <QThread>
33 #include <QMutex>
34 #include <QList>
35 #include <QVariant>
37 #define MAX_PACKET_DATA_LEN 255
38 #define MAX_PACKET_BUF_SIZE (1 + 1 + MAX_PACKET_DATA_LEN + 2)
40 #define BUF_LEN 64
42 // serial
43 class qsspt;
45 // usb
46 class opHID_hidapi;
48 namespace DFU {
49 #if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0)
50 Q_NAMESPACE
51 #endif
52 enum TransferTypes {
53 FW,
54 Descript
57 enum CompareType {
58 crccompare,
59 bytetobytecompare
62 Q_ENUMS(Status)
63 enum Status {
64 DFUidle, // 0
65 uploading, // 1
66 wrong_packet_received, // 2
67 too_many_packets, // 3
68 too_few_packets, // 4
69 Last_operation_Success, // 5
70 downloading, // 6
71 idle, // 7
72 Last_operation_failed, // 8
73 uploadingStarting, // 9
74 outsideDevCapabilities, // 10
75 CRC_Fail, // 11
76 failed_jump, // 12
77 abort // 13
80 enum Actions {
81 actionNone,
82 actionProgram,
83 actionProgramAndVerify,
84 actionDownload,
85 actionCompareAll,
86 actionCompareCrc,
87 actionListDevs,
88 actionStatusReq,
89 actionReset,
90 actionJump
93 enum Commands {
94 Reserved, // 0
95 Req_Capabilities, // 1
96 Rep_Capabilities, // 2
97 EnterDFU, // 3
98 JumpFW, // 4
99 Reset, // 5
100 Abort_Operation, // 6
101 Upload, // 7
102 END, // 8
103 Download_Req, // 9
104 Download, // 10
105 Status_Request, // 11
106 Status_Rep, // 12
109 enum eBoardType {
110 eBoardUnkwn = 0,
111 eBoardMainbrd = 1,
112 eBoardINS,
113 eBoardPip = 3,
114 eBoardCC = 4,
115 eBoardRevo = 9,
116 eBoardSparky2 = 0x92,
119 struct device {
120 quint16 ID;
121 quint32 FW_CRC;
122 quint8 BL_Version;
123 int SizeOfDesc;
124 quint32 SizeOfCode;
125 bool Readable;
126 bool Writable;
129 class DFUObject : public QThread {
130 Q_OBJECT;
132 public:
133 static quint32 CRCFromQBArray(QByteArray array, quint32 Size);
135 DFUObject(bool debug, bool use_serial, QString port);
137 virtual ~DFUObject();
139 // Service commands:
140 bool enterDFU(int const &devNumber);
141 bool findDevices();
142 int JumpToApp(bool safeboot, bool erase);
143 int ResetDevice(void);
144 DFU::Status StatusRequest();
145 bool EndOperation();
146 int AbortOperation(void);
147 bool ready()
149 return mready;
152 // Upload (send to device) commands
153 DFU::Status UploadDescription(QVariant description);
154 bool UploadFirmware(const QString &sfile, const bool &verify, int device);
156 // Download (get from device) commands:
157 // DownloadDescription is synchronous
158 QString DownloadDescription(int const & numberOfChars);
159 QByteArray DownloadDescriptionAsBA(int const & numberOfChars);
160 // Asynchronous firmware download: initiates fw download,
161 // and a downloadFinished signal is emitted when download
162 // if finished:
163 bool DownloadFirmware(QByteArray *byteArray, int device);
165 // Comparison functions (is this needed?)
166 DFU::Status CompareFirmware(const QString &sfile, const CompareType &type, int device);
168 bool SaveByteArrayToFile(QString const & file, QByteArray const &array);
170 // Variables:
171 QList<device> devices;
172 int numberOfDevices;
173 int send_delay;
174 bool use_delay;
176 // Helper functions:
177 QString StatusToString(DFU::Status const & status);
178 static quint32 CRC32WideFast(quint32 Crc, quint32 Size, quint32 *Buffer);
179 DFU::eBoardType GetBoardType(int boardNum);
181 signals:
182 void progressUpdated(int);
183 void downloadFinished();
184 void uploadFinished(DFU::Status);
185 void operationProgress(QString status);
187 private:
188 // Generic variables:
189 bool debug;
190 bool use_serial;
191 bool mready;
192 int RWFlags;
194 // Serial
195 qsspt *serialhandle;
197 // USB
198 // opHID_hidapi *hidHandle;
200 int sendData(void *, int);
201 int receiveData(void *data, int size);
202 uint8_t sspTxBuf[MAX_PACKET_BUF_SIZE];
203 uint8_t sspRxBuf[MAX_PACKET_BUF_SIZE];
205 int setStartBit(int command)
207 return command | 0x20;
210 void CopyWords(char *source, char *destination, int count);
211 void printProgBar(int const & percent, QString const & label);
212 bool StartUpload(qint32 const &numberOfBytes, TransferTypes const & type, quint32 crc);
213 bool UploadData(qint32 const & numberOfPackets, QByteArray & data);
215 // Thread management:
216 // Same as startDownload except that we store in an external array:
217 bool StartDownloadT(QByteArray *fw, qint32 const & numberOfBytes, TransferTypes const & type);
218 DFU::Status UploadFirmwareT(const QString &sfile, const bool &verify, int device);
219 QMutex mutex;
220 DFU::Commands requestedOperation;
221 qint32 requestSize;
222 DFU::TransferTypes requestTransferType;
223 QByteArray *requestStorage;
224 QString requestFilename;
225 bool requestVerify;
226 int requestDevice;
228 protected:
229 void run(); // Executes the upload or download operations
233 Q_DECLARE_METATYPE(DFU::Status)
236 #endif // DFU_H