QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
qserialport_p.h
Go to the documentation of this file.
1// Copyright (C) 2011-2012 Denis Shienkov <denis.shienkov@gmail.com>
2// Copyright (C) 2011 Sergey Belyashov <Sergey.Belyashov@gmail.com>
3// Copyright (C) 2012 Laszlo Papp <lpapp@kde.org>
4// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
5
6#pragma once
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/QLoggingCategory>
20#include <QtCore/QMutex>
21#include <QtCore/QWaitCondition>
22#include <QtCore/private/qiodevice_p.h>
23#include <QtCore/private/qproperty_p.h>
24
25#include <atomic>
26
27#include "AndroidSerial.h"
28#include "qserialport.h"
29
30constexpr int INVALID_DEVICE_ID = 0;
31constexpr int MIN_READ_TIMEOUT = 500;
32constexpr qint64 MAX_READ_SIZE = 16 * 1024;
34constexpr qint64 DEFAULT_WRITE_BUFFER_SIZE = 16 * 1024;
35constexpr int DEFAULT_WRITE_TIMEOUT = 5000;
36constexpr int DEFAULT_READ_TIMEOUT = 0;
37constexpr int EMIT_THRESHOLD = 64;
38
39#ifndef QSERIALPORT_BUFFERSIZE
40#define QSERIALPORT_BUFFERSIZE DEFAULT_WRITE_BUFFER_SIZE
41#endif
42
43Q_DECLARE_LOGGING_CATEGORY(AndroidSerialPortLog)
44
46
55
56class QSerialPortPrivate : public QIODevicePrivate
57{
58public:
59 Q_DECLARE_PUBLIC(QSerialPort)
60
62
63 bool open(QIODevice::OpenMode mode);
64 void close();
65
66 bool flush();
67 bool clear(QSerialPort::Directions directions);
68
69 QSerialPort::PinoutSignals pinoutSignals();
70
71 bool setDataTerminalReady(bool set);
72 bool setRequestToSend(bool set);
73
74 bool setBaudRate();
75 bool setBaudRate(qint32 baudRate, QSerialPort::Directions directions);
77 bool setParity(QSerialPort::Parity parity);
80
81 bool setBreakEnabled(bool set);
82
83 void setError(const QSerialPortErrorInfo& errorInfo);
84
86 {
87 setError(error_);
88 }
91
92 bool setBindableDataBits(QSerialPort::DataBits dataBits_)
93 {
94 return q_func()->setDataBits(dataBits_);
95 }
97 &QSerialPortPrivate::setBindableDataBits, QSerialPort::Data8)
98
99 bool setBindableParity(QSerialPort::Parity parity_)
100 {
101 return q_func()->setParity(parity_);
102 }
104 &QSerialPortPrivate::setBindableParity, QSerialPort::NoParity)
105
106 bool setBindableStopBits(QSerialPort::StopBits stopBits_)
107 {
108 return q_func()->setStopBits(stopBits_);
109 }
111 &QSerialPortPrivate::setBindableStopBits, QSerialPort::OneStop)
112
113 bool setBindableFlowControl(QSerialPort::FlowControl flowControl_)
114 {
115 return q_func()->setFlowControl(flowControl_);
116 }
118 &QSerialPortPrivate::setBindableFlowControl, QSerialPort::NoFlowControl)
119
120 bool setBindableBreakEnabled(bool isBreakEnabled_)
121 {
122 return q_func()->setBreakEnabled(isBreakEnabled_);
123 }
125 &QSerialPortPrivate::setBindableBreakEnabled, false)
126
127 bool waitForReadyRead(int msec);
128 bool waitForBytesWritten(int msec);
129
130 bool startAsyncRead();
131
132 qint64 writeData(const char* data, qint64 maxSize);
133
134 void newDataArrived(const char* bytes, int length);
135 void exceptionArrived(const QString& ex);
136
137 static QList<qint32> standardBaudRates();
138
140 qint32 inputBaudRate = QSerialPort::Baud9600;
141 qint32 outputBaudRate = QSerialPort::Baud9600;
143 int descriptor = -1;
144
145private:
146 qint64 _writeToPort(const char* data, qint64 maxSize, int timeout = DEFAULT_WRITE_TIMEOUT, bool async = false);
147 bool _stopAsyncRead();
148 void _scheduleReadyRead();
149 qsizetype _pendingSizeLocked() const;
150 void _compactPendingDataLocked();
151 qint64 _drainPendingDataLocked(qint64 maxBytes = -1);
152 bool _setParameters(qint32 baudRate, QSerialPort::DataBits dataBits, QSerialPort::StopBits stopBits,
153 QSerialPort::Parity parity);
154 bool _writeDataOneShot(int msecs = DEFAULT_WRITE_TIMEOUT);
155
156 static int _stopBitsToAndroidStopBits(QSerialPort::StopBits stopBits);
157 static int _dataBitsToAndroidDataBits(QSerialPort::DataBits dataBits);
158 static int _parityToAndroidParity(QSerialPort::Parity parity);
159 static int _flowControlToAndroidFlowControl(QSerialPort::FlowControl flowControl);
160
161 int _deviceId = INVALID_DEVICE_ID;
162
163 std::atomic<bool> _readyReadPending{false};
164 std::atomic<qint64> _bufferBytesEstimate{0};
165 QMutex _readMutex;
166 QWaitCondition _readWaitCondition;
167 QByteArray _pendingData;
168 qsizetype _pendingDataOffset = 0;
169};
170
171QT_END_NAMESPACE
Q_DECLARE_LOGGING_CATEGORY(AndroidSerialLog)
Error error
QSerialPort::SerialPortError errorCode
Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS(QSerialPortPrivate, QSerialPort::Parity, parity, &QSerialPortPrivate::setBindableParity, QSerialPort::NoParity) bool setBindableStopBits(QSerialPort
bool waitForBytesWritten(int msec)
void setError(const QSerialPortErrorInfo &errorInfo)
bool setDataTerminalReady(bool set)
bool setStopBits(QSerialPort::StopBits stopBits)
bool setFlowControl(QSerialPort::FlowControl flowControl)
Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS(QSerialPortPrivate, QSerialPort::StopBits, stopBits, &QSerialPortPrivate::setBindableStopBits, QSerialPort::OneStop) bool setBindableFlowControl(QSerialPort
Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS(QSerialPortPrivate, QSerialPort::DataBits, dataBits, &QSerialPortPrivate::setBindableDataBits, QSerialPort::Data8) bool setBindableParity(QSerialPort
QSerialPort::PinoutSignals pinoutSignals()
bool clear(QSerialPort::Directions directions)
bool setDataBits(QSerialPort::DataBits dataBits)
bool open(QIODevice::OpenMode mode)
void setBindableError(QSerialPort::SerialPortError error_)
Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS(QSerialPortPrivate, QSerialPort::SerialPortError, error, &QSerialPortPrivate::setBindableError, QSerialPort::NoError) bool setBindableDataBits(QSerialPort
static QList< qint32 > standardBaudRates()
qint64 writeData(const char *data, qint64 maxSize)
bool setRequestToSend(bool set)
void exceptionArrived(const QString &ex)
Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS(QSerialPortPrivate, bool, isBreakEnabled, &QSerialPortPrivate::setBindableBreakEnabled, false) bool waitForReadyRead(int msec)
bool setParity(QSerialPort::Parity parity)
void newDataArrived(const char *bytes, int length)
Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS(QSerialPortPrivate, QSerialPort::FlowControl, flowControl, &QSerialPortPrivate::setBindableFlowControl, QSerialPort::NoFlowControl) bool setBindableBreakEnabled(bool isBreakEnabled_)
Provides functions to access serial ports.
Definition qserialport.h:17
constexpr qint64 DEFAULT_WRITE_BUFFER_SIZE
constexpr qint64 MAX_READ_SIZE
constexpr int MIN_READ_TIMEOUT
constexpr int DEFAULT_READ_TIMEOUT
constexpr int EMIT_THRESHOLD
constexpr int INVALID_DEVICE_ID
constexpr qint64 DEFAULT_READ_BUFFER_SIZE
constexpr int DEFAULT_WRITE_TIMEOUT
QT_BEGIN_NAMESPACE