Skip to content

Commit dcb02b3

Browse files
committed
Implement try to connect functionality
- try to connect 20 seconds, after that time, display failed to connect message - increase timeout in SerialPort::openConnBlocking() method to 20 seconds - do not try to detect board if port is not opened
1 parent a263608 commit dcb02b3

5 files changed

Lines changed: 78 additions & 38 deletions

File tree

flasher.cpp

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@ Flasher::Flasher() :
6060
m_fileFirmware(std::make_unique<QFile>()),
6161
m_keysFile(std::make_shared<QFile>()),
6262
m_socketClient(std::make_unique<SocketClient>()),
63-
m_state(FlasherStates::TRY_CONNECT),
63+
m_state(FlasherStates::INIT),
6464
m_firmwareSize(0),
6565
m_tryOpen(false),
6666
m_isPortOpen(false),
6767
m_isSecureBoot(true),
6868
m_boardId(),
6969
m_boardKey(),
70-
m_jsonObject()
70+
m_jsonObject(),
71+
m_isTryConnectStart(false)
7172
{
7273
m_keysFile->setFileName(KEY_FILE_NAME);
7374
m_keysFile->open(QIODevice::ReadWrite);
@@ -106,7 +107,7 @@ void Flasher::deinit()
106107

107108
void Flasher::openSerialPort()
108109
{
109-
this->setState(FlasherStates::PLUG_USB);
110+
this->setState(FlasherStates::TRY_TO_CONNECT);
110111
m_tryOpen = true;
111112
}
112113

@@ -119,35 +120,61 @@ void Flasher::closeSerialPort()
119120
void Flasher::loopHandler()
120121
{
121122
bool success;
122-
bool manufacturer;
123123

124-
switch (m_state)
125-
{
124+
switch (m_state) {
126125

127126
case FlasherStates::IDLE:
128127
if(m_fileFirmware->isOpen() && m_serialPort->isOpen()) {
129128
emit readyToFlashId();
130129
}
131130
break;
132131

133-
case FlasherStates::TRY_CONNECT:
134-
if(m_tryOpen) {
135-
manufacturer = m_serialPort->tryOpenPort();
132+
case FlasherStates::INIT:
136133

137-
if(!manufacturer) {
138-
this->setState(FlasherStates::PLUG_USB);
134+
if (m_tryOpen) {
135+
136+
if (!(m_serialPort->tryOpenPort())) {
137+
this->setState(FlasherStates::TRY_TO_CONNECT);
139138
}
140139
}
141140

142141
break;
143142

144-
case FlasherStates::PLUG_USB:
145-
emit connectUsbToPc();
146-
m_serialPort->tryOpenPort();
147-
if(m_serialPort->isOpen()) {
148-
this->setState(FlasherStates::CONNECTED);
143+
case FlasherStates::TRY_TO_CONNECT:
144+
{
145+
bool isConnected = false;
146+
147+
if (m_isTryConnectStart) {
148+
149+
if (m_serialPort->tryOpenPort()) {
150+
151+
if (m_serialPort->isOpen()) {
152+
this->setState(FlasherStates::CONNECTED);
153+
m_isTryConnectStart = false;
154+
isConnected = true;
155+
156+
} else {
157+
emit connectUsbToPc("Trying to connect!");
158+
}
159+
160+
} else {
161+
emit connectUsbToPc("Trying to connect!");
162+
}
163+
164+
if ((!isConnected) && (m_timerTryConnect.hasExpired(TRY_TO_CONNECT_TIMEOUT_IN_MS))) {
165+
emit failedToConnect();
166+
this->setState(FlasherStates::INIT);
167+
m_tryOpen = false;
168+
m_isTryConnectStart = false;
169+
}
170+
171+
} else {
172+
m_isTryConnectStart = true;
173+
m_timerTryConnect.start();
149174
}
175+
150176
break;
177+
}
151178

152179
case FlasherStates::CONNECTED:
153180

@@ -166,6 +193,7 @@ void Flasher::loopHandler()
166193
break;
167194

168195
case FlasherStates::DISCONNECTED:
196+
m_isTryConnectStart = false;
169197
m_serialPort->closeConn();
170198
if(!(m_serialPort->isOpen())) {
171199
emit disconnectedSerialPort();
@@ -180,7 +208,7 @@ void Flasher::loopHandler()
180208
this->setState(FlasherStates::BOARD_CHECK_REGISTRATION);
181209
} else {
182210
emit textInBrowser("Board ID Error. Unplug your board, press disconnect/connect, and plug your board again.");
183-
this->setState(FlasherStates::TRY_CONNECT);
211+
this->setState(FlasherStates::INIT);
184212
}
185213

186214
break;

flasher.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@
3838
#include <QJsonObject>
3939
#include <QThread>
4040
#include <QFile>
41+
#include <QElapsedTimer>
4142
#include <socket.h>
4243

4344
enum class FlasherStates {
4445
IDLE,
45-
TRY_CONNECT,
46-
PLUG_USB,
46+
INIT,
47+
TRY_TO_CONNECT,
4748
CONNECTED,
4849
DISCONNECTED,
4950
BOARD_ID,
@@ -101,9 +102,10 @@ Q_OBJECT
101102
signals:
102103
void updateProgress(const qint64& dataPosition, const qint64& firmwareSize);
103104
void clearProgress();
104-
void connectUsbToPc();
105+
void connectUsbToPc(const QString& text);
105106
void connectedSerialPort();
106107
void disconnectedSerialPort();
108+
void failedToConnect();
107109
void openSerialPortInThread();
108110
void closeSerialPortInThread();
109111
void runLoop();
@@ -131,6 +133,8 @@ public slots:
131133
QString m_boardKey;
132134
QJsonObject m_jsonObject;
133135
QString m_filePath;
136+
bool m_isTryConnectStart;
137+
QElapsedTimer m_timerTryConnect;
134138

135139
static constexpr qint64 SIGNATURE_SIZE {64};
136140
static constexpr int ERASE_TIMEOUT_IN_MS {5000};
@@ -143,6 +147,7 @@ public slots:
143147
static constexpr int BOARD_ID_SIZE_STRING {BOARD_ID_SIZE * 2};
144148
static constexpr int KEY_SIZE {32};
145149
static constexpr int KEY_SIZE_STRING {KEY_SIZE * 2};
150+
static constexpr int TRY_TO_CONNECT_TIMEOUT_IN_MS {20000};
146151

147152
static const QString KEY_FILE_NAME;
148153
static const QByteArray NOT_SECURED_MAGIC_STRING;

mainwindow.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,25 @@ MainWindow::MainWindow(std::shared_ptr<Flasher> flasher, QWidget *parent) :
7070
m_ui->progressBar->setValue(0);
7171
});
7272

73-
connect(m_flasher.get(), &Flasher::connectUsbToPc, this, [&] (void) { this->showStatusMessage(tr("Reconnect board to PC")); });
73+
connect(m_flasher.get(), &Flasher::connectUsbToPc, this, [&] (const QString& text) { this->showStatusMessage(text); });
7474

7575
connect(m_flasher.get(), &Flasher::connectedSerialPort, this, [&] (void) {
7676
this->showStatusMessage(tr("Connected"));
77-
this->openSerialPortUi();
77+
m_ui->actionConnect->setEnabled(false);
78+
m_ui->actionDisconnect->setEnabled(true);
7879
});
7980

8081
connect(m_flasher.get(), &Flasher::disconnectedSerialPort, this, [&] (void) {
8182
this->showStatusMessage(tr("Disconnected"));
8283
this->closeSerialPortUi();
8384
});
8485

86+
connect(m_flasher.get(), &Flasher::failedToConnect, this, [&] (void) {
87+
this->showStatusMessage(tr("Failed to connect!"));
88+
m_ui->actionConnect->setEnabled(true);
89+
m_ui->actionDisconnect->setEnabled(false);
90+
});
91+
8592
connect(m_flasher.get(), &Flasher::textInBrowser, this, [&] (const auto& text) { m_ui->textBrowser->append(text); });
8693

8794
connect(m_flasher.get(), &Flasher::isBootloader, this, &MainWindow::isBootloaderUi);

serial_port.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "serial_port.h"
3636
#include <QDebug>
3737
#include <QSerialPortInfo>
38+
#include <QElapsedTimer>
3839

3940
const char SerialPort::SOFTWARE_TYPE_CMD[14] = "software_type";
4041
const QString SerialPort::MANUFACT_IMBOOT = "IMBOOT";
@@ -46,12 +47,8 @@ const QString SerialPort::SW_TYPE_IMAPP = "IMApplication";
4647
SerialPort::SerialPort()
4748
: m_settings(std::make_unique<SettingsDialog>()),
4849
m_isOpen(false),
49-
m_timer(new QTimer),
5050
m_isBootlaoder(false)
5151
{
52-
m_timer.setSingleShot(true);
53-
m_timer.setInterval(TIMER_TIMEOUT_IN_MS);
54-
m_timer.start();
5552
}
5653

5754
void SerialPort::openConn()
@@ -74,9 +71,13 @@ void SerialPort::openConn()
7471

7572
void SerialPort::openConnBlocking()
7673
{
77-
while(!m_isOpen) {
74+
QElapsedTimer timer;
75+
timer.start();
76+
77+
while (!m_isOpen) {
7878
tryOpenPort();
79-
if(m_timer.remainingTime() == 0){
79+
80+
if (timer.hasExpired(TIMER_TIMEOUT_IN_MS)) {
8081
qInfo() << "Timeout";
8182
break;
8283
}
@@ -127,13 +128,14 @@ bool SerialPort::tryOpenPort()
127128
if(success) {
128129
openConn();
129130

130-
// For Microsoft, we can't be sure if we are connected to the proper board so we need to check this.
131-
bool isBoardDetected = detectBoard();
132-
if(isBoardDetected) {
133-
// we are connected to proper board, exit for loop
134-
break;
135-
} else {
136-
if(m_isOpen) {
131+
if (m_isOpen) {
132+
133+
// For Microsoft, we can't be sure if we are connected to the proper board so we need to check this.
134+
if (detectBoard()) {
135+
// we are connected to proper board, exit for loop
136+
break;
137+
138+
} else {
137139
closeConn();
138140
}
139141
}

serial_port.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#define SERIAL_PORT_H
3737

3838
#include <QSerialPort>
39-
#include <QTimer>
4039
#include "settingsdialog.h"
4140

4241
class SerialPort : public QSerialPort {
@@ -58,10 +57,9 @@ class SerialPort : public QSerialPort {
5857
std::unique_ptr<SettingsDialog> m_settings;
5958
bool m_isOpen;
6059
SettingsDialog::Settings m_port;
61-
QTimer m_timer;
6260
bool m_isBootlaoder;
6361

64-
static constexpr int TIMER_TIMEOUT_IN_MS {10000};
62+
static constexpr int TIMER_TIMEOUT_IN_MS {20000};
6563
static constexpr int SERIAL_TIMEOUT_IN_MS {100};
6664
static const char SOFTWARE_TYPE_CMD[14];
6765
static const QString MANUFACT_IMBOOT;

0 commit comments

Comments
 (0)