-
Notifications
You must be signed in to change notification settings - Fork 1
/
flasher.h
executable file
·438 lines (367 loc) · 13.9 KB
/
flasher.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/****************************************************************************
*
* Copyright (c) 2021 IMProject Development Team. All rights reserved.
* Authors: Igor Misic <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name IMProject nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef FLASHER_H_
#define FLASHER_H_
#include <QElapsedTimer>
#include <QFile>
#include <QJsonObject>
#include <QJsonArray>
#include <QThread>
#include "flasher_states.h"
#include "flashing_info.h"
#include "serial_port.h"
namespace socket {
class SocketClient;
} // namespace socket
namespace file_downloader {
class FileDownloader;
} // namespace file_downloader
namespace flasher {
/*!
* \brief The Flasher class, main class used to handle flashing process
*/
class Flasher : public QObject {
Q_OBJECT
public:
/*!
* \brief Flasher default constructor
*/
Flasher();
/*!
* \brief Flasher destructor
*/
~Flasher();
/*!
* \brief Method used to collect board ID
* \return True if board ID is successfully collected, false otherwise
*/
bool CollectBoardId();
/*!
* \brief Method used to collect board information
* \return True if board information is successfully collected, false otherwise
*/
bool CollectBoardInfo();
/*!
* \brief Method used to collect security data
* \return True if data is successfully collected, false otherwise
*/
bool CollectSecurityDataFromBoard();
/*!
* \brief Method used to flash from the console
* \return Flashing information
*/
FlashingInfo ConsoleFlash();
/*!
* \brief Send erase command while flashing
* \return Flashing information
*/
FlashingInfo Erase();
/*!
* \brief Flasher initialization
*/
void Init();
/*!
* \brief Check if bootloader is detected
* \return True if bootloader is detected, false otherwise
*/
bool IsBootloaderDetected() const;
/*!
* \brief Check if read protection is enable
* \return True if read protection is enabled, false otherwise
*/
bool IsReadProtectionEnabled() const;
/*!
* \brief Open file
* \param file_path - File path
* \return True if file is successfully opened, false otherwise
*/
bool OpenFile(const QString& file_path);
/*!
* \brief Send enter bootloader command
* \return True if command is successfully sent, false otherwise
*/
bool SendEnterBootloaderCommand();
/*!
* \brief Send flash command
*/
void SendFlashCommand();
/*!
* \brief Set local file content
* \return True if local file content is successully set, false otherwise
*/
bool SetLocalFileContent();
/*!
* \brief Set flasher state
* \param state - Flasher state that will be set
*/
void SetState(const FlasherStates& state);
/*!
* \brief Set selected file version
* \param selected_file_version - File version that will be set
*/
void SetSelectedFileVersion(const QString& selected_file_version);
/*!
* \brief Try to connect to the board over console
*/
void TryToConnectConsole();
/*!
* \brief Update progress bar
* \param sent_size - Size that is sent
* \param total_size - Total size
*/
void UpdateProgressBar(const quint64& sent_size, const quint64& total_size);
signals:
/*!
* \brief Update progress bar signal
* \param progress_percentage - value for the update in percentage
*/
void UpdateProgressBarSignal(const qint8& progress_percentage);
/*!
* \brief Clear progress signal
*/
void ClearProgress();
/*!
* \brief Show status message signal
* \param text - Text that will be displayed
*/
void ShowStatusMsg(const QString& text);
/*!
* \brief Clear status message signal
*/
void ClearStatusMsg();
/*!
* \brief Failed to connect signal
*/
void FailedToConnect();
/*!
* \brief RunLoop signal
*/
void RunLoop();
/*!
* \brief Show text in browser signal
* \param text - Text that will be displayed
*/
void ShowTextInBrowser(const QString& text);
/*!
* \brief Clear text in browser signal
*/
void ClearTextInBrowser();
/*!
* \brief Set buttons signal
* \param isBootloader - Flag that determines if bootloader is entered, and therefore it determines button text
*/
void SetButtons(const bool& isBootloader);
/*!
* \brief Set read protection button text signal
* \param isEnabled - Flag that determines button text
*/
void SetReadProtectionButtonText(const bool& isEnabled);
/*!
* \brief Disable all buttons signal
*/
void DisableAllButtons();
/*!
* \brief Disable all buttons signal
*/
void DisableBrowseFileButton();
/*!
* \brief Enable connect button, connect button is enabled and disconnect button disabled
*/
void EnableConnectButton();
/*!
* \brief Enable disconnect button signal, disconnect button is enabled and connect button disabled
*/
void EnableDisconnectButton();
/*!
* \brief Enable load button signal
*/
void EnableLoadButton();
/*!
* \brief Set file versions list signal
* \param product_info - Json array that contains product information
*/
void SetFileVersionsList(const QJsonArray& product_info);
public slots:
/*!
* \brief LoopHandler slot
*/
void LoopHandler();
/*!
* \brief FileDownloaded slot, file downloading is finished
*/
void FileDownloaded();
/*!
* \brief DownloadProgress slot
* \param bytes_received - Number of received bytes
* \param bytes_total - Total number of bytes
*/
void DownloadProgress(const qint64& bytes_received, const qint64& bytes_total);
/*!
* \brief HandleSerialPortError slot
* \param error - Serial port error
*/
void HandleSerialPortError(QSerialPort::SerialPortError error);
private:
QString board_id_; //!< Board ID
QJsonObject board_info_; //!< Board information
QJsonObject client_security_data_; //!< Security data client
QJsonObject server_security_data_; //!< Security data server
QJsonObject bl_sw_info; //!< Bootloader software info
QJsonObject fw_sw_info; //!< Firmware software info
QJsonArray product_info_; //!< Product information
QString selected_file_version_; //!< Selected file version
QString file_source_; //!< File source (URL or Server)
QFile config_file_; //!< Configuration file
QFile file_to_flash_; //!< File to flash
qint64 signature_size_{0}; //!< Signature size
qint64 packet_size_{0}; //!< Size of the packets that will be used to send data for flashing
quint8 last_progress_percentage_{0}; //!< Last progress percentage
bool is_bootloader_ {false}; //!< Is bootloader detected flag
bool is_bootloader_expected_ {false}; //!< Is bootloader expected after board reset
bool is_read_protection_enabled_ {false}; //!< Is read protection enabled flag
bool is_timer_started_ {false}; //!< Is timer started flag
bool is_file_downloaded_{false}; //!< Is file downloaded flag
bool is_download_success_{false}; //!< Is download successfully done
bool is_signature_warning_enabled_{false}; //!< Is signature warning enabled
bool is_secure_communication_{false}; //!< Is communication with the server secure
bool is_secure_bootloader_{false}; //!< Is secure bootloader variant
QByteArray file_content_; //!< File content
communication::SerialPort serial_port_; //!< Serial port object
std::shared_ptr<socket::SocketClient> socket_client_; //!< Shared pointer to SocketClient object
std::unique_ptr<file_downloader::FileDownloader> file_downloader_; //!< Pointer to FileDownloader object
FlasherStates state_ {FlasherStates::kIdle}; //!< Flasher state
QElapsedTimer timer_; //!< Timer
QThread worker_thread_; //!< Worker thread
/*!
* \brief Method is used to check if acknowledge is received from bootloader side
* \return True if ACK is recevied, false otherwise
*/
bool CheckAck();
/*!
* \brief Method used to check signature
* \return Flashing info structure
*/
FlashingInfo CheckSignature();
/*!
* \brief CheckTrue
* \return True if TRUE is received, false otherwise
*/
bool CheckTrue();
/*!
* \brief Method used to check CRC
* \return Flashing info structure
*/
FlashingInfo CrcCheck();
/*!
* \brief Method used to create default configuration file
*/
void CreateDefaultConfigFile();
/*!
* \brief Method used to donwload file from URL
*/
void DownloadFileFromUrl();
/*!
* \brief Method used to perform flash process
* \return Flashing info structure
*/
FlashingInfo Flash();
/*!
* \brief Method used to get version of bootloader/firmware
*/
void GetVersion();
/*!
* \brief Method used to get version of bootloader/firmware in JSON format
* \param out_json_object - Json object where version will be saved
* \return True if version is fetched successfully, false otherwise
*/
bool GetVersionJson(QJsonObject& out_json_object);
/*!
* \brief Method used to check if firmware is protected or not
* \return True if firmware is protected, false otherwise
*/
bool IsFirmwareProtected();
/*!
* \brief Method used to open configuration file
* \param json_document - Json document where configuration file is saved
* \return True if configuration file is successfully opened, false otherwise
*/
bool OpenConfigFile(QJsonDocument& json_document);
/*!
* \brief Method used to read message with CRC
* \param in_data - Pointer to the input data
* \param length - Data length
* \param timeout_ms - Timeout for reading from serial port [ms]
* \param out_data - Byte array of output data
* \return True if message with CRC is successfully read, false otherwise
*/
bool ReadMessageWithCrc(const char *in_data, qint64 length, int timeout_ms, QByteArray& out_data);
/*!
* \brief Method used to reconnect to board
*/
void ReconnectingToBoard();
/*!
* \brief Method used to send file size to the bootloader
* \return Flashing info structure
*/
FlashingInfo SendFileSize();
/*!
* \brief Method used to send message to the bootloader/firmware
* \param data - Pointer to data that will be sent
* \param length - Data length
* \param timeout_ms - Timeout for reading from serial port [ms]
* \return True if message is sent correctly, false otherwise
*/
bool SendMessage(const char *data, qint64 length, int timeout_ms);
/*!
* \brief Method used to send server security data to the bootloader
* \return Flashing info structure
*/
FlashingInfo SendServerSecurityData();
/*!
* \brief Method used to send signature to the bootloader
* \return Flashing info structure
*/
FlashingInfo SendSignature();
/*!
* \brief Method used to verify flasher
* \return Flashing info structure
*/
FlashingInfo VerifyFlasher();
/*!
* \brief Method used to try to connect to the board
*/
void TryToConnect();
};
} // namespace flasher
#endif // FLASHER_H_