diff options
-rw-r--r-- | .qmake.conf | 2 | ||||
-rw-r--r-- | src/serialbus/qcanbus.h | 2 | ||||
-rw-r--r-- | src/serialbus/qcanbusdeviceinfo.h | 2 | ||||
-rw-r--r-- | src/serialbus/qcanbusfactory.h | 2 | ||||
-rw-r--r-- | src/serialbus/qcanbusframe.h | 2 | ||||
-rw-r--r-- | src/serialbus/qmodbuscommevent_p.h | 2 | ||||
-rw-r--r-- | src/serialbus/qmodbusdevice.h | 2 | ||||
-rw-r--r-- | src/serialbus/qmodbusdeviceidentification.h | 2 | ||||
-rw-r--r-- | src/serialbus/qmodbuspdu.h | 2 | ||||
-rw-r--r-- | src/serialbus/qmodbusrtuserialslave_p.h | 4 | ||||
-rw-r--r-- | src/serialbus/qmodbustcpserver.cpp | 64 | ||||
-rw-r--r-- | src/serialbus/qmodbustcpserver.h | 14 | ||||
-rw-r--r-- | src/serialbus/qmodbustcpserver_p.h | 16 | ||||
-rw-r--r-- | src/serialbus/qtserialbusglobal.h (renamed from src/serialbus/qserialbusglobal.h) | 6 | ||||
-rw-r--r-- | src/serialbus/serialbus.pro | 2 | ||||
-rw-r--r-- | sync.profile | 5 |
16 files changed, 112 insertions, 17 deletions
diff --git a/.qmake.conf b/.qmake.conf index 81e7e4e..ebb0ade 100644 --- a/.qmake.conf +++ b/.qmake.conf @@ -2,4 +2,4 @@ load(qt_build_config) CONFIG += warning_clean DEFINES += QT_NO_FOREACH -MODULE_VERSION = 5.12.0 +MODULE_VERSION = 5.13.0 diff --git a/src/serialbus/qcanbus.h b/src/serialbus/qcanbus.h index 695d312..6170fcc 100644 --- a/src/serialbus/qcanbus.h +++ b/src/serialbus/qcanbus.h @@ -38,7 +38,7 @@ #define QCANBUS_H #include <QtCore/qobject.h> -#include <QtSerialBus/qserialbusglobal.h> +#include <QtSerialBus/qtserialbusglobal.h> #include <QtSerialBus/qcanbusdevice.h> #include <QtSerialBus/qcanbusdeviceinfo.h> diff --git a/src/serialbus/qcanbusdeviceinfo.h b/src/serialbus/qcanbusdeviceinfo.h index fbe2c71..c46302d 100644 --- a/src/serialbus/qcanbusdeviceinfo.h +++ b/src/serialbus/qcanbusdeviceinfo.h @@ -39,7 +39,7 @@ #include <QtCore/qshareddata.h> #include <QtCore/qstring.h> -#include <QtSerialBus/qserialbusglobal.h> +#include <QtSerialBus/qtserialbusglobal.h> QT_BEGIN_NAMESPACE diff --git a/src/serialbus/qcanbusfactory.h b/src/serialbus/qcanbusfactory.h index d74903c..cd7322b 100644 --- a/src/serialbus/qcanbusfactory.h +++ b/src/serialbus/qcanbusfactory.h @@ -38,7 +38,7 @@ #define QCANBUSFACTORY_H #include <QtCore/qstringlist.h> -#include <QtSerialBus/qserialbusglobal.h> +#include <QtSerialBus/qtserialbusglobal.h> #include <QtSerialBus/qcanbusdevice.h> #include <QtSerialBus/qcanbusdeviceinfo.h> diff --git a/src/serialbus/qcanbusframe.h b/src/serialbus/qcanbusframe.h index d1f8857..69bde41 100644 --- a/src/serialbus/qcanbusframe.h +++ b/src/serialbus/qcanbusframe.h @@ -39,7 +39,7 @@ #include <QtCore/qmetatype.h> #include <QtCore/qobject.h> -#include <QtSerialBus/qserialbusglobal.h> +#include <QtSerialBus/qtserialbusglobal.h> QT_BEGIN_NAMESPACE diff --git a/src/serialbus/qmodbuscommevent_p.h b/src/serialbus/qmodbuscommevent_p.h index 47967e7..087c060 100644 --- a/src/serialbus/qmodbuscommevent_p.h +++ b/src/serialbus/qmodbuscommevent_p.h @@ -37,7 +37,7 @@ #ifndef QMODBUSCOMMEVENT_P_H #define QMODBUSCOMMEVENT_P_H -#include <QtSerialBus/qserialbusglobal.h> +#include <QtSerialBus/qtserialbusglobal.h> // // W A R N I N G diff --git a/src/serialbus/qmodbusdevice.h b/src/serialbus/qmodbusdevice.h index 20fa1b3..468fdf4 100644 --- a/src/serialbus/qmodbusdevice.h +++ b/src/serialbus/qmodbusdevice.h @@ -38,7 +38,7 @@ #include <QtCore/qobject.h> #include <QtCore/qiodevice.h> -#include <QtSerialBus/qserialbusglobal.h> +#include <QtSerialBus/qtserialbusglobal.h> QT_BEGIN_NAMESPACE diff --git a/src/serialbus/qmodbusdeviceidentification.h b/src/serialbus/qmodbusdeviceidentification.h index 63d7ee3..9f08f55 100644 --- a/src/serialbus/qmodbusdeviceidentification.h +++ b/src/serialbus/qmodbusdeviceidentification.h @@ -39,7 +39,7 @@ #include <QtCore/qmap.h> #include <QtCore/qmetatype.h> -#include <QtSerialBus/qserialbusglobal.h> +#include <QtSerialBus/qtserialbusglobal.h> QT_BEGIN_NAMESPACE diff --git a/src/serialbus/qmodbuspdu.h b/src/serialbus/qmodbuspdu.h index 28b1f8a..14cef59 100644 --- a/src/serialbus/qmodbuspdu.h +++ b/src/serialbus/qmodbuspdu.h @@ -39,7 +39,7 @@ #include <QtCore/qdatastream.h> #include <QtCore/qmetatype.h> #include <QtCore/qvector.h> -#include <QtSerialBus/qserialbusglobal.h> +#include <QtSerialBus/qtserialbusglobal.h> QT_BEGIN_NAMESPACE diff --git a/src/serialbus/qmodbusrtuserialslave_p.h b/src/serialbus/qmodbusrtuserialslave_p.h index be9cf10..70f28cd 100644 --- a/src/serialbus/qmodbusrtuserialslave_p.h +++ b/src/serialbus/qmodbusrtuserialslave_p.h @@ -90,7 +90,7 @@ public: m_interFrameTimer.start(); - const int size = m_serialPort->size(); + const qint64 size = m_serialPort->size(); m_requestBuffer += m_serialPort->read(size); const QModbusSerialAdu adu(QModbusSerialAdu::Rtu, m_requestBuffer); @@ -218,7 +218,7 @@ public: return; } - int writtenBytes = m_serialPort->write(result); + qint64 writtenBytes = m_serialPort->write(result); if ((writtenBytes == -1) || (writtenBytes < result.size())) { qCDebug(QT_MODBUS) << "(RTU server) Cannot write requested response to serial port."; q->setError(QModbusRtuSerialSlave::tr("Could not write response to client"), diff --git a/src/serialbus/qmodbustcpserver.cpp b/src/serialbus/qmodbustcpserver.cpp index 39aeda8..3a15e9e 100644 --- a/src/serialbus/qmodbustcpserver.cpp +++ b/src/serialbus/qmodbustcpserver.cpp @@ -110,7 +110,7 @@ bool QModbusTcpServer::open() return false; } - if (d->m_tcpServer->listen(QHostAddress(url.host()), url.port())) + if (d->m_tcpServer->listen(QHostAddress(url.host()), quint16(url.port()))) setState(QModbusDevice::ConnectedState); else setError(d->m_tcpServer->errorString(), QModbusDevice::ConnectionError); @@ -171,4 +171,66 @@ QModbusResponse QModbusTcpServer::processRequest(const QModbusPdu &request) return QModbusServer::processRequest(request); } +/*! + Installs an \a observer that can be used to obtain notifications when a + new TCP client connects to this server instance. In addition, the \a observer + can be used to reject the incoming TCP connection. + + QModbusTcpServer takes ownership of the given \a observer. Any previously set + observer will be deleted. The observer can be uninstalled by calling this + function with \c nullptr as parameter. + + \sa QModbusTcpConnectionObserver + \since 5.13 +*/ +void QModbusTcpServer::installConnectionObserver(QModbusTcpConnectionObserver *observer) +{ + Q_D(QModbusTcpServer); + + d->m_observer.reset(observer); +} + +/*! + \class QModbusTcpConnectionObserver + \inmodule QtSerialBus + \since 5.13 + + \brief The QModbusTcpConnectionObserver class represents the interface for + objects that can be passed to \l QModbusTcpServer::installConnectionObserver. + + The interface must be implemented by the developer to be able to monitor + every incoming TCP connection from another Modbus client. + + \sa QModbusTcpServer::installConnectionObserver +*/ + +QModbusTcpConnectionObserver::~QModbusTcpConnectionObserver() +{ +} + +/*! + \fn bool QModbusTcpConnectionObserver::acceptNewConnection(QTcpSocket *newClient) + + This function is a callback for every incoming TCP connection. The user should + provide \a newClient to receive a notification when a new client connection + is established and to determine whether the connection is to be accepted. + + The function should return \c true if the connection is to be accepted. Otherwise, + the socket is closed/rejected. +*/ + +/*! + \fn void QModbusTcpServer::modbusClientDisconnected(QTcpSocket *modbusClient) + + This signal is emitted when a current TCP based \a modbusClient disconnects + from this Modbus TCP server. Note that there might be several TCP clients + connected at the same time. + + Notifications on incoming new connections can be received by installing a + QModbusTcpConnectionObserver via \l installConnectionObserver(). + + \sa installConnectionObserver + \since 5.13 +*/ + QT_END_NAMESPACE diff --git a/src/serialbus/qmodbustcpserver.h b/src/serialbus/qmodbustcpserver.h index 57276a5..cf24d42 100644 --- a/src/serialbus/qmodbustcpserver.h +++ b/src/serialbus/qmodbustcpserver.h @@ -43,6 +43,15 @@ QT_BEGIN_NAMESPACE class QModbusTcpServerPrivate; +class QTcpSocket; + +class Q_SERIALBUS_EXPORT QModbusTcpConnectionObserver +{ +public: + virtual ~QModbusTcpConnectionObserver(); + + virtual bool acceptNewConnection(QTcpSocket *newClient) = 0; +}; class Q_SERIALBUS_EXPORT QModbusTcpServer : public QModbusServer { @@ -53,6 +62,11 @@ public: explicit QModbusTcpServer(QObject *parent = nullptr); ~QModbusTcpServer(); + void installConnectionObserver(QModbusTcpConnectionObserver *observer); + +Q_SIGNALS: + void modbusClientDisconnected(QTcpSocket *modbusClient); + protected: QModbusTcpServer(QModbusTcpServerPrivate &dd, QObject *parent = nullptr); diff --git a/src/serialbus/qmodbustcpserver_p.h b/src/serialbus/qmodbustcpserver_p.h index 90d1eaf..91e3bfe 100644 --- a/src/serialbus/qmodbustcpserver_p.h +++ b/src/serialbus/qmodbustcpserver_p.h @@ -48,6 +48,8 @@ #include <private/qmodbusserver_p.h> +#include <memory> + // // W A R N I N G // ------------- @@ -123,6 +125,13 @@ public: qCDebug(QT_MODBUS) << "(TCP server) Incoming socket from" << socket->peerAddress() << socket->peerName() << socket->peerPort(); + if (m_observer && !m_observer->acceptNewConnection(socket)) { + qCDebug(QT_MODBUS) << "(TCP server) Connection rejected by observer"; + socket->close(); + socket->deleteLater(); + return; + } + connections.append(socket); auto buffer = new QByteArray(); @@ -133,6 +142,9 @@ public: }); QObject::connect(socket, &QTcpSocket::disconnected, [socket, this]() { connections.removeAll(socket); + + Q_Q(QModbusTcpServer); + emit q->modbusClientDisconnected(socket); socket->deleteLater(); }); QObject::connect(socket, &QTcpSocket::readyRead, [buffer, socket, this]() { @@ -193,7 +205,7 @@ public: return; } - int writtenBytes = socket->write(result); + qint64 writtenBytes = socket->write(result); if (writtenBytes == -1 || writtenBytes < result.size()) { qCDebug(QT_MODBUS) << "(TCP server) Cannot write requested response to socket."; forwardError(QModbusTcpServer::tr("Could not write response to client"), @@ -214,6 +226,8 @@ public: QTcpServer *m_tcpServer; QVector<QTcpSocket *> connections; + std::unique_ptr<QModbusTcpConnectionObserver> m_observer; + static const qint8 mbpaHeaderSize = 7; static const qint16 maxBytesModbusADU = 260; }; diff --git a/src/serialbus/qserialbusglobal.h b/src/serialbus/qtserialbusglobal.h index 736c4a8..70d7799 100644 --- a/src/serialbus/qserialbusglobal.h +++ b/src/serialbus/qtserialbusglobal.h @@ -34,8 +34,8 @@ ** ****************************************************************************/ -#ifndef QSERIALBUSGLOBAL_H -#define QSERIALBUSGLOBAL_H +#ifndef QTSERIALBUSGLOBAL_H +#define QTSERIALBUSGLOBAL_H #include <QtCore/qglobal.h> @@ -53,4 +53,4 @@ QT_BEGIN_NAMESPACE QT_END_NAMESPACE -#endif // QSERIALBUSGLOBAL_H +#endif // QTSERIALBUSGLOBAL_H diff --git a/src/serialbus/serialbus.pro b/src/serialbus/serialbus.pro index 14583bc..57a5a6b 100644 --- a/src/serialbus/serialbus.pro +++ b/src/serialbus/serialbus.pro @@ -12,7 +12,7 @@ PUBLIC_HEADERS += \ qcanbusfactory.h \ qcanbusframe.h \ qcanbus.h \ - qserialbusglobal.h \ + qtserialbusglobal.h \ qmodbusserver.h \ qmodbusdevice.h \ qmodbusdataunit.h \ diff --git a/sync.profile b/sync.profile index e7125c6..588f4e1 100644 --- a/sync.profile +++ b/sync.profile @@ -1,3 +1,8 @@ %modules = ( "QtSerialBus" => "$basedir/src/serialbus", ); +%deprecatedheaders = ( + "QtSerialBus" => { + "qserialbusglobal.h" => "QtSerialBus/qtserialbusglobal.h" + }, +); |