| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2011-2012 Denis Shienkov <denis.shienkov@gmail.com> |
| 4 | ** Copyright (C) 2011 Sergey Belyashov <Sergey.Belyashov@gmail.com> |
| 5 | ** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> |
| 6 | ** Contact: https://www.qt.io/licensing/ |
| 7 | ** |
| 8 | ** This file is part of the QtSerialPort module of the Qt Toolkit. |
| 9 | ** |
| 10 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 11 | ** Commercial License Usage |
| 12 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 13 | ** accordance with the commercial license agreement provided with the |
| 14 | ** Software or, alternatively, in accordance with the terms contained in |
| 15 | ** a written agreement between you and The Qt Company. For licensing terms |
| 16 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 17 | ** information use the contact form at https://www.qt.io/contact-us. |
| 18 | ** |
| 19 | ** GNU Lesser General Public License Usage |
| 20 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 21 | ** General Public License version 3 as published by the Free Software |
| 22 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 23 | ** packaging of this file. Please review the following information to |
| 24 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 25 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 26 | ** |
| 27 | ** GNU General Public License Usage |
| 28 | ** Alternatively, this file may be used under the terms of the GNU |
| 29 | ** General Public License version 2.0 or (at your option) the GNU General |
| 30 | ** Public license version 3 or any later version approved by the KDE Free |
| 31 | ** Qt Foundation. The licenses are as published by the Free Software |
| 32 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 33 | ** included in the packaging of this file. Please review the following |
| 34 | ** information to ensure the GNU General Public License requirements will |
| 35 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 36 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 37 | ** |
| 38 | ** $QT_END_LICENSE$ |
| 39 | ** |
| 40 | ****************************************************************************/ |
| 41 | |
| 42 | #ifndef QSERIALPORT_P_H |
| 43 | #define QSERIALPORT_P_H |
| 44 | |
| 45 | // |
| 46 | // W A R N I N G |
| 47 | // ------------- |
| 48 | // |
| 49 | // This file is not part of the Qt API. It exists purely as an |
| 50 | // implementation detail. This header file may change from version to |
| 51 | // version without notice, or even be removed. |
| 52 | // |
| 53 | // We mean it. |
| 54 | // |
| 55 | |
| 56 | #include "qserialport.h" |
| 57 | |
| 58 | #include <private/qiodevice_p.h> |
| 59 | #include <qdeadlinetimer.h> |
| 60 | |
| 61 | #if defined(Q_OS_WIN32) |
| 62 | # include <qt_windows.h> |
| 63 | #elif defined(Q_OS_UNIX) |
| 64 | # include <QtCore/qlockfile.h> |
| 65 | # include <QtCore/qscopedpointer.h> |
| 66 | # include <QtCore/qfileinfo.h> |
| 67 | # include <QtCore/qstringlist.h> |
| 68 | # include <limits.h> |
| 69 | # include <termios.h> |
| 70 | # ifdef Q_OS_ANDROID |
| 71 | struct serial_struct { |
| 72 | int type; |
| 73 | int line; |
| 74 | unsigned int port; |
| 75 | int irq; |
| 76 | int flags; |
| 77 | int xmit_fifo_size; |
| 78 | int custom_divisor; |
| 79 | int baud_base; |
| 80 | unsigned short close_delay; |
| 81 | char io_type; |
| 82 | char reserved_char[1]; |
| 83 | int hub6; |
| 84 | unsigned short closing_wait; |
| 85 | unsigned short closing_wait2; |
| 86 | unsigned char *iomem_base; |
| 87 | unsigned short iomem_reg_shift; |
| 88 | unsigned int port_high; |
| 89 | unsigned long iomap_base; |
| 90 | }; |
| 91 | # define ASYNC_SPD_CUST 0x0030 |
| 92 | # define ASYNC_SPD_MASK 0x1030 |
| 93 | # define PORT_UNKNOWN 0 |
| 94 | # elif defined(Q_OS_LINUX) |
| 95 | # include <linux/serial.h> |
| 96 | # endif |
| 97 | #else |
| 98 | # error Unsupported OS |
| 99 | #endif |
| 100 | |
| 101 | #ifndef QSERIALPORT_BUFFERSIZE |
| 102 | #define QSERIALPORT_BUFFERSIZE 32768 |
| 103 | #endif |
| 104 | |
| 105 | QT_BEGIN_NAMESPACE |
| 106 | |
| 107 | class QTimer; |
| 108 | class QSocketNotifier; |
| 109 | |
| 110 | #if defined(Q_OS_UNIX) |
| 111 | QString serialPortLockFilePath(const QString &portName); |
| 112 | #endif |
| 113 | |
| 114 | class QSerialPortErrorInfo |
| 115 | { |
| 116 | public: |
| 117 | explicit QSerialPortErrorInfo(QSerialPort::SerialPortError newErrorCode = QSerialPort::UnknownError, |
| 118 | const QString &newErrorString = QString()); |
| 119 | QSerialPort::SerialPortError errorCode = QSerialPort::UnknownError; |
| 120 | QString errorString; |
| 121 | }; |
| 122 | |
| 123 | class QSerialPortPrivate : public QIODevicePrivate |
| 124 | { |
| 125 | Q_DECLARE_PUBLIC(QSerialPort) |
| 126 | public: |
| 127 | QSerialPortPrivate(); |
| 128 | |
| 129 | bool open(QIODevice::OpenMode mode); |
| 130 | void close(); |
| 131 | |
| 132 | QSerialPort::PinoutSignals pinoutSignals(); |
| 133 | |
| 134 | bool setDataTerminalReady(bool set); |
| 135 | bool setRequestToSend(bool set); |
| 136 | |
| 137 | bool flush(); |
| 138 | bool clear(QSerialPort::Directions directions); |
| 139 | |
| 140 | bool sendBreak(int duration); |
| 141 | bool setBreakEnabled(bool set); |
| 142 | |
| 143 | bool waitForReadyRead(int msec); |
| 144 | bool waitForBytesWritten(int msec); |
| 145 | |
| 146 | bool setBaudRate(); |
| 147 | bool setBaudRate(qint32 baudRate, QSerialPort::Directions directions); |
| 148 | bool setDataBits(QSerialPort::DataBits dataBits); |
| 149 | bool setParity(QSerialPort::Parity parity); |
| 150 | bool setStopBits(QSerialPort::StopBits stopBits); |
| 151 | bool setFlowControl(QSerialPort::FlowControl flowControl); |
| 152 | |
| 153 | QSerialPortErrorInfo getSystemError(int systemErrorCode = -1) const; |
| 154 | |
| 155 | void setError(const QSerialPortErrorInfo &errorInfo); |
| 156 | |
| 157 | qint64 writeData(const char *data, qint64 maxSize); |
| 158 | |
| 159 | bool initialize(QIODevice::OpenMode mode); |
| 160 | |
| 161 | static QString portNameToSystemLocation(const QString &port); |
| 162 | static QString portNameFromSystemLocation(const QString &location); |
| 163 | |
| 164 | static QList<qint32> standardBaudRates(); |
| 165 | |
| 166 | qint64 readBufferMaxSize = 0; |
| 167 | QSerialPort::SerialPortError error = QSerialPort::NoError; |
| 168 | QString systemLocation; |
| 169 | qint32 inputBaudRate = QSerialPort::Baud9600; |
| 170 | qint32 outputBaudRate = QSerialPort::Baud9600; |
| 171 | QSerialPort::DataBits dataBits = QSerialPort::Data8; |
| 172 | QSerialPort::Parity parity = QSerialPort::NoParity; |
| 173 | QSerialPort::StopBits stopBits = QSerialPort::OneStop; |
| 174 | QSerialPort::FlowControl flowControl = QSerialPort::NoFlowControl; |
| 175 | bool settingsRestoredOnClose = true; |
| 176 | bool isBreakEnabled = false; |
| 177 | |
| 178 | bool startAsyncRead(); |
| 179 | |
| 180 | #if defined(Q_OS_WIN32) |
| 181 | |
| 182 | bool setDcb(DCB *dcb); |
| 183 | bool getDcb(DCB *dcb); |
| 184 | |
| 185 | qint64 queuedBytesCount(QSerialPort::Direction direction) const; |
| 186 | |
| 187 | bool completeAsyncCommunication(qint64 bytesTransferred); |
| 188 | bool completeAsyncRead(qint64 bytesTransferred); |
| 189 | bool completeAsyncWrite(qint64 bytesTransferred); |
| 190 | |
| 191 | bool startAsyncCommunication(); |
| 192 | bool _q_startAsyncWrite(); |
| 193 | void handleNotification(DWORD bytesTransferred, DWORD errorCode, |
| 194 | OVERLAPPED *overlapped); |
| 195 | |
| 196 | void emitReadyRead(); |
| 197 | |
| 198 | static void CALLBACK ioCompletionRoutine( |
| 199 | DWORD errorCode, DWORD bytesTransfered, |
| 200 | OVERLAPPED *overlappedBase); |
| 201 | |
| 202 | DCB restoredDcb; |
| 203 | COMMTIMEOUTS currentCommTimeouts; |
| 204 | COMMTIMEOUTS restoredCommTimeouts; |
| 205 | HANDLE handle = INVALID_HANDLE_VALUE; |
| 206 | QByteArray readChunkBuffer; |
| 207 | QByteArray writeChunkBuffer; |
| 208 | bool communicationStarted = false; |
| 209 | bool writeStarted = false; |
| 210 | bool readStarted = false; |
| 211 | qint64 writeBytesTransferred = 0; |
| 212 | qint64 readBytesTransferred = 0; |
| 213 | QTimer *startAsyncWriteTimer = nullptr; |
| 214 | class Overlapped *communicationCompletionOverlapped = nullptr; |
| 215 | class Overlapped *readCompletionOverlapped = nullptr; |
| 216 | class Overlapped *writeCompletionOverlapped = nullptr; |
| 217 | DWORD triggeredEventMask = 0; |
| 218 | |
| 219 | #elif defined(Q_OS_UNIX) |
| 220 | |
| 221 | static qint32 settingFromBaudRate(qint32 baudRate); |
| 222 | |
| 223 | bool setTermios(const termios *tio); |
| 224 | bool getTermios(termios *tio); |
| 225 | |
| 226 | bool setCustomBaudRate(qint32 baudRate, QSerialPort::Directions directions); |
| 227 | bool setStandardBaudRate(qint32 baudRate, QSerialPort::Directions directions); |
| 228 | |
| 229 | bool isReadNotificationEnabled() const; |
| 230 | void setReadNotificationEnabled(bool enable); |
| 231 | bool isWriteNotificationEnabled() const; |
| 232 | void setWriteNotificationEnabled(bool enable); |
| 233 | |
| 234 | bool waitForReadOrWrite(bool *selectForRead, bool *selectForWrite, |
| 235 | bool checkRead, bool checkWrite, |
| 236 | int msecs); |
| 237 | |
| 238 | qint64 readFromPort(char *data, qint64 maxSize); |
| 239 | qint64 writeToPort(const char *data, qint64 maxSize); |
| 240 | |
| 241 | #ifndef CMSPAR |
| 242 | qint64 writePerChar(const char *data, qint64 maxSize); |
| 243 | #endif |
| 244 | |
| 245 | bool readNotification(); |
| 246 | bool startAsyncWrite(); |
| 247 | bool completeAsyncWrite(); |
| 248 | |
| 249 | struct termios restoredTermios; |
| 250 | int descriptor = -1; |
| 251 | |
| 252 | QSocketNotifier *readNotifier = nullptr; |
| 253 | QSocketNotifier *writeNotifier = nullptr; |
| 254 | |
| 255 | bool readPortNotifierCalled = false; |
| 256 | bool readPortNotifierState = false; |
| 257 | bool readPortNotifierStateSet = false; |
| 258 | |
| 259 | bool emittedReadyRead = false; |
| 260 | bool emittedBytesWritten = false; |
| 261 | |
| 262 | qint64 pendingBytesWritten = 0; |
| 263 | bool writeSequenceStarted = false; |
| 264 | |
| 265 | QScopedPointer<QLockFile> lockFileScopedPointer; |
| 266 | |
| 267 | #endif |
| 268 | }; |
| 269 | |
| 270 | QT_END_NAMESPACE |
| 271 | |
| 272 | #endif // QSERIALPORT_P_H |
| 273 | |