-
Notifications
You must be signed in to change notification settings - Fork 6
/
sessionmanager.cpp
246 lines (205 loc) · 7.16 KB
/
sessionmanager.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
* \file
* <!--
* Copyright 2015 Develer S.r.l. (http://www.develer.com/)
* -->
*
* \brief SessionManager class implementation
*
* \author Aurelien Rainone <aurelien@develer.com>
*/
#include "sessionmanager.h"
#include "outputmanager.h"
#include "xmodemtransfer.h"
#include <QCoreApplication>
#include <QSerialPortInfo>
#include <QProgressDialog>
#include <QMessageBox>
#include <QFile>
SessionManager::SessionManager(QObject *parent) :
QObject(parent)
{
serial = new QSerialPort();
in_progress = false;
file_transfer = 0;
connect(serial, &QSerialPort::readyRead, this, &SessionManager::readData);
connect(serial, static_cast<void (QSerialPort::*)(QSerialPort::SerialPortError)>
(&QSerialPort::error), this, &SessionManager::handleError);
}
SessionManager::~SessionManager()
{
if (serial)
{
// closes connection if needed
if (serial->isOpen())
serial->close();
delete serial;
}
}
void SessionManager::handleError(QSerialPort::SerialPortError serialPortError)
{
switch (serialPortError)
{
// no error
case QSerialPort::NoError:
break;
// recoverable errors : inform user and clear error
case QSerialPort::OpenError:
QMessageBox::warning(NULL, tr("Error"), serial->errorString());
// reset error
serial->clearError();
break;
// unrecoverable errors : inform user and close the port/connection
default:
if (in_progress)
{
QMessageBox::critical(NULL, tr("Error"), serial->errorString());
// on some error (ex: hot unplugging) the 'QSerialPort::error' property successively
// takes multiple values.
// to prevent from displaying successive error messages, the in_progress flag is
// set to indicate that we are not interested by next messages, until the user tries to open
// again the serial port
in_progress = false;
if (serial->isOpen())
{
serial->clearError();
closeSession();
}
}
break;
}
}
void SessionManager::openSession(const QHash<QString, QString>& port_cfg)
{
bool cfg_ok = true, ok;
// try converting port config from the hash
QSerialPort::BaudRate baud_rate = static_cast<QSerialPort::BaudRate>
(port_cfg["baud_rate"].toInt(&ok));
cfg_ok &= ok;
QSerialPort::DataBits data_bits = static_cast<QSerialPort::DataBits>
(port_cfg["data_bits"].toInt(&ok));
cfg_ok &= ok;
QSerialPort::Parity parity = static_cast<QSerialPort::Parity>
(port_cfg["parity"].toInt(&ok));
cfg_ok &= ok;
QSerialPort::StopBits stop_bits = static_cast<QSerialPort::StopBits>
(port_cfg["stop_bits"].toInt(&ok));
cfg_ok &= ok;
QSerialPort::FlowControl flow_control = static_cast<QSerialPort::FlowControl>
(port_cfg["flow_control"].toInt(&ok));
cfg_ok &= ok;
// a conversion didn't make it
Q_ASSERT_X(cfg_ok, "SessionManager::openSession", "a conversion didn't make it");
// configure port
#if (QT_VERSION < QT_VERSION_CHECK(5, 5, 0)) && defined(Q_OS_MAC)
// 'device not found' error on MacOsX when we try to open port
// after calling setPortName with a full device name (eg: /dev/ttyUSB01)
// issues:
// - https://github.com/develersrl/cutecom-ng/issues/7
// - https://github.com/develersrl/cutecom-ng/issues/17
// this is a QSerialPort bug, corrected for Qt > 5.5:
// - https://codereview.qt-project.org/#/c/108571/
if (port_cfg[QStringLiteral("device")].contains('/'))
serial->setPortName(port_cfg[QStringLiteral("device")]);
else
serial->setPort(QSerialPortInfo(port_cfg[QStringLiteral("device")]));
#else
// tested on linux and windows
// and this is necessary to make QSerialPort work with pseudo
// terminal created with socat for example
serial->setPortName(port_cfg[QStringLiteral("device")]);
#endif
serial->setBaudRate(baud_rate);
serial->setDataBits(data_bits);
serial->setParity(parity);
serial->setStopBits(stop_bits);
serial->setFlowControl(flow_control);
// flag indicating that a connection is in progress (eventually successful or not)
in_progress = true;
// open serial port
if (serial->open(QIODevice::ReadWrite))
{
curr_cfg = port_cfg;
emit sessionOpened();
}
else
{
// here, stopped means 'no connection is in progress'
emit sessionClosed();
}
}
void SessionManager::closeSession()
{
if (serial->isOpen())
{
serial->close();
emit sessionClosed();
}
}
bool SessionManager::isSessionOpen() const
{
return serial->isOpen();
}
void SessionManager::saveToFile(const QByteArray &data)
{
QFile dump(curr_cfg["dump_file"]);
// mode is OR'ed with 'Text' flag in "Ascii" mode
QIODevice::OpenMode mode = QIODevice::Append;
if (curr_cfg["dump_file"] == QString::number(ConnectDialog::Ascii))
mode |= QIODevice::Text;
if (dump.open(mode))
dump.write(data);
}
void SessionManager::readData()
{
QByteArray data(serial->readAll());
emit dataReceived(data);
// append to dump file if configured
if (curr_cfg["dump_enabled"] == "1")
saveToFile(data);
}
void SessionManager::sendToSerial(const QByteArray &data)
{
serial->write(data);
}
void SessionManager::transferFile(const QString &filename, Protocol type)
{
switch (type)
{
case XMODEM:
file_transfer = new XModemTransfer(0, serial, filename);
break;
case YMODEM:
case ZMODEM:
Q_ASSERT_X(false, "SessionManager::transferFile", "not implemented");
break;
default:
return;
}
connect(file_transfer, &FileTransfer::transferEnded,
this, &SessionManager::handleFileTransferEnded);
// forward FileTransfer::transferProgressed signals
connect(file_transfer, &FileTransfer::transferProgressed,
this, &SessionManager::fileTransferProgressed);
disconnect(serial, static_cast<void (QSerialPort::*)(QSerialPort::SerialPortError)>
(&QSerialPort::error), this, &SessionManager::handleError);
// perform transfer
if (!file_transfer->startTransfer())
// transfer never started, manually delete FileTransfer instance
delete file_transfer;
}
void SessionManager::handleFileTransferEnded(FileTransfer::TransferError error)
{
// re-connect serial port error handling for non-blocking use
connect(serial, static_cast<void (QSerialPort::*)(QSerialPort::SerialPortError)>
(&QSerialPort::error), this, &SessionManager::handleError);
// schedule file_transfer object deletion on main thread
QCoreApplication::postEvent(file_transfer, new QEvent(QEvent::DeferredDelete));
emit fileTransferEnded(error);
}
void SessionManager::handleTransferCancelledByUser()
{
Q_ASSERT_X(file_transfer, "SessionManager::handleTransferCancelledByUser",
"file_transfer shound not be NULL");
file_transfer->quit_requested = true;
}