| 1 | // Copyright (C) 2011-2012 Denis Shienkov <[email protected]> |
| 2 | // Copyright (C) 2011 Sergey Belyashov <[email protected]> |
| 3 | // Copyright (C) 2012 Laszlo Papp <[email protected]> |
| 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 |
| 38 | struct 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 | |
| 72 | QT_BEGIN_NAMESPACE |
| 73 | |
| 74 | class QWinOverlappedIoNotifier; |
| 75 | class QTimer; |
| 76 | class QSocketNotifier; |
| 77 | |
| 78 | #if defined(Q_OS_UNIX) |
| 79 | QString serialPortLockFilePath(const QString &portName); |
| 80 | #endif |
| 81 | |
| 82 | class QSerialPortErrorInfo |
| 83 | { |
| 84 | public: |
| 85 | QSerialPortErrorInfo(QSerialPort::SerialPortError newErrorCode = QSerialPort::UnknownError, |
| 86 | const QString &newErrorString = QString()); |
| 87 | QSerialPort::SerialPortError errorCode = QSerialPort::UnknownError; |
| 88 | QString errorString; |
| 89 | }; |
| 90 | |
| 91 | class QSerialPortPrivate : public QIODevicePrivate |
| 92 | { |
| 93 | public: |
| 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 | bool emittedReadyRead = false; |
| 173 | |
| 174 | #if defined(Q_OS_WIN32) |
| 175 | |
| 176 | bool setDcb(DCB *dcb); |
| 177 | bool getDcb(DCB *dcb); |
| 178 | OVERLAPPED *waitForNotified(QDeadlineTimer deadline); |
| 179 | |
| 180 | qint64 queuedBytesCount(QSerialPort::Direction direction) const; |
| 181 | |
| 182 | bool completeAsyncCommunication(qint64 bytesTransferred); |
| 183 | bool completeAsyncRead(qint64 bytesTransferred); |
| 184 | bool completeAsyncWrite(qint64 bytesTransferred); |
| 185 | |
| 186 | bool startAsyncCommunication(); |
| 187 | bool _q_startAsyncWrite(); |
| 188 | void _q_notified(DWORD numberOfBytes, DWORD errorCode, OVERLAPPED *overlapped); |
| 189 | |
| 190 | void emitReadyRead(); |
| 191 | |
| 192 | DCB restoredDcb; |
| 193 | COMMTIMEOUTS currentCommTimeouts; |
| 194 | COMMTIMEOUTS restoredCommTimeouts; |
| 195 | HANDLE handle = INVALID_HANDLE_VALUE; |
| 196 | QByteArray readChunkBuffer; |
| 197 | QByteArray writeChunkBuffer; |
| 198 | bool communicationStarted = false; |
| 199 | bool writeStarted = false; |
| 200 | bool readStarted = false; |
| 201 | QWinOverlappedIoNotifier *notifier = nullptr; |
| 202 | QTimer *startAsyncWriteTimer = nullptr; |
| 203 | OVERLAPPED communicationOverlapped; |
| 204 | OVERLAPPED readCompletionOverlapped; |
| 205 | OVERLAPPED writeCompletionOverlapped; |
| 206 | DWORD triggeredEventMask = 0; |
| 207 | |
| 208 | #elif defined(Q_OS_UNIX) |
| 209 | |
| 210 | static qint32 settingFromBaudRate(qint32 baudRate); |
| 211 | |
| 212 | bool setTermios(const termios *tio); |
| 213 | bool getTermios(termios *tio); |
| 214 | |
| 215 | bool setCustomBaudRate(qint32 baudRate, QSerialPort::Directions directions); |
| 216 | bool setStandardBaudRate(qint32 baudRate, QSerialPort::Directions directions); |
| 217 | |
| 218 | bool isReadNotificationEnabled() const; |
| 219 | void setReadNotificationEnabled(bool enable); |
| 220 | bool isWriteNotificationEnabled() const; |
| 221 | void setWriteNotificationEnabled(bool enable); |
| 222 | |
| 223 | bool waitForReadOrWrite(bool *selectForRead, bool *selectForWrite, |
| 224 | bool checkRead, bool checkWrite, |
| 225 | int msecs); |
| 226 | |
| 227 | qint64 readFromPort(char *data, qint64 maxSize); |
| 228 | qint64 writeToPort(const char *data, qint64 maxSize); |
| 229 | |
| 230 | #ifndef CMSPAR |
| 231 | qint64 writePerChar(const char *data, qint64 maxSize); |
| 232 | #endif |
| 233 | |
| 234 | bool readNotification(); |
| 235 | bool startAsyncWrite(); |
| 236 | bool completeAsyncWrite(); |
| 237 | |
| 238 | struct termios restoredTermios; |
| 239 | int descriptor = -1; |
| 240 | |
| 241 | QSocketNotifier *readNotifier = nullptr; |
| 242 | QSocketNotifier *writeNotifier = nullptr; |
| 243 | |
| 244 | bool readPortNotifierCalled = false; |
| 245 | bool readPortNotifierState = false; |
| 246 | bool readPortNotifierStateSet = false; |
| 247 | |
| 248 | bool emittedBytesWritten = false; |
| 249 | |
| 250 | qint64 pendingBytesWritten = 0; |
| 251 | bool writeSequenceStarted = false; |
| 252 | |
| 253 | std::unique_ptr<QLockFile> lockFileScopedPointer; |
| 254 | |
| 255 | #endif |
| 256 | }; |
| 257 | |
| 258 | QT_END_NAMESPACE |
| 259 | |
| 260 | #endif // QSERIALPORT_P_H |
| 261 | |