Skip to content

Commit e0ebd8d

Browse files
committed
Clean up and refactor; make sure loader releases on destructor (PROPMAN-29)
1 parent 8fe17d9 commit e0ebd8d

File tree

4 files changed

+112
-113
lines changed

4 files changed

+112
-113
lines changed

src/loader/propellerloader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
#include "../util/logging.h"
99

10-
PropellerLoader::PropellerLoader(PropellerManager * manager,
11-
const QString & portname,
10+
PropellerLoader::PropellerLoader(PropellerManager * manager, const QString & portname,
1211
QObject * parent)
1312
: QObject(parent)
1413
{
@@ -113,6 +112,7 @@ PropellerLoader::PropellerLoader(PropellerManager * manager,
113112

114113
PropellerLoader::~PropellerLoader()
115114
{
115+
session->release();
116116
delete session;
117117
}
118118

src/manager/propellermanager.cpp

Lines changed: 66 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ void PropellerManager::removeConnection(PropellerSession * session, PropellerDev
141141

142142
if (!_active_sessions[device])
143143
{
144-
// qCDebug(pmanager) << "closing" << _devices.key(device);
145144
device->close();
146145
}
147146
}
@@ -159,18 +158,18 @@ void PropellerManager::removeDevice(QString port)
159158
_devices.remove(port);
160159
}
161160

162-
bool PropellerManager::portIsBusy(PropellerSession * session, const QString & name)
161+
bool PropellerManager::portIsBusy(PropellerSession * session)
163162
{
164-
if (isPaused(session)) return true;
163+
QString port = session->portName();
165164

166-
PropellerDevice * device = getDevice(name, false);
167-
if (!_connections.contains(session) || _connections[session] != device) return true;
165+
if (isPaused(session)) return true;
168166

169-
// qDebug() << "CONNECTION" << session << name << device << _connections[session];
167+
PropellerDevice * device = getDevice(port, false);
170168

169+
if (!_connections.contains(session)) return true;
171170
if (_busy.contains(device) && _busy[device] != session) return true;
172171

173-
addConnectionByName(session, name);
172+
addConnectionByName(session, port);
174173
return false;
175174
}
176175

@@ -195,10 +194,10 @@ void PropellerManager::restoreConnections(PropellerDevice * device)
195194
}
196195
}
197196

198-
bool PropellerManager::reserve(PropellerSession * session, QString port)
197+
bool PropellerManager::reserve(PropellerSession * session)
199198
{
200-
if (portIsBusy(session, port)) return false;
201-
PropellerDevice * device = getDevice(port);
199+
if (portIsBusy(session)) return false;
200+
PropellerDevice * device = getDevice(session->portName());
202201
_active_sessions[device]++; // prevent port from closing while reserving device
203202

204203
saveConnections(device);
@@ -210,23 +209,23 @@ bool PropellerManager::reserve(PropellerSession * session, QString port)
210209
return true;
211210
}
212211

213-
bool PropellerManager::isReserved(PropellerSession * session, QString port)
212+
bool PropellerManager::isReserved(PropellerSession * session)
214213
{
215-
if (portIsBusy(session, port)) return false;
214+
if (portIsBusy(session)) return false;
216215

217-
PropellerDevice * device = getDevice(port);
216+
PropellerDevice * device = getDevice(session->portName());
218217
if (!_busy.contains(device) || _busy[device] != session)
219218
return false;
220219

221220
return true;
222221
}
223222

224223

225-
void PropellerManager::release(PropellerSession * session, QString port)
224+
void PropellerManager::release(PropellerSession * session)
226225
{
227-
if (!isReserved(session, port)) return;
226+
if (!isReserved(session)) return;
228227

229-
PropellerDevice * device = getDevice(port);
228+
PropellerDevice * device = getDevice(session->portName());
230229
_active_sessions[device]++; // prevent port from closing while releasing device
231230

232231
removeConnection(session, device);
@@ -251,112 +250,112 @@ void PropellerManager::unpause(PropellerSession * session)
251250
_paused.remove(session);
252251
}
253252

254-
bool PropellerManager::clear(PropellerSession * session, QString port)
253+
bool PropellerManager::clear(PropellerSession * session)
255254
{
256-
if (portIsBusy(session, port)) return false;
257-
return getDevice(port)->clear();
255+
if (portIsBusy(session)) return false;
256+
return getDevice(session->portName())->clear();
258257
}
259258

260-
bool PropellerManager::isOpen(PropellerSession * session, QString port)
259+
bool PropellerManager::isOpen(PropellerSession * session)
261260
{
262-
if (portIsBusy(session, port)) return false;
263-
return getDevice(port, false)->isOpen();
261+
if (portIsBusy(session)) return false;
262+
return getDevice(session->portName(), false)->isOpen();
264263
}
265264

266-
bool PropellerManager::setBaudRate(PropellerSession * session, QString port, quint32 baudRate)
265+
bool PropellerManager::setBaudRate(PropellerSession * session, quint32 baudRate)
267266
{
268-
if (portIsBusy(session, port)) return false;
269-
return getDevice(port)->setBaudRate(baudRate);
267+
if (portIsBusy(session)) return false;
268+
return getDevice(session->portName())->setBaudRate(baudRate);
270269
}
271270

272-
qint64 PropellerManager::bytesToWrite(PropellerSession * session, QString port)
271+
qint64 PropellerManager::bytesToWrite(PropellerSession * session)
273272
{
274-
if (portIsBusy(session, port)) return 0;
275-
return getDevice(port)->bytesToWrite();
273+
if (portIsBusy(session)) return 0;
274+
return getDevice(session->portName())->bytesToWrite();
276275
}
277276

278-
qint64 PropellerManager::bytesAvailable(PropellerSession * session, QString port)
277+
qint64 PropellerManager::bytesAvailable(PropellerSession * session)
279278
{
280-
if (portIsBusy(session, port)) return 0;
279+
if (portIsBusy(session)) return 0;
281280
return _buffers[session]->bytesAvailable();
282281
}
283282

284-
QByteArray PropellerManager::read(PropellerSession * session, QString port, qint64 maxSize)
283+
QByteArray PropellerManager::read(PropellerSession * session, qint64 maxSize)
285284
{
286-
if (portIsBusy(session, port)) return QByteArray();
285+
if (portIsBusy(session)) return QByteArray();
287286
return _buffers[session]->read(maxSize);
288287
}
289288

290-
QByteArray PropellerManager::readAll(PropellerSession * session, QString port)
289+
QByteArray PropellerManager::readAll(PropellerSession * session)
291290
{
292-
if (portIsBusy(session, port)) return QByteArray();
291+
if (portIsBusy(session)) return QByteArray();
293292
return _buffers[session]->readAll();
294293
}
295294

296-
bool PropellerManager::putChar(PropellerSession * session, QString port, char c)
295+
bool PropellerManager::putChar(PropellerSession * session, char c)
297296
{
298-
if (portIsBusy(session, port)) return false;
299-
return getDevice(port)->putChar(c);
297+
if (portIsBusy(session)) return false;
298+
return getDevice(session->portName())->putChar(c);
300299
}
301300

302-
qint64 PropellerManager::write(PropellerSession * session, QString port, const QByteArray & byteArray)
301+
qint64 PropellerManager::write(PropellerSession * session, const QByteArray & byteArray)
303302
{
304-
if (portIsBusy(session, port)) return -1;
305-
return getDevice(port)->write(byteArray);
303+
if (portIsBusy(session)) return -1;
304+
return getDevice(session->portName())->write(byteArray);
306305
}
307306

308-
quint32 PropellerManager::minimumTimeout(PropellerSession * session, QString port)
307+
quint32 PropellerManager::minimumTimeout(PropellerSession * session)
309308
{
310-
if (portIsBusy(session, port)) return 0;
311-
return getDevice(port)->minimumTimeout();
309+
if (portIsBusy(session)) return 0;
310+
return getDevice(session->portName())->minimumTimeout();
312311
}
313312

314-
void PropellerManager::setMinimumTimeout(PropellerSession * session, QString port, quint32 milliseconds)
313+
void PropellerManager::setMinimumTimeout(PropellerSession * session, quint32 milliseconds)
315314
{
316-
if (portIsBusy(session, port)) return;
317-
getDevice(port)->setMinimumTimeout(milliseconds);
315+
if (portIsBusy(session)) return;
316+
getDevice(session->portName())->setMinimumTimeout(milliseconds);
318317
}
319318

320-
quint32 PropellerManager::calculateTimeout(PropellerSession * session, QString port, quint32 bytes)
319+
quint32 PropellerManager::calculateTimeout(PropellerSession * session, quint32 bytes)
321320
{
322-
if (portIsBusy(session, port)) return 0;
323-
return getDevice(port)->calculateTimeout(bytes);
321+
if (portIsBusy(session)) return 0;
322+
return getDevice(session->portName())->calculateTimeout(bytes);
324323
}
325324

326-
void PropellerManager::useReset(PropellerSession * session, QString port, QString name, int pin)
325+
void PropellerManager::useReset(PropellerSession * session, QString name, int pin)
327326
{
328-
if (portIsBusy(session, port)) return;
329-
getDevice(port)->useReset(name, pin);
327+
if (portIsBusy(session)) return;
328+
getDevice(session->portName())->useReset(name, pin);
330329
}
331330

332-
void PropellerManager::useDefaultReset(PropellerSession * session, QString port)
331+
void PropellerManager::useDefaultReset(PropellerSession * session)
333332
{
334-
if (portIsBusy(session, port)) return;
335-
return getDevice(port)->useDefaultReset();
333+
if (portIsBusy(session)) return;
334+
return getDevice(session->portName())->useDefaultReset();
336335
}
337336

338-
bool PropellerManager::reset(PropellerSession * session, QString port)
337+
bool PropellerManager::reset(PropellerSession * session)
339338
{
340-
if (portIsBusy(session, port)) return false;
341-
return getDevice(port)->reset();
339+
if (portIsBusy(session)) return false;
340+
return getDevice(session->portName())->reset();
342341
}
343342

344-
quint32 PropellerManager::resetPeriod(PropellerSession * session, QString port)
343+
quint32 PropellerManager::resetPeriod(PropellerSession * session)
345344
{
346-
if (portIsBusy(session, port)) return 0;
347-
return getDevice(port)->resetPeriod();
345+
if (portIsBusy(session)) return 0;
346+
return getDevice(session->portName())->resetPeriod();
348347
}
349348

350-
int PropellerManager::error(PropellerSession * session, QString port)
349+
int PropellerManager::error(PropellerSession * session)
351350
{
352-
if (portIsBusy(session, port)) return 0;
353-
return getDevice(port)->error();
351+
if (portIsBusy(session)) return 0;
352+
return getDevice(session->portName())->error();
354353
}
355354

356-
QString PropellerManager::errorString(PropellerSession * session, QString port)
355+
QString PropellerManager::errorString(PropellerSession * session)
357356
{
358-
if (portIsBusy(session, port)) return QString();
359-
return getDevice(port)->errorString();
357+
if (portIsBusy(session)) return QString();
358+
return getDevice(session->portName())->errorString();
360359
}
361360

362361
QStringList PropellerManager::listPorts()

src/manager/propellermanager.h

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class PropellerManager : public QObject
107107
void addConnection (PropellerSession * session, PropellerDevice * device);
108108
void addConnectionByName (PropellerSession * session, QString port);
109109
void removeConnection (PropellerSession * session, PropellerDevice * device);
110-
bool portIsBusy (PropellerSession * session, const QString & name);
110+
bool portIsBusy (PropellerSession * session);
111111

112112
void saveConnections (PropellerDevice * device);
113113
void restoreConnections (PropellerDevice * device);
@@ -129,33 +129,33 @@ private slots:
129129
bool beginSession(PropellerSession * session);
130130
void endSession(PropellerSession * session);
131131

132-
bool isOpen(PropellerSession * session, QString port);
133-
bool clear(PropellerSession * session, QString port);
132+
bool isOpen(PropellerSession * session);
133+
bool clear(PropellerSession * session);
134134

135-
bool setBaudRate(PropellerSession * session, QString port, quint32 baudRate);
135+
bool setBaudRate(PropellerSession * session, quint32 baudRate);
136136

137-
qint64 bytesToWrite(PropellerSession * session, QString port);
138-
qint64 bytesAvailable(PropellerSession * session, QString port);
137+
qint64 bytesToWrite(PropellerSession * session);
138+
qint64 bytesAvailable(PropellerSession * session);
139139

140-
QByteArray read(PropellerSession * session, QString port, qint64 maxSize);
141-
QByteArray readAll(PropellerSession * session, QString port);
140+
QByteArray read(PropellerSession * session, qint64 maxSize);
141+
QByteArray readAll(PropellerSession * session);
142142

143-
bool putChar(PropellerSession * session, QString port, char c);
144-
qint64 write(PropellerSession * session, QString port, const QByteArray & byteArray);
145-
QString errorString(PropellerSession * session, QString port);
146-
int error(PropellerSession * session, QString port);
143+
bool putChar(PropellerSession * session, char c);
144+
qint64 write(PropellerSession * session, const QByteArray & byteArray);
145+
QString errorString(PropellerSession * session);
146+
int error(PropellerSession * session);
147147

148-
quint32 minimumTimeout(PropellerSession * session, QString port);
149-
void setMinimumTimeout(PropellerSession * session, QString port, quint32 milliseconds);
150-
quint32 calculateTimeout(PropellerSession * session, QString port, quint32 bytes);
151-
void useReset(PropellerSession * session, QString port, QString name, int pin);
152-
void useDefaultReset(PropellerSession * session, QString port);
153-
bool reset(PropellerSession * session, QString port);
154-
quint32 resetPeriod(PropellerSession * session, QString port);
148+
quint32 minimumTimeout(PropellerSession * session);
149+
void setMinimumTimeout(PropellerSession * session, quint32 milliseconds);
150+
quint32 calculateTimeout(PropellerSession * session, quint32 bytes);
151+
void useReset(PropellerSession * session, QString name, int pin);
152+
void useDefaultReset(PropellerSession * session);
153+
bool reset(PropellerSession * session);
154+
quint32 resetPeriod(PropellerSession * session);
155155

156-
bool reserve(PropellerSession * session, QString port);
157-
bool isReserved(PropellerSession * session, QString port);
158-
void release(PropellerSession * session, QString port);
156+
bool reserve(PropellerSession * session);
157+
bool isReserved(PropellerSession * session);
158+
void release(PropellerSession * session);
159159

160160
void pause(PropellerSession * session);
161161
bool isPaused(PropellerSession * session);

0 commit comments

Comments
 (0)