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#ifndef QSERIALPORT_P_H
7#define QSERIALPORT_P_H
8
9//
10// W A R N I N G
11// -------------
12//
13// This file is not part of the Qt API. It exists purely as an
14// implementation detail. This header file may change from version to
15// version without notice, or even be removed.
16//
17// We mean it.
18//
19
20#include "qserialport.h"
21
22#include <qdeadlinetimer.h>
23
24#include <private/qiodevice_p.h>
25#include <private/qproperty_p.h>
26
27#include <memory>
28
29#if defined(Q_OS_WIN32)
30# include <qt_windows.h>
31#elif defined(Q_OS_UNIX)
32# include <QtCore/qlockfile.h>
33# include <QtCore/qfileinfo.h>
34# include <QtCore/qstringlist.h>
35# include <limits.h>
36# include <termios.h>
37# ifdef Q_OS_ANDROID
38struct serial_struct {
39 int type;
40 int line;
41 unsigned int port;
42 int irq;
43 int flags;
44 int xmit_fifo_size;
45 int custom_divisor;
46 int baud_base;
47 unsigned short close_delay;
48 char io_type;
49 char reserved_char[1];
50 int hub6;
51 unsigned short closing_wait;
52 unsigned short closing_wait2;
53 unsigned char *iomem_base;
54 unsigned short iomem_reg_shift;
55 unsigned int port_high;
56 unsigned long iomap_base;
57};
58# define ASYNC_SPD_CUST 0x0030
59# define ASYNC_SPD_MASK 0x1030
60# define PORT_UNKNOWN 0
61# elif defined(Q_OS_LINUX)
62# include <linux/serial.h>
63# endif
64#else
65# error Unsupported OS
66#endif
67
68#ifndef QSERIALPORT_BUFFERSIZE
69#define QSERIALPORT_BUFFERSIZE 32768
70#endif
71
72QT_BEGIN_NAMESPACE
73
74class QWinOverlappedIoNotifier;
75class QTimer;
76class QSocketNotifier;
77
78#if defined(Q_OS_UNIX)
79QString serialPortLockFilePath(const QString &portName);
80#endif
81
82class QSerialPortErrorInfo
83{
84public:
85 QSerialPortErrorInfo(QSerialPort::SerialPortError newErrorCode = QSerialPort::UnknownError,
86 const QString &newErrorString = QString());
87 QSerialPort::SerialPortError errorCode = QSerialPort::UnknownError;
88 QString errorString;
89};
90
91class QSerialPortPrivate : public QIODevicePrivate
92{
93public:
94 Q_DECLARE_PUBLIC(QSerialPort)
95
96 QSerialPortPrivate();
97
98 bool open(QIODevice::OpenMode mode);
99 void close();
100
101 QSerialPort::PinoutSignals pinoutSignals();
102
103 bool setDataTerminalReady(bool set);
104 bool setRequestToSend(bool set);
105
106 bool flush();
107 bool clear(QSerialPort::Directions directions);
108
109 bool sendBreak(int duration);
110 bool setBreakEnabled(bool set);
111
112 bool waitForReadyRead(int msec);
113 bool waitForBytesWritten(int msec);
114
115 bool setBaudRate();
116 bool setBaudRate(qint32 baudRate, QSerialPort::Directions directions);
117 bool setDataBits(QSerialPort::DataBits dataBits);
118 bool setParity(QSerialPort::Parity parity);
119 bool setStopBits(QSerialPort::StopBits stopBits);
120 bool setFlowControl(QSerialPort::FlowControl flowControl);
121
122 QSerialPortErrorInfo getSystemError(int systemErrorCode = -1) const;
123
124 void setError(const QSerialPortErrorInfo &errorInfo);
125
126 qint64 writeData(const char *data, qint64 maxSize);
127
128 bool initialize(QIODevice::OpenMode mode);
129
130 static QList<qint32> standardBaudRates();
131
132 qint64 readBufferMaxSize = 0;
133
134 void setBindableError(QSerialPort::SerialPortError error)
135 { setError(error); }
136 Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS(QSerialPortPrivate, QSerialPort::SerialPortError, error,
137 &QSerialPortPrivate::setBindableError, QSerialPort::NoError)
138
139 QString systemLocation;
140 qint32 inputBaudRate = QSerialPort::Baud9600;
141 qint32 outputBaudRate = QSerialPort::Baud9600;
142
143 bool setBindableDataBits(QSerialPort::DataBits dataBits)
144 { return q_func()->setDataBits(dataBits); }
145 Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS(QSerialPortPrivate, QSerialPort::DataBits, dataBits,
146 &QSerialPortPrivate::setBindableDataBits, QSerialPort::Data8)
147
148 bool setBindableParity(QSerialPort::Parity parity)
149 { return q_func()->setParity(parity); }
150 Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS(QSerialPortPrivate, QSerialPort::Parity, parity,
151 &QSerialPortPrivate::setBindableParity, QSerialPort::NoParity)
152
153 bool setBindableStopBits(QSerialPort::StopBits stopBits)
154 { return q_func()->setStopBits(stopBits); }
155 Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS(QSerialPortPrivate, QSerialPort::StopBits, stopBits,
156 &QSerialPortPrivate::setBindableStopBits, QSerialPort::OneStop)
157
158 bool setBindableFlowControl(QSerialPort::FlowControl flowControl)
159 { return q_func()->setFlowControl(flowControl); }
160 Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS(QSerialPortPrivate, QSerialPort::FlowControl, flowControl,
161 &QSerialPortPrivate::setBindableFlowControl, QSerialPort::NoFlowControl)
162
163 bool settingsRestoredOnClose = true;
164
165 bool setBindableBreakEnabled(bool isBreakEnabled)
166 { return q_func()->setBreakEnabled(isBreakEnabled); }
167 Q_OBJECT_COMPAT_PROPERTY_WITH_ARGS(QSerialPortPrivate, bool, isBreakEnabled,
168 &QSerialPortPrivate::setBindableBreakEnabled, false)
169
170 bool startAsyncRead();
171
172#if defined(Q_OS_WIN32)
173
174 bool setDcb(DCB *dcb);
175 bool getDcb(DCB *dcb);
176 OVERLAPPED *waitForNotified(QDeadlineTimer deadline);
177
178 qint64 queuedBytesCount(QSerialPort::Direction direction) const;
179
180 bool completeAsyncCommunication(qint64 bytesTransferred);
181 bool completeAsyncRead(qint64 bytesTransferred);
182 bool completeAsyncWrite(qint64 bytesTransferred);
183
184 bool startAsyncCommunication();
185 bool _q_startAsyncWrite();
186 void _q_notified(DWORD numberOfBytes, DWORD errorCode, OVERLAPPED *overlapped);
187
188 void emitReadyRead();
189
190 DCB restoredDcb;
191 COMMTIMEOUTS currentCommTimeouts;
192 COMMTIMEOUTS restoredCommTimeouts;
193 HANDLE handle = INVALID_HANDLE_VALUE;
194 QByteArray readChunkBuffer;
195 QByteArray writeChunkBuffer;
196 bool communicationStarted = false;
197 bool writeStarted = false;
198 bool readStarted = false;
199 QWinOverlappedIoNotifier *notifier = nullptr;
200 QTimer *startAsyncWriteTimer = nullptr;
201 OVERLAPPED communicationOverlapped;
202 OVERLAPPED readCompletionOverlapped;
203 OVERLAPPED writeCompletionOverlapped;
204 DWORD triggeredEventMask = 0;
205
206#elif defined(Q_OS_UNIX)
207
208 static qint32 settingFromBaudRate(qint32 baudRate);
209
210 bool setTermios(const termios *tio);
211 bool getTermios(termios *tio);
212
213 bool setCustomBaudRate(qint32 baudRate, QSerialPort::Directions directions);
214 bool setStandardBaudRate(qint32 baudRate, QSerialPort::Directions directions);
215
216 bool isReadNotificationEnabled() const;
217 void setReadNotificationEnabled(bool enable);
218 bool isWriteNotificationEnabled() const;
219 void setWriteNotificationEnabled(bool enable);
220
221 bool waitForReadOrWrite(bool *selectForRead, bool *selectForWrite,
222 bool checkRead, bool checkWrite,
223 int msecs);
224
225 qint64 readFromPort(char *data, qint64 maxSize);
226 qint64 writeToPort(const char *data, qint64 maxSize);
227
228#ifndef CMSPAR
229 qint64 writePerChar(const char *data, qint64 maxSize);
230#endif
231
232 bool readNotification();
233 bool startAsyncWrite();
234 bool completeAsyncWrite();
235
236 struct termios restoredTermios;
237 int descriptor = -1;
238
239 QSocketNotifier *readNotifier = nullptr;
240 QSocketNotifier *writeNotifier = nullptr;
241
242 bool readPortNotifierCalled = false;
243 bool readPortNotifierState = false;
244 bool readPortNotifierStateSet = false;
245
246 bool emittedReadyRead = false;
247 bool emittedBytesWritten = false;
248
249 qint64 pendingBytesWritten = 0;
250 bool writeSequenceStarted = false;
251
252 std::unique_ptr<QLockFile> lockFileScopedPointer;
253
254#endif
255};
256
257QT_END_NAMESPACE
258
259#endif // QSERIALPORT_P_H
260

Provided by KDAB

Privacy Policy
Learn Advanced QML with KDAB
Find out more

source code of qtserialport/src/serialport/qserialport_p.h