2015-03-01 20:17:46 +00:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
|
2015-03-01 20:17:46 +00:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2015-03-01 20:17:46 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DEVICELINEREADER_H
|
|
|
|
#define DEVICELINEREADER_H
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QIODevice>
|
2015-03-01 20:17:46 +00:00
|
|
|
#include <QObject>
|
|
|
|
#include <QQueue>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QString>
|
2015-03-01 20:17:46 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Encapsulates a QIODevice and implements the same methods of its API that are
|
2016-11-11 14:55:16 +00:00
|
|
|
* used by LanDeviceLink and BluetoothDeviceLink, but readyRead is emitted only
|
|
|
|
* when a newline is found.
|
2015-03-01 20:17:46 +00:00
|
|
|
*/
|
2022-09-10 22:23:52 +01:00
|
|
|
class DeviceLineReader : public QObject
|
2015-03-01 20:17:46 +00:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2022-09-10 22:23:52 +01:00
|
|
|
DeviceLineReader(QIODevice *device, QObject *parent = 0);
|
|
|
|
|
|
|
|
QByteArray readLine()
|
|
|
|
{
|
|
|
|
return m_packets.dequeue();
|
|
|
|
}
|
|
|
|
qint64 write(const QByteArray &data)
|
|
|
|
{
|
|
|
|
return m_device->write(data);
|
|
|
|
}
|
|
|
|
qint64 bytesAvailable() const
|
|
|
|
{
|
|
|
|
return m_packets.size();
|
|
|
|
}
|
2015-03-01 20:17:46 +00:00
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
void readyRead();
|
|
|
|
void disconnected();
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void dataReceived();
|
|
|
|
|
|
|
|
private:
|
2017-09-03 20:39:44 +01:00
|
|
|
QByteArray m_lastChunk;
|
2022-09-10 22:23:52 +01:00
|
|
|
QIODevice *m_device;
|
2018-03-04 19:48:51 +00:00
|
|
|
QQueue<QByteArray> m_packets;
|
2015-03-01 20:17:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|