QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
AndroidSerialPort.h
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <memory>
5#include <optional>
6
7#include <QtCore/QByteArray>
8#include <QtCore/QIODevice>
9#include <QtCore/QLoggingCategory>
10#include <QtCore/QMutex>
11#include <QtCore/QString>
12#include <QtCore/QWaitCondition>
13
14Q_DECLARE_LOGGING_CATEGORY(AndroidSerialPortLog)
15
17class QDeadlineTimer;
18class QSerialPortInfo;
19
28class QSerialPort : public QIODevice
29{
30 Q_OBJECT
31
32public:
33 explicit QSerialPort(QObject* parent = nullptr);
34 explicit QSerialPort(const QString& name, QObject* parent = nullptr);
35 explicit QSerialPort(const QSerialPortInfo& info, QObject* parent = nullptr);
36 ~QSerialPort() override;
37
44 Q_FLAG(Direction)
45 Q_DECLARE_FLAGS(Directions, Direction)
46
48 {
49 Baud1200 = 1200,
50 Baud2400 = 2400,
51 Baud4800 = 4800,
52 Baud9600 = 9600,
53 Baud19200 = 19200,
54 Baud38400 = 38400,
55 Baud57600 = 57600,
56 Baud115200 = 115200
57 };
58 Q_ENUM(BaudRate)
59
61 {
62 Data5 = 5,
63 Data6 = 6,
64 Data7 = 7,
66 };
67 Q_ENUM(DataBits)
68
77 Q_ENUM(Parity)
78
80 {
84 };
85 Q_ENUM(StopBits)
86
93 Q_ENUM(FlowControl)
94
107 Q_FLAG(PinoutSignal)
108 Q_DECLARE_FLAGS(PinoutSignals, PinoutSignal)
109
124 Q_ENUM(SerialPortError)
125
126 void setPortName(const QString& name);
127 QString portName() const;
128 void setPort(const QSerialPortInfo& info);
129
130 bool open(OpenMode mode) override;
131 void close() override;
132
135 bool setBaudRate(qint32 baudRate, Directions directions = AllDirections);
136 qint32 baudRate(Directions directions = AllDirections) const;
138
139 DataBits dataBits() const { return _dataBits; }
140
141 bool setParity(Parity parity);
142
143 Parity parity() const { return _parity; }
144
146
147 StopBits stopBits() const { return _stopBits; }
148
150
151 FlowControl flowControl() const { return _flowControl; }
152
155 bool setDataTerminalReady(bool set);
156 bool isDataTerminalReady();
157 bool setRequestToSend(bool set);
158 bool isRequestToSend();
159 PinoutSignals pinoutSignals();
160 bool setBreakEnabled(bool set = true);
161
162 bool isBreakEnabled() const { return _breakEnabled; }
163
165 bool flush();
166 bool clear(Directions directions = AllDirections);
167
168 SerialPortError error() const { return _error; }
169
170 void clearError();
171
175 void setReadBufferSize(qint64 size);
176
177 qint64 readBufferSize() const { return _readBufferMaxSize; }
178
180 int handle() const;
181
182 bool isSequential() const override;
183 qint64 bytesAvailable() const override;
184 qint64 bytesToWrite() const override;
185 bool canReadLine() const override;
187 bool waitForReadyRead(int msecs = 30000) override;
188 bool waitForBytesWritten(int msecs = 30000) override;
189
190signals:
191 void baudRateChanged(qint32 baudRate, QSerialPort::Directions directions);
197 void requestToSendChanged(bool set);
200 void breakEnabledChanged(bool set);
201
202protected:
203 qint64 readData(char* data, qint64 maxSize) override;
204 qint64 writeData(const char* data, qint64 maxSize) override;
205
206private:
207 // The only way a backend reaches into the port.
209
210 void _setError(SerialPortError error, const QString& text = QString());
211
213 AndroidSerialBackend* _backendFor(const QString& location);
215 AndroidSerialBackend* _openBackend();
218 bool _openDevice(OpenMode mode);
219 void _closeDevice();
221 void _discardBuffers();
224 bool _clearDevice(AndroidSerialBackend* backend, Directions directions);
225
228 bool _readOn();
231 bool _readOnIfRoom();
232 qsizetype _hiddenSizeLocked() const;
233 bool _hasRoomForInputLocked() const;
236 bool _canAdmitInputLocked() const;
239 bool _admitInputLocked();
240 bool _admitArrivedInput();
241 void _scheduleAdmission();
242 void _admitQueuedInput();
244 void _announceAdmittedInput();
245
246 void _scheduleWrite();
247 void _pumpQueuedWrites();
250 bool _pumpWrites();
253 bool _writeUntil(const QDeadlineTimer& deadline);
254 void _flushBeforeClose();
255
256 // For the backend, as AndroidSerialBackendHost describes them.
257 bool _newDataArrived(const char* bytes, int length);
258 bool _hasRoomForInput();
259 bool _inputArrived();
260 void _waitForArrivedInput(const QDeadlineTimer& deadline);
261 void _writeCompleted(qint64 bytes);
262 void _waitForWriteProgress(const QDeadlineTimer& deadline);
263 void _interruptWait();
264
265 QString _systemLocation;
266 qint32 _baudRate = Baud9600;
267 DataBits _dataBits = Data8;
268 Parity _parity = NoParity;
269 StopBits _stopBits = OneStop;
270 FlowControl _flowControl = NoFlowControl;
271 bool _breakEnabled = false;
272 // What the port last set each line to since it opened, by which it tells a change. Asking the device instead
273 // would read all its control lines, for which a CDC-ACM device is polled from then until the port closes.
274 std::optional<bool> _dataTerminalReady;
275 std::optional<bool> _requestToSend;
276 SerialPortError _error = NoError;
277
278 // Each is made when first needed and then kept. A backend reports an error from inside its own function,
279 // and the port's owner may close the port in response, so a close must not destroy it.
280 std::unique_ptr<AndroidSerialBackend> _usbBackend;
281 std::unique_ptr<AndroidSerialBackend> _posixBackend;
282 // The backend of the open port, or null.
283 AndroidSerialBackend* _backend = nullptr;
284
285 // Guards what other threads bring, and announces it: input that arrived and reports of output written.
286 mutable QMutex _arrivalMutex;
287 QWaitCondition _arrivalCondition;
288 // All input that has arrived and that the owner has not read.
289 QByteArray _input;
290 // How much of it, from the front, the owner may read. The rest is hidden until the read buffer size allows
291 // it. Only the port's thread changes it.
292 qsizetype _admitted = 0;
293 qint64 _readBufferMaxSize = 0;
294 // Whether the backend was last told that the port had no room, and has not been asked to read since.
295 bool _readingHeldBack = false;
296 // What _writeCompleted() has been told since _pumpWrites() last looked, and whether it was called at all.
297 qint64 _completedWriteBytes = 0;
298 bool _writeProgress = false;
299 // Whether _interruptWait() has been called since a wait last ended.
300 bool _waitInterrupted = false;
301
302 std::atomic<bool> _admissionPending{false};
303 bool _emittingReadyRead = false;
304
305 // Output that the backend has not taken yet.
306 QByteArray _output;
307 std::atomic<bool> _writePending{false};
308 // Output that the backend has taken and not yet reported written. bytesToWrite() counts it.
309 qint64 _bytesInFlight = 0;
310 // Output that clear() gave up while the backend had it. The backend still reports all of it, sent or not,
311 // which the port does not announce.
312 qint64 _discardedInFlight = 0;
313};
314
315Q_DECLARE_OPERATORS_FOR_FLAGS(QSerialPort::Directions)
316Q_DECLARE_OPERATORS_FOR_FLAGS(QSerialPort::PinoutSignals)
qint64 readData(char *data, qint64 maxSize) override
qint32 baudRate(Directions directions=AllDirections) const
bool setDataTerminalReady(bool set)
void requestToSendChanged(bool set)
qint64 readBufferSize() const
void dataTerminalReadyChanged(bool set)
Parity parity() const
bool isSequential() const override
bool open(OpenMode mode) override
void close() override
bool setStopBits(StopBits stopBits)
void setPortName(const QString &name)
void baudRateChanged(qint32 baudRate, QSerialPort::Directions directions)
void parityChanged(QSerialPort::Parity parity)
qint64 writeData(const char *data, qint64 maxSize) override
bool waitForBytesWritten(int msecs=30000) override
void errorOccurred(QSerialPort::SerialPortError error)
Emitted whenever the error is set, also to NoError.
void setPort(const QSerialPortInfo &info)
bool waitForReadyRead(int msecs=30000) override
Returns true only for input that was not announced before.
StopBits stopBits() const
qint64 bytesToWrite() const override
void breakEnabledChanged(bool set)
bool clear(Directions directions=AllDirections)
bool setRequestToSend(bool set)
bool canReadLine() const override
QSerialPort(QObject *parent=nullptr)
void stopBitsChanged(QSerialPort::StopBits stopBits)
void dataBitsChanged(QSerialPort::DataBits dataBits)
bool flush()
Hands buffered output to the device, as far as it takes it now, and returns whether it took any.
friend class AndroidSerialBackendHost
PinoutSignals pinoutSignals()
FlowControl flowControl() const
QString portName() const
void flowControlChanged(QSerialPort::FlowControl flowControl)
bool setBreakEnabled(bool set=true)
DataBits dataBits() const
int handle() const
The file descriptor of the open device, or -1.
SerialPortError error() const
bool setBaudRate(qint32 baudRate, Directions directions=AllDirections)
~QSerialPort() override
bool setDataBits(DataBits dataBits)
bool setParity(Parity parity)
bool isBreakEnabled() const
qint64 bytesAvailable() const override
bool setFlowControl(FlowControl flowControl)
void setReadBufferSize(qint64 size)