/
flasher.cpp
222 lines (190 loc) · 6.89 KB
/
flasher.cpp
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
#include "flasher.h"
#include "logger.h"
#include <QCoreApplication>
#include <QDebug>
#include <QFileInfo>
#include <QProcess>
#include <QRegularExpression>
#include <QSerialPortInfo>
#include <QStandardPaths>
#include <QThread>
PING_LOGGING_CATEGORY(FLASH, "ping.flash")
Flasher::Flasher(QObject* parent)
: QObject(parent)
{
_binRelativePath =
#ifdef Q_OS_OSX
// macdeployqt file do not put stm32flash binary in the same folder of pingviewer
QCoreApplication::applicationDirPath() + "/../..";
#else
QCoreApplication::applicationDirPath();
#endif
}
bool Flasher::setBaudRate(int baudRate)
{
if (!_validBaudRates.contains(baudRate)) {
qCCritical(FLASH) << "Invalid baud rate:" << baudRate;
qCCritical(FLASH) << "Valid baud rates are:" << _validBaudRates;
return false;
}
_baudRate = baudRate;
return true;
};
bool Flasher::setFirmwarePath(const QString& firmwareFilePath)
{
QFileInfo fileInfo(firmwareFilePath);
if (!fileInfo.exists()) {
qCCritical(FLASH) << "Firmware file does not exist:" << firmwareFilePath;
return false;
}
_firmwareFilePath = fileInfo.absoluteFilePath();
return true;
};
bool Flasher::setLink(const LinkConfiguration& link)
{
if (!link.checkType(LinkType::Serial)) {
qCCritical(FLASH) << "Link configuration is not valid:" << link;
return false;
}
_link = link;
return true;
};
void Flasher::setVerify(bool verify) { _verify = verify; };
QString Flasher::stm32flashPath()
{
static const QString binaryName =
#ifdef Q_OS_WIN
"stm32flash.exe";
#else
"stm32flash";
#endif
#ifdef Q_OS_OSX
// macdeployqt file do not put stm32flash binary in the same folder of pingviewer
static const QString absoluteBinPath = QCoreApplication::applicationDirPath() + "/../..";
#else
static const QString absoluteBinPath = QCoreApplication::applicationDirPath();
#endif
const QString localPathString = absoluteBinPath + "/" + binaryName;
if (QFile::exists(localPathString)) {
qCDebug(FLASH) << "Found program in:" << localPathString;
return localPathString;
}
// Check if program is installed
QString absoluteSystemBinPath = QStandardPaths::findExecutable(binaryName);
if (!absoluteSystemBinPath.isEmpty()) {
qCDebug(FLASH) << "Found program in:" << absoluteSystemBinPath;
}
return absoluteSystemBinPath;
}
void Flasher::flash()
{
if (!QFile::exists(stm32flashPath())) {
QString output = QStringLiteral("stm32flash is not available! Flash procedure will abort.");
qCCritical(FLASH) << "Searching in: " << stm32flashPath();
qCCritical(FLASH) << output;
setState(Error, output);
return;
}
QSerialPortInfo pInfo(_link.serialPort());
auto portLocation = pInfo.systemLocation();
auto baudRate = QString::number(_baudRate);
auto verifyArgument = _verify ? QStringLiteral("-v") : QString();
QFileInfo firmwareFileInfo(_firmwareFilePath);
if (!firmwareFileInfo.exists()) {
auto errorMsg = QStringLiteral("Firmware file does not exist: %1").arg(_firmwareFilePath);
qCCritical(FLASH) << errorMsg;
setState(Error, errorMsg);
return;
}
QStringList cmd {stm32flashPath(), "-w", firmwareFileInfo.absoluteFilePath(), _verify ? "-v" : "", "-g", "0x0",
"-b", baudRate, portLocation};
_firmwareProcess = QSharedPointer<QProcess>(new QProcess);
connect(_firmwareProcess.data(), &QProcess::readyReadStandardOutput, this, [this] {
// Error strings used to detect important messages for the user
static const QStringList errorStrings = {
"error",
"fail",
"fatal",
"invalid",
"unexpected",
};
QString output(_firmwareProcess->readAllStandardOutput());
for (const auto& errorString : errorStrings) {
if (output.contains(errorString, Qt::CaseInsensitive)) {
qCCritical(FLASH) << output;
setState(Error, output);
// Break is necessary to avoid messages with multiple keys like:
// Error number... Fatal behaviour.. Unexpected port.
break;
}
}
firmwareUpdatePercentage(output);
});
connect(_firmwareProcess.data(), &QProcess::errorOccurred, this, [this](QProcess::ProcessError error) {
QString message = QStringLiteral("Unexpected error in process (%1): %2")
.arg(QString::number(error), _firmwareProcess->errorString());
qCCritical(FLASH) << message;
setState(Error, message);
});
connect(_firmwareProcess.data(), qOverload<int, QProcess::ExitStatus>(&QProcess::finished), this,
[](int exitCode, QProcess::ExitStatus exitStatus) {
if (exitCode == 0) {
return;
}
// It's not necessary to use setMessage here, since this signal is a result of the process
// and not from stm32flash
static QString message;
if (exitStatus == QProcess::NormalExit) {
message = QStringLiteral("Process exited normally with exit code: %1").arg(exitCode);
qCWarning(FLASH) << message;
} else {
message = QStringLiteral("Process crashed with exit code: %1").arg(exitCode);
qCCritical(FLASH) << message;
}
});
connect(_firmwareProcess.data(), &QProcess::stateChanged, this,
[this](QProcess::ProcessState state) { qCDebug(FLASH) << "New process state:" << state; });
_firmwareProcess->setEnvironment(QProcess::systemEnvironment());
_firmwareProcess->setProcessChannelMode(QProcess::MergedChannels);
qCDebug(FLASH) << "3... 2... 1...";
qCDebug(FLASH) << cmd;
auto program = cmd.takeFirst();
_firmwareProcess->start(program, cmd);
emit flashProgress(0);
}
void Flasher::firmwareUpdatePercentage(const QString& output)
{
// Track values like: (12.23%)
QRegularExpression regex("\\d{1,3}[.]\\d\\d");
QRegularExpressionMatch match = regex.match(output);
if (match.hasMatch()) {
QStringList percs = match.capturedTexts();
for (const auto& perc : percs) {
float _fw_update_perc = perc.toFloat();
qCDebug(FLASH) << _fw_update_perc;
if (_fw_update_perc > 99.99) {
QThread::msleep(1000);
setState(FlashFinished);
} else {
emit flashProgress(_fw_update_perc);
}
}
}
qCDebug(FLASH) << output;
}
void Flasher::setState(Flasher::States state, QString message)
{
if (_state != state) {
_state = state;
emit stateChanged(state);
}
setMessage(message);
}
void Flasher::setMessage(const QString& message)
{
auto msg = message.trimmed();
if (msg != _message) {
_message = msg;
emit messageChanged();
}
}