From 804b10f3297822f4b12a4b971633f78965b756b0 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Thu, 9 May 2019 18:01:35 +0200 Subject: Hide the APIs of QCoapConnection and QCoapQUdpConnection At the moment only the QCoapQUdpConnection class implements the QCoapConnection interface. If later we decide to add other implementations, and it turns out that the current API is not good enough, it won't be possible to change it without breaking the source compatibility. Let's make these classes private, we can make them public when there is a need for it. Change-Id: I41336dda0e6eaa762f0eb9c8f4aa98a9c7b62a2c Reviewed-by: Simon Hausmann --- src/coap/coap.pro | 2 - src/coap/qcoapclient.cpp | 2 +- src/coap/qcoapclient_p.h | 1 - src/coap/qcoapconnection.cpp | 28 +++++++ src/coap/qcoapconnection.h | 87 ---------------------- src/coap/qcoapconnection_p.h | 49 +++++++++++- src/coap/qcoapinternalrequest_p.h | 2 +- src/coap/qcoapprotocol.cpp | 1 - src/coap/qcoapqudpconnection.cpp | 8 +- src/coap/qcoapqudpconnection.h | 81 -------------------- src/coap/qcoapqudpconnection_p.h | 35 ++++++++- tests/auto/qcoapclient/tst_qcoapclient.cpp | 19 +++++ .../tst_qcoapqudpconnection.cpp | 28 ++++--- 13 files changed, 153 insertions(+), 190 deletions(-) delete mode 100644 src/coap/qcoapconnection.h delete mode 100644 src/coap/qcoapqudpconnection.h diff --git a/src/coap/coap.pro b/src/coap/coap.pro index 024de15..11e6932 100644 --- a/src/coap/coap.pro +++ b/src/coap/coap.pro @@ -7,14 +7,12 @@ QMAKE_DOCS = $$PWD/doc/qtcoap.qdocconf PUBLIC_HEADERS += \ qcoapclient.h \ - qcoapconnection.h \ qcoapdiscoveryreply.h \ qcoapglobal.h \ qcoapmessage.h \ qcoapnamespace.h \ qcoapoption.h \ qcoapprotocol.h \ - qcoapqudpconnection.h \ qcoapreply.h \ qcoaprequest.h \ qcoapresource.h \ diff --git a/src/coap/qcoapclient.cpp b/src/coap/qcoapclient.cpp index 767f7f0..21af3e2 100644 --- a/src/coap/qcoapclient.cpp +++ b/src/coap/qcoapclient.cpp @@ -34,7 +34,7 @@ #include "qcoapdiscoveryreply.h" #include "qcoapnamespace.h" #include "qcoapsecurityconfiguration.h" -#include "qcoapqudpconnection.h" +#include "qcoapqudpconnection_p.h" #include #include #include diff --git a/src/coap/qcoapclient_p.h b/src/coap/qcoapclient_p.h index 40db811..60cd59d 100644 --- a/src/coap/qcoapclient_p.h +++ b/src/coap/qcoapclient_p.h @@ -31,7 +31,6 @@ #ifndef QCOAPCLIENT_P_H #define QCOAPCLIENT_P_H -#include #include #include #include diff --git a/src/coap/qcoapconnection.cpp b/src/coap/qcoapconnection.cpp index 96e98fa..e104fee 100644 --- a/src/coap/qcoapconnection.cpp +++ b/src/coap/qcoapconnection.cpp @@ -36,6 +36,8 @@ QT_BEGIN_NAMESPACE Q_LOGGING_CATEGORY(lcCoapConnection, "qt.coap.connection") /*! + \internal + \class QCoapConnection \inmodule QtCoap @@ -47,6 +49,8 @@ Q_LOGGING_CATEGORY(lcCoapConnection, "qt.coap.connection") */ /*! + \internal + \enum QCoapConnection::ConnectionState This enum specifies the state of the underlying transport. @@ -60,6 +64,8 @@ Q_LOGGING_CATEGORY(lcCoapConnection, "qt.coap.connection") */ /*! + \internal + \fn void QCoapConnection::error(QAbstractSocket::SocketError error) This signal is emitted when a connection error occurs. The \a error @@ -67,6 +73,8 @@ Q_LOGGING_CATEGORY(lcCoapConnection, "qt.coap.connection") */ /*! + \internal + \fn void QCoapConnection::readyRead(const QByteArray &data, const QHostAddress &sender) This signal is emitted when a network reply is available. The \a data @@ -75,6 +83,8 @@ Q_LOGGING_CATEGORY(lcCoapConnection, "qt.coap.connection") */ /*! + \internal + \fn void QCoapConnection::bound() This signal is emitted when the underlying transport is ready for data transmission. @@ -91,6 +101,8 @@ Q_LOGGING_CATEGORY(lcCoapConnection, "qt.coap.connection") */ /*! + \internal + \fn void QCoapConnection::bind(const QString &host, quint16 port) Prepares the underlying transport for data transmission to to the given \a host @@ -102,6 +114,8 @@ Q_LOGGING_CATEGORY(lcCoapConnection, "qt.coap.connection") */ /*! + \internal + \fn void QCoapConnection::close() Closes the open sockets and connections to free the underlying transport. @@ -111,6 +125,8 @@ Q_LOGGING_CATEGORY(lcCoapConnection, "qt.coap.connection") */ /*! + \internal + \fn void QCoapConnection::writeData(const QByteArray &data, const QString &host, quint16 port) Sends the given \a data frame to the host address \a host at port \a port. @@ -151,6 +167,8 @@ QCoapConnection::QCoapConnection(QObjectPrivate &dd, QObject *parent) } /*! + \internal + Releases any resources held by QCoapConnection. */ QCoapConnection::~QCoapConnection() @@ -182,6 +200,8 @@ QCoapConnectionPrivate::sendRequest(const QByteArray &request, const QString &ho } /*! + \internal + Returns \c true if security is used, returns \c false otherwise. */ bool QCoapConnection::isSecure() const @@ -191,6 +211,8 @@ bool QCoapConnection::isSecure() const } /*! + \internal + Returns the security mode. */ QtCoap::SecurityMode QCoapConnection::securityMode() const @@ -200,6 +222,8 @@ QtCoap::SecurityMode QCoapConnection::securityMode() const } /*! + \internal + Returns the connection state. */ QCoapConnection::ConnectionState QCoapConnection::state() const @@ -243,6 +267,8 @@ void QCoapConnection::setSecurityConfiguration(const QCoapSecurityConfiguration } /*! + \internal + Returns the security configuration. */ QCoapSecurityConfiguration QCoapConnection::securityConfiguration() const @@ -252,6 +278,8 @@ QCoapSecurityConfiguration QCoapConnection::securityConfiguration() const } /*! + \internal + Closes the open sockets and connections to free the transport and clears the connection state. */ diff --git a/src/coap/qcoapconnection.h b/src/coap/qcoapconnection.h deleted file mode 100644 index 6f2931f..0000000 --- a/src/coap/qcoapconnection.h +++ /dev/null @@ -1,87 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2019 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCoap module. -** -** $QT_BEGIN_LICENSE:GPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 or (at your option) any later version -** approved by the KDE Free Qt Foundation. The licenses are as published by -** the Free Software Foundation and appearing in the file LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QCOAPCONNECTION_H -#define QCOAPCONNECTION_H - -#include -#include -#include - -#include - -QT_BEGIN_NAMESPACE - -class QCoapConnectionPrivate; -class Q_COAP_EXPORT QCoapConnection : public QObject -{ - Q_OBJECT -public: - enum class ConnectionState : quint8 { - Unconnected, - Bound - }; - - explicit QCoapConnection(QtCoap::SecurityMode securityMode = QtCoap::SecurityMode::NoSec, - QObject *parent = nullptr); - virtual ~QCoapConnection(); - - bool isSecure() const; - QtCoap::SecurityMode securityMode() const; - ConnectionState state() const; - QCoapSecurityConfiguration securityConfiguration() const; - - Q_INVOKABLE void setSecurityConfiguration(const QCoapSecurityConfiguration &configuration); - Q_INVOKABLE void disconnect(); - -Q_SIGNALS: - void error(QAbstractSocket::SocketError error); - void readyRead(const QByteArray &data, const QHostAddress &sender); - void bound(); - void securityConfigurationChanged(); - -private: - void startToSendRequest(); - -protected: - QCoapConnection(QObjectPrivate &dd, QObject *parent = nullptr); - - virtual void bind(const QString &host, quint16 port) = 0; - virtual void writeData(const QByteArray &data, const QString &host, quint16 port) = 0; - virtual void close() = 0; - -private: - friend class QCoapProtocolPrivate; - - Q_DECLARE_PRIVATE(QCoapConnection) -}; - -QT_END_NAMESPACE - -#endif // QCOAPCONNECTION_H diff --git a/src/coap/qcoapconnection_p.h b/src/coap/qcoapconnection_p.h index d75c794..07abe4a 100644 --- a/src/coap/qcoapconnection_p.h +++ b/src/coap/qcoapconnection_p.h @@ -30,9 +30,12 @@ #ifndef QCOAPCONNECTION_P_H #define QCOAPCONNECTION_P_H -#include +#include +#include #include +#include +#include #include // @@ -48,6 +51,50 @@ QT_BEGIN_NAMESPACE +class QCoapConnectionPrivate; +class Q_AUTOTEST_EXPORT QCoapConnection : public QObject +{ + Q_OBJECT +public: + enum class ConnectionState : quint8 { + Unconnected, + Bound + }; + + explicit QCoapConnection(QtCoap::SecurityMode securityMode = QtCoap::SecurityMode::NoSec, + QObject *parent = nullptr); + virtual ~QCoapConnection(); + + bool isSecure() const; + QtCoap::SecurityMode securityMode() const; + ConnectionState state() const; + QCoapSecurityConfiguration securityConfiguration() const; + + Q_INVOKABLE void setSecurityConfiguration(const QCoapSecurityConfiguration &configuration); + Q_INVOKABLE void disconnect(); + +Q_SIGNALS: + void error(QAbstractSocket::SocketError error); + void readyRead(const QByteArray &data, const QHostAddress &sender); + void bound(); + void securityConfigurationChanged(); + +private: + void startToSendRequest(); + +protected: + QCoapConnection(QObjectPrivate &dd, QObject *parent = nullptr); + + virtual void bind(const QString &host, quint16 port) = 0; + virtual void writeData(const QByteArray &data, const QString &host, quint16 port) = 0; + virtual void close() = 0; + +private: + friend class QCoapProtocolPrivate; + + Q_DECLARE_PRIVATE(QCoapConnection) +}; + struct CoapFrame { QByteArray currentPdu; QString host; diff --git a/src/coap/qcoapinternalrequest_p.h b/src/coap/qcoapinternalrequest_p.h index 549f5f0..f6875b3 100644 --- a/src/coap/qcoapinternalrequest_p.h +++ b/src/coap/qcoapinternalrequest_p.h @@ -35,7 +35,7 @@ #include #include -#include +#include #include #include diff --git a/src/coap/qcoapprotocol.cpp b/src/coap/qcoapprotocol.cpp index dd5f25d..63ee393 100644 --- a/src/coap/qcoapprotocol.cpp +++ b/src/coap/qcoapprotocol.cpp @@ -31,7 +31,6 @@ #include "qcoapprotocol_p.h" #include "qcoapinternalrequest_p.h" #include "qcoapinternalreply_p.h" -#include "qcoapconnection.h" #include "qcoapconnection_p.h" #include diff --git a/src/coap/qcoapqudpconnection.cpp b/src/coap/qcoapqudpconnection.cpp index e38fe5b..08d96d1 100644 --- a/src/coap/qcoapqudpconnection.cpp +++ b/src/coap/qcoapqudpconnection.cpp @@ -45,6 +45,8 @@ QT_BEGIN_NAMESPACE Q_DECLARE_LOGGING_CATEGORY(lcCoapConnection) /*! + \internal + \class QCoapQUdpConnection \inmodule QtCoap @@ -266,6 +268,8 @@ void QCoapQUdpConnection::close() } /*! + \internal + Sets the QUdpSocket socket \a option to \a value. */ void QCoapQUdpConnection::setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value) @@ -339,6 +343,8 @@ void QCoapQUdpConnectionPrivate::socketReadyRead() } /*! + \internal + Returns the socket. */ QUdpSocket *QCoapQUdpConnection::socket() const @@ -468,5 +474,3 @@ void QCoapQUdpConnectionPrivate::handleEncryptedDatagram() #endif // dtls QT_END_NAMESPACE - -#include "moc_qcoapqudpconnection.cpp" diff --git a/src/coap/qcoapqudpconnection.h b/src/coap/qcoapqudpconnection.h deleted file mode 100644 index f9ca356..0000000 --- a/src/coap/qcoapqudpconnection.h +++ /dev/null @@ -1,81 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 Witekio. -** Copyright (C) 2018 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCoap module. -** -** $QT_BEGIN_LICENSE:GPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 or (at your option) any later version -** approved by the KDE Free Qt Foundation. The licenses are as published by -** the Free Software Foundation and appearing in the file LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QCOAPQUDPCONNECTION_H -#define QCOAPQUDPCONNECTION_H - -#include -#include -#include - -#include -#include -#include - -QT_BEGIN_NAMESPACE - -class QCoapQUdpConnectionPrivate; -class QSslPreSharedKeyAuthenticator; -class Q_COAP_EXPORT QCoapQUdpConnection : public QCoapConnection -{ - Q_OBJECT - -public: - explicit QCoapQUdpConnection(QtCoap::SecurityMode security = QtCoap::SecurityMode::NoSec, - QObject *parent = nullptr); - - ~QCoapQUdpConnection() override = default; - - QUdpSocket *socket() const; - -public Q_SLOTS: - void setSocketOption(QAbstractSocket::SocketOption, const QVariant &value); - -#if QT_CONFIG(dtls) -private Q_SLOTS: - void pskRequired(QSslPreSharedKeyAuthenticator *authenticator); - void handshakeTimeout(); -#endif - -protected: - explicit QCoapQUdpConnection(QCoapQUdpConnectionPrivate &dd, QObject *parent = nullptr); - - void bind(const QString &host, quint16 port) override; - void writeData(const QByteArray &data, const QString &host, quint16 port) override; - void close() override; - - void createSocket(); - - Q_DECLARE_PRIVATE(QCoapQUdpConnection) -}; - -QT_END_NAMESPACE - -#endif // QCOAPQUDPCONNECTION_H diff --git a/src/coap/qcoapqudpconnection_p.h b/src/coap/qcoapqudpconnection_p.h index 3901af8..1ac1603 100644 --- a/src/coap/qcoapqudpconnection_p.h +++ b/src/coap/qcoapqudpconnection_p.h @@ -31,7 +31,6 @@ #ifndef QCOAPQUDPCONNECTION_P_H #define QCOAPQUDPCONNECTION_P_H -#include #include #include @@ -53,6 +52,40 @@ QT_BEGIN_NAMESPACE class QDtls; class QSslPreSharedKeyAuthenticator; +class QCoapQUdpConnectionPrivate; +class Q_AUTOTEST_EXPORT QCoapQUdpConnection : public QCoapConnection +{ + Q_OBJECT + +public: + explicit QCoapQUdpConnection(QtCoap::SecurityMode security = QtCoap::SecurityMode::NoSec, + QObject *parent = nullptr); + + ~QCoapQUdpConnection() override = default; + + QUdpSocket *socket() const; + +public Q_SLOTS: + void setSocketOption(QAbstractSocket::SocketOption, const QVariant &value); + +#if QT_CONFIG(dtls) +private Q_SLOTS: + void pskRequired(QSslPreSharedKeyAuthenticator *authenticator); + void handshakeTimeout(); +#endif + +protected: + explicit QCoapQUdpConnection(QCoapQUdpConnectionPrivate &dd, QObject *parent = nullptr); + + void bind(const QString &host, quint16 port) override; + void writeData(const QByteArray &data, const QString &host, quint16 port) override; + void close() override; + + void createSocket(); + + Q_DECLARE_PRIVATE(QCoapQUdpConnection) +}; + class Q_AUTOTEST_EXPORT QCoapQUdpConnectionPrivate : public QCoapConnectionPrivate { public: diff --git a/tests/auto/qcoapclient/tst_qcoapclient.cpp b/tests/auto/qcoapclient/tst_qcoapclient.cpp index 3e5eac0..90b44cf 100644 --- a/tests/auto/qcoapclient/tst_qcoapclient.cpp +++ b/tests/auto/qcoapclient/tst_qcoapclient.cpp @@ -77,6 +77,8 @@ private Q_SLOTS: void multicast_blockwise(); }; +#ifdef QT_BUILD_INTERNAL + class QCoapQUdpConnectionSocketTestsPrivate : public QCoapQUdpConnectionPrivate { bool bind() override @@ -172,6 +174,7 @@ public: } }; +#endif class Helper : public QObject { @@ -347,6 +350,7 @@ void tst_QCoapClient::setBlockSize_data() void tst_QCoapClient::setBlockSize() { +#ifdef QT_BUILD_INTERNAL QFETCH(int, blockSizeSet); QFETCH(int, blockSizeExpected); @@ -358,6 +362,9 @@ void tst_QCoapClient::setBlockSize() eventLoop.exec(); QCOMPARE(client.protocol()->blockSize(), blockSizeExpected); +#else + QSKIP("Not an internal build, skipping this test"); +#endif } void tst_QCoapClient::requestWithQIODevice_data() @@ -479,6 +486,7 @@ void tst_QCoapClient::timeout_data() void tst_QCoapClient::timeout() { +#ifdef QT_BUILD_INTERNAL QFETCH(int, timeout); QFETCH(int, maxRetransmit); @@ -520,6 +528,9 @@ void tst_QCoapClient::timeout() QCOMPARE(spyReplyFinished.count(), 1); QCOMPARE(spyReplyAborted.count(), 0); QCOMPARE(spyClientError.count(), 1); +#else + QSKIP("Not an internal build, skipping this test"); +#endif } void tst_QCoapClient::abort() @@ -786,6 +797,7 @@ void tst_QCoapClient::confirmableMulticast() void tst_QCoapClient::multicast() { +#ifdef QT_BUILD_INTERNAL QCoapClientForMulticastTests client; QCoapRequest request = QCoapRequest(QUrl("224.0.1.187")); request.setToken("abc"); @@ -811,10 +823,14 @@ void tst_QCoapClient::multicast() QCOMPARE(message1.payload(), "Reply1"); QHostAddress sender1 = qvariant_cast(spyMulticastResponse.at(1).at(2)); QCOMPARE(sender1, host1); +#else + QSKIP("Not an internal build, skipping this test"); +#endif } void tst_QCoapClient::multicast_blockwise() { +#ifdef QT_BUILD_INTERNAL QCoapClientForMulticastTests client; QCoapRequest request = QCoapRequest(QUrl("224.0.1.187")); request.setToken("abc"); @@ -842,6 +858,9 @@ void tst_QCoapClient::multicast_blockwise() QCOMPARE(message1.payload(), "Reply3Reply4"); QHostAddress sender1 = qvariant_cast(spyMulticastResponse.at(1).at(2)); QCOMPARE(sender1, host1); +#else + QSKIP("Not an internal build, skipping this test"); +#endif } QTEST_MAIN(tst_QCoapClient) diff --git a/tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp b/tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp index 6ce5a87..daed5fb 100644 --- a/tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp +++ b/tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp @@ -37,12 +37,13 @@ #include #include #include -#include #include #include #include #include "../coapnetworksettings.h" +#ifdef QT_BUILD_INTERNAL + using namespace QtCoapNetworkSettings; struct QCoapRequestForTest : public QCoapRequest @@ -86,7 +87,6 @@ void tst_QCoapQUdpConnection::ctor() void tst_QCoapQUdpConnection::connectToHost() { -#ifdef QT_BUILD_INTERNAL QCoapQUdpConnectionForTest connection; QUdpSocket *socket = qobject_cast(connection.socket()); @@ -101,14 +101,10 @@ void tst_QCoapQUdpConnection::connectToHost() QTRY_COMPARE(spySocketStateChanged.count(), 1); QTRY_COMPARE(spyConnectionBound.count(), 1); QCOMPARE(connection.state(), QCoapQUdpConnection::ConnectionState::Bound); -#else - QSKIP("Not an internal build, skipping this test"); -#endif } void tst_QCoapQUdpConnection::reconnect() { -#ifdef QT_BUILD_INTERNAL QCoapQUdpConnectionForTest connection; // This will trigger connection.bind() @@ -124,9 +120,6 @@ void tst_QCoapQUdpConnection::reconnect() connection.sendRequest(QByteArray(), QString(), 0); QTRY_COMPARE(connectionBoundSpy.count(), 2); QCOMPARE(connection.state(), QCoapQUdpConnection::ConnectionState::Bound); -#else - QSKIP("Not an internal build, skipping this test"); -#endif } void tst_QCoapQUdpConnection::sendRequest_data() @@ -180,7 +173,6 @@ void tst_QCoapQUdpConnection::sendRequest_data() void tst_QCoapQUdpConnection::sendRequest() { -#ifdef QT_BUILD_INTERNAL QFETCH(QString, protocol); QFETCH(QString, host); QFETCH(QString, path); @@ -208,10 +200,22 @@ void tst_QCoapQUdpConnection::sendRequest() QByteArray data = spyConnectionReadyRead.first().first().value(); QVERIFY(QString(data.toHex()).startsWith(dataHexaHeader)); QVERIFY(QString(data.toHex()).endsWith(dataHexaPayload)); +} + #else - QSKIP("Not an internal build, skipping this test"); + +class tst_QCoapQUdpConnection : public QObject +{ + Q_OBJECT + +private slots: + void initTestCase() + { + QSKIP("Not an internal build, nothing to test"); + } +}; + #endif -} QTEST_MAIN(tst_QCoapQUdpConnection) -- cgit v1.2.3 From ae8d1fef58fa6df1408b9b11cf4b99dc7815255f Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Fri, 10 May 2019 09:58:21 +0200 Subject: Hide the API of QCoapProtocol No need to keep this class public, there is no public API for accessing it anyway. Change-Id: I231a560becb5799667819c30eca6372282adeb41 Reviewed-by: Simon Hausmann --- src/coap/coap.pro | 1 - src/coap/qcoapclient.cpp | 2 - src/coap/qcoapclient_p.h | 1 - src/coap/qcoapdiscoveryreply.h | 1 - src/coap/qcoapprotocol.cpp | 44 +++++++++++++- src/coap/qcoapprotocol.h | 96 ------------------------------ src/coap/qcoapprotocol_p.h | 56 ++++++++++++++++- tests/auto/qcoapclient/tst_qcoapclient.cpp | 1 + 8 files changed, 98 insertions(+), 104 deletions(-) delete mode 100644 src/coap/qcoapprotocol.h diff --git a/src/coap/coap.pro b/src/coap/coap.pro index 11e6932..fc81397 100644 --- a/src/coap/coap.pro +++ b/src/coap/coap.pro @@ -12,7 +12,6 @@ PUBLIC_HEADERS += \ qcoapmessage.h \ qcoapnamespace.h \ qcoapoption.h \ - qcoapprotocol.h \ qcoapreply.h \ qcoaprequest.h \ qcoapresource.h \ diff --git a/src/coap/qcoapclient.cpp b/src/coap/qcoapclient.cpp index 21af3e2..197e407 100644 --- a/src/coap/qcoapclient.cpp +++ b/src/coap/qcoapclient.cpp @@ -610,8 +610,6 @@ void QCoapClient::setSecurityConfiguration(const QCoapSecurityConfiguration &con Sets the maximum block size used by the protocol to \a blockSize when sending requests and receiving replies. The block size must be a power of two. - - \sa QCoapProtocol::setBlockSize() */ void QCoapClient::setBlockSize(quint16 blockSize) { diff --git a/src/coap/qcoapclient_p.h b/src/coap/qcoapclient_p.h index 60cd59d..e853f60 100644 --- a/src/coap/qcoapclient_p.h +++ b/src/coap/qcoapclient_p.h @@ -32,7 +32,6 @@ #define QCOAPCLIENT_P_H #include -#include #include #include #include diff --git a/src/coap/qcoapdiscoveryreply.h b/src/coap/qcoapdiscoveryreply.h index 0a09ccf..9411192 100644 --- a/src/coap/qcoapdiscoveryreply.h +++ b/src/coap/qcoapdiscoveryreply.h @@ -33,7 +33,6 @@ #include #include -#include #include QT_BEGIN_NAMESPACE diff --git a/src/coap/qcoapprotocol.cpp b/src/coap/qcoapprotocol.cpp index 63ee393..0e50452 100644 --- a/src/coap/qcoapprotocol.cpp +++ b/src/coap/qcoapprotocol.cpp @@ -43,6 +43,8 @@ QT_BEGIN_NAMESPACE Q_LOGGING_CATEGORY(lcCoapProtocol, "qt.coap.protocol") /*! + \internal + \class QCoapProtocol \inmodule QtCoap @@ -60,6 +62,8 @@ Q_LOGGING_CATEGORY(lcCoapProtocol, "qt.coap.protocol") */ /*! + \internal + \fn void QCoapProtocol::finished(QCoapReply *reply) This signal is emitted along with the \l QCoapReply::finished() signal @@ -71,6 +75,8 @@ Q_LOGGING_CATEGORY(lcCoapProtocol, "qt.coap.protocol") */ /*! + \internal + \fn void QCoapProtocol::responseToMulticastReceived(QCoapReply *reply, const QCoapMessage& message, const QHostAddress &sender) @@ -84,6 +90,8 @@ Q_LOGGING_CATEGORY(lcCoapProtocol, "qt.coap.protocol") */ /*! + \internal + \fn void QCoapProtocol::error(QCoapReply *reply, QtCoap::Error error) This signal is emitted whenever an error occurs. The \a reply parameter @@ -94,6 +102,8 @@ Q_LOGGING_CATEGORY(lcCoapProtocol, "qt.coap.protocol") */ /*! + \internal + Constructs a new QCoapProtocol and sets \a parent as the parent object. */ QCoapProtocol::QCoapProtocol(QObject *parent) : @@ -876,6 +886,8 @@ bool QCoapProtocolPrivate::isMessageIdRegistered(quint16 id) const } /*! + \internal + Returns the ACK_TIMEOUT value in milliseconds. The default is 2000. @@ -888,6 +900,8 @@ uint QCoapProtocol::ackTimeout() const } /*! + \internal + Returns the ACK_RANDOM_FACTOR value. The default is 1.5. @@ -900,6 +914,8 @@ double QCoapProtocol::ackRandomFactor() const } /*! + \internal + Returns the MAX_RETRANSMIT value. This is the maximum number of retransmissions of a message, before notifying a timeout error. The default is 4. @@ -913,6 +929,8 @@ uint QCoapProtocol::maxRetransmit() const } /*! + \internal + Returns the max block size wanted. The default is 0, which invites the server to choose the block size. @@ -925,6 +943,8 @@ quint16 QCoapProtocol::blockSize() const } /*! + \internal + Returns the MAX_TRANSMIT_SPAN in milliseconds, as defined in \l{https://tools.ietf.org/search/rfc7252#section-4.8.2}{RFC 7252}. @@ -937,6 +957,8 @@ uint QCoapProtocol::maxTransmitSpan() const } /*! + \internal + Returns the MAX_TRANSMIT_WAIT in milliseconds, as defined in \l{https://tools.ietf.org/search/rfc7252#section-4.8.2}{RFC 7252}. @@ -951,6 +973,8 @@ uint QCoapProtocol::maxTransmitWait() const } /*! + \internal + Returns the MAX_LATENCY in milliseconds, as defined in \l{https://tools.ietf.org/search/rfc7252#section-4.8.2}{RFC 7252}. This value is arbitrarily set to 100 seconds by the standard. @@ -964,6 +988,8 @@ constexpr uint QCoapProtocol::maxLatency() } /*! + \internal + Returns the minimum duration for messages timeout. The timeout is defined as a random value between minTimeout() and maxTimeout(). This is a convenience method identical to ackTimeout(). @@ -977,6 +1003,8 @@ uint QCoapProtocol::minTimeout() const } /*! + \internal + Returns the maximum duration for messages timeout in milliseconds. \sa maxTimeout(), setAckTimeout(), setAckRandomFactor() @@ -988,6 +1016,8 @@ uint QCoapProtocol::maxTimeout() const } /*! + \internal + Returns the \c NON_LIFETIME in milliseconds, as defined in \l{https://tools.ietf.org/search/rfc7252#section-4.8.2}{RFC 7252}. @@ -1000,6 +1030,8 @@ uint QCoapProtocol::nonConfirmLifetime() const } /*! + \internal + Returns the \c MAX_SERVER_RESPONSE_DELAY in milliseconds, as defined in \l {RFC 7390 - Section 2.5}. @@ -1015,6 +1047,8 @@ uint QCoapProtocol::maxServerResponseDelay() const } /*! + \internal + Sets the ACK_TIMEOUT value to \a ackTimeout in milliseconds. The default is 2000 ms. @@ -1031,6 +1065,8 @@ void QCoapProtocol::setAckTimeout(uint ackTimeout) } /*! + \internal + Sets the ACK_RANDOM_FACTOR value to \a ackRandomFactor. This value should be greater than or equal to 1. The default is 1.5. @@ -1047,6 +1083,8 @@ void QCoapProtocol::setAckRandomFactor(double ackRandomFactor) } /*! + \internal + Sets the MAX_RETRANSMIT value to \a maxRetransmit, but never to more than 25. The default is 4. @@ -1066,6 +1104,8 @@ void QCoapProtocol::setMaxRetransmit(uint maxRetransmit) } /*! + \internal + Sets the max block size wanted to \a blockSize. The \a blockSize should be zero, or range from 16 to 1024 and be a @@ -1092,6 +1132,8 @@ void QCoapProtocol::setBlockSize(quint16 blockSize) } /*! + \internal + Sets the \c MAX_SERVER_RESPONSE_DELAY value to \a responseDelay in milliseconds. The default is 250 seconds. @@ -1107,5 +1149,3 @@ void QCoapProtocol::setMaxServerResponseDelay(uint responseDelay) } QT_END_NAMESPACE - -#include "moc_qcoapprotocol.cpp" diff --git a/src/coap/qcoapprotocol.h b/src/coap/qcoapprotocol.h deleted file mode 100644 index 0c91443..0000000 --- a/src/coap/qcoapprotocol.h +++ /dev/null @@ -1,96 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 Witekio. -** Copyright (C) 2018 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCoap module. -** -** $QT_BEGIN_LICENSE:GPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 or (at your option) any later version -** approved by the KDE Free Qt Foundation. The licenses are as published by -** the Free Software Foundation and appearing in the file LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QCOAPPROTOCOL_H -#define QCOAPPROTOCOL_H - -#include -#include -#include -#include -#include -#include - -QT_BEGIN_NAMESPACE - -class QCoapInternalRequest; -class QCoapInternalReply; -class QCoapProtocolPrivate; -class QCoapConnection; -class Q_COAP_EXPORT QCoapProtocol : public QObject -{ - Q_OBJECT -public: - explicit QCoapProtocol(QObject *parent = nullptr); - ~QCoapProtocol(); - - uint ackTimeout() const; - double ackRandomFactor() const; - uint maxRetransmit() const; - quint16 blockSize() const; - uint maxTransmitSpan() const; - uint maxTransmitWait() const; - static constexpr uint maxLatency(); - - uint minTimeout() const; - uint maxTimeout() const; - - uint nonConfirmLifetime() const; - uint maxServerResponseDelay() const; - -Q_SIGNALS: - void finished(QCoapReply *reply); - void responseToMulticastReceived(QCoapReply *reply, const QCoapMessage &message, - const QHostAddress &sender); - void error(QCoapReply *reply, QtCoap::Error error); - -public: - Q_INVOKABLE void setAckTimeout(uint ackTimeout); - Q_INVOKABLE void setAckRandomFactor(double ackRandomFactor); - Q_INVOKABLE void setMaxRetransmit(uint maxRetransmit); - Q_INVOKABLE void setBlockSize(quint16 blockSize); - Q_INVOKABLE void setMaxServerResponseDelay(uint responseDelay); - -private: - Q_INVOKABLE void sendRequest(QPointer reply, QCoapConnection *connection); - Q_INVOKABLE void cancelObserve(QPointer reply) const; - Q_INVOKABLE void cancelObserve(const QUrl &url) const; - -private: - Q_DECLARE_PRIVATE(QCoapProtocol) - - friend class QCoapClient; -}; - -QT_END_NAMESPACE - -Q_DECLARE_METATYPE(QHostAddress) - -#endif // QCOAPPROTOCOL_H diff --git a/src/coap/qcoapprotocol_p.h b/src/coap/qcoapprotocol_p.h index 602ef73..b50453f 100644 --- a/src/coap/qcoapprotocol_p.h +++ b/src/coap/qcoapprotocol_p.h @@ -31,10 +31,13 @@ #ifndef QCOAPPROTOCOL_P_H #define QCOAPPROTOCOL_P_H -#include +#include +#include +#include #include #include #include +#include #include // @@ -50,6 +53,55 @@ QT_BEGIN_NAMESPACE +class QCoapInternalRequest; +class QCoapInternalReply; +class QCoapProtocolPrivate; +class QCoapConnection; +class Q_AUTOTEST_EXPORT QCoapProtocol : public QObject +{ + Q_OBJECT +public: + explicit QCoapProtocol(QObject *parent = nullptr); + ~QCoapProtocol(); + + uint ackTimeout() const; + double ackRandomFactor() const; + uint maxRetransmit() const; + quint16 blockSize() const; + uint maxTransmitSpan() const; + uint maxTransmitWait() const; + static constexpr uint maxLatency(); + + uint minTimeout() const; + uint maxTimeout() const; + + uint nonConfirmLifetime() const; + uint maxServerResponseDelay() const; + +Q_SIGNALS: + void finished(QCoapReply *reply); + void responseToMulticastReceived(QCoapReply *reply, const QCoapMessage &message, + const QHostAddress &sender); + void error(QCoapReply *reply, QtCoap::Error error); + +public: + Q_INVOKABLE void setAckTimeout(uint ackTimeout); + Q_INVOKABLE void setAckRandomFactor(double ackRandomFactor); + Q_INVOKABLE void setMaxRetransmit(uint maxRetransmit); + Q_INVOKABLE void setBlockSize(quint16 blockSize); + Q_INVOKABLE void setMaxServerResponseDelay(uint responseDelay); + +private: + Q_INVOKABLE void sendRequest(QPointer reply, QCoapConnection *connection); + Q_INVOKABLE void cancelObserve(QPointer reply) const; + Q_INVOKABLE void cancelObserve(const QUrl &url) const; + +private: + Q_DECLARE_PRIVATE(QCoapProtocol) + + friend class QCoapClient; +}; + struct CoapExchangeData { QPointer userReply; QSharedPointer request; @@ -115,4 +167,6 @@ public: QT_END_NAMESPACE +Q_DECLARE_METATYPE(QHostAddress) + #endif // QCOAPPROTOCOL_P_H diff --git a/tests/auto/qcoapclient/tst_qcoapclient.cpp b/tests/auto/qcoapclient/tst_qcoapclient.cpp index 90b44cf..2a9f9d4 100644 --- a/tests/auto/qcoapclient/tst_qcoapclient.cpp +++ b/tests/auto/qcoapclient/tst_qcoapclient.cpp @@ -39,6 +39,7 @@ #include #include #include +#include #include "../coapnetworksettings.h" -- cgit v1.2.3 From d2dd87b0a69f0bcc23c4f7ebf9a1210731f3e2c3 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Fri, 10 May 2019 15:02:43 +0200 Subject: Remove the protected constructor from QCoapClient This change is based on the feedback from API review. Change-Id: I99f00d15e9474b2844a436113ac7771ead7b33c6 Reviewed-by: Simon Hausmann --- src/coap/qcoapclient.cpp | 13 ++----------- src/coap/qcoapclient.h | 3 --- tests/auto/qcoapclient/tst_qcoapclient.cpp | 7 ++----- 3 files changed, 4 insertions(+), 19 deletions(-) diff --git a/src/coap/qcoapclient.cpp b/src/coap/qcoapclient.cpp index 197e407..b11a5b2 100644 --- a/src/coap/qcoapclient.cpp +++ b/src/coap/qcoapclient.cpp @@ -160,7 +160,7 @@ QCoapClientPrivate::~QCoapClientPrivate() constructors. */ QCoapClient::QCoapClient(QtCoap::SecurityMode securityMode, QObject *parent) : - QCoapClient(new QCoapProtocol, new QCoapQUdpConnection(securityMode), parent) + QCoapClient(new QCoapQUdpConnection(securityMode), parent) { } @@ -169,16 +169,7 @@ QCoapClient::QCoapClient(QtCoap::SecurityMode securityMode, QObject *parent) : sets \a parent as the parent object. */ QCoapClient::QCoapClient(QCoapConnection *connection, QObject *parent) : - QCoapClient(new QCoapProtocol, connection, parent) -{ -} - -/*! - Base constructor, taking the \a protocol, \a connection, and \a parent - as arguments. -*/ -QCoapClient::QCoapClient(QCoapProtocol *protocol, QCoapConnection *connection, QObject *parent) : - QObject(*new QCoapClientPrivate(protocol, connection), parent) + QObject(*new QCoapClientPrivate(new QCoapProtocol, connection), parent) { Q_D(QCoapClient); diff --git a/src/coap/qcoapclient.h b/src/coap/qcoapclient.h index fb06903..06290ce 100644 --- a/src/coap/qcoapclient.h +++ b/src/coap/qcoapclient.h @@ -95,9 +95,6 @@ Q_SIGNALS: void error(QCoapReply *reply, QtCoap::Error error); protected: - explicit QCoapClient(QCoapProtocol *protocol, QCoapConnection *connection, - QObject *parent = nullptr); - Q_DECLARE_PRIVATE(QCoapClient) }; diff --git a/tests/auto/qcoapclient/tst_qcoapclient.cpp b/tests/auto/qcoapclient/tst_qcoapclient.cpp index 2a9f9d4..8f0302e 100644 --- a/tests/auto/qcoapclient/tst_qcoapclient.cpp +++ b/tests/auto/qcoapclient/tst_qcoapclient.cpp @@ -108,7 +108,7 @@ class QCoapClientForSocketErrorTests : public QCoapClient { public: QCoapClientForSocketErrorTests() : - QCoapClient(new QCoapProtocol, new QCoapQUdpConnectionSocketTests) + QCoapClient(new QCoapQUdpConnectionSocketTests) {} QCoapQUdpConnection *connection() @@ -122,9 +122,6 @@ class QCoapClientForTests : public QCoapClient { public: QCoapClientForTests() {} - QCoapClientForTests(QCoapProtocol *protocol, QCoapQUdpConnection *connection) : - QCoapClient(protocol, connection) - {} QCoapProtocol *protocol() { @@ -165,7 +162,7 @@ class QCoapClientForMulticastTests : public QCoapClient { public: QCoapClientForMulticastTests() : - QCoapClient(new QCoapProtocol, new QCoapConnectionMulticastTests) + QCoapClient(new QCoapConnectionMulticastTests) {} QCoapConnection *connection() -- cgit v1.2.3 From 149ca6289869a30cfd033160b5662d02671fe3e1 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Fri, 10 May 2019 15:41:13 +0200 Subject: Rename QtCoap::SecurityMode::NoSec -> QtCoap::SecurityMode::NoSecurity This change is based on the feedback from API review. Change-Id: I345bc519d6753608ead0bf4b006cf68c4e52c313 Reviewed-by: Simon Hausmann --- examples/coap/quickmulticastclient/qmlcoapmulticastclient.cpp | 2 +- examples/coap/quicksecureclient/qmlcoapsecureclient.cpp | 2 +- examples/coap/simplecoapclient/mainwindow.cpp | 2 +- src/coap/qcoapclient.cpp | 4 ++-- src/coap/qcoapclient.h | 2 +- src/coap/qcoapconnection.cpp | 6 +++--- src/coap/qcoapconnection_p.h | 4 ++-- src/coap/qcoapnamespace.cpp | 2 +- src/coap/qcoapnamespace.h | 2 +- src/coap/qcoapqudpconnection.cpp | 8 ++++---- src/coap/qcoapqudpconnection_p.h | 4 ++-- tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp | 2 +- 12 files changed, 20 insertions(+), 20 deletions(-) diff --git a/examples/coap/quickmulticastclient/qmlcoapmulticastclient.cpp b/examples/coap/quickmulticastclient/qmlcoapmulticastclient.cpp index 4c3592b..196dfe5 100644 --- a/examples/coap/quickmulticastclient/qmlcoapmulticastclient.cpp +++ b/examples/coap/quickmulticastclient/qmlcoapmulticastclient.cpp @@ -56,7 +56,7 @@ Q_LOGGING_CATEGORY(lcCoapClient, "qt.coap.client") QmlCoapMulticastClient::QmlCoapMulticastClient(QObject *parent) - : QCoapClient(QtCoap::SecurityMode::NoSec, parent) + : QCoapClient(QtCoap::SecurityMode::NoSecurity, parent) { connect(this, &QCoapClient::finished, this, [this](QCoapReply *reply) { diff --git a/examples/coap/quicksecureclient/qmlcoapsecureclient.cpp b/examples/coap/quicksecureclient/qmlcoapsecureclient.cpp index 608ed0c..1d141bb 100644 --- a/examples/coap/quicksecureclient/qmlcoapsecureclient.cpp +++ b/examples/coap/quicksecureclient/qmlcoapsecureclient.cpp @@ -61,7 +61,7 @@ Q_LOGGING_CATEGORY(lcCoapClient, "qt.coap.client") QmlCoapSecureClient::QmlCoapSecureClient(QObject *parent) : QObject(parent) , m_coapClient(nullptr) - , m_securityMode(QtCoap::SecurityMode::NoSec) + , m_securityMode(QtCoap::SecurityMode::NoSecurity) { } diff --git a/examples/coap/simplecoapclient/mainwindow.cpp b/examples/coap/simplecoapclient/mainwindow.cpp index e5cda55..bff0dbc 100644 --- a/examples/coap/simplecoapclient/mainwindow.cpp +++ b/examples/coap/simplecoapclient/mainwindow.cpp @@ -65,7 +65,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { - m_client = new QCoapClient(QtCoap::SecurityMode::NoSec, this); + m_client = new QCoapClient(QtCoap::SecurityMode::NoSecurity, this); connect(m_client, &QCoapClient::finished, this, &MainWindow::onFinished); connect(m_client, &QCoapClient::error, this, &MainWindow::onError); diff --git a/src/coap/qcoapclient.cpp b/src/coap/qcoapclient.cpp index b11a5b2..c86096c 100644 --- a/src/coap/qcoapclient.cpp +++ b/src/coap/qcoapclient.cpp @@ -152,7 +152,7 @@ QCoapClientPrivate::~QCoapClientPrivate() Constructs a QCoapClient object for the given \a securityMode and sets \a parent as the parent object. - The default for \a securityMode is SecurityMode::NoSec, which + The default for \a securityMode is QtCoap::NoSecurity, which disables security. This connects using a QCoapQUdpConnection; to use a custom transport, @@ -585,7 +585,7 @@ bool QCoapClientPrivate::send(QCoapReply *reply) /*! Sets the security configuration parameters from \a configuration. - Configuration will be ignored if the QtCoap::NoSec mode is used. + Configuration will be ignored if the QtCoap::NoSecurity mode is used. \note This method must be called before the handshake starts. */ diff --git a/src/coap/qcoapclient.h b/src/coap/qcoapclient.h index 06290ce..0703ab1 100644 --- a/src/coap/qcoapclient.h +++ b/src/coap/qcoapclient.h @@ -53,7 +53,7 @@ class Q_COAP_EXPORT QCoapClient : public QObject { Q_OBJECT public: - explicit QCoapClient(QtCoap::SecurityMode securityMode = QtCoap::SecurityMode::NoSec, + explicit QCoapClient(QtCoap::SecurityMode securityMode = QtCoap::SecurityMode::NoSecurity, QObject *parent = nullptr); explicit QCoapClient(QCoapConnection *connection, QObject *parent = nullptr); ~QCoapClient(); diff --git a/src/coap/qcoapconnection.cpp b/src/coap/qcoapconnection.cpp index e104fee..23cb0d9 100644 --- a/src/coap/qcoapconnection.cpp +++ b/src/coap/qcoapconnection.cpp @@ -207,7 +207,7 @@ QCoapConnectionPrivate::sendRequest(const QByteArray &request, const QString &ho bool QCoapConnection::isSecure() const { Q_D(const QCoapConnection); - return d->securityMode != QtCoap::SecurityMode::NoSec; + return d->securityMode != QtCoap::SecurityMode::NoSecurity; } /*! @@ -249,8 +249,8 @@ void QCoapConnection::startToSendRequest() /*! Sets the security configuration parameters from the \a configuration. - The security configuration will be ignored if the QtCoap::NoSec mode is used - for connection. + The security configuration will be ignored if the QtCoap::NoSecurity mode is + used for connection. \note This method must be called before the handshake starts. */ diff --git a/src/coap/qcoapconnection_p.h b/src/coap/qcoapconnection_p.h index 07abe4a..200744d 100644 --- a/src/coap/qcoapconnection_p.h +++ b/src/coap/qcoapconnection_p.h @@ -61,7 +61,7 @@ public: Bound }; - explicit QCoapConnection(QtCoap::SecurityMode securityMode = QtCoap::SecurityMode::NoSec, + explicit QCoapConnection(QtCoap::SecurityMode securityMode = QtCoap::SecurityMode::NoSecurity, QObject *parent = nullptr); virtual ~QCoapConnection(); @@ -107,7 +107,7 @@ struct CoapFrame { class Q_AUTOTEST_EXPORT QCoapConnectionPrivate : public QObjectPrivate { public: - QCoapConnectionPrivate(QtCoap::SecurityMode security = QtCoap::SecurityMode::NoSec); + QCoapConnectionPrivate(QtCoap::SecurityMode security = QtCoap::SecurityMode::NoSecurity); ~QCoapConnectionPrivate() override = default; diff --git a/src/coap/qcoapnamespace.cpp b/src/coap/qcoapnamespace.cpp index b1e5fc4..a20a152 100644 --- a/src/coap/qcoapnamespace.cpp +++ b/src/coap/qcoapnamespace.cpp @@ -238,7 +238,7 @@ QT_BEGIN_NAMESPACE Specifies the security mode used for securing a CoAP connection, as defined in \l{https://tools.ietf.org/html/rfc7252#section-9}{RFC 7252}. - \value NoSec There is no protocol-level security (DTLS is disabled). + \value NoSecurity There is no protocol-level security (DTLS is disabled). \value PreSharedKey DTLS is enabled. PSK authentication will be used for security. diff --git a/src/coap/qcoapnamespace.h b/src/coap/qcoapnamespace.h index 353a046..1a64311 100644 --- a/src/coap/qcoapnamespace.h +++ b/src/coap/qcoapnamespace.h @@ -104,7 +104,7 @@ namespace QtCoap Q_ENUM_NS(Port) enum class SecurityMode : quint8 { - NoSec = 0, + NoSecurity = 0, PreSharedKey, RawPublicKey, Certificate diff --git a/src/coap/qcoapqudpconnection.cpp b/src/coap/qcoapqudpconnection.cpp index 08d96d1..4ca78dd 100644 --- a/src/coap/qcoapqudpconnection.cpp +++ b/src/coap/qcoapqudpconnection.cpp @@ -70,7 +70,7 @@ Q_DECLARE_LOGGING_CATEGORY(lcCoapConnection) sets \a parent as the parent object. \note Since QtCoap::RawPublicKey is not supported yet, the connection - will fall back to the QtCoap::NoSec in the QtCoap::RawPublicKey mode. + will fall back to the QtCoap::NoSecurity in the QtCoap::RawPublicKey mode. That is, the connection won't be secure in this mode. */ QCoapQUdpConnection::QCoapQUdpConnection(QtCoap::SecurityMode securityMode, QObject *parent) : @@ -106,7 +106,7 @@ QCoapQUdpConnection::QCoapQUdpConnection(QCoapQUdpConnectionPrivate &dd, QObject case QtCoap::SecurityMode::RawPublicKey: qCWarning(lcCoapConnection, "RawPublicKey security is not supported yet," "disabling security"); - d->securityMode = QtCoap::SecurityMode::NoSec; + d->securityMode = QtCoap::SecurityMode::NoSecurity; break; case QtCoap::SecurityMode::PreSharedKey: d->dtls = new QDtls(QSslSocket::SslClientMode, this); @@ -127,8 +127,8 @@ QCoapQUdpConnection::QCoapQUdpConnection(QCoapQUdpConnectionPrivate &dd, QObject break; } #else - qCWarning(lcCoapConnection, "DTLS is disabled, falling back to QtCoap::NoSec mode."); - d->securityMode = QtCoap::SecurityMode::NoSec; + qCWarning(lcCoapConnection, "DTLS is disabled, falling back to QtCoap::NoSecurity mode."); + d->securityMode = QtCoap::SecurityMode::NoSecurity; #endif } } diff --git a/src/coap/qcoapqudpconnection_p.h b/src/coap/qcoapqudpconnection_p.h index 1ac1603..ba953e4 100644 --- a/src/coap/qcoapqudpconnection_p.h +++ b/src/coap/qcoapqudpconnection_p.h @@ -58,7 +58,7 @@ class Q_AUTOTEST_EXPORT QCoapQUdpConnection : public QCoapConnection Q_OBJECT public: - explicit QCoapQUdpConnection(QtCoap::SecurityMode security = QtCoap::SecurityMode::NoSec, + explicit QCoapQUdpConnection(QtCoap::SecurityMode security = QtCoap::SecurityMode::NoSecurity, QObject *parent = nullptr); ~QCoapQUdpConnection() override = default; @@ -89,7 +89,7 @@ protected: class Q_AUTOTEST_EXPORT QCoapQUdpConnectionPrivate : public QCoapConnectionPrivate { public: - QCoapQUdpConnectionPrivate(QtCoap::SecurityMode security = QtCoap::SecurityMode::NoSec); + QCoapQUdpConnectionPrivate(QtCoap::SecurityMode security = QtCoap::SecurityMode::NoSecurity); ~QCoapQUdpConnectionPrivate() override; virtual bool bind(); diff --git a/tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp b/tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp index daed5fb..947cb5d 100644 --- a/tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp +++ b/tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp @@ -69,7 +69,7 @@ class QCoapQUdpConnectionForTest : public QCoapQUdpConnection Q_OBJECT public: QCoapQUdpConnectionForTest(QObject *parent = nullptr) : - QCoapQUdpConnection(QtCoap::SecurityMode::NoSec, parent) + QCoapQUdpConnection(QtCoap::SecurityMode::NoSecurity, parent) {} void bindSocketForTest() { d_func()->bindSocket(); } -- cgit v1.2.3 From a5beff7436aba965d16b63d5677b6d3920724c59 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Fri, 10 May 2019 17:44:44 +0200 Subject: Hide internally used methods from QtCoap namespace Additionally rename responseCodeError() -> errorForResponseCode(). This change is based on the feedback from API review. Change-Id: Ida63a854f628d3d92a93bd54bd80f2368de48d8d Reviewed-by: Simon Hausmann --- src/coap/coap.pro | 1 + src/coap/qcoapdiscoveryreply.cpp | 1 + src/coap/qcoapnamespace.cpp | 10 ++++-- src/coap/qcoapnamespace.h | 4 --- src/coap/qcoapnamespace_p.h | 57 ++++++++++++++++++++++++++++++++ src/coap/qcoapprotocol.cpp | 3 +- src/coap/qcoapreply.cpp | 4 ++- tests/auto/qcoapreply/tst_qcoapreply.cpp | 24 ++++++++++---- 8 files changed, 89 insertions(+), 15 deletions(-) create mode 100644 src/coap/qcoapnamespace_p.h diff --git a/src/coap/coap.pro b/src/coap/coap.pro index fc81397..54df74b 100644 --- a/src/coap/coap.pro +++ b/src/coap/coap.pro @@ -25,6 +25,7 @@ PRIVATE_HEADERS += \ qcoapinternalreply_p.h \ qcoapinternalrequest_p.h \ qcoapmessage_p.h \ + qcoapnamespace_p.h \ qcoapoption_p.h \ qcoapprotocol_p.h \ qcoapqudpconnection_p.h \ diff --git a/src/coap/qcoapdiscoveryreply.cpp b/src/coap/qcoapdiscoveryreply.cpp index def9d19..abde474 100644 --- a/src/coap/qcoapdiscoveryreply.cpp +++ b/src/coap/qcoapdiscoveryreply.cpp @@ -30,6 +30,7 @@ #include "qcoapdiscoveryreply_p.h" #include "qcoapinternalreply_p.h" +#include "qcoapnamespace_p.h" QT_BEGIN_NAMESPACE diff --git a/src/coap/qcoapnamespace.cpp b/src/coap/qcoapnamespace.cpp index a20a152..dbf0f52 100644 --- a/src/coap/qcoapnamespace.cpp +++ b/src/coap/qcoapnamespace.cpp @@ -27,7 +27,7 @@ ** ****************************************************************************/ -#include "qcoapnamespace.h" +#include "qcoapnamespace_p.h" QT_BEGIN_NAMESPACE @@ -269,6 +269,8 @@ QT_BEGIN_NAMESPACE */ /*! + \internal + Returns \c true if \a code corresponds to an error, returns \c false otherwise. */ bool QtCoap::isError(QtCoap::ResponseCode code) @@ -277,10 +279,12 @@ bool QtCoap::isError(QtCoap::ResponseCode code) } /*! + \internal + Returns the QtCoap::Error corresponding to the \a code passed to this method. */ -QtCoap::Error QtCoap::responseCodeError(QtCoap::ResponseCode code) +QtCoap::Error QtCoap::errorForResponseCode(QtCoap::ResponseCode code) { if (!isError(code)) return QtCoap::Error::Ok; @@ -295,6 +299,8 @@ QtCoap::Error QtCoap::responseCodeError(QtCoap::ResponseCode code) } /*! + \internal + Returns the internal random generator used for generating token values and message IDs. */ diff --git a/src/coap/qcoapnamespace.h b/src/coap/qcoapnamespace.h index 1a64311..f81c92b 100644 --- a/src/coap/qcoapnamespace.h +++ b/src/coap/qcoapnamespace.h @@ -118,10 +118,6 @@ namespace QtCoap }; Q_ENUM_NS(MulticastGroup) - Q_COAP_EXPORT bool isError(ResponseCode code); - Q_COAP_EXPORT Error responseCodeError(ResponseCode code); - Q_COAP_EXPORT QRandomGenerator &randomGenerator(); - Q_CLASSINFO("RegisterEnumClassesUnscoped", "false") } diff --git a/src/coap/qcoapnamespace_p.h b/src/coap/qcoapnamespace_p.h new file mode 100644 index 0000000..0417a04 --- /dev/null +++ b/src/coap/qcoapnamespace_p.h @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCoap module. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QCOAPNAMESPACE_P_H +#define QCOAPNAMESPACE_P_H + +#include "qcoapnamespace.h" + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +QT_BEGIN_NAMESPACE + +namespace QtCoap +{ + bool Q_AUTOTEST_EXPORT isError(QtCoap::ResponseCode code); + Error Q_AUTOTEST_EXPORT errorForResponseCode(QtCoap::ResponseCode code); + QRandomGenerator Q_AUTOTEST_EXPORT &randomGenerator(); +} + +QT_END_NAMESPACE + +#endif // QCOAPNAMESPACE_P_H diff --git a/src/coap/qcoapprotocol.cpp b/src/coap/qcoapprotocol.cpp index 0e50452..2de5583 100644 --- a/src/coap/qcoapprotocol.cpp +++ b/src/coap/qcoapprotocol.cpp @@ -32,6 +32,7 @@ #include "qcoapinternalrequest_p.h" #include "qcoapinternalreply_p.h" #include "qcoapconnection_p.h" +#include "qcoapnamespace_p.h" #include #include @@ -291,7 +292,7 @@ void QCoapProtocolPrivate::onMulticastRequestExpired(QCoapInternalRequest *reque */ void QCoapProtocolPrivate::onRequestError(QCoapInternalRequest *request, QCoapInternalReply *reply) { - QtCoap::Error error = QtCoap::responseCodeError(reply->responseCode()); + QtCoap::Error error = QtCoap::errorForResponseCode(reply->responseCode()); onRequestError(request, error, reply); } diff --git a/src/coap/qcoapreply.cpp b/src/coap/qcoapreply.cpp index 9122551..847539e 100644 --- a/src/coap/qcoapreply.cpp +++ b/src/coap/qcoapreply.cpp @@ -30,6 +30,8 @@ #include "qcoapreply_p.h" #include "qcoapinternalreply_p.h" +#include "qcoapnamespace_p.h" + #include #include @@ -161,7 +163,7 @@ void QCoapReplyPrivate::_q_setError(QtCoap::Error newError) */ void QCoapReplyPrivate::_q_setError(QtCoap::ResponseCode code) { - _q_setError(QtCoap::responseCodeError(code)); + _q_setError(QtCoap::errorForResponseCode(code)); } /*! diff --git a/tests/auto/qcoapreply/tst_qcoapreply.cpp b/tests/auto/qcoapreply/tst_qcoapreply.cpp index d9a7562..f22098e 100644 --- a/tests/auto/qcoapreply/tst_qcoapreply.cpp +++ b/tests/auto/qcoapreply/tst_qcoapreply.cpp @@ -33,6 +33,9 @@ #include #include +#include + +#ifdef QT_BUILD_INTERNAL class tst_QCoapReply : public QObject { @@ -128,20 +131,15 @@ void tst_QCoapReply::updateReply() void tst_QCoapReply::requestData() { -#ifdef QT_BUILD_INTERNAL QCoapReplyForTests reply((QCoapRequest())); reply.setRunning("token", 543); QCOMPARE(reply.request().token(), QByteArray("token")); QCOMPARE(reply.request().messageId(), 543); -#else - QSKIP("Not an internal build, skipping this test"); -#endif } void tst_QCoapReply::abortRequest() { -#ifdef QT_BUILD_INTERNAL QCoapReplyForTests reply((QCoapRequest())); reply.setRunning("token", 543); @@ -154,10 +152,22 @@ void tst_QCoapReply::abortRequest() QTRY_COMPARE_WITH_TIMEOUT(spyFinished.count(), 1, 1000); QVERIFY(arguments.at(0).toByteArray() == "token"); QCOMPARE(reply.isSuccessful(), false); +} + #else - QSKIP("Not an internal build, skipping this test"); + +class tst_QCoapReply : public QObject +{ + Q_OBJECT + +private slots: + void initTestCase() + { + QSKIP("Not an internal build, nothing to test"); + } +}; + #endif -} QTEST_MAIN(tst_QCoapReply) -- cgit v1.2.3 From 218d796a1e9a2ae9ad87c71f7487a62d3a7dbab2 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Sat, 11 May 2019 00:26:02 +0200 Subject: Rename QCoapDiscoveryReply -> QCoapResourceDiscoveryReply This change is based on the feedback from API review. Change-Id: I43f08d5bd7f7f172ea4d337718be7bd14e81d64c Reviewed-by: Simon Hausmann --- README.md | 4 +- examples/coap/consolecoapclient/coaphandler.cpp | 8 +- examples/coap/consolecoapclient/coaphandler.h | 4 +- examples/coap/doc/simplecoapclient.qdoc | 4 +- .../qmlcoapmulticastclient.cpp | 12 +- .../quickmulticastclient/qmlcoapmulticastclient.h | 2 +- examples/coap/simplecoapclient/mainwindow.cpp | 8 +- examples/coap/simplecoapclient/mainwindow.h | 2 +- src/coap/coap.pro | 8 +- src/coap/doc/src/qtcoap-overview.qdoc | 8 +- src/coap/qcoapclient.cpp | 26 ++-- src/coap/qcoapclient.h | 14 +- src/coap/qcoapclient_p.h | 2 +- src/coap/qcoapdiscoveryreply.cpp | 161 -------------------- src/coap/qcoapdiscoveryreply.h | 62 -------- src/coap/qcoapdiscoveryreply_p.h | 66 --------- src/coap/qcoapreply.cpp | 2 +- src/coap/qcoaprequest.cpp | 2 +- src/coap/qcoapresource.cpp | 2 +- src/coap/qcoapresourcediscoveryreply.cpp | 162 +++++++++++++++++++++ src/coap/qcoapresourcediscoveryreply.h | 62 ++++++++ src/coap/qcoapresourcediscoveryreply_p.h | 66 +++++++++ tests/auto/qcoapclient/tst_qcoapclient.cpp | 4 +- tests/auto/qcoapresource/tst_qcoapresource.cpp | 4 +- 24 files changed, 349 insertions(+), 346 deletions(-) delete mode 100644 src/coap/qcoapdiscoveryreply.cpp delete mode 100644 src/coap/qcoapdiscoveryreply.h delete mode 100644 src/coap/qcoapdiscoveryreply_p.h create mode 100644 src/coap/qcoapresourcediscoveryreply.cpp create mode 100644 src/coap/qcoapresourcediscoveryreply.h create mode 100644 src/coap/qcoapresourcediscoveryreply_p.h diff --git a/README.md b/README.md index 3d98801..c14ee3e 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ The notified signal will provide the `QCoapReply` and most recent message. For machine to machine communication, CoAP Discovery requests is used to query the resources available to an endpoint, or to the complete network. ```c++ -QCoapDiscoveryReply* reply = client->discover("coap://coap.me/"); +QCoapResourceDiscoveryReply *reply = client->discover("coap://coap.me/"); connect(reply, &QCoapReply::discovered, this, &MyClass::onDiscovered); ``` @@ -63,7 +63,7 @@ For multicast discovery use one of the groups from the `QtCoap::MulticastGroup` specifying the discovery path: ```c++ -QCoapDiscoveryReply* reply = client->discover(QtCoap::AllCoapNodesIPv6LinkLocal); +QCoapResourceDiscoveryReply *reply = client->discover(QtCoap::AllCoapNodesIPv6LinkLocal); ``` If no group is specified, `QtCoap::AllCoapNodesIPv4` will be used by default. diff --git a/examples/coap/consolecoapclient/coaphandler.cpp b/examples/coap/consolecoapclient/coaphandler.cpp index 650cf5c..fa9fc34 100644 --- a/examples/coap/consolecoapclient/coaphandler.cpp +++ b/examples/coap/consolecoapclient/coaphandler.cpp @@ -34,7 +34,7 @@ #include #include #include -#include +#include Q_LOGGING_CATEGORY(lcCoapClient, "qt.coap.client") @@ -78,11 +78,11 @@ bool CoapHandler::runObserve(const QUrl &url) bool CoapHandler::runDiscover(const QUrl &url) { - QCoapDiscoveryReply *discoverReply = m_coapClient.discover(url); + QCoapResourceDiscoveryReply *discoverReply = m_coapClient.discover(url); if (!discoverReply) return false; - connect(discoverReply, &QCoapDiscoveryReply::discovered, this, &CoapHandler::onDiscovered); + connect(discoverReply, &QCoapResourceDiscoveryReply::discovered, this, &CoapHandler::onDiscovered); return true; } @@ -105,7 +105,7 @@ void CoapHandler::onNotified(QCoapReply *reply, QCoapMessage message) qCInfo(lcCoapClient) << "Received Observe notification with payload:" << reply->readAll(); } -void CoapHandler::onDiscovered(QCoapDiscoveryReply *reply, QVector resources) +void CoapHandler::onDiscovered(QCoapResourceDiscoveryReply *reply, QVector resources) { Q_UNUSED(reply) diff --git a/examples/coap/consolecoapclient/coaphandler.h b/examples/coap/consolecoapclient/coaphandler.h index 1034ad1..a109a4c 100644 --- a/examples/coap/consolecoapclient/coaphandler.h +++ b/examples/coap/consolecoapclient/coaphandler.h @@ -40,7 +40,7 @@ QT_BEGIN_NAMESPACE class QCoapReply; -class QCoapDiscoveryReply; +class QCoapResourceDiscoveryReply; class QCoapResource; QT_END_NAMESPACE @@ -61,7 +61,7 @@ public: public Q_SLOTS: void onFinished(QCoapReply *reply); void onNotified(QCoapReply *reply, QCoapMessage message); - void onDiscovered(QCoapDiscoveryReply *reply, QVector resources); + void onDiscovered(QCoapResourceDiscoveryReply *reply, QVector resources); void onResponseToMulticast(QCoapReply *reply, const QCoapMessage& message, const QHostAddress &sender); void onError(QCoapReply *reply, QtCoap::Error error); diff --git a/examples/coap/doc/simplecoapclient.qdoc b/examples/coap/doc/simplecoapclient.qdoc index 961d473..438442d 100644 --- a/examples/coap/doc/simplecoapclient.qdoc +++ b/examples/coap/doc/simplecoapclient.qdoc @@ -134,8 +134,8 @@ \printuntil onDiscovered \dots - \note Instead of QCoapReply class, we use the QCoapDiscoveryReply to keep - the reply for a discovery request. It has the QCoapDiscoveryReply::discovered + \note Instead of QCoapReply class, we use the QCoapResourceDiscoveryReply to keep + the reply for a discovery request. It has the QCoapResourceDiscoveryReply::discovered signal, which returns the list of QCoapResources that has been discovered. If there are \e observable resources on the server (meaning that they have the diff --git a/examples/coap/quickmulticastclient/qmlcoapmulticastclient.cpp b/examples/coap/quickmulticastclient/qmlcoapmulticastclient.cpp index 196dfe5..7c868eb 100644 --- a/examples/coap/quickmulticastclient/qmlcoapmulticastclient.cpp +++ b/examples/coap/quickmulticastclient/qmlcoapmulticastclient.cpp @@ -50,7 +50,7 @@ #include "qmlcoapmulticastclient.h" -#include +#include #include Q_LOGGING_CATEGORY(lcCoapClient, "qt.coap.client") @@ -73,9 +73,9 @@ void QmlCoapMulticastClient::discover(const QString &host, int port, const QStri url.setHost(host); url.setPort(port); - QCoapDiscoveryReply *discoverReply = QCoapClient::discover(url, discoveryPath); + QCoapResourceDiscoveryReply *discoverReply = QCoapClient::discover(url, discoveryPath); if (discoverReply) { - connect(discoverReply, &QCoapDiscoveryReply::discovered, + connect(discoverReply, &QCoapResourceDiscoveryReply::discovered, this, &QmlCoapMulticastClient::onDiscovered); } else { qCWarning(lcCoapClient, "Discovery request failed."); @@ -85,16 +85,16 @@ void QmlCoapMulticastClient::discover(const QString &host, int port, const QStri void QmlCoapMulticastClient::discover(QtCoap::MulticastGroup group, int port, const QString &discoveryPath) { - QCoapDiscoveryReply *discoverReply = QCoapClient::discover(group, port, discoveryPath); + QCoapResourceDiscoveryReply *discoverReply = QCoapClient::discover(group, port, discoveryPath); if (discoverReply) { - connect(discoverReply, &QCoapDiscoveryReply::discovered, + connect(discoverReply, &QCoapResourceDiscoveryReply::discovered, this, &QmlCoapMulticastClient::onDiscovered); } else { qCWarning(lcCoapClient, "Discovery request failed."); } } -void QmlCoapMulticastClient::onDiscovered(QCoapDiscoveryReply *reply, +void QmlCoapMulticastClient::onDiscovered(QCoapResourceDiscoveryReply *reply, const QVector &resources) { Q_UNUSED(reply) diff --git a/examples/coap/quickmulticastclient/qmlcoapmulticastclient.h b/examples/coap/quickmulticastclient/qmlcoapmulticastclient.h index 3dd91fc..7770ade 100644 --- a/examples/coap/quickmulticastclient/qmlcoapmulticastclient.h +++ b/examples/coap/quickmulticastclient/qmlcoapmulticastclient.h @@ -85,7 +85,7 @@ Q_SIGNALS: void finished(QtCoap::Error error); public slots: - void onDiscovered(QCoapDiscoveryReply *reply, const QVector &resources); + void onDiscovered(QCoapResourceDiscoveryReply *reply, const QVector &resources); }; #endif // QMLCOAPMULTICASTCLIENT_H diff --git a/examples/coap/simplecoapclient/mainwindow.cpp b/examples/coap/simplecoapclient/mainwindow.cpp index bff0dbc..fc103ad 100644 --- a/examples/coap/simplecoapclient/mainwindow.cpp +++ b/examples/coap/simplecoapclient/mainwindow.cpp @@ -52,7 +52,7 @@ #include "optiondialog.h" #include "ui_mainwindow.h" -#include +#include #include #include #include @@ -122,7 +122,7 @@ void MainWindow::onError(QCoapReply *reply, QtCoap::Error error) addMessage(errorMessage(errorCode), true); } -void MainWindow::onDiscovered(QCoapDiscoveryReply *reply, QVector resources) +void MainWindow::onDiscovered(QCoapResourceDiscoveryReply *reply, QVector resources) { if (reply->errorReceived() != QtCoap::Error::Ok) return; @@ -192,9 +192,9 @@ void MainWindow::on_discoverButton_clicked() url.setHost(tryToResolveHostName(ui->hostComboBox->currentText())); url.setPort(ui->portSpinBox->value()); - QCoapDiscoveryReply *discoverReply = m_client->discover(url, ui->discoveryPathEdit->text()); + QCoapResourceDiscoveryReply *discoverReply = m_client->discover(url, ui->discoveryPathEdit->text()); if (discoverReply) - connect(discoverReply, &QCoapDiscoveryReply::discovered, this, &MainWindow::onDiscovered); + connect(discoverReply, &QCoapResourceDiscoveryReply::discovered, this, &MainWindow::onDiscovered); else QMessageBox::critical(this, "Error", "Something went wrong, discovery request failed."); } diff --git a/examples/coap/simplecoapclient/mainwindow.h b/examples/coap/simplecoapclient/mainwindow.h index a561c0e..2cb9f98 100644 --- a/examples/coap/simplecoapclient/mainwindow.h +++ b/examples/coap/simplecoapclient/mainwindow.h @@ -78,7 +78,7 @@ private: private slots: void onFinished(QCoapReply *reply); void onError(QCoapReply *reply, QtCoap::Error error); - void onDiscovered(QCoapDiscoveryReply *reply, QVector resources); + void onDiscovered(QCoapResourceDiscoveryReply *reply, QVector resources); void onNotified(QCoapReply *reply, const QCoapMessage &message); void on_runButton_clicked(); diff --git a/src/coap/coap.pro b/src/coap/coap.pro index 54df74b..dbd9e6c 100644 --- a/src/coap/coap.pro +++ b/src/coap/coap.pro @@ -7,7 +7,6 @@ QMAKE_DOCS = $$PWD/doc/qtcoap.qdocconf PUBLIC_HEADERS += \ qcoapclient.h \ - qcoapdiscoveryreply.h \ qcoapglobal.h \ qcoapmessage.h \ qcoapnamespace.h \ @@ -15,12 +14,12 @@ PUBLIC_HEADERS += \ qcoapreply.h \ qcoaprequest.h \ qcoapresource.h \ + qcoapresourcediscoveryreply.h \ qcoapsecurityconfiguration.h PRIVATE_HEADERS += \ qcoapclient_p.h \ qcoapconnection_p.h \ - qcoapdiscoveryreply_p.h \ qcoapinternalmessage_p.h \ qcoapinternalreply_p.h \ qcoapinternalrequest_p.h \ @@ -31,12 +30,12 @@ PRIVATE_HEADERS += \ qcoapqudpconnection_p.h \ qcoapreply_p.h \ qcoaprequest_p.h \ - qcoapresource_p.h + qcoapresource_p.h \ + qcoapresourcediscoveryreply_p.h SOURCES += \ qcoapclient.cpp \ qcoapconnection.cpp \ - qcoapdiscoveryreply.cpp \ qcoapinternalmessage.cpp \ qcoapinternalreply.cpp \ qcoapinternalrequest.cpp \ @@ -48,6 +47,7 @@ SOURCES += \ qcoapreply.cpp \ qcoaprequest.cpp \ qcoapresource.cpp \ + qcoapresourcediscoveryreply.cpp \ qcoapsecurityconfiguration.cpp HEADERS += $$PUBLIC_HEADERS $$PRIVATE_HEADERS diff --git a/src/coap/doc/src/qtcoap-overview.qdoc b/src/coap/doc/src/qtcoap-overview.qdoc index 5400362..c686974 100644 --- a/src/coap/doc/src/qtcoap-overview.qdoc +++ b/src/coap/doc/src/qtcoap-overview.qdoc @@ -106,15 +106,15 @@ firmware resource available in the network. The reply is represented in \l {https://tools.ietf.org/html/rfc6690}{CoRE Link Format}. - The specialized QCoapDiscoveryReply class is used to get the discovery replies: + The specialized QCoapResourceDiscoveryReply class is used to get the discovery replies: \code // This will make a multicast discovery request to the CoAP IPv4 multicast group. - QCoapDiscoveryReply *discoverReply = client->discover(); - connect(discoverReply, &QCoapDiscoveryReply::discovered, this, &MyClass::onDiscovered); + QCoapResourceDiscoveryReply *discoverReply = client->discover(); + connect(discoverReply, &QCoapResourceDiscoveryReply::discovered, this, &MyClass::onDiscovered); \endcode - The QCoapDiscoveryReply::discovered will return the list of CoAP resources found in the + The QCoapResourceDiscoveryReply::discovered will return the list of CoAP resources found in the network. \section2 Resource Observation diff --git a/src/coap/qcoapclient.cpp b/src/coap/qcoapclient.cpp index c86096c..f2205a0 100644 --- a/src/coap/qcoapclient.cpp +++ b/src/coap/qcoapclient.cpp @@ -31,7 +31,7 @@ #include "qcoapclient_p.h" #include "qcoapprotocol_p.h" #include "qcoapreply.h" -#include "qcoapdiscoveryreply.h" +#include "qcoapresourcediscoveryreply.h" #include "qcoapnamespace.h" #include "qcoapsecurityconfiguration.h" #include "qcoapqudpconnection_p.h" @@ -107,11 +107,11 @@ QCoapClientPrivate::~QCoapClientPrivate() When a reply arrives, the QCoapClient emits a finished() signal. - \note For a discovery request, the returned object is a QCoapDiscoveryReply. + \note For a discovery request, the returned object is a QCoapResourceDiscoveryReply. It can be used the same way as a QCoapReply but contains also a list of resources. - \sa QCoapRequest, QCoapReply, QCoapDiscoveryReply + \sa QCoapRequest, QCoapReply, QCoapResourceDiscoveryReply */ /*! @@ -176,7 +176,7 @@ QCoapClient::QCoapClient(QCoapConnection *connection, QObject *parent) : qRegisterMetaType(); qRegisterMetaType(); qRegisterMetaType>(); - qRegisterMetaType>(); + qRegisterMetaType>(); qRegisterMetaType(); qRegisterMetaType(); qRegisterMetaType(); @@ -373,8 +373,8 @@ QCoapReply *QCoapClient::deleteResource(const QUrl &url) \overload Discovers the resources available at the endpoints which have joined - the \a group at the given \a port. Returns a new QCoapDiscoveryReply - object which emits the \l QCoapDiscoveryReply::discovered() signal whenever + the \a group at the given \a port. Returns a new QCoapResourceDiscoveryReply + object which emits the \l QCoapResourceDiscoveryReply::discovered() signal whenever a response arrives. The \a group is one of the CoAP multicast group addresses and defaults to QtCoap::AllCoapNodesIPv4. @@ -384,7 +384,7 @@ QCoapReply *QCoapClient::deleteResource(const QUrl &url) \sa get(), post(), put(), deleteResource(), observe() */ -QCoapDiscoveryReply *QCoapClient::discover(QtCoap::MulticastGroup group, int port, +QCoapResourceDiscoveryReply *QCoapClient::discover(QtCoap::MulticastGroup group, int port, const QString &discoveryPath) { Q_D(QCoapClient); @@ -416,8 +416,8 @@ QCoapDiscoveryReply *QCoapClient::discover(QtCoap::MulticastGroup group, int por /*! Discovers the resources available at the given \a url and returns - a new QCoapDiscoveryReply object which emits the - \l QCoapDiscoveryReply::discovered() signal whenever the response + a new QCoapResourceDiscoveryReply object which emits the + \l QCoapResourceDiscoveryReply::discovered() signal whenever the response arrives. Discovery path defaults to "/.well-known/core", but can be changed @@ -426,7 +426,7 @@ QCoapDiscoveryReply *QCoapClient::discover(QtCoap::MulticastGroup group, int por \sa get(), post(), put(), deleteResource(), observe() */ -QCoapDiscoveryReply *QCoapClient::discover(const QUrl &url, const QString &discoveryPath) +QCoapResourceDiscoveryReply *QCoapClient::discover(const QUrl &url, const QString &discoveryPath) { Q_D(QCoapClient); @@ -532,14 +532,14 @@ QCoapReply *QCoapClientPrivate::sendRequest(const QCoapRequest &request) \internal Sends the CoAP \a request to its own URL and returns a - new QCoapDiscoveryReply object. + new QCoapResourceDiscoveryReply object. */ -QCoapDiscoveryReply *QCoapClientPrivate::sendDiscovery(const QCoapRequest &request) +QCoapResourceDiscoveryReply *QCoapClientPrivate::sendDiscovery(const QCoapRequest &request) { Q_Q(QCoapClient); // Prepare the reply - QCoapDiscoveryReply *reply = new QCoapDiscoveryReply(request, q); + QCoapResourceDiscoveryReply *reply = new QCoapResourceDiscoveryReply(request, q); if (!send(reply)) { delete reply; diff --git a/src/coap/qcoapclient.h b/src/coap/qcoapclient.h index 0703ab1..b64905b 100644 --- a/src/coap/qcoapclient.h +++ b/src/coap/qcoapclient.h @@ -40,7 +40,7 @@ QT_BEGIN_NAMESPACE class QCoapReply; -class QCoapDiscoveryReply; +class QCoapResourceDiscoveryReply; class QCoapRequest; class QCoapProtocol; class QCoapConnection; @@ -74,11 +74,13 @@ public: void cancelObserve(const QUrl &url); void disconnect(); - QCoapDiscoveryReply *discover(QtCoap::MulticastGroup group = QtCoap::MulticastGroup::AllCoapNodesIPv4, - int port = QtCoap::DefaultPort, - const QString &discoveryPath = QLatin1String("/.well-known/core")); - QCoapDiscoveryReply *discover(const QUrl &baseUrl, - const QString &discoveryPath = QLatin1String("/.well-known/core")); + QCoapResourceDiscoveryReply *discover( + QtCoap::MulticastGroup group = QtCoap::MulticastGroup::AllCoapNodesIPv4, + int port = QtCoap::DefaultPort, + const QString &discoveryPath = QLatin1String("/.well-known/core")); + QCoapResourceDiscoveryReply *discover( + const QUrl &baseUrl, + const QString &discoveryPath = QLatin1String("/.well-known/core")); void setSecurityConfiguration(const QCoapSecurityConfiguration &configuration); void setBlockSize(quint16 blockSize); diff --git a/src/coap/qcoapclient_p.h b/src/coap/qcoapclient_p.h index e853f60..84d3a83 100644 --- a/src/coap/qcoapclient_p.h +++ b/src/coap/qcoapclient_p.h @@ -60,7 +60,7 @@ public: QThread *workerThread = nullptr; QCoapReply *sendRequest(const QCoapRequest &request); - QCoapDiscoveryReply *sendDiscovery(const QCoapRequest &request); + QCoapResourceDiscoveryReply *sendDiscovery(const QCoapRequest &request); bool send(QCoapReply *reply); Q_DECLARE_PUBLIC(QCoapClient) diff --git a/src/coap/qcoapdiscoveryreply.cpp b/src/coap/qcoapdiscoveryreply.cpp deleted file mode 100644 index abde474..0000000 --- a/src/coap/qcoapdiscoveryreply.cpp +++ /dev/null @@ -1,161 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 Witekio. -** Copyright (C) 2018 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCoap module. -** -** $QT_BEGIN_LICENSE:GPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 or (at your option) any later version -** approved by the KDE Free Qt Foundation. The licenses are as published by -** the Free Software Foundation and appearing in the file LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qcoapdiscoveryreply_p.h" -#include "qcoapinternalreply_p.h" -#include "qcoapnamespace_p.h" - -QT_BEGIN_NAMESPACE - -QCoapDiscoveryReplyPrivate::QCoapDiscoveryReplyPrivate(const QCoapRequest &request) : - QCoapReplyPrivate(request) -{ -} - -/*! - \internal - - Updates the QCoapDiscoveryReply object, its message and list of resources - with data of the internal reply \a internalReply. -*/ -void QCoapDiscoveryReplyPrivate::_q_setContent(const QHostAddress &sender, const QCoapMessage &msg, - QtCoap::ResponseCode code) -{ - Q_Q(QCoapDiscoveryReply); - - if (q->isFinished()) - return; - - message = msg; - responseCode = code; - - if (QtCoap::isError(responseCode)) { - _q_setError(responseCode); - } else { - auto res = QCoapDiscoveryReply::resourcesFromCoreLinkList(sender, message.payload()); - resources.append(res); - emit q->discovered(q, res); - } -} - -/*! - \class QCoapDiscoveryReply - \inmodule QtCoap - - \brief The QCoapDiscoveryReply class holds the data of a CoAP reply - for a resource discovery request. - - \reentrant - - This class is used for discovery requests. It emits the discovered() - signal if and when resources are discovered. When using a multicast - address for discovery, the discovered() signal will be emitted once - for each response received. - - \note A QCoapDiscoveryReply is a QCoapReply that stores also a list - of QCoapResources. - - \sa QCoapClient, QCoapRequest, QCoapReply, QCoapResource -*/ - -/*! - \fn void QCoapDiscoveryReply::discovered(QCoapDiscoveryReply *reply, - QVector resources); - - This signal is emitted whenever a CoAP resource is discovered. - - The \a reply parameter contains a pointer to the reply that has just been - received, and \a resources contains a list of resources that were discovered. - - \sa QCoapReply::finished() -*/ - -/*! - Constructs a new CoAP discovery reply from the \a request and sets \a parent - as its parent. -*/ -QCoapDiscoveryReply::QCoapDiscoveryReply(const QCoapRequest &request, QObject *parent) : - QCoapReply(*new QCoapDiscoveryReplyPrivate(request), parent) -{ -} - -/*! - Returns the list of resources. -*/ -QVector QCoapDiscoveryReply::resources() const -{ - Q_D(const QCoapDiscoveryReply); - return d->resources; -} - -/*! - Decodes the \a data received from the \a sender to a list of QCoapResource - objects. The \a data byte array contains the frame returned by the - discovery request. -*/ -QVector -QCoapDiscoveryReply::resourcesFromCoreLinkList(const QHostAddress &sender, const QByteArray &data) -{ - QVector resourceList; - - QLatin1String quote = QLatin1String("\""); - const QList links = data.split(','); - for (QByteArray link : links) { - QCoapResource resource; - resource.setHost(sender); - - const QList parameterList = link.split(';'); - for (QByteArray parameter : parameterList) { - QString parameterString = QString::fromUtf8(parameter); - int length = parameterString.length(); - if (parameter.startsWith('<')) - resource.setPath(parameterString.mid(1, length - 2)); - else if (parameter.startsWith("title=")) - resource.setTitle(parameterString.mid(6).remove(quote)); - else if (parameter.startsWith("rt=")) - resource.setResourceType(parameterString.mid(3).remove(quote)); - else if (parameter.startsWith("if=")) - resource.setInterface(parameterString.mid(3).remove(quote)); - else if (parameter.startsWith("sz=")) - resource.setMaximumSize(parameterString.mid(3).remove(quote).toInt()); - else if (parameter.startsWith("ct=")) - resource.setContentFormat(parameterString.mid(3).remove(quote).toUInt()); - else if (parameter == "obs") - resource.setObservable(true); - } - - if (!resource.path().isEmpty()) - resourceList.push_back(resource); - } - - return resourceList; -} - -QT_END_NAMESPACE diff --git a/src/coap/qcoapdiscoveryreply.h b/src/coap/qcoapdiscoveryreply.h deleted file mode 100644 index 9411192..0000000 --- a/src/coap/qcoapdiscoveryreply.h +++ /dev/null @@ -1,62 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 Witekio. -** Copyright (C) 2018 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCoap module. -** -** $QT_BEGIN_LICENSE:GPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 or (at your option) any later version -** approved by the KDE Free Qt Foundation. The licenses are as published by -** the Free Software Foundation and appearing in the file LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QCOAPDISCOVERYREPLY_H -#define QCOAPDISCOVERYREPLY_H - -#include -#include -#include - -QT_BEGIN_NAMESPACE - -class QCoapDiscoveryReplyPrivate; -class Q_COAP_EXPORT QCoapDiscoveryReply : public QCoapReply -{ - Q_OBJECT - -public: - explicit QCoapDiscoveryReply(const QCoapRequest &request, QObject *parent = nullptr); - - QVector resources() const; - - static QVector resourcesFromCoreLinkList( - const QHostAddress &sender, const QByteArray &data); - -Q_SIGNALS: - void discovered(QCoapDiscoveryReply *reply, QVector resources); - -private: - Q_DECLARE_PRIVATE(QCoapDiscoveryReply) -}; - -QT_END_NAMESPACE - -#endif // QCOAPDISCOVERYREPLY_H diff --git a/src/coap/qcoapdiscoveryreply_p.h b/src/coap/qcoapdiscoveryreply_p.h deleted file mode 100644 index 5f4856e..0000000 --- a/src/coap/qcoapdiscoveryreply_p.h +++ /dev/null @@ -1,66 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 Witekio. -** Copyright (C) 2018 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCoap module. -** -** $QT_BEGIN_LICENSE:GPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 or (at your option) any later version -** approved by the KDE Free Qt Foundation. The licenses are as published by -** the Free Software Foundation and appearing in the file LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QCOAPDISCOVERYREPLY_P_H -#define QCOAPDISCOVERYREPLY_P_H - -#include -#include -#include -#include - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -QT_BEGIN_NAMESPACE - -class Q_AUTOTEST_EXPORT QCoapDiscoveryReplyPrivate : public QCoapReplyPrivate -{ -public: - QCoapDiscoveryReplyPrivate(const QCoapRequest &request); - - void _q_setContent(const QHostAddress &sender, const QCoapMessage &, QtCoap::ResponseCode) override; - - QVector resources; - - Q_DECLARE_PUBLIC(QCoapDiscoveryReply) -}; - -QT_END_NAMESPACE - -#endif // QCOAPDISCOVERYREPLY_P_H diff --git a/src/coap/qcoapreply.cpp b/src/coap/qcoapreply.cpp index 847539e..7d6ba40 100644 --- a/src/coap/qcoapreply.cpp +++ b/src/coap/qcoapreply.cpp @@ -183,7 +183,7 @@ void QCoapReplyPrivate::_q_setError(QtCoap::ResponseCode code) For \e Observe requests specifically, the notified() signal is emitted whenever a notification is received. - \sa QCoapClient, QCoapRequest, QCoapDiscoveryReply + \sa QCoapClient, QCoapRequest, QCoapResourceDiscoveryReply */ /*! diff --git a/src/coap/qcoaprequest.cpp b/src/coap/qcoaprequest.cpp index 1d9a441..47abc2b 100644 --- a/src/coap/qcoaprequest.cpp +++ b/src/coap/qcoaprequest.cpp @@ -107,7 +107,7 @@ void QCoapRequestPrivate::setUrl(const QUrl &url) The QCoapRequest contains data needed to make CoAP frames that can be sent to the URL it holds. - \sa QCoapClient, QCoapReply, QCoapDiscoveryReply + \sa QCoapClient, QCoapReply, QCoapResourceDiscoveryReply */ /*! diff --git a/src/coap/qcoapresource.cpp b/src/coap/qcoapresource.cpp index 7e20209..3ce1eea 100644 --- a/src/coap/qcoapresource.cpp +++ b/src/coap/qcoapresource.cpp @@ -44,7 +44,7 @@ QT_BEGIN_NAMESPACE The QCoapRequest contains data as the path and title of the resource and other ancillary information. - \sa QCoapDiscoveryReply + \sa QCoapResourceDiscoveryReply */ /*! diff --git a/src/coap/qcoapresourcediscoveryreply.cpp b/src/coap/qcoapresourcediscoveryreply.cpp new file mode 100644 index 0000000..81affc2 --- /dev/null +++ b/src/coap/qcoapresourcediscoveryreply.cpp @@ -0,0 +1,162 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Witekio. +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCoap module. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qcoapresourcediscoveryreply_p.h" +#include "qcoapinternalreply_p.h" +#include "qcoapnamespace_p.h" + +QT_BEGIN_NAMESPACE + +QCoapResourceDiscoveryReplyPrivate::QCoapResourceDiscoveryReplyPrivate(const QCoapRequest &request) : + QCoapReplyPrivate(request) +{ +} + +/*! + \internal + + Updates the QCoapResourceDiscoveryReply object, its message and list of resources + with data of the internal reply \a internalReply. +*/ +void QCoapResourceDiscoveryReplyPrivate::_q_setContent(const QHostAddress &sender, + const QCoapMessage &msg, + QtCoap::ResponseCode code) +{ + Q_Q(QCoapResourceDiscoveryReply); + + if (q->isFinished()) + return; + + message = msg; + responseCode = code; + + if (QtCoap::isError(responseCode)) { + _q_setError(responseCode); + } else { + auto res = QCoapResourceDiscoveryReply::resourcesFromCoreLinkList(sender, message.payload()); + resources.append(res); + emit q->discovered(q, res); + } +} + +/*! + \class QCoapResourceDiscoveryReply + \inmodule QtCoap + + \brief The QCoapResourceDiscoveryReply class holds the data of a CoAP reply + for a resource discovery request. + + \reentrant + + This class is used for discovery requests. It emits the discovered() + signal if and when resources are discovered. When using a multicast + address for discovery, the discovered() signal will be emitted once + for each response received. + + \note A QCoapResourceDiscoveryReply is a QCoapReply that stores also a list + of QCoapResources. + + \sa QCoapClient, QCoapRequest, QCoapReply, QCoapResource +*/ + +/*! + \fn void QCoapResourceDiscoveryReply::discovered(QCoapResourceDiscoveryReply *reply, + QVector resources); + + This signal is emitted whenever a CoAP resource is discovered. + + The \a reply parameter contains a pointer to the reply that has just been + received, and \a resources contains a list of resources that were discovered. + + \sa QCoapReply::finished() +*/ + +/*! + Constructs a new CoAP discovery reply from the \a request and sets \a parent + as its parent. +*/ +QCoapResourceDiscoveryReply::QCoapResourceDiscoveryReply(const QCoapRequest &request, QObject *parent) : + QCoapReply(*new QCoapResourceDiscoveryReplyPrivate(request), parent) +{ +} + +/*! + Returns the list of resources. +*/ +QVector QCoapResourceDiscoveryReply::resources() const +{ + Q_D(const QCoapResourceDiscoveryReply); + return d->resources; +} + +/*! + Decodes the \a data received from the \a sender to a list of QCoapResource + objects. The \a data byte array contains the frame returned by the + discovery request. +*/ +QVector +QCoapResourceDiscoveryReply::resourcesFromCoreLinkList(const QHostAddress &sender, const QByteArray &data) +{ + QVector resourceList; + + QLatin1String quote = QLatin1String("\""); + const QList links = data.split(','); + for (QByteArray link : links) { + QCoapResource resource; + resource.setHost(sender); + + const QList parameterList = link.split(';'); + for (QByteArray parameter : parameterList) { + QString parameterString = QString::fromUtf8(parameter); + int length = parameterString.length(); + if (parameter.startsWith('<')) + resource.setPath(parameterString.mid(1, length - 2)); + else if (parameter.startsWith("title=")) + resource.setTitle(parameterString.mid(6).remove(quote)); + else if (parameter.startsWith("rt=")) + resource.setResourceType(parameterString.mid(3).remove(quote)); + else if (parameter.startsWith("if=")) + resource.setInterface(parameterString.mid(3).remove(quote)); + else if (parameter.startsWith("sz=")) + resource.setMaximumSize(parameterString.mid(3).remove(quote).toInt()); + else if (parameter.startsWith("ct=")) + resource.setContentFormat(parameterString.mid(3).remove(quote).toUInt()); + else if (parameter == "obs") + resource.setObservable(true); + } + + if (!resource.path().isEmpty()) + resourceList.push_back(resource); + } + + return resourceList; +} + +QT_END_NAMESPACE diff --git a/src/coap/qcoapresourcediscoveryreply.h b/src/coap/qcoapresourcediscoveryreply.h new file mode 100644 index 0000000..132f3d7 --- /dev/null +++ b/src/coap/qcoapresourcediscoveryreply.h @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Witekio. +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCoap module. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QCOAPRESOURCEDISCOVERYREPLY_H +#define QCOAPRESOURCEDISCOVERYREPLY_H + +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class QCoapResourceDiscoveryReplyPrivate; +class Q_COAP_EXPORT QCoapResourceDiscoveryReply : public QCoapReply +{ + Q_OBJECT + +public: + explicit QCoapResourceDiscoveryReply(const QCoapRequest &request, QObject *parent = nullptr); + + QVector resources() const; + + static QVector resourcesFromCoreLinkList( + const QHostAddress &sender, const QByteArray &data); + +Q_SIGNALS: + void discovered(QCoapResourceDiscoveryReply *reply, QVector resources); + +private: + Q_DECLARE_PRIVATE(QCoapResourceDiscoveryReply) +}; + +QT_END_NAMESPACE + +#endif // QCOAPRESOURCEDISCOVERYREPLY_H diff --git a/src/coap/qcoapresourcediscoveryreply_p.h b/src/coap/qcoapresourcediscoveryreply_p.h new file mode 100644 index 0000000..3d2a539 --- /dev/null +++ b/src/coap/qcoapresourcediscoveryreply_p.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Witekio. +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCoap module. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QCOAPRESOURCEDISCOVERYREPLY_P_H +#define QCOAPRESOURCEDISCOVERYREPLY_P_H + +#include +#include +#include +#include + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +QT_BEGIN_NAMESPACE + +class Q_AUTOTEST_EXPORT QCoapResourceDiscoveryReplyPrivate : public QCoapReplyPrivate +{ +public: + QCoapResourceDiscoveryReplyPrivate(const QCoapRequest &request); + + void _q_setContent(const QHostAddress &sender, const QCoapMessage &, QtCoap::ResponseCode) override; + + QVector resources; + + Q_DECLARE_PUBLIC(QCoapResourceDiscoveryReply) +}; + +QT_END_NAMESPACE + +#endif // QCOAPRESOURCEDISCOVERYREPLY_P_H diff --git a/tests/auto/qcoapclient/tst_qcoapclient.cpp b/tests/auto/qcoapclient/tst_qcoapclient.cpp index 8f0302e..b06fa21 100644 --- a/tests/auto/qcoapclient/tst_qcoapclient.cpp +++ b/tests/auto/qcoapclient/tst_qcoapclient.cpp @@ -34,7 +34,7 @@ #include #include #include -#include +#include #include #include #include @@ -699,7 +699,7 @@ void tst_QCoapClient::discover() QCoapClient client; - QScopedPointer resourcesReply(client.discover(url)); // /.well-known/core + QScopedPointer resourcesReply(client.discover(url)); // /.well-known/core QSignalSpy spyReplyFinished(resourcesReply.data(), SIGNAL(finished(QCoapReply *))); QTRY_COMPARE_WITH_TIMEOUT(spyReplyFinished.count(), 1, 30000); diff --git a/tests/auto/qcoapresource/tst_qcoapresource.cpp b/tests/auto/qcoapresource/tst_qcoapresource.cpp index 0d54029..c71b1d9 100644 --- a/tests/auto/qcoapresource/tst_qcoapresource.cpp +++ b/tests/auto/qcoapresource/tst_qcoapresource.cpp @@ -32,7 +32,7 @@ #include #include -#include +#include class tst_QCoapResource : public QObject { @@ -145,7 +145,7 @@ void tst_QCoapResource::parseCoreLink() QFETCH(QByteArray, coreLinkList); const QVector resourceList = - QCoapDiscoveryReply::resourcesFromCoreLinkList(QHostAddress(senderAddress), coreLinkList); + QCoapResourceDiscoveryReply::resourcesFromCoreLinkList(QHostAddress(senderAddress), coreLinkList); QCOMPARE(resourceList.size(), resourceNumber); -- cgit v1.2.3 From 04b0424b8cf1aae3e0d9e8496650ce3329872917 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Sun, 12 May 2019 21:39:37 +0200 Subject: Move resourcesFromCoreLinkList() to the private class This change is based on the feedback from API review. Change-Id: I189b2505c64ba01f6a6294853a12074a0035585a Reviewed-by: Simon Hausmann --- src/coap/qcoapresourcediscoveryreply.cpp | 8 ++++++-- src/coap/qcoapresourcediscoveryreply.h | 3 --- src/coap/qcoapresourcediscoveryreply_p.h | 3 +++ tests/auto/qcoapresource/qcoapresource.pro | 2 +- tests/auto/qcoapresource/tst_qcoapresource.cpp | 8 +++++++- 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/coap/qcoapresourcediscoveryreply.cpp b/src/coap/qcoapresourcediscoveryreply.cpp index 81affc2..dabdeb6 100644 --- a/src/coap/qcoapresourcediscoveryreply.cpp +++ b/src/coap/qcoapresourcediscoveryreply.cpp @@ -60,7 +60,8 @@ void QCoapResourceDiscoveryReplyPrivate::_q_setContent(const QHostAddress &sende if (QtCoap::isError(responseCode)) { _q_setError(responseCode); } else { - auto res = QCoapResourceDiscoveryReply::resourcesFromCoreLinkList(sender, message.payload()); + auto res = QCoapResourceDiscoveryReplyPrivate::resourcesFromCoreLinkList(sender, + message.payload()); resources.append(res); emit q->discovered(q, res); } @@ -117,12 +118,15 @@ QVector QCoapResourceDiscoveryReply::resources() const } /*! + \internal + Decodes the \a data received from the \a sender to a list of QCoapResource objects. The \a data byte array contains the frame returned by the discovery request. */ QVector -QCoapResourceDiscoveryReply::resourcesFromCoreLinkList(const QHostAddress &sender, const QByteArray &data) +QCoapResourceDiscoveryReplyPrivate::resourcesFromCoreLinkList(const QHostAddress &sender, + const QByteArray &data) { QVector resourceList; diff --git a/src/coap/qcoapresourcediscoveryreply.h b/src/coap/qcoapresourcediscoveryreply.h index 132f3d7..65f386d 100644 --- a/src/coap/qcoapresourcediscoveryreply.h +++ b/src/coap/qcoapresourcediscoveryreply.h @@ -47,9 +47,6 @@ public: QVector resources() const; - static QVector resourcesFromCoreLinkList( - const QHostAddress &sender, const QByteArray &data); - Q_SIGNALS: void discovered(QCoapResourceDiscoveryReply *reply, QVector resources); diff --git a/src/coap/qcoapresourcediscoveryreply_p.h b/src/coap/qcoapresourcediscoveryreply_p.h index 3d2a539..d074dbf 100644 --- a/src/coap/qcoapresourcediscoveryreply_p.h +++ b/src/coap/qcoapresourcediscoveryreply_p.h @@ -56,6 +56,9 @@ public: void _q_setContent(const QHostAddress &sender, const QCoapMessage &, QtCoap::ResponseCode) override; + static QVector resourcesFromCoreLinkList( + const QHostAddress &sender, const QByteArray &data); + QVector resources; Q_DECLARE_PUBLIC(QCoapResourceDiscoveryReply) diff --git a/tests/auto/qcoapresource/qcoapresource.pro b/tests/auto/qcoapresource/qcoapresource.pro index 6687ad2..8acf36b 100644 --- a/tests/auto/qcoapresource/qcoapresource.pro +++ b/tests/auto/qcoapresource/qcoapresource.pro @@ -1,4 +1,4 @@ -QT = testlib network core-private core coap +QT = testlib network core-private core coap coap-private CONFIG += testcase SOURCES += tst_qcoapresource.cpp diff --git a/tests/auto/qcoapresource/tst_qcoapresource.cpp b/tests/auto/qcoapresource/tst_qcoapresource.cpp index c71b1d9..52743ee 100644 --- a/tests/auto/qcoapresource/tst_qcoapresource.cpp +++ b/tests/auto/qcoapresource/tst_qcoapresource.cpp @@ -33,6 +33,7 @@ #include #include +#include class tst_QCoapResource : public QObject { @@ -133,6 +134,7 @@ void tst_QCoapResource::parseCoreLink_data() void tst_QCoapResource::parseCoreLink() { +#ifdef QT_BUILD_INTERNAL QFETCH(int, resourceNumber); QFETCH(QString, senderAddress); QFETCH(QList, pathList); @@ -145,7 +147,8 @@ void tst_QCoapResource::parseCoreLink() QFETCH(QByteArray, coreLinkList); const QVector resourceList = - QCoapResourceDiscoveryReply::resourcesFromCoreLinkList(QHostAddress(senderAddress), coreLinkList); + QCoapResourceDiscoveryReplyPrivate::resourcesFromCoreLinkList( + QHostAddress(senderAddress), coreLinkList); QCOMPARE(resourceList.size(), resourceNumber); @@ -161,6 +164,9 @@ void tst_QCoapResource::parseCoreLink() QCOMPARE(resource.observable(), observableList[resourceIndex]); ++resourceIndex; } +#else + QSKIP("Not an internal build, skipping this test"); +#endif } QTEST_APPLESS_MAIN(tst_QCoapResource) -- cgit v1.2.3 From 3d10238a8be9cecdf6850791d0520d17e33297a6 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Sun, 12 May 2019 22:17:32 +0200 Subject: Improve the API of QCoapMessage - Rename QCoapMessage::MessageType -> QCoapMessage::Type - Rename option() -> optionAt() - Rename removeAllOptions() -> clearOptions() - Hide findOption(). option() is more convenient to use instead. - Add a setOptions() method. This change is based on the feedback from API review. Change-Id: I2093a06ca09eebb95865a82e09d2aa2265379e5d Reviewed-by: Simon Hausmann --- examples/coap/simplecoapclient/mainwindow.cpp | 4 +- src/coap/qcoapclient.cpp | 2 +- src/coap/qcoapinternalreply.cpp | 2 +- src/coap/qcoapinternalrequest.cpp | 8 ++-- src/coap/qcoapmessage.cpp | 35 ++++++++++----- src/coap/qcoapmessage.h | 14 +++--- src/coap/qcoapmessage_p.h | 6 ++- src/coap/qcoapprotocol.cpp | 8 ++-- src/coap/qcoaprequest.cpp | 6 +-- src/coap/qcoaprequest.h | 7 ++- src/coap/qcoaprequest_p.h | 4 +- tests/auto/qcoapclient/tst_qcoapclient.cpp | 51 +++++++++++----------- .../qcoapinternalreply/tst_qcoapinternalreply.cpp | 16 +++---- .../tst_qcoapinternalrequest.cpp | 22 +++++----- tests/auto/qcoapmessage/tst_qcoapmessage.cpp | 26 +++++------ tests/auto/qcoaprequest/tst_qcoaprequest.cpp | 4 +- 16 files changed, 113 insertions(+), 102 deletions(-) diff --git a/examples/coap/simplecoapclient/mainwindow.cpp b/examples/coap/simplecoapclient/mainwindow.cpp index fc103ad..19f907a 100644 --- a/examples/coap/simplecoapclient/mainwindow.cpp +++ b/examples/coap/simplecoapclient/mainwindow.cpp @@ -154,8 +154,8 @@ static QString tryToResolveHostName(const QString hostName) void MainWindow::on_runButton_clicked() { - const auto msgType = ui->msgTypeCheckBox->isChecked() ? QCoapMessage::MessageType::Confirmable - : QCoapMessage::MessageType::NonConfirmable; + const auto msgType = ui->msgTypeCheckBox->isChecked() ? QCoapMessage::Type::Confirmable + : QCoapMessage::Type::NonConfirmable; QUrl url; url.setHost(tryToResolveHostName(ui->hostComboBox->currentText())); url.setPort(ui->portSpinBox->value()); diff --git a/src/coap/qcoapclient.cpp b/src/coap/qcoapclient.cpp index f2205a0..6e1e52b 100644 --- a/src/coap/qcoapclient.cpp +++ b/src/coap/qcoapclient.cpp @@ -570,7 +570,7 @@ bool QCoapClientPrivate::send(QCoapReply *reply) // According to https://tools.ietf.org/html/rfc7252#section-8.1, // multicast requests MUST be Non-confirmable. if (QHostAddress(reply->url().host()).isMulticast() - && reply->request().type() == QCoapMessage::MessageType::Confirmable) { + && reply->request().type() == QCoapMessage::Type::Confirmable) { qCWarning(lcCoapClient, "Failed to send request, " "multicast requests must be non-confirmable."); return false; diff --git a/src/coap/qcoapinternalreply.cpp b/src/coap/qcoapinternalreply.cpp index 191e8ba..fe27ee0 100644 --- a/src/coap/qcoapinternalreply.cpp +++ b/src/coap/qcoapinternalreply.cpp @@ -92,7 +92,7 @@ QCoapInternalReply *QCoapInternalReply::createFromFrame(const QByteArray &reply, // Parse Header and Token d->message.setVersion((pduData[0] >> 6) & 0x03); - d->message.setType(QCoapMessage::MessageType((pduData[0] >> 4) & 0x03)); + d->message.setType(QCoapMessage::Type((pduData[0] >> 4) & 0x03)); quint8 tokenLength = (pduData[0]) & 0x0F; d->responseCode = static_cast(pduData[1]); d->message.setMessageId(static_cast((static_cast(pduData[2]) << 8) diff --git a/src/coap/qcoapinternalrequest.cpp b/src/coap/qcoapinternalrequest.cpp index 2e10432..0237259 100644 --- a/src/coap/qcoapinternalrequest.cpp +++ b/src/coap/qcoapinternalrequest.cpp @@ -111,11 +111,11 @@ void QCoapInternalRequest::initForAcknowledgment(quint16 messageId, const QByteA Q_D(QCoapInternalRequest); setMethod(QtCoap::Method::Invalid); - d->message.setType(QCoapMessage::MessageType::Acknowledgment); + d->message.setType(QCoapMessage::Type::Acknowledgment); d->message.setMessageId(messageId); d->message.setToken(token); d->message.setPayload(QByteArray()); - d->message.removeAllOptions(); + d->message.clearOptions(); } /*! @@ -130,11 +130,11 @@ void QCoapInternalRequest::initForReset(quint16 messageId) Q_D(QCoapInternalRequest); setMethod(QtCoap::Method::Invalid); - d->message.setType(QCoapMessage::MessageType::Reset); + d->message.setType(QCoapMessage::Type::Reset); d->message.setMessageId(messageId); d->message.setToken(QByteArray()); d->message.setPayload(QByteArray()); - d->message.removeAllOptions(); + d->message.clearOptions(); } /*! diff --git a/src/coap/qcoapmessage.cpp b/src/coap/qcoapmessage.cpp index d07611c..21462f7 100644 --- a/src/coap/qcoapmessage.cpp +++ b/src/coap/qcoapmessage.cpp @@ -32,7 +32,7 @@ QT_BEGIN_NAMESPACE -QCoapMessagePrivate::QCoapMessagePrivate(QCoapMessage::MessageType _type) : +QCoapMessagePrivate::QCoapMessagePrivate(QCoapMessage::Type _type) : type(_type) { } @@ -64,7 +64,7 @@ QCoapMessagePrivate::~QCoapMessagePrivate() */ /*! - \enum QCoapMessage::MessageType + \enum QCoapMessage::Type Indicates the type of the message. @@ -179,7 +179,7 @@ void QCoapMessage::removeOption(QCoapOption::OptionName name) /*! Removes all options. */ -void QCoapMessage::removeAllOptions() +void QCoapMessage::clearOptions() { Q_D(QCoapMessage); d->options.clear(); @@ -201,7 +201,7 @@ quint8 QCoapMessage::version() const \sa setType() */ -QCoapMessage::MessageType QCoapMessage::type() const +QCoapMessage::Type QCoapMessage::type() const { Q_D(const QCoapMessage); return d->type; @@ -252,7 +252,7 @@ QByteArray QCoapMessage::payload() const /*! Returns the option at \a index position. */ -QCoapOption QCoapMessage::option(int index) const +QCoapOption QCoapMessage::optionAt(int index) const { Q_D(const QCoapMessage); return d->options.at(index); @@ -260,25 +260,27 @@ QCoapOption QCoapMessage::option(int index) const /*! Finds and returns the first option with the given \a name. - If there is no such option, returns an Invalid CoapOption with an empty value. + If there is no such option, returns an invalid QCoapOption with an empty value. */ QCoapOption QCoapMessage::option(QCoapOption::OptionName name) const { Q_D(const QCoapMessage); - auto it = findOption(name); + auto it = d->findOption(name); return it != d->options.end() ? *it : QCoapOption(); } /*! + \internal + Finds and returns a constant iterator to the first option with the given \a name. If there is no such option, returns \c d->options.end(). */ -QVector::const_iterator QCoapMessage::findOption(QCoapOption::OptionName name) const +QVector::const_iterator +QCoapMessagePrivate::findOption(QCoapOption::OptionName name) const { - Q_D(const QCoapMessage); - return std::find_if(d->options.begin(), d->options.end(), [name](const QCoapOption &option) { + return std::find_if(options.begin(), options.end(), [name](const QCoapOption &option) { return option.name() == name; }); } @@ -290,7 +292,7 @@ QVector::const_iterator QCoapMessage::findOption(QCoapOption::Optio bool QCoapMessage::hasOption(QCoapOption::OptionName name) const { Q_D(const QCoapMessage); - return findOption(name) != d->options.end(); + return d->findOption(name) != d->options.end(); } /*! @@ -342,7 +344,7 @@ void QCoapMessage::setVersion(quint8 version) \sa type() */ -void QCoapMessage::setType(const MessageType &type) +void QCoapMessage::setType(const Type &type) { Q_D(QCoapMessage); d->type = type; @@ -387,6 +389,15 @@ void QCoapMessage::setPayload(const QByteArray &payload) d->payload = payload; } +/*! + Sets the message options to \a options. +*/ +void QCoapMessage::setOptions(const QVector &options) +{ + Q_D(QCoapMessage); + d->options = options; +} + void QCoapMessage::swap(QCoapMessage &other) Q_DECL_NOTHROW { qSwap(d_ptr, other.d_ptr); diff --git a/src/coap/qcoapmessage.h b/src/coap/qcoapmessage.h index 36288a7..b88a41a 100644 --- a/src/coap/qcoapmessage.h +++ b/src/coap/qcoapmessage.h @@ -44,7 +44,7 @@ class QCoapMessagePrivate; class Q_COAP_EXPORT QCoapMessage { public: - enum class MessageType : quint8 { + enum class Type : quint8 { Confirmable, NonConfirmable, Acknowledgment, @@ -60,20 +60,20 @@ public: QCoapMessage &operator=(QCoapMessage &&other) Q_DECL_NOTHROW; quint8 version() const; - MessageType type() const; + Type type() const; QByteArray token() const; quint8 tokenLength() const; quint16 messageId() const; QByteArray payload() const; void setVersion(quint8 version); - void setType(const MessageType &type); + void setType(const Type &type); void setToken(const QByteArray &token); void setMessageId(quint16); void setPayload(const QByteArray &payload); + void setOptions(const QVector &options); - QCoapOption option(int index) const; + QCoapOption optionAt(int index) const; QCoapOption option(QCoapOption::OptionName name) const; - QVector::const_iterator findOption(QCoapOption::OptionName name) const; bool hasOption(QCoapOption::OptionName name) const; const QVector &options() const; QVector options(QCoapOption::OptionName name) const; @@ -82,7 +82,7 @@ public: void addOption(const QCoapOption &option); void removeOption(const QCoapOption &option); void removeOption(QCoapOption::OptionName name); - void removeAllOptions(); + void clearOptions(); protected: explicit QCoapMessage(QCoapMessagePrivate &dd); @@ -99,6 +99,6 @@ Q_DECLARE_SHARED(QCoapMessage) QT_END_NAMESPACE Q_DECLARE_METATYPE(QCoapMessage) -Q_DECLARE_METATYPE(QCoapMessage::MessageType) +Q_DECLARE_METATYPE(QCoapMessage::Type) #endif // QCOAPMESSAGE_H diff --git a/src/coap/qcoapmessage_p.h b/src/coap/qcoapmessage_p.h index b16ba2b..fbff1b8 100644 --- a/src/coap/qcoapmessage_p.h +++ b/src/coap/qcoapmessage_p.h @@ -51,12 +51,14 @@ QT_BEGIN_NAMESPACE class Q_AUTOTEST_EXPORT QCoapMessagePrivate : public QSharedData { public: - QCoapMessagePrivate(QCoapMessage::MessageType type = QCoapMessage::MessageType::NonConfirmable); + QCoapMessagePrivate(QCoapMessage::Type type = QCoapMessage::Type::NonConfirmable); QCoapMessagePrivate(const QCoapMessagePrivate &other); ~QCoapMessagePrivate(); + QVector::const_iterator findOption(QCoapOption::OptionName name) const; + quint8 version = 1; - QCoapMessage::MessageType type = QCoapMessage::MessageType::NonConfirmable; + QCoapMessage::Type type = QCoapMessage::Type::NonConfirmable; quint16 messageId = 0; QByteArray token; QVector options; diff --git a/src/coap/qcoapprotocol.cpp b/src/coap/qcoapprotocol.cpp index 2de5583..2f4c278 100644 --- a/src/coap/qcoapprotocol.cpp +++ b/src/coap/qcoapprotocol.cpp @@ -178,7 +178,7 @@ void QCoapProtocol::sendRequest(QPointer reply, QCoapConnection *con internalRequest->setToSendBlock(0, d->blockSize); } - if (requestMessage->type() == QCoapMessage::MessageType::Confirmable) + if (requestMessage->type() == QCoapMessage::Type::Confirmable) internalRequest->setTimeout(QtCoap::randomGenerator().bounded(minTimeout(), maxTimeout())); else internalRequest->setTimeout(maxTimeout()); @@ -239,7 +239,7 @@ void QCoapProtocolPrivate::onRequestTimeout(QCoapInternalRequest *request) if (!isRequestRegistered(request)) return; - if (request->message()->type() == QCoapMessage::MessageType::Confirmable + if (request->message()->type() == QCoapMessage::Type::Confirmable && request->retransmissionCounter() < maxRetransmit) { sendRequest(request); } else { @@ -376,7 +376,7 @@ void QCoapProtocolPrivate::onFrameReceived(const QByteArray &data, const QHostAd // Remove option to ensure that it will stop request->removeOption(QCoapOption::Observe); sendReset(request); - } else if (messageReceived->type() == QCoapMessage::MessageType::Confirmable) { + } else if (messageReceived->type() == QCoapMessage::Type::Confirmable) { sendAcknowledgment(request); } @@ -515,7 +515,7 @@ void QCoapProtocolPrivate::onLastMessageReceived(QCoapInternalRequest *request, auto lastReply = replies.last(); // Ignore empty ACK messages - if (lastReply->message()->type() == QCoapMessage::MessageType::Acknowledgment + if (lastReply->message()->type() == QCoapMessage::Type::Acknowledgment && lastReply->responseCode() == QtCoap::ResponseCode::EmptyMessage) { exchangeMap[request->token()].replies.takeLast(); return; diff --git a/src/coap/qcoaprequest.cpp b/src/coap/qcoaprequest.cpp index 47abc2b..59cdb59 100644 --- a/src/coap/qcoaprequest.cpp +++ b/src/coap/qcoaprequest.cpp @@ -44,7 +44,7 @@ const auto CoapScheme = QLatin1String("coap"); const auto CoapSecureScheme = QLatin1String("coaps"); } -QCoapRequestPrivate::QCoapRequestPrivate(const QUrl &url, QCoapMessage::MessageType type, +QCoapRequestPrivate::QCoapRequestPrivate(const QUrl &url, QCoapMessage::Type type, const QUrl &proxyUrl) : QCoapMessagePrivate(type), proxyUri(proxyUrl) @@ -114,7 +114,7 @@ void QCoapRequestPrivate::setUrl(const QUrl &url) Constructs a QCoapRequest object with the target \a url, the proxy URL \a proxyUrl and the \a type of the message. */ -QCoapRequest::QCoapRequest(const QUrl &url, MessageType type, const QUrl &proxyUrl) : +QCoapRequest::QCoapRequest(const QUrl &url, Type type, const QUrl &proxyUrl) : QCoapMessage(*new QCoapRequestPrivate(url, type, proxyUrl)) { } @@ -122,7 +122,7 @@ QCoapRequest::QCoapRequest(const QUrl &url, MessageType type, const QUrl &proxyU /*! Constructs a QCoapRequest from a string literal */ -QCoapRequest::QCoapRequest(const char *url, MessageType type) : +QCoapRequest::QCoapRequest(const char *url, Type type) : QCoapMessage(*new QCoapRequestPrivate(QUrl(QString::fromUtf8(url)), type)) { } diff --git a/src/coap/qcoaprequest.h b/src/coap/qcoaprequest.h index c833334..e506c83 100644 --- a/src/coap/qcoaprequest.h +++ b/src/coap/qcoaprequest.h @@ -46,10 +46,9 @@ class Q_COAP_EXPORT QCoapRequest : public QCoapMessage { public: explicit QCoapRequest(const QUrl &url = QUrl(), - MessageType type = MessageType::NonConfirmable, - const QUrl &proxyUrl = QUrl()); - explicit QCoapRequest(const char* url, - MessageType type = MessageType::NonConfirmable); + Type type = Type::NonConfirmable, + const QUrl &proxyUrl = QUrl()); + explicit QCoapRequest(const char* url, Type type = Type::NonConfirmable); QCoapRequest(const QCoapRequest &other); ~QCoapRequest(); diff --git a/src/coap/qcoaprequest_p.h b/src/coap/qcoaprequest_p.h index 67c26c6..eae236d 100644 --- a/src/coap/qcoaprequest_p.h +++ b/src/coap/qcoaprequest_p.h @@ -52,8 +52,8 @@ class Q_AUTOTEST_EXPORT QCoapRequestPrivate : public QCoapMessagePrivate { public: QCoapRequestPrivate(const QUrl &url = QUrl(), - QCoapMessage::MessageType type = QCoapMessage::MessageType::NonConfirmable, - const QUrl &proxyUrl = QUrl()); + QCoapMessage::Type type = QCoapMessage::Type::NonConfirmable, + const QUrl &proxyUrl = QUrl()); QCoapRequestPrivate(const QCoapRequestPrivate &other) = default; ~QCoapRequestPrivate(); diff --git a/tests/auto/qcoapclient/tst_qcoapclient.cpp b/tests/auto/qcoapclient/tst_qcoapclient.cpp index b06fa21..c7fce74 100644 --- a/tests/auto/qcoapclient/tst_qcoapclient.cpp +++ b/tests/auto/qcoapclient/tst_qcoapclient.cpp @@ -498,7 +498,7 @@ void tst_QCoapClient::timeout() QElapsedTimer timeoutTimer; timeoutTimer.start(); QScopedPointer reply( - client.get(QCoapRequest(url, QCoapMessage::MessageType::Confirmable))); + client.get(QCoapRequest(url, QCoapMessage::Type::Confirmable))); QSignalSpy spyClientError(&client, &QCoapClient::error); QSignalSpy spyReplyError(reply.data(), &QCoapReply::error); QSignalSpy spyReplyAborted(reply.data(), &QCoapReply::aborted); @@ -555,7 +555,7 @@ void tst_QCoapClient::abort() void tst_QCoapClient::blockwiseReply_data() { QTest::addColumn("url"); - QTest::addColumn("type"); + QTest::addColumn("type"); QTest::addColumn("replyData"); QByteArray data; @@ -582,34 +582,34 @@ void tst_QCoapClient::blockwiseReply_data() QTest::newRow("get_large") << QUrl(testServerUrl() + "/large") - << QCoapMessage::MessageType::NonConfirmable + << QCoapMessage::Type::NonConfirmable << data; QTest::newRow("get_large_separate") << QUrl(testServerUrl() + "/large-separate") - << QCoapMessage::MessageType::NonConfirmable + << QCoapMessage::Type::NonConfirmable << data; QTest::newRow("get_large_confirmable") << QUrl(testServerUrl() + "/large") - << QCoapMessage::MessageType::Confirmable + << QCoapMessage::Type::Confirmable << data; QTest::newRow("get_large_separate_confirmable") << QUrl(testServerUrl() + "/large-separate") - << QCoapMessage::MessageType::Confirmable + << QCoapMessage::Type::Confirmable << data; QTest::newRow("get_large_16bits") << QUrl(testServerUrl() + "/large") - << QCoapMessage::MessageType::NonConfirmable + << QCoapMessage::Type::NonConfirmable << data; QTest::newRow("get_large_16bits_confirmable") << QUrl(testServerUrl() + "/large") - << QCoapMessage::MessageType::Confirmable + << QCoapMessage::Type::Confirmable << data; } void tst_QCoapClient::blockwiseReply() { QFETCH(QUrl, url); - QFETCH(QCoapMessage::MessageType, type); + QFETCH(QCoapMessage::Type, type); QFETCH(QByteArray, replyData); QCoapClient client; @@ -633,7 +633,7 @@ void tst_QCoapClient::blockwiseReply() void tst_QCoapClient::blockwiseRequest_data() { QTest::addColumn("url"); - QTest::addColumn("type"); + QTest::addColumn("type"); QTest::addColumn("requestData"); QTest::addColumn("responseCode"); QTest::addColumn("replyData"); @@ -644,12 +644,12 @@ void tst_QCoapClient::blockwiseRequest_data() data.append(alphabet); QTest::newRow("large_post_empty_reply") << QUrl(testServerUrl() + "/query") - << QCoapMessage::MessageType::NonConfirmable + << QCoapMessage::Type::NonConfirmable << data << QtCoap::ResponseCode::MethodNotAllowed << QByteArray(); QTest::newRow("large_post_large_reply") << QUrl(testServerUrl() + "/large-post") - << QCoapMessage::MessageType::NonConfirmable + << QCoapMessage::Type::NonConfirmable << data << QtCoap::ResponseCode::Changed << data.toUpper(); @@ -658,7 +658,7 @@ void tst_QCoapClient::blockwiseRequest_data() void tst_QCoapClient::blockwiseRequest() { QFETCH(QUrl, url); - QFETCH(QCoapMessage::MessageType, type); + QFETCH(QCoapMessage::Type, type); QFETCH(QByteArray, requestData); QFETCH(QtCoap::ResponseCode, responseCode); QFETCH(QByteArray, replyData); @@ -716,49 +716,49 @@ void tst_QCoapClient::observe_data() { QWARN("Observe tests may take some time, don't forget to raise Tests timeout in settings."); QTest::addColumn("url"); - QTest::addColumn("type"); + QTest::addColumn("type"); QTest::newRow("observe") << QUrl(testServerUrl() + "/obs") - << QCoapMessage::MessageType::NonConfirmable; + << QCoapMessage::Type::NonConfirmable; QTest::newRow("observe_no_scheme_no_port") << QUrl(testServerHost() + "/obs") - << QCoapMessage::MessageType::NonConfirmable; + << QCoapMessage::Type::NonConfirmable; QTest::newRow("observe_confirmable") << QUrl(testServerUrl() + "/obs") - << QCoapMessage::MessageType::Confirmable; + << QCoapMessage::Type::Confirmable; QTest::newRow("observe_receive") << QUrl(testServerUrl() + "/obs-non") - << QCoapMessage::MessageType::NonConfirmable; + << QCoapMessage::Type::NonConfirmable; QTest::newRow("observe_receive_confirmable") << QUrl(testServerUrl() + "/obs-non") - << QCoapMessage::MessageType::Confirmable; + << QCoapMessage::Type::Confirmable; QTest::newRow("observe_large") << QUrl(testServerUrl() + "/obs-large") - << QCoapMessage::MessageType::NonConfirmable; + << QCoapMessage::Type::NonConfirmable; QTest::newRow("observe_large_confirmable") << QUrl(testServerUrl() + "/obs-large") - << QCoapMessage::MessageType::Confirmable; + << QCoapMessage::Type::Confirmable; QTest::newRow("observe_pumping") << QUrl(testServerUrl() + "/obs-pumping") - << QCoapMessage::MessageType::NonConfirmable; + << QCoapMessage::Type::NonConfirmable; QTest::newRow("observe_pumping_confirmable") << QUrl(testServerUrl() + "/obs-pumping") - << QCoapMessage::MessageType::Confirmable; + << QCoapMessage::Type::Confirmable; } void tst_QCoapClient::observe() { QFETCH(QUrl, url); - QFETCH(QCoapMessage::MessageType, type); + QFETCH(QCoapMessage::Type, type); QCoapClient client; QCoapRequest request(url); @@ -788,8 +788,7 @@ void tst_QCoapClient::observe() void tst_QCoapClient::confirmableMulticast() { QCoapClient client; - const auto reply = client.get(QCoapRequest("224.0.1.187", - QCoapMessage::MessageType::Confirmable)); + const auto reply = client.get(QCoapRequest("224.0.1.187", QCoapMessage::Type::Confirmable)); QVERIFY2(!reply, "Confirmable multicast request didn't fail as expected."); } diff --git a/tests/auto/qcoapinternalreply/tst_qcoapinternalreply.cpp b/tests/auto/qcoapinternalreply/tst_qcoapinternalreply.cpp index da2531b..61a3dbc 100644 --- a/tests/auto/qcoapinternalreply/tst_qcoapinternalreply.cpp +++ b/tests/auto/qcoapinternalreply/tst_qcoapinternalreply.cpp @@ -52,7 +52,7 @@ private Q_SLOTS: void tst_QCoapInternalReply::parseReplyPdu_data() { QTest::addColumn("responseCode"); - QTest::addColumn("type"); + QTest::addColumn("type"); QTest::addColumn("messageId"); QTest::addColumn("token"); QTest::addColumn("tokenLength"); @@ -73,7 +73,7 @@ void tst_QCoapInternalReply::parseReplyPdu_data() QTest::newRow("reply_with_options_and_payload") << QtCoap::ResponseCode::Content - << QCoapMessage::MessageType::NonConfirmable + << QCoapMessage::Type::NonConfirmable << quint16(64463) << QByteArray("4647f09b") << quint8(4) @@ -87,7 +87,7 @@ void tst_QCoapInternalReply::parseReplyPdu_data() QTest::newRow("reply_with_payload") << QtCoap::ResponseCode::Content - << QCoapMessage::MessageType::NonConfirmable + << QCoapMessage::Type::NonConfirmable << quint16(64463) << QByteArray("4647f09b") << quint8(4) @@ -100,7 +100,7 @@ void tst_QCoapInternalReply::parseReplyPdu_data() QTest::newRow("reply_with_options") << QtCoap::ResponseCode::Content - << QCoapMessage::MessageType::NonConfirmable + << QCoapMessage::Type::NonConfirmable << quint16(64463) << QByteArray("4647f09b") << quint8(4) @@ -112,7 +112,7 @@ void tst_QCoapInternalReply::parseReplyPdu_data() QTest::newRow("reply_only") << QtCoap::ResponseCode::Content - << QCoapMessage::MessageType::NonConfirmable + << QCoapMessage::Type::NonConfirmable << quint16(64463) << QByteArray("4647f09b") << quint8(4) @@ -124,7 +124,7 @@ void tst_QCoapInternalReply::parseReplyPdu_data() QTest::newRow("reply_with_big_option") << QtCoap::ResponseCode::Content - << QCoapMessage::MessageType::NonConfirmable + << QCoapMessage::Type::NonConfirmable << quint16(64463) << QByteArray("4647f09b") << quint8(4) @@ -139,7 +139,7 @@ void tst_QCoapInternalReply::parseReplyPdu_data() void tst_QCoapInternalReply::parseReplyPdu() { QFETCH(QtCoap::ResponseCode, responseCode); - QFETCH(QCoapMessage::MessageType, type); + QFETCH(QCoapMessage::Type, type); QFETCH(quint16, messageId); QFETCH(QByteArray, token); QFETCH(quint8, tokenLength); @@ -159,7 +159,7 @@ void tst_QCoapInternalReply::parseReplyPdu() QCOMPARE(reply->message()->token().toHex(), token); QCOMPARE(reply->message()->optionCount(), optionsNames.count()); for (int i = 0; i < reply->message()->optionCount(); ++i) { - QCoapOption option = reply->message()->option(i); + QCoapOption option = reply->message()->optionAt(i); QCOMPARE(option.name(), optionsNames.at(i)); QCOMPARE(option.length(), optionsLengths.at(i)); QCOMPARE(option.value(), optionsValues.at(i)); diff --git a/tests/auto/qcoapinternalrequest/tst_qcoapinternalrequest.cpp b/tests/auto/qcoapinternalrequest/tst_qcoapinternalrequest.cpp index 6a61eb1..1e5c732 100644 --- a/tests/auto/qcoapinternalrequest/tst_qcoapinternalrequest.cpp +++ b/tests/auto/qcoapinternalrequest/tst_qcoapinternalrequest.cpp @@ -63,7 +63,7 @@ void tst_QCoapInternalRequest::requestToFrame_data() { QTest::addColumn("url"); QTest::addColumn("method"); - QTest::addColumn("type"); + QTest::addColumn("type"); QTest::addColumn("messageId"); QTest::addColumn("token"); QTest::addColumn("pduHeader"); @@ -72,7 +72,7 @@ void tst_QCoapInternalRequest::requestToFrame_data() QTest::newRow("request_with_option_and_payload") << QUrl("coap://10.20.30.40:5683/test") << QtCoap::Method::Get - << QCoapRequest::MessageType::NonConfirmable + << QCoapRequest::Type::NonConfirmable << quint16(56400) << QByteArray::fromHex("4647f09b") << "5401dc504647f09bb474657374ff" @@ -81,7 +81,7 @@ void tst_QCoapInternalRequest::requestToFrame_data() QTest::newRow("request_domain") << QUrl("coap://domain.com:5683/test") << QtCoap::Method::Get - << QCoapRequest::MessageType::NonConfirmable + << QCoapRequest::Type::NonConfirmable << quint16(56400) << QByteArray::fromHex("4647f09b") << "5401dc504647f09b3a646f6d61696e2e636f6d8474657374ff" @@ -90,7 +90,7 @@ void tst_QCoapInternalRequest::requestToFrame_data() QTest::newRow("request_ipv6") << QUrl("coap://[::ffff:ac11:3]:5683/test") << QtCoap::Method::Get - << QCoapRequest::MessageType::NonConfirmable + << QCoapRequest::Type::NonConfirmable << quint16(56400) << QByteArray::fromHex("4647f09b") << "5401dc504647f09bb474657374ff" @@ -99,7 +99,7 @@ void tst_QCoapInternalRequest::requestToFrame_data() QTest::newRow("request_without_payload") << QUrl("coap://10.20.30.40:5683/test") << QtCoap::Method::Get - << QCoapRequest::MessageType::NonConfirmable + << QCoapRequest::Type::NonConfirmable << quint16(56400) << QByteArray::fromHex("4647f09b") << "5401dc504647f09bb474657374" @@ -108,7 +108,7 @@ void tst_QCoapInternalRequest::requestToFrame_data() QTest::newRow("request_without_option") << QUrl("coap://10.20.30.40:5683/") << QtCoap::Method::Put - << QCoapRequest::MessageType::Confirmable + << QCoapRequest::Type::Confirmable << quint16(56400) << QByteArray::fromHex("4647f09b") << "4403dc504647f09bff" @@ -117,7 +117,7 @@ void tst_QCoapInternalRequest::requestToFrame_data() QTest::newRow("request_only") << QUrl("coap://10.20.30.40:5683/") << QtCoap::Method::Get - << QCoapRequest::MessageType::NonConfirmable + << QCoapRequest::Type::NonConfirmable << quint16(56400) << QByteArray::fromHex("4647f09b") << "5401dc504647f09b" @@ -126,7 +126,7 @@ void tst_QCoapInternalRequest::requestToFrame_data() QTest::newRow("request_with_multiple_options") << QUrl("coap://10.20.30.40:5683/test/oui") << QtCoap::Method::Get - << QCoapRequest::MessageType::NonConfirmable + << QCoapRequest::Type::NonConfirmable << quint16(56400) << QByteArray::fromHex("4647f09b") << "5401dc504647f09bb474657374036f7569" @@ -135,7 +135,7 @@ void tst_QCoapInternalRequest::requestToFrame_data() QTest::newRow("request_with_big_option_number") << QUrl("coap://10.20.30.40:5683/test") << QtCoap::Method::Get - << QCoapRequest::MessageType::NonConfirmable + << QCoapRequest::Type::NonConfirmable << quint16(56400) << QByteArray::fromHex("4647f09b") << "5401dc504647f09bb474657374dd240d6162636465666768696a6b6c6d6e6f70" @@ -147,7 +147,7 @@ void tst_QCoapInternalRequest::requestToFrame() { QFETCH(QUrl, url); QFETCH(QtCoap::Method, method); - QFETCH(QCoapMessage::MessageType, type); + QFETCH(QCoapMessage::Type, type); QFETCH(quint16, messageId); QFETCH(QByteArray, token); QFETCH(QString, pduHeader); @@ -218,7 +218,7 @@ void tst_QCoapInternalRequest::parseUri() QFETCH(QUrl, proxyUri); QFETCH(QVector, options); - QCoapRequest request(uri, QCoapMessage::MessageType::NonConfirmable, proxyUri); + QCoapRequest request(uri, QCoapMessage::Type::NonConfirmable, proxyUri); QCoapInternalRequest internalRequest(request); for (QCoapOption opt : options) diff --git a/tests/auto/qcoapmessage/tst_qcoapmessage.cpp b/tests/auto/qcoapmessage/tst_qcoapmessage.cpp index 8801ed0..e13281e 100644 --- a/tests/auto/qcoapmessage/tst_qcoapmessage.cpp +++ b/tests/auto/qcoapmessage/tst_qcoapmessage.cpp @@ -39,8 +39,8 @@ class tst_QCoapMessage : public QObject private Q_SLOTS: void copyAndDetach(); - void setMessageType_data(); - void setMessageType(); + void setType_data(); + void setType(); void addOption(); void addOption_string_data(); void addOption_string(); @@ -59,7 +59,7 @@ void tst_QCoapMessage::copyAndDetach() a.setMessageId(3); a.setPayload("payload"); a.setToken("token"); - a.setType(QCoapMessage::MessageType::Acknowledgment); + a.setType(QCoapMessage::Type::Acknowledgment); a.setVersion(5); // Test the copy @@ -67,7 +67,7 @@ void tst_QCoapMessage::copyAndDetach() QVERIFY2(b.messageId() == 3, "Message not copied correctly"); QVERIFY2(b.payload() == "payload", "Message not copied correctly"); QVERIFY2(b.token() == "token", "Message not copied correctly"); - QVERIFY2(b.type() == QCoapMessage::MessageType::Acknowledgment, "Message not copied correctly"); + QVERIFY2(b.type() == QCoapMessage::Type::Acknowledgment, "Message not copied correctly"); QVERIFY2(b.version() == 5, "Message not copied correctly"); // Detach @@ -76,19 +76,19 @@ void tst_QCoapMessage::copyAndDetach() QCOMPARE(a.messageId(), 3); } -void tst_QCoapMessage::setMessageType_data() +void tst_QCoapMessage::setType_data() { - QTest::addColumn("type"); + QTest::addColumn("type"); - QTest::newRow("acknowledgment") << QCoapMessage::MessageType::Acknowledgment; - QTest::newRow("confirmable") << QCoapMessage::MessageType::Confirmable; - QTest::newRow("non-confirmable") << QCoapMessage::MessageType::NonConfirmable; - QTest::newRow("reset") << QCoapMessage::MessageType::Reset; + QTest::newRow("acknowledgment") << QCoapMessage::Type::Acknowledgment; + QTest::newRow("confirmable") << QCoapMessage::Type::Confirmable; + QTest::newRow("non-confirmable") << QCoapMessage::Type::NonConfirmable; + QTest::newRow("reset") << QCoapMessage::Type::Reset; } -void tst_QCoapMessage::setMessageType() +void tst_QCoapMessage::setType() { - QFETCH(QCoapMessage::MessageType, type); + QFETCH(QCoapMessage::Type, type); QCoapMessage message; message.setType(type); QCOMPARE(message.type(), type); @@ -256,7 +256,7 @@ void tst_QCoapMessage::removeAll() message.addOption(QCoapOption::LocationPath, "path"); message.addOption(QCoapOption::ProxyUri, "proxy1"); message.addOption(QCoapOption::ProxyUri, "proxy2"); - message.removeAllOptions(); + message.clearOptions(); QVERIFY(message.options().isEmpty()); } diff --git a/tests/auto/qcoaprequest/tst_qcoaprequest.cpp b/tests/auto/qcoaprequest/tst_qcoaprequest.cpp index 9f66b98..592d4eb 100644 --- a/tests/auto/qcoaprequest/tst_qcoaprequest.cpp +++ b/tests/auto/qcoaprequest/tst_qcoaprequest.cpp @@ -160,7 +160,7 @@ void tst_QCoapRequest::copyAndDetach() a.setMessageId(3); a.setPayload("payload"); a.setToken("token"); - a.setType(QCoapMessage::MessageType::Acknowledgment); + a.setType(QCoapMessage::Type::Acknowledgment); a.setVersion(5); a.setMethod(QtCoap::Method::Delete); QUrl testUrl("coap://url:500/resource"); @@ -173,7 +173,7 @@ void tst_QCoapRequest::copyAndDetach() QVERIFY2(b.messageId() == 3, "Message not copied correctly"); QVERIFY2(b.payload() == "payload", "Message not copied correctly"); QVERIFY2(b.token() == "token", "Message not copied correctly"); - QVERIFY2(b.type() == QCoapMessage::MessageType::Acknowledgment, "Message not copied correctly"); + QVERIFY2(b.type() == QCoapMessage::Type::Acknowledgment, "Message not copied correctly"); QVERIFY2(b.version() == 5, "Message not copied correctly"); // Test the QCoapRequest copy -- cgit v1.2.3 From e04667b55c2055200da93382dee44299293735a6 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Mon, 13 May 2019 15:49:02 +0200 Subject: Improve the API of QCoapRequest - Moved internally used methods to the private class. - Replaced the internally used protected constructor with a static method in the private class. - Removed the internally used setMethod(), no need for it anymore. - Removed the isValid() method, no point in keeping it. This change is based on the feedback from API review. Change-Id: I177efdb1d436266549dea3e8d2b01160648fce90 Reviewed-by: Simon Hausmann --- src/coap/qcoapclient.cpp | 40 +++++++------- src/coap/qcoapinternalmessage.cpp | 3 +- src/coap/qcoapprotocol.cpp | 4 +- src/coap/qcoaprequest.cpp | 62 +++++++++------------- src/coap/qcoaprequest.h | 12 +---- src/coap/qcoaprequest_p.h | 6 +++ tests/auto/qcoapclient/tst_qcoapclient.cpp | 13 +++-- .../tst_qcoapinternalrequest.cpp | 10 +--- .../tst_qcoapqudpconnection.cpp | 11 ++-- tests/auto/qcoaprequest/tst_qcoaprequest.cpp | 22 ++++---- 10 files changed, 82 insertions(+), 101 deletions(-) diff --git a/src/coap/qcoapclient.cpp b/src/coap/qcoapclient.cpp index 6e1e52b..af09686 100644 --- a/src/coap/qcoapclient.cpp +++ b/src/coap/qcoapclient.cpp @@ -35,6 +35,7 @@ #include "qcoapnamespace.h" #include "qcoapsecurityconfiguration.h" #include "qcoapqudpconnection_p.h" +#include "qcoaprequest_p.h" #include #include #include @@ -227,9 +228,8 @@ QCoapReply *QCoapClient::get(const QCoapRequest &request) { Q_D(QCoapClient); - QCoapRequest copyRequest(request, QtCoap::Method::Get); - copyRequest.adjustUrl(d->connection->isSecure()); - + QCoapRequest copyRequest = QCoapRequestPrivate::createRequest(request, QtCoap::Method::Get, + d->connection->isSecure()); return d->sendRequest(copyRequest); } @@ -256,10 +256,9 @@ QCoapReply *QCoapClient::put(const QCoapRequest &request, const QByteArray &data { Q_D(QCoapClient); - QCoapRequest copyRequest(request, QtCoap::Method::Put); + QCoapRequest copyRequest = QCoapRequestPrivate::createRequest(request, QtCoap::Method::Put, + d->connection->isSecure()); copyRequest.setPayload(data); - copyRequest.adjustUrl(d->connection->isSecure()); - return d->sendRequest(copyRequest); } @@ -302,10 +301,9 @@ QCoapReply *QCoapClient::post(const QCoapRequest &request, const QByteArray &dat { Q_D(QCoapClient); - QCoapRequest copyRequest(request, QtCoap::Method::Post); + QCoapRequest copyRequest = QCoapRequestPrivate::createRequest(request, QtCoap::Method::Post, + d->connection->isSecure()); copyRequest.setPayload(data); - copyRequest.adjustUrl(d->connection->isSecure()); - return d->sendRequest(copyRequest); } @@ -351,9 +349,8 @@ QCoapReply *QCoapClient::deleteResource(const QCoapRequest &request) { Q_D(QCoapClient); - QCoapRequest copyRequest(request, QtCoap::Method::Delete); - copyRequest.adjustUrl(d->connection->isSecure()); - + QCoapRequest copyRequest = QCoapRequestPrivate::createRequest(request, QtCoap::Method::Delete, + d->connection->isSecure()); return d->sendRequest(copyRequest); } @@ -407,9 +404,9 @@ QCoapResourceDiscoveryReply *QCoapClient::discover(QtCoap::MulticastGroup group, discoveryUrl.setPath(discoveryPath); discoveryUrl.setPort(port); - QCoapRequest request(discoveryUrl); - request.setMethod(QtCoap::Method::Get); - request.adjustUrl(d->connection->isSecure()); + QCoapRequest request = QCoapRequestPrivate::createRequest(QCoapRequest(discoveryUrl), + QtCoap::Method::Get, + d->connection->isSecure()); return d->sendDiscovery(request); } @@ -433,10 +430,9 @@ QCoapResourceDiscoveryReply *QCoapClient::discover(const QUrl &url, const QStrin QUrl discoveryUrl(url); discoveryUrl.setPath(url.path() + discoveryPath); - QCoapRequest request(discoveryUrl); - request.setMethod(QtCoap::Method::Get); - request.adjustUrl(d->connection->isSecure()); - + QCoapRequest request = QCoapRequestPrivate::createRequest(QCoapRequest(discoveryUrl), + QtCoap::Method::Get, + d->connection->isSecure()); return d->sendDiscovery(request); } @@ -449,7 +445,7 @@ QCoapResourceDiscoveryReply *QCoapClient::discover(const QUrl &url, const QStrin */ QCoapReply *QCoapClient::observe(const QCoapRequest &request) { - QCoapRequest copyRequest(request, QtCoap::Method::Get); + QCoapRequest copyRequest = QCoapRequestPrivate::createRequest(request, QtCoap::Method::Get); copyRequest.enableObserve(); return get(copyRequest); @@ -494,7 +490,7 @@ void QCoapClient::cancelObserve(QCoapReply *notifiedReply) void QCoapClient::cancelObserve(const QUrl &url) { Q_D(QCoapClient); - const auto adjustedUrl = QCoapRequest::adjustedUrl(url, d->connection->isSecure()); + const auto adjustedUrl = QCoapRequestPrivate::adjustedUrl(url, d->connection->isSecure()); QMetaObject::invokeMethod(d->protocol, "cancelObserve", Q_ARG(QUrl, adjustedUrl)); } @@ -562,7 +558,7 @@ bool QCoapClientPrivate::send(QCoapReply *reply) return false; } - if (!QCoapRequest::isUrlValid(reply->request().url())) { + if (!QCoapRequestPrivate::isUrlValid(reply->request().url())) { qCWarning(lcCoapClient, "Failed to send request for an invalid URL."); return false; } diff --git a/src/coap/qcoapinternalmessage.cpp b/src/coap/qcoapinternalmessage.cpp index 85c2263..4da627e 100644 --- a/src/coap/qcoapinternalmessage.cpp +++ b/src/coap/qcoapinternalmessage.cpp @@ -29,6 +29,7 @@ ****************************************************************************/ #include "qcoapinternalmessage_p.h" +#include "qcoaprequest_p.h" #include #include @@ -261,7 +262,7 @@ bool QCoapInternalMessage::isValid() const */ bool QCoapInternalMessage::isUrlValid(const QUrl &url) { - return QCoapRequest::isUrlValid(url); + return QCoapRequestPrivate::isUrlValid(url); } QT_END_NAMESPACE diff --git a/src/coap/qcoapprotocol.cpp b/src/coap/qcoapprotocol.cpp index 2f4c278..28ee7e8 100644 --- a/src/coap/qcoapprotocol.cpp +++ b/src/coap/qcoapprotocol.cpp @@ -31,6 +31,7 @@ #include "qcoapprotocol_p.h" #include "qcoapinternalrequest_p.h" #include "qcoapinternalreply_p.h" +#include "qcoaprequest_p.h" #include "qcoapconnection_p.h" #include "qcoapnamespace_p.h" @@ -134,7 +135,8 @@ void QCoapProtocol::sendRequest(QPointer reply, QCoapConnection *con Q_D(QCoapProtocol); Q_ASSERT(QThread::currentThread() == thread()); - if (reply.isNull() || !reply->request().isValid()) + if (reply.isNull() || reply->request().method() == QtCoap::Method::Invalid + || !QCoapRequestPrivate::isUrlValid(reply->request().url())) return; connect(reply, &QCoapReply::aborted, this, [this](const QCoapToken &token) { diff --git a/src/coap/qcoaprequest.cpp b/src/coap/qcoaprequest.cpp index 59cdb59..4a61643 100644 --- a/src/coap/qcoaprequest.cpp +++ b/src/coap/qcoaprequest.cpp @@ -137,19 +137,6 @@ QCoapRequest::QCoapRequest(const QCoapRequest &other) : { } -/*! - \internal - - Constructs a copy of the \a other QCoapRequest and sets the request - method to \a method. -*/ -QCoapRequest::QCoapRequest(const QCoapRequest &other, QtCoap::Method method) : - QCoapRequest(other) -{ - if (method != QtCoap::Method::Invalid) - setMethod(method); -} - /*! Destroys the QCoapRequest. */ @@ -224,19 +211,6 @@ void QCoapRequest::setProxyUrl(const QUrl &proxyUrl) d->proxyUri = proxyUrl; } -/*! - \internal - - Sets the method of the request to the given \a method. - - \sa method() -*/ -void QCoapRequest::setMethod(QtCoap::Method method) -{ - Q_D(QCoapRequest); - d->method = method; -} - /*! Sets the observe to \c true to make an observe request. @@ -251,6 +225,8 @@ void QCoapRequest::enableObserve() } /*! + \internal + Adjusts the request URL by setting the correct default scheme and port (if not indicated) based on the \a secure parameter. @@ -258,10 +234,9 @@ void QCoapRequest::enableObserve() its port will default to \e 5683. In secure mode the scheme will default to \c coaps, and the port will default to \e 5684. */ -void QCoapRequest::adjustUrl(bool secure) +void QCoapRequestPrivate::adjustUrl(bool secure) { - Q_D(QCoapRequest); - d->uri = adjustedUrl(d->uri, secure); + uri = adjustedUrl(uri, secure); } /*! @@ -274,17 +249,11 @@ QCoapRequest &QCoapRequest::operator=(const QCoapRequest &other) } /*! - Returns \c true if the request is valid, \c false otherwise. -*/ -bool QCoapRequest::isValid() const -{ - return isUrlValid(url()) && method() != QtCoap::Method::Invalid; -} + \internal -/*! Returns \c true if the \a url is a valid CoAP URL. */ -bool QCoapRequest::isUrlValid(const QUrl &url) +bool QCoapRequestPrivate::isUrlValid(const QUrl &url) { return (url.isValid() && !url.isLocalFile() && !url.isRelative() && (url.scheme() == CoapScheme || url.scheme() == CoapSecureScheme) @@ -292,6 +261,8 @@ bool QCoapRequest::isUrlValid(const QUrl &url) } /*! + \internal + Adjusts the \a url by setting the correct default scheme and port (if not indicated) based on the \a secure parameter. Returns the adjusted URL. @@ -300,7 +271,7 @@ bool QCoapRequest::isUrlValid(const QUrl &url) its port will default to \e 5683. In secure mode the scheme will default to \c coaps, and the port will default to \e 5684. */ -QUrl QCoapRequest::adjustedUrl(const QUrl &url, bool secure) +QUrl QCoapRequestPrivate::adjustedUrl(const QUrl &url, bool secure) { if (url.isEmpty() || !url.isValid()) return QUrl(); @@ -338,4 +309,19 @@ QCoapRequestPrivate* QCoapRequest::d_func() return static_cast(d_ptr.data()); } +/*! + \internal + + Creates a copy of \a other request and sets \a method as its request method. + Adjusts the request URL based on \a isSecure parameter. +*/ +QCoapRequest +QCoapRequestPrivate::createRequest(const QCoapRequest &other, QtCoap::Method method, bool isSecure) +{ + QCoapRequest request(other); + request.d_func()->method = method; + request.d_func()->adjustUrl(isSecure); + return request; +} + QT_END_NAMESPACE diff --git a/src/coap/qcoaprequest.h b/src/coap/qcoaprequest.h index e506c83..848d357 100644 --- a/src/coap/qcoaprequest.h +++ b/src/coap/qcoaprequest.h @@ -61,16 +61,6 @@ public: void setUrl(const QUrl &url); void setProxyUrl(const QUrl &proxyUrl); void enableObserve(); - void adjustUrl(bool secure); - - bool isValid() const; - static bool isUrlValid(const QUrl &url); - static QUrl adjustedUrl(const QUrl &url, bool secure); - -protected: - QCoapRequest(const QCoapRequest &other, QtCoap::Method method); - - void setMethod(QtCoap::Method method); private: // Q_DECLARE_PRIVATE equivalent for shared data pointers @@ -78,7 +68,7 @@ private: const QCoapRequestPrivate* d_func() const { return reinterpret_cast(d_ptr.constData()); } - friend class QCoapClient; + friend class QCoapRequestPrivate; }; QT_END_NAMESPACE diff --git a/src/coap/qcoaprequest_p.h b/src/coap/qcoaprequest_p.h index eae236d..20af324 100644 --- a/src/coap/qcoaprequest_p.h +++ b/src/coap/qcoaprequest_p.h @@ -58,6 +58,12 @@ public: ~QCoapRequestPrivate(); void setUrl(const QUrl &url); + void adjustUrl(bool secure); + + static QCoapRequest createRequest(const QCoapRequest &other, QtCoap::Method method, + bool isSecure = false); + static QUrl adjustedUrl(const QUrl &url, bool secure); + static bool isUrlValid(const QUrl &url); QUrl uri; QUrl proxyUri; diff --git a/tests/auto/qcoapclient/tst_qcoapclient.cpp b/tests/auto/qcoapclient/tst_qcoapclient.cpp index c7fce74..2b914ab 100644 --- a/tests/auto/qcoapclient/tst_qcoapclient.cpp +++ b/tests/auto/qcoapclient/tst_qcoapclient.cpp @@ -40,6 +40,7 @@ #include #include #include +#include #include "../coapnetworksettings.h" @@ -273,7 +274,9 @@ void tst_QCoapClient::methods() } QVERIFY2(!reply.isNull(), "Request failed unexpectedly"); - QCOMPARE(reply->url(), QCoapRequest::adjustedUrl(url, false)); +#ifdef QT_BUILD_INTERNAL + QCOMPARE(reply->url(), QCoapRequestPrivate::adjustedUrl(url, false)); +#endif QSignalSpy spyReplyFinished(reply.data(), SIGNAL(finished(QCoapReply *))); QTRY_COMPARE(spyReplyFinished.count(), 1); QTRY_COMPARE(spyClientFinished.count(), 1); @@ -705,7 +708,9 @@ void tst_QCoapClient::discover() QTRY_COMPARE_WITH_TIMEOUT(spyReplyFinished.count(), 1, 30000); const auto discoverUrl = QUrl(url.toString() + "/.well-known/core"); - QCOMPARE(resourcesReply->url(), QCoapRequest::adjustedUrl(discoverUrl, false)); +#ifdef QT_BUILD_INTERNAL + QCOMPARE(resourcesReply->url(), QCoapRequestPrivate::adjustedUrl(discoverUrl, false)); +#endif QCOMPARE(resourcesReply->resources().length(), resourceNumber); QCOMPARE(resourcesReply->request().method(), QtCoap::Method::Get); @@ -771,7 +776,9 @@ void tst_QCoapClient::observe() QTRY_COMPARE_WITH_TIMEOUT(spyReplyNotified.count(), 3, 30000); client.cancelObserve(reply.data()); - QCOMPARE(reply->url(), QCoapRequest::adjustedUrl(url, false)); +#ifdef QT_BUILD_INTERNAL + QCOMPARE(reply->url(), QCoapRequestPrivate::adjustedUrl(url, false)); +#endif QCOMPARE(reply->request().method(), QtCoap::Method::Get); QVERIFY2(!spyReplyNotified.wait(7000), "'Notify' signal received after cancelling observe"); diff --git a/tests/auto/qcoapinternalrequest/tst_qcoapinternalrequest.cpp b/tests/auto/qcoapinternalrequest/tst_qcoapinternalrequest.cpp index 1e5c732..1a1a293 100644 --- a/tests/auto/qcoapinternalrequest/tst_qcoapinternalrequest.cpp +++ b/tests/auto/qcoapinternalrequest/tst_qcoapinternalrequest.cpp @@ -33,6 +33,7 @@ #include #include +#include #ifdef QT_BUILD_INTERNAL @@ -53,12 +54,6 @@ private Q_SLOTS: void isMulticast(); }; -struct QCoapRequestForTest : public QCoapRequest -{ - QCoapRequestForTest(const QUrl& url) : QCoapRequest(url) {} - using QCoapRequest::setMethod; -}; - void tst_QCoapInternalRequest::requestToFrame_data() { QTest::addColumn("url"); @@ -153,9 +148,8 @@ void tst_QCoapInternalRequest::requestToFrame() QFETCH(QString, pduHeader); QFETCH(QString, pduPayload); - QCoapRequestForTest request(url); + QCoapRequest request = QCoapRequestPrivate::createRequest(QCoapRequest(url), method); request.setType(type); - request.setMethod(method); request.setPayload(pduPayload.toUtf8()); request.setMessageId(messageId); request.setToken(token); diff --git a/tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp b/tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp index 947cb5d..ee8c295 100644 --- a/tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp +++ b/tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp @@ -40,18 +40,13 @@ #include #include #include +#include #include "../coapnetworksettings.h" #ifdef QT_BUILD_INTERNAL using namespace QtCoapNetworkSettings; -struct QCoapRequestForTest : public QCoapRequest -{ - QCoapRequestForTest(const QUrl &url) : QCoapRequest(url) {} - using QCoapRequest::setMethod; -}; - class tst_QCoapQUdpConnection : public QObject { Q_OBJECT @@ -186,10 +181,10 @@ void tst_QCoapQUdpConnection::sendRequest() QSignalSpy spySocketReadyRead(connection.socket(), &QUdpSocket::readyRead); QSignalSpy spyConnectionReadyRead(&connection, &QCoapQUdpConnection::readyRead); - QCoapRequestForTest request(protocol + host + path); + QCoapRequest request = + QCoapRequestPrivate::createRequest(QCoapRequest(protocol + host + path), method); request.setMessageId(24806); request.setToken(QByteArray("abcd")); - request.setMethod(method); QVERIFY(connection.socket() != nullptr); QCoapInternalRequest internalRequest(request); connection.sendRequest(internalRequest.toQByteArray(), host, port); diff --git a/tests/auto/qcoaprequest/tst_qcoaprequest.cpp b/tests/auto/qcoaprequest/tst_qcoaprequest.cpp index 592d4eb..2b042b5 100644 --- a/tests/auto/qcoaprequest/tst_qcoaprequest.cpp +++ b/tests/auto/qcoaprequest/tst_qcoaprequest.cpp @@ -34,6 +34,7 @@ #include #include #include +#include class tst_QCoapRequest : public QObject { @@ -50,11 +51,6 @@ private Q_SLOTS: void copyAndDetach(); }; -struct QCoapRequestForTest : public QCoapRequest -{ - using QCoapRequest::setMethod; -}; - void tst_QCoapRequest::ctor_data() { QTest::addColumn("url"); @@ -103,13 +99,18 @@ void tst_QCoapRequest::adjustUrl_data() void tst_QCoapRequest::adjustUrl() { +#ifdef QT_BUILD_INTERNAL QFETCH(QUrl, inputUrl); QFETCH(QUrl, expectedUrl); QFETCH(bool, secure); - QCoapRequest request(inputUrl); - request.adjustUrl(secure); + // createRequest() will adjust the url + QCoapRequest request = QCoapRequestPrivate::createRequest(QCoapRequest(inputUrl), + QtCoap::Method::Get, secure); QCOMPARE(request.url(), expectedUrl); +#else + QSKIP("Not an internal build, skipping this test"); +#endif } void tst_QCoapRequest::setUrl_data() @@ -156,13 +157,13 @@ void tst_QCoapRequest::enableObserve() void tst_QCoapRequest::copyAndDetach() { - QCoapRequestForTest a; +#ifdef QT_BUILD_INTERNAL + QCoapRequest a = QCoapRequestPrivate::createRequest(QCoapRequest(), QtCoap::Method::Delete); a.setMessageId(3); a.setPayload("payload"); a.setToken("token"); a.setType(QCoapMessage::Type::Acknowledgment); a.setVersion(5); - a.setMethod(QtCoap::Method::Delete); QUrl testUrl("coap://url:500/resource"); a.setUrl(testUrl); QUrl testProxyUrl("test://proxyurl"); @@ -186,6 +187,9 @@ void tst_QCoapRequest::copyAndDetach() c.setMessageId(9); QCOMPARE(c.messageId(), 9); QCOMPARE(a.messageId(), 3); +#else + QSKIP("Not an internal build, skipping this test"); +#endif } QTEST_APPLESS_MAIN(tst_QCoapRequest) -- cgit v1.2.3 From c90feba464526c9d00ccc63c0f372963fffc8d21 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Tue, 14 May 2019 11:20:12 +0200 Subject: Forbid creation of QCoapReply and QCoapResourceDiscoveryReply objects Replies are created internally, and pointers to created objects are returned to the user. Users should not be able to create instances of QCoapReply or QCoapResourceDiscoveryReply. This change is based on the feedback from API review. Change-Id: Ibd59c62fd19c4b312253d4c4621b195bf5816340 Reviewed-by: Simon Hausmann --- src/coap/qcoapclient.cpp | 3 +- src/coap/qcoapreply.cpp | 19 +++---- src/coap/qcoapreply.h | 11 ++-- src/coap/qcoapreply_p.h | 2 + src/coap/qcoapresourcediscoveryreply.cpp | 2 + src/coap/qcoapresourcediscoveryreply.h | 5 +- .../qcoapinternalreply/tst_qcoapinternalreply.cpp | 56 +++---------------- tests/auto/qcoapreply/tst_qcoapreply.cpp | 63 ++++++++++------------ 8 files changed, 58 insertions(+), 103 deletions(-) diff --git a/src/coap/qcoapclient.cpp b/src/coap/qcoapclient.cpp index af09686..372128a 100644 --- a/src/coap/qcoapclient.cpp +++ b/src/coap/qcoapclient.cpp @@ -36,6 +36,7 @@ #include "qcoapsecurityconfiguration.h" #include "qcoapqudpconnection_p.h" #include "qcoaprequest_p.h" +#include "qcoapreply_p.h" #include #include #include @@ -514,7 +515,7 @@ QCoapReply *QCoapClientPrivate::sendRequest(const QCoapRequest &request) Q_Q(QCoapClient); // Prepare the reply - QCoapReply *reply = new QCoapReply(request, q); + QCoapReply *reply = QCoapReplyPrivate::createCoapReply(request, q); if (!send(reply)) { delete reply; diff --git a/src/coap/qcoapreply.cpp b/src/coap/qcoapreply.cpp index 7d6ba40..2880fd6 100644 --- a/src/coap/qcoapreply.cpp +++ b/src/coap/qcoapreply.cpp @@ -242,15 +242,6 @@ void QCoapReplyPrivate::_q_setError(QtCoap::ResponseCode code) \sa finished(), error() */ -/*! - Constructs a new CoAP reply for the \a request and sets \a parent as - its parent. -*/ -QCoapReply::QCoapReply(const QCoapRequest &request, QObject *parent) : - QCoapReply(*new QCoapReplyPrivate(request), parent) -{ -} - /*! \internal Constructs a new CoAP reply with \a dd as the d_ptr. @@ -427,6 +418,16 @@ void QCoapReply::abortRequest() emit finished(this); } +/*! + \internal + + Creates a new instance of QCoapReply and returns a pointer to it. +*/ +QCoapReply *QCoapReplyPrivate::createCoapReply(const QCoapRequest &request, QObject *parent) +{ + return new QCoapReply(*new QCoapReplyPrivate(request), parent); +} + QT_END_NAMESPACE #include "moc_qcoapreply.cpp" diff --git a/src/coap/qcoapreply.h b/src/coap/qcoapreply.h index afdd5ea..019fba5 100644 --- a/src/coap/qcoapreply.h +++ b/src/coap/qcoapreply.h @@ -45,8 +45,6 @@ class Q_COAP_EXPORT QCoapReply : public QIODevice { Q_OBJECT public: - - explicit QCoapReply(const QCoapRequest &request, QObject *parent = nullptr); ~QCoapReply() override; QtCoap::ResponseCode responseCode() const; @@ -68,11 +66,6 @@ Q_SIGNALS: void aborted(const QCoapToken &token); protected: - friend class QCoapProtocol; - friend class QCoapProtocolPrivate; - - explicit QCoapReply(QCoapReplyPrivate &dd, QObject *parent = nullptr); - qint64 readData(char *data, qint64 maxSize) override; qint64 writeData(const char *data, qint64 maxSize) override; @@ -85,6 +78,10 @@ protected: Q_PRIVATE_SLOT(d_func(), void _q_setFinished(QtCoap::Error)) Q_PRIVATE_SLOT(d_func(), void _q_setError(QtCoap::ResponseCode)) Q_PRIVATE_SLOT(d_func(), void _q_setError(QtCoap::Error)) + +private: + explicit QCoapReply(QCoapReplyPrivate &dd, QObject *parent = nullptr); + friend class QCoapResourceDiscoveryReply; }; QT_END_NAMESPACE diff --git a/src/coap/qcoapreply_p.h b/src/coap/qcoapreply_p.h index 68e9062..500c1dd 100644 --- a/src/coap/qcoapreply_p.h +++ b/src/coap/qcoapreply_p.h @@ -62,6 +62,8 @@ public: void _q_setError(QtCoap::ResponseCode code); void _q_setError(QtCoap::Error); + static QCoapReply *createCoapReply(const QCoapRequest &request, QObject *parent = nullptr); + QCoapRequest request; QCoapMessage message; QtCoap::ResponseCode responseCode = QtCoap::ResponseCode::InvalidCode; diff --git a/src/coap/qcoapresourcediscoveryreply.cpp b/src/coap/qcoapresourcediscoveryreply.cpp index dabdeb6..c709331 100644 --- a/src/coap/qcoapresourcediscoveryreply.cpp +++ b/src/coap/qcoapresourcediscoveryreply.cpp @@ -100,6 +100,8 @@ void QCoapResourceDiscoveryReplyPrivate::_q_setContent(const QHostAddress &sende */ /*! + \internal + Constructs a new CoAP discovery reply from the \a request and sets \a parent as its parent. */ diff --git a/src/coap/qcoapresourcediscoveryreply.h b/src/coap/qcoapresourcediscoveryreply.h index 65f386d..bd575e0 100644 --- a/src/coap/qcoapresourcediscoveryreply.h +++ b/src/coap/qcoapresourcediscoveryreply.h @@ -43,14 +43,15 @@ class Q_COAP_EXPORT QCoapResourceDiscoveryReply : public QCoapReply Q_OBJECT public: - explicit QCoapResourceDiscoveryReply(const QCoapRequest &request, QObject *parent = nullptr); - QVector resources() const; Q_SIGNALS: void discovered(QCoapResourceDiscoveryReply *reply, QVector resources); private: + explicit QCoapResourceDiscoveryReply(const QCoapRequest &request, QObject *parent = nullptr); + friend class QCoapClientPrivate; + Q_DECLARE_PRIVATE(QCoapResourceDiscoveryReply) }; diff --git a/tests/auto/qcoapinternalreply/tst_qcoapinternalreply.cpp b/tests/auto/qcoapinternalreply/tst_qcoapinternalreply.cpp index 61a3dbc..7915f44 100644 --- a/tests/auto/qcoapinternalreply/tst_qcoapinternalreply.cpp +++ b/tests/auto/qcoapinternalreply/tst_qcoapinternalreply.cpp @@ -45,8 +45,6 @@ private Q_SLOTS: void parseReplyPdu(); void updateReply_data(); void updateReply(); - void requestData(); - void abortRequest(); }; void tst_QCoapInternalReply::parseReplyPdu_data() @@ -167,24 +165,6 @@ void tst_QCoapInternalReply::parseReplyPdu() QCOMPARE(reply->message()->payload(), payload); } -class QCoapReplyForTests : public QCoapReply -{ -public: - QCoapReplyForTests(const QCoapRequest &req) : QCoapReply (req) {} - - void setRunning(const QCoapToken &token, QCoapMessageId messageId) - { - Q_D(QCoapReply); - d->_q_setRunning(token, messageId); - } - void setContentAndFinished(const QCoapInternalReply *internal) - { - Q_D(QCoapReply); - d->_q_setContent(internal->senderAddress(), *internal->message(), internal->responseCode()); - d->_q_setFinished(); - } -}; - void tst_QCoapInternalReply::updateReply_data() { QTest::addColumn("data"); @@ -196,39 +176,19 @@ void tst_QCoapInternalReply::updateReply() { QFETCH(QByteArray, data); - QCoapReplyForTests reply((QCoapRequest())); + QScopedPointer reply(QCoapReplyPrivate::createCoapReply(QCoapRequest())); QCoapInternalReply internalReply; internalReply.message()->setPayload(data); - QSignalSpy spyReplyFinished(&reply, &QCoapReply::finished); + QSignalSpy spyReplyFinished(reply.data(), &QCoapReply::finished); - reply.setContentAndFinished(&internalReply); + QMetaObject::invokeMethod(reply.data(), "_q_setContent", + Q_ARG(QHostAddress, internalReply.senderAddress()), + Q_ARG(QCoapMessage, *internalReply.message()), + Q_ARG(QtCoap::ResponseCode, internalReply.responseCode())); + QMetaObject::invokeMethod(reply.data(), "_q_setFinished", Q_ARG(QtCoap::Error, QtCoap::Error::Ok)); QTRY_COMPARE_WITH_TIMEOUT(spyReplyFinished.count(), 1, 1000); - QCOMPARE(reply.readAll(), data); -} - -void tst_QCoapInternalReply::requestData() -{ - QCoapReplyForTests reply((QCoapRequest())); - reply.setRunning("token", 543); - - QCOMPARE(reply.request().token(), QByteArray("token")); - QCOMPARE(reply.request().messageId(), 543); -} - -void tst_QCoapInternalReply::abortRequest() -{ - QCoapReplyForTests reply((QCoapRequest())); - reply.setRunning("token", 543); - - QSignalSpy spyAborted(&reply, &QCoapReply::aborted); - QSignalSpy spyFinished(&reply, &QCoapReply::finished); - reply.abortRequest(); - - QTRY_COMPARE_WITH_TIMEOUT(spyAborted.count(), 1, 1000); - QList arguments = spyAborted.takeFirst(); - QTRY_COMPARE_WITH_TIMEOUT(spyFinished.count(), 1, 1000); - QVERIFY(arguments.at(0).toByteArray() == "token"); + QCOMPARE(reply->readAll(), data); } #else diff --git a/tests/auto/qcoapreply/tst_qcoapreply.cpp b/tests/auto/qcoapreply/tst_qcoapreply.cpp index f22098e..f893840 100644 --- a/tests/auto/qcoapreply/tst_qcoapreply.cpp +++ b/tests/auto/qcoapreply/tst_qcoapreply.cpp @@ -48,18 +48,6 @@ private Q_SLOTS: void abortRequest(); }; -class QCoapReplyForTests : public QCoapReply -{ -public: - QCoapReplyForTests(const QCoapRequest &req) : QCoapReply (req) {} - - void setRunning(const QCoapToken &token, QCoapMessageId messageId) - { - Q_D(QCoapReply); - d->_q_setRunning(token, messageId); - } -}; - void tst_QCoapReply::updateReply_data() { QTest::addColumn("payload"); @@ -93,22 +81,22 @@ void tst_QCoapReply::updateReply() const QByteArray token = "\xAF\x01\xC2"; const quint16 id = 645; - QCoapReply reply(QCoapRequest{}); + QScopedPointer reply(QCoapReplyPrivate::createCoapReply(QCoapRequest())); QCoapMessage message; message.setToken(token); message.setMessageId(id); message.setPayload(payload); - QSignalSpy spyReplyFinished(&reply, &QCoapReply::finished); - QSignalSpy spyReplyNotified(&reply, &QCoapReply::notified); - QSignalSpy spyReplyError(&reply, &QCoapReply::error); - QSignalSpy spyReplyAborted(&reply, &QCoapReply::aborted); + QSignalSpy spyReplyFinished(reply.data(), &QCoapReply::finished); + QSignalSpy spyReplyNotified(reply.data(), &QCoapReply::notified); + QSignalSpy spyReplyError(reply.data(), &QCoapReply::error); + QSignalSpy spyReplyAborted(reply.data(), &QCoapReply::aborted); - QMetaObject::invokeMethod(&reply, "_q_setContent", + QMetaObject::invokeMethod(reply.data(), "_q_setContent", Q_ARG(QHostAddress, QHostAddress()), Q_ARG(QCoapMessage, message), Q_ARG(QtCoap::ResponseCode, responseCode)); - QMetaObject::invokeMethod(&reply, "_q_setFinished", + QMetaObject::invokeMethod(reply.data(), "_q_setFinished", Q_ARG(QtCoap::Error, error)); QCOMPARE(spyReplyFinished.count(), 1); @@ -116,42 +104,45 @@ void tst_QCoapReply::updateReply() QCOMPARE(spyReplyAborted.count(), 0); if (error != QtCoap::Error::Ok || QtCoap::isError(responseCode)) { QVERIFY(spyReplyError.count() > 0); - QCOMPARE(reply.isSuccessful(), false); + QCOMPARE(reply->isSuccessful(), false); } else { QCOMPARE(spyReplyError.count(), 0); - QCOMPARE(reply.isSuccessful(), true); + QCOMPARE(reply->isSuccessful(), true); } - QCOMPARE(reply.readAll(), payload); - QCOMPARE(reply.readAll(), QByteArray()); - QCOMPARE(reply.responseCode(), responseCode); - QCOMPARE(reply.message().token(), token); - QCOMPARE(reply.message().messageId(), id); + QCOMPARE(reply->readAll(), payload); + QCOMPARE(reply->readAll(), QByteArray()); + QCOMPARE(reply->responseCode(), responseCode); + QCOMPARE(reply->message().token(), token); + QCOMPARE(reply->message().messageId(), id); } void tst_QCoapReply::requestData() { - QCoapReplyForTests reply((QCoapRequest())); - reply.setRunning("token", 543); + QScopedPointer reply(QCoapReplyPrivate::createCoapReply(QCoapRequest())); + QMetaObject::invokeMethod(reply.data(), "_q_setRunning", + Q_ARG(QCoapToken, "token"), + Q_ARG(QCoapMessageId, 543)); - QCOMPARE(reply.request().token(), QByteArray("token")); - QCOMPARE(reply.request().messageId(), 543); + QCOMPARE(reply->request().token(), QByteArray("token")); + QCOMPARE(reply->request().messageId(), 543); } void tst_QCoapReply::abortRequest() { - QCoapReplyForTests reply((QCoapRequest())); - reply.setRunning("token", 543); + QScopedPointer reply(QCoapReplyPrivate::createCoapReply(QCoapRequest())); + QMetaObject::invokeMethod(reply.data(), "_q_setRunning", + Q_ARG(QCoapToken, "token"), + Q_ARG(QCoapMessageId, 543)); - QSignalSpy spyAborted(&reply, &QCoapReply::aborted); - QSignalSpy spyFinished(&reply, &QCoapReply::finished); - reply.abortRequest(); + QSignalSpy spyAborted(reply.data(), &QCoapReply::aborted); + QSignalSpy spyFinished(reply.data(), &QCoapReply::finished); + reply->abortRequest(); QTRY_COMPARE_WITH_TIMEOUT(spyAborted.count(), 1, 1000); QList arguments = spyAborted.takeFirst(); QTRY_COMPARE_WITH_TIMEOUT(spyFinished.count(), 1, 1000); QVERIFY(arguments.at(0).toByteArray() == "token"); - QCOMPARE(reply.isSuccessful(), false); } #else -- cgit v1.2.3 From 273f3fa6cee9c215fcdea6b5ee52ad34eb146452 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Tue, 14 May 2019 13:07:23 +0200 Subject: Improve the API of QCoapOption - The RFC specifies the following option formats: empty, opaque, uint and string. Leave only the construcors for the specified types. - Rename value getters to reflect the expected value type. - Move the internally used setter methods to the private class. This change is based on the feedback from API review. Change-Id: I0b482770b9c02e64b0f345384ebf58ec29a7e9bc Reviewed-by: Simon Hausmann --- src/coap/qcoapinternalmessage.cpp | 2 +- src/coap/qcoapinternalreply.cpp | 2 +- src/coap/qcoapinternalrequest.cpp | 2 +- src/coap/qcoapoption.cpp | 122 ++++++++++----------- src/coap/qcoapoption.h | 18 +-- src/coap/qcoapoption_p.h | 4 + .../qcoapinternalreply/tst_qcoapinternalreply.cpp | 2 +- .../tst_qcoapinternalrequest.cpp | 31 +++--- tests/auto/qcoapmessage/tst_qcoapmessage.cpp | 22 ++-- tests/auto/qcoapoption/tst_qcoapoption.cpp | 21 +--- 10 files changed, 102 insertions(+), 124 deletions(-) diff --git a/src/coap/qcoapinternalmessage.cpp b/src/coap/qcoapinternalmessage.cpp index 4da627e..121ee74 100644 --- a/src/coap/qcoapinternalmessage.cpp +++ b/src/coap/qcoapinternalmessage.cpp @@ -124,7 +124,7 @@ void QCoapInternalMessage::setFromDescriptiveBlockOption(const QCoapOption &opti Q_D(QCoapInternalMessage); //! TODO Cover with tests - const quint8 *optionData = reinterpret_cast(option.value().data()); + const quint8 *optionData = reinterpret_cast(option.opaqueValue().data()); const quint8 lastByte = optionData[option.length() - 1]; quint32 blockNumber = 0; diff --git a/src/coap/qcoapinternalreply.cpp b/src/coap/qcoapinternalreply.cpp index fe27ee0..2480a05 100644 --- a/src/coap/qcoapinternalreply.cpp +++ b/src/coap/qcoapinternalreply.cpp @@ -188,7 +188,7 @@ int QCoapInternalReply::nextBlockToSend() const if (!option.isValid()) return -1; - const quint8 *optionData = reinterpret_cast(option.value().data()); + const quint8 *optionData = reinterpret_cast(option.opaqueValue().data()); const quint8 lastByte = optionData[option.length() - 1]; // M field diff --git a/src/coap/qcoapinternalrequest.cpp b/src/coap/qcoapinternalrequest.cpp index 0237259..ff6354c 100644 --- a/src/coap/qcoapinternalrequest.cpp +++ b/src/coap/qcoapinternalrequest.cpp @@ -231,7 +231,7 @@ QByteArray QCoapInternalRequest::toQByteArray() const if (isOptionLengthExtended) appendByte(&pdu, optionLengthExtended); - pdu.append(option.value()); + pdu.append(option.opaqueValue()); lastOptionNumber = option.name(); } diff --git a/src/coap/qcoapoption.cpp b/src/coap/qcoapoption.cpp index ddb42fd..4973091 100644 --- a/src/coap/qcoapoption.cpp +++ b/src/coap/qcoapoption.cpp @@ -88,59 +88,45 @@ Q_LOGGING_CATEGORY(lcCoapOption, "qt.coap.option") /*! Constructs a new CoAP option with the given \a name - and QByteArray \a value. + and QByteArray \a opaqueValue. If no parameters are passed, constructs an Invalid object. \sa isValid() */ -QCoapOption::QCoapOption(OptionName name, const QByteArray &value) : +QCoapOption::QCoapOption(OptionName name, const QByteArray &opaqueValue) : d_ptr(new QCoapOptionPrivate) { Q_D(QCoapOption); d->name = name; - setValue(value); + d->setValue(opaqueValue); } /*! Constructs a new CoAP option with the given \a name - and the QStringView \a value. + and the QString \a stringValue. \sa isValid() */ -QCoapOption::QCoapOption(OptionName name, QStringView value) : +QCoapOption::QCoapOption(OptionName name, const QString &stringValue) : d_ptr(new QCoapOptionPrivate) { Q_D(QCoapOption); d->name = name; - setValue(value); + d->setValue(stringValue); } /*! Constructs a new CoAP option with the given \a name - and the string \a value. + and the unsigned integer \a intValue. \sa isValid() */ -QCoapOption::QCoapOption(OptionName name, const char *value) : +QCoapOption::QCoapOption(OptionName name, quint32 intValue) : d_ptr(new QCoapOptionPrivate) { Q_D(QCoapOption); d->name = name; - setValue(value); -} - -/*! - Constructs a new CoAP option with the given \a name - and the unsigned integer \a value. - - \sa isValid() - */ -QCoapOption::QCoapOption(OptionName name, quint32 value) : - d_ptr(new QCoapOptionPrivate) -{ - Q_D(QCoapOption); - d->name = name; - setValue(value); + d->setValue(intValue); } /*! @@ -203,7 +189,7 @@ void QCoapOption::swap(QCoapOption &other) Q_DECL_NOTHROW /*! Returns the value of the option. */ -QByteArray QCoapOption::value() const +QByteArray QCoapOption::opaqueValue() const { Q_D(const QCoapOption); return d->value; @@ -212,7 +198,7 @@ QByteArray QCoapOption::value() const /*! Returns the integer value of the option. */ -quint32 QCoapOption::valueToInt() const +quint32 QCoapOption::uintValue() const { Q_D(const QCoapOption); @@ -223,6 +209,15 @@ quint32 QCoapOption::valueToInt() const return intValue; } +/*! + Returns the QString value of the option. +*/ +QString QCoapOption::stringValue() const +{ + Q_D(const QCoapOption); + return QString::fromUtf8(d->value); +} + /*! Returns the length of the value of the option. */ @@ -269,95 +264,88 @@ bool QCoapOption::operator!=(const QCoapOption &other) const } /*! + \internal + Sets the \a value for the option. */ -void QCoapOption::setValue(const QByteArray &value) +void QCoapOptionPrivate::setValue(const QByteArray &opaqueValue) { - Q_D(QCoapOption); bool oversized = false; // Check for value maximum size, according to section 5.10 of RFC 7252 // https://tools.ietf.org/html/rfc7252#section-5.10 - switch (d_ptr->name) { - case IfNoneMatch: - if (value.size() > 0) + switch (name) { + case QCoapOption::IfNoneMatch: + if (opaqueValue.size() > 0) oversized = true; break; - case UriPort: - case ContentFormat: - case Accept: - if (value.size() > 2) + case QCoapOption::UriPort: + case QCoapOption::ContentFormat: + case QCoapOption::Accept: + if (opaqueValue.size() > 2) oversized = true; break; - case MaxAge: - case Size1: - if (value.size() > 4) + case QCoapOption::MaxAge: + case QCoapOption::Size1: + if (opaqueValue.size() > 4) oversized = true; break; - case IfMatch: - case Etag: - if (value.size() > 8) + case QCoapOption::IfMatch: + case QCoapOption::Etag: + if (opaqueValue.size() > 8) oversized = true; break; - case UriHost: - case LocationPath: - case UriPath: - case UriQuery: - case LocationQuery: - case ProxyScheme: - if (value.size() > 255) + case QCoapOption::UriHost: + case QCoapOption::LocationPath: + case QCoapOption::UriPath: + case QCoapOption::UriQuery: + case QCoapOption::LocationQuery: + case QCoapOption::ProxyScheme: + if (opaqueValue.size() > 255) oversized = true; break; - case ProxyUri: - if (value.size() > 1034) + case QCoapOption::ProxyUri: + if (opaqueValue.size() > 1034) oversized = true; break; - case Observe: - case Block2: - case Block1: - case Size2: + case QCoapOption::Observe: + case QCoapOption::Block2: + case QCoapOption::Block1: + case QCoapOption::Size2: default: break; } if (oversized) - qCWarning(lcCoapOption) << "Value" << value << "is probably too big for option" << d->name; + qCWarning(lcCoapOption) << "Value" << opaqueValue << "is probably too big for option" << name; - d->value = value; + value = opaqueValue; } /*! + \internal \overload Sets the \a value for the option. */ -void QCoapOption::setValue(QStringView value) +void QCoapOptionPrivate::setValue(const QString &value) { setValue(value.toUtf8()); } /*! + \internal \overload Sets the \a value for the option. */ -void QCoapOption::setValue(const char *value) -{ - setValue(QByteArray(value, static_cast(strlen(value)))); -} - -/*! - \overload - - Sets the \a value for the option. - */ -void QCoapOption::setValue(quint32 value) +void QCoapOptionPrivate::setValue(quint32 value) { QByteArray data; for (; value; value >>= 8) diff --git a/src/coap/qcoapoption.h b/src/coap/qcoapoption.h index ea9dc3c..ed8ebaf 100644 --- a/src/coap/qcoapoption.h +++ b/src/coap/qcoapoption.h @@ -64,10 +64,9 @@ public: Size1 = 60 }; - QCoapOption(OptionName name = Invalid, const QByteArray &value = QByteArray()); - QCoapOption(OptionName name, QStringView value); - QCoapOption(OptionName name, const char *value); - QCoapOption(OptionName name, quint32 value); + QCoapOption(OptionName name = Invalid, const QByteArray &opaqueValue = QByteArray()); + QCoapOption(OptionName name, const QString &stringValue); + QCoapOption(OptionName name, quint32 intValue); QCoapOption(const QCoapOption &other); QCoapOption(QCoapOption &&other); ~QCoapOption(); @@ -76,8 +75,9 @@ public: QCoapOption &operator=(QCoapOption &&other) Q_DECL_NOTHROW; void swap(QCoapOption &other) Q_DECL_NOTHROW; - QByteArray value() const; - quint32 valueToInt() const; + QByteArray opaqueValue() const; + quint32 uintValue() const; + QString stringValue() const; int length() const; OptionName name() const; bool isValid() const; @@ -85,12 +85,6 @@ public: bool operator==(const QCoapOption &other) const; bool operator!=(const QCoapOption &other) const; -protected: - void setValue(const QByteArray &value); - void setValue(QStringView value); - void setValue(const char *value); - void setValue(quint32 value); - private: QCoapOptionPrivate *d_ptr; diff --git a/src/coap/qcoapoption_p.h b/src/coap/qcoapoption_p.h index 9cc1bac..ff60acd 100644 --- a/src/coap/qcoapoption_p.h +++ b/src/coap/qcoapoption_p.h @@ -52,6 +52,10 @@ class Q_AUTOTEST_EXPORT QCoapOptionPrivate public: QCoapOptionPrivate() = default; + void setValue(const QByteArray &opaqueValue); + void setValue(const QString &value); + void setValue(quint32 value); + QCoapOption::OptionName name = QCoapOption::Invalid; QByteArray value; }; diff --git a/tests/auto/qcoapinternalreply/tst_qcoapinternalreply.cpp b/tests/auto/qcoapinternalreply/tst_qcoapinternalreply.cpp index 7915f44..f9f46bd 100644 --- a/tests/auto/qcoapinternalreply/tst_qcoapinternalreply.cpp +++ b/tests/auto/qcoapinternalreply/tst_qcoapinternalreply.cpp @@ -160,7 +160,7 @@ void tst_QCoapInternalReply::parseReplyPdu() QCoapOption option = reply->message()->optionAt(i); QCOMPARE(option.name(), optionsNames.at(i)); QCOMPARE(option.length(), optionsLengths.at(i)); - QCOMPARE(option.value(), optionsValues.at(i)); + QCOMPARE(option.opaqueValue(), optionsValues.at(i)); } QCOMPARE(reply->message()->payload(), payload); } diff --git a/tests/auto/qcoapinternalrequest/tst_qcoapinternalrequest.cpp b/tests/auto/qcoapinternalrequest/tst_qcoapinternalrequest.cpp index 1a1a293..d3e367b 100644 --- a/tests/auto/qcoapinternalrequest/tst_qcoapinternalrequest.cpp +++ b/tests/auto/qcoapinternalrequest/tst_qcoapinternalrequest.cpp @@ -177,33 +177,34 @@ void tst_QCoapInternalRequest::parseUri_data() << QUrl() << QVector({ QCoapOption(QCoapOption::UriPort, 1234), - QCoapOption(QCoapOption::UriPath, "test"), - QCoapOption(QCoapOption::UriPath, "path1") }); + QCoapOption(QCoapOption::UriPath, QByteArray("test")), + QCoapOption(QCoapOption::UriPath, QByteArray("path1")) }); QTest::newRow("path_query") << QUrl("coap://10.20.30.40/test/path1/?rd=25&nd=4") << QUrl() << QVector({ - QCoapOption(QCoapOption::UriPath, "test"), - QCoapOption(QCoapOption::UriPath, "path1"), - QCoapOption(QCoapOption::UriQuery, "rd=25"), - QCoapOption(QCoapOption::UriQuery, "nd=4") }); + QCoapOption(QCoapOption::UriPath, QByteArray("test")), + QCoapOption(QCoapOption::UriPath, QByteArray("path1")), + QCoapOption(QCoapOption::UriQuery, QByteArray("rd=25")), + QCoapOption(QCoapOption::UriQuery, QByteArray("nd=4")) }); QTest::newRow("host_path_query") << QUrl("coap://aa.bb.cc.com:5683/test/path1/?rd=25&nd=4") << QUrl() << QVector({ - QCoapOption(QCoapOption::UriHost, "aa.bb.cc.com"), - QCoapOption(QCoapOption::UriPath, "test"), - QCoapOption(QCoapOption::UriPath, "path1"), - QCoapOption(QCoapOption::UriQuery, "rd=25"), - QCoapOption(QCoapOption::UriQuery, "nd=4") }); + QCoapOption(QCoapOption::UriHost, QByteArray("aa.bb.cc.com")), + QCoapOption(QCoapOption::UriPath, QByteArray("test")), + QCoapOption(QCoapOption::UriPath, QByteArray("path1")), + QCoapOption(QCoapOption::UriQuery, QByteArray("rd=25")), + QCoapOption(QCoapOption::UriQuery, QByteArray("nd=4")) }); QTest::newRow("proxy_url") << QUrl("coap://aa.bb.cc.com:5683/test/path1/?rd=25&nd=4") << QUrl("coap://10.20.30.40/test:5684/othertest/path") << QVector({ - QCoapOption(QCoapOption::ProxyUri, "coap://10.20.30.40/test:5684/othertest/path") }); + QCoapOption(QCoapOption::ProxyUri, + QByteArray("coap://10.20.30.40/test:5684/othertest/path")) }); } void tst_QCoapInternalRequest::parseUri() @@ -227,9 +228,9 @@ void tst_QCoapInternalRequest::urlOptions_data() QTest::addColumn>("options"); QVector options = { - { QCoapOption::UriHost, "example.com" }, - { QCoapOption::UriPath, "~sensors" }, - { QCoapOption::UriPath, "temp.xml" } + { QCoapOption::UriHost, QByteArray("example.com") }, + { QCoapOption::UriPath, QByteArray("~sensors") }, + { QCoapOption::UriPath, QByteArray("temp.xml") } }; QTest::newRow("url_with_default_port") diff --git a/tests/auto/qcoapmessage/tst_qcoapmessage.cpp b/tests/auto/qcoapmessage/tst_qcoapmessage.cpp index e13281e..56579e5 100644 --- a/tests/auto/qcoapmessage/tst_qcoapmessage.cpp +++ b/tests/auto/qcoapmessage/tst_qcoapmessage.cpp @@ -123,7 +123,7 @@ void tst_QCoapMessage::addOption() QVERIFY(std::all_of(message.options().cbegin(), message.options().cend(), [value](const QCoapOption opt) -> bool { - return opt.value() == value; + return opt.opaqueValue() == value; })); } @@ -131,7 +131,7 @@ void tst_QCoapMessage::addOption_string_data() { QTest::addColumn>("options"); - QVector single_char_option = { { QCoapOption::LocationPath, "path1" } }; + QVector single_string_option = { { QCoapOption::LocationPath, QString("path1") } }; QVector single_ba_option = { { QCoapOption::LocationPath, QByteArray("\xAF\x01\xC2") } }; @@ -140,7 +140,7 @@ void tst_QCoapMessage::addOption_string_data() { QCoapOption::LocationPath, QString("str_path3") } }; - QTest::newRow("single_char_option") << single_char_option; + QTest::newRow("single_char_option") << single_string_option; QTest::newRow("single_ba_option") << single_ba_option; QTest::newRow("multiple_string_options") << multiple_string_options; } @@ -184,18 +184,18 @@ void tst_QCoapMessage::addOption_uint() message.addOption(option); QCOMPARE(message.options(name).size(), 1); - QCOMPARE(message.option(name).valueToInt(), value); - QCOMPARE(option.value().size(), size); + QCOMPARE(message.option(name).uintValue(), value); + QCOMPARE(option.opaqueValue().size(), size); } void tst_QCoapMessage::removeOption_data() { QTest::addColumn>("options"); - QVector single_option = { { QCoapOption::LocationPath, "path1" } }; + QVector single_option = { { QCoapOption::LocationPath, QByteArray("path1") } }; QVector multiple_options = { - { QCoapOption::LocationPath, "path2" }, - { QCoapOption::LocationPath, "path3" } + { QCoapOption::LocationPath, QByteArray("path2") }, + { QCoapOption::LocationPath, QByteArray("path3") } }; QTest::newRow("single_option") << single_option; @@ -227,10 +227,10 @@ void tst_QCoapMessage::removeOptionByName_data() QTest::addColumn>("options"); QTest::addColumn("name"); - QVector single_option = { { QCoapOption::LocationPath, "path1" } }; + QVector single_option = { { QCoapOption::LocationPath, QByteArray("path1") } }; QVector multiple_options = { - { QCoapOption::LocationPath, "path2" }, - { QCoapOption::LocationPath, "path3" } + { QCoapOption::LocationPath, QByteArray("path2") }, + { QCoapOption::LocationPath, QByteArray("path3") } }; QTest::newRow("remove_single_option") << single_option << single_option.back().name(); diff --git a/tests/auto/qcoapoption/tst_qcoapoption.cpp b/tests/auto/qcoapoption/tst_qcoapoption.cpp index 2541fad..fec22b1 100644 --- a/tests/auto/qcoapoption/tst_qcoapoption.cpp +++ b/tests/auto/qcoapoption/tst_qcoapoption.cpp @@ -38,8 +38,7 @@ class tst_QCoapOption : public QObject private Q_SLOTS: void constructWithQByteArray(); - void constructWithQStringView(); - void constructWithCString(); + void constructWithQString(); void constructWithInteger(); void constructWithUtf8Characters(); }; @@ -49,23 +48,15 @@ void tst_QCoapOption::constructWithQByteArray() QByteArray ba = "some data"; QCoapOption option(QCoapOption::LocationPath, ba); - QCOMPARE(option.value(), ba); + QCOMPARE(option.opaqueValue(), ba); } -void tst_QCoapOption::constructWithQStringView() +void tst_QCoapOption::constructWithQString() { QString str = "some data"; QCoapOption option(QCoapOption::LocationPath, str); - QCOMPARE(option.value(), str.toUtf8()); -} - -void tst_QCoapOption::constructWithCString() -{ - const char *str = "some data"; - QCoapOption option(QCoapOption::LocationPath, str); - - QCOMPARE(option.value(), QByteArray(str)); + QCOMPARE(option.opaqueValue(), str.toUtf8()); } void tst_QCoapOption::constructWithInteger() @@ -73,7 +64,7 @@ void tst_QCoapOption::constructWithInteger() quint32 value = 64000; QCoapOption option(QCoapOption::Size1, value); - QCOMPARE(option.valueToInt(), value); + QCOMPARE(option.uintValue(), value); } void tst_QCoapOption::constructWithUtf8Characters() @@ -81,7 +72,7 @@ void tst_QCoapOption::constructWithUtf8Characters() QByteArray ba = "\xc3\xa9~\xce\xbb\xe2\x82\xb2"; QCoapOption option(QCoapOption::LocationPath, ba); - QCOMPARE(option.value(), ba); + QCOMPARE(option.opaqueValue(), ba); } QTEST_APPLESS_MAIN(tst_QCoapOption) -- cgit v1.2.3 From 38f8e9feb94b1a552795af711c3c68c73dd1caba Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Tue, 14 May 2019 13:38:49 +0200 Subject: Improve namings of QCoapSecurityConfiguration - Rename QCoapPrivateKey::isEmpty() -> QCoapPrivateKey::isNull(). - Use 'preSharedKeyIdentity' instead of 'identity'. This change is based on the feedback from API review. Change-Id: Iba517a01a16edede86c4572204d560978b3dc067 Reviewed-by: Simon Hausmann --- examples/coap/quicksecureclient/qmlcoapsecureclient.cpp | 2 +- src/coap/qcoapqudpconnection.cpp | 4 ++-- src/coap/qcoapsecurityconfiguration.cpp | 12 ++++++------ src/coap/qcoapsecurityconfiguration.h | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/coap/quicksecureclient/qmlcoapsecureclient.cpp b/examples/coap/quicksecureclient/qmlcoapsecureclient.cpp index 1d141bb..dd40f4e 100644 --- a/examples/coap/quicksecureclient/qmlcoapsecureclient.cpp +++ b/examples/coap/quicksecureclient/qmlcoapsecureclient.cpp @@ -127,7 +127,7 @@ QmlCoapSecureClient::setSecurityConfiguration(const QString &preSharedKey, const { QCoapSecurityConfiguration configuration; configuration.setPreSharedKey(preSharedKey.toUtf8()); - configuration.setIdentity(identity.toUtf8()); + configuration.setPreSharedKeyIdentity(identity.toUtf8()); m_configuration = configuration; } diff --git a/src/coap/qcoapqudpconnection.cpp b/src/coap/qcoapqudpconnection.cpp index 4ca78dd..ebb3fa9 100644 --- a/src/coap/qcoapqudpconnection.cpp +++ b/src/coap/qcoapqudpconnection.cpp @@ -376,7 +376,7 @@ void QCoapQUdpConnectionPrivate::setSecurityConfiguration( if (!configuration.localCertificateChain().isEmpty()) dtlsConfig.setLocalCertificateChain(configuration.localCertificateChain().toList()); - if (!configuration.privateKey().isEmpty()) { + if (!configuration.privateKey().isNull()) { if (configuration.privateKey().algorithm() != QSsl::Opaque) { QSslKey privateKey(configuration.privateKey().key(), configuration.privateKey().algorithm(), @@ -409,7 +409,7 @@ void QCoapQUdpConnectionPrivate::setSecurityConfiguration( void QCoapQUdpConnection::pskRequired(QSslPreSharedKeyAuthenticator *authenticator) { Q_ASSERT(authenticator); - authenticator->setIdentity(securityConfiguration().identity()); + authenticator->setIdentity(securityConfiguration().preSharedKeyIdentity()); authenticator->setPreSharedKey(securityConfiguration().preSharedKey()); } diff --git a/src/coap/qcoapsecurityconfiguration.cpp b/src/coap/qcoapsecurityconfiguration.cpp index 02b4022..5fb8b26 100644 --- a/src/coap/qcoapsecurityconfiguration.cpp +++ b/src/coap/qcoapsecurityconfiguration.cpp @@ -149,9 +149,9 @@ QCoapPrivateKey &QCoapPrivateKey::operator=(const QCoapPrivateKey &other) } /*! - Returns \c true if the private key is empty, returns \c false otherwise. + Returns \c true if the private key is null, returns \c false otherwise. */ -bool QCoapPrivateKey::isEmpty() const +bool QCoapPrivateKey::isNull() const { return d->algorithm == QSsl::Opaque ? !d->opaqueKey : d->key.isEmpty(); } @@ -262,9 +262,9 @@ QCoapSecurityConfiguration::~QCoapSecurityConfiguration() /*! Sets the PSK client identity (to be advised to the server) to \a identity. - \sa identity() + \sa preSharedKeyIdentity() */ -void QCoapSecurityConfiguration::setIdentity(const QByteArray &identity) +void QCoapSecurityConfiguration::setPreSharedKeyIdentity(const QByteArray &identity) { d->identity = identity; } @@ -272,9 +272,9 @@ void QCoapSecurityConfiguration::setIdentity(const QByteArray &identity) /*! Returns the PSK client identity. - \sa setIdentity() + \sa setPreSharedKeyIdentity() */ -QByteArray QCoapSecurityConfiguration::identity() const +QByteArray QCoapSecurityConfiguration::preSharedKeyIdentity() const { return d->identity; } diff --git a/src/coap/qcoapsecurityconfiguration.h b/src/coap/qcoapsecurityconfiguration.h index 7dc00bf..d91ac52 100644 --- a/src/coap/qcoapsecurityconfiguration.h +++ b/src/coap/qcoapsecurityconfiguration.h @@ -59,7 +59,7 @@ public: void swap(QCoapPrivateKey &other) Q_DECL_NOTHROW { qSwap(d, other.d); } - bool isEmpty() const; + bool isNull() const; QByteArray key() const; Qt::HANDLE handle() const; @@ -86,8 +86,8 @@ public: void swap(QCoapSecurityConfiguration &other) Q_DECL_NOTHROW { qSwap(d, other.d); } - void setIdentity(const QByteArray &identity); - QByteArray identity() const; + void setPreSharedKeyIdentity(const QByteArray &preSharedKeyIdentity); + QByteArray preSharedKeyIdentity() const; void setPreSharedKey(const QByteArray &preSharedKey); QByteArray preSharedKey() const; -- cgit v1.2.3 From 86b77d2da82697f914889b8377eaf03f671fcb6e Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Tue, 14 May 2019 14:20:24 +0200 Subject: Use maximum/minimum instead of max/min This change is based on the feedback from API review. Change-Id: Ia17d061c60fcf7aa4da38542d96d75ae621f657a Reviewed-by: Simon Hausmann --- src/coap/qcoapclient.cpp | 12 ++--- src/coap/qcoapclient.h | 4 +- src/coap/qcoapprotocol.cpp | 72 +++++++++++++++--------------- src/coap/qcoapprotocol_p.h | 22 ++++----- tests/auto/qcoapclient/tst_qcoapclient.cpp | 14 +++--- 5 files changed, 62 insertions(+), 62 deletions(-) diff --git a/src/coap/qcoapclient.cpp b/src/coap/qcoapclient.cpp index 372128a..5469ef2 100644 --- a/src/coap/qcoapclient.cpp +++ b/src/coap/qcoapclient.cpp @@ -626,10 +626,10 @@ void QCoapClient::setSocketOption(QAbstractSocket::SocketOption option, const QV As defined in \l {RFC 7390 - Section 2.5}, \c MAX_SERVER_RESPONSE_DELAY is the expected maximum response delay over all servers that the client can send a multicast request to. */ -void QCoapClient::setMaxServerResponseDelay(uint responseDelay) +void QCoapClient::setMaximumServerResponseDelay(uint responseDelay) { Q_D(QCoapClient); - QMetaObject::invokeMethod(d->protocol, "setMaxServerResponseDelay", Qt::QueuedConnection, + QMetaObject::invokeMethod(d->protocol, "setMaximumServerResponseDelay", Qt::QueuedConnection, Q_ARG(uint, responseDelay)); } @@ -666,14 +666,14 @@ void QCoapClient::setAckRandomFactor(double ackRandomFactor) /*! Sets the \c MAX_RETRANSMIT value defined in \l {RFC 7252 - Section 4.2} - to \a maxRetransmit. This value should be less than or equal to 25. + to \a maximumRetransmitCount. This value should be less than or equal to 25. The default is 4. */ -void QCoapClient::setMaxRetransmit(uint maxRetransmit) +void QCoapClient::setMaximumRetransmitCount(uint maximumRetransmitCount) { Q_D(QCoapClient); - QMetaObject::invokeMethod(d->protocol, "setMaxRetransmit", Qt::QueuedConnection, - Q_ARG(uint, maxRetransmit)); + QMetaObject::invokeMethod(d->protocol, "setMaximumRetransmitCount", Qt::QueuedConnection, + Q_ARG(uint, maximumRetransmitCount)); } QT_END_NAMESPACE diff --git a/src/coap/qcoapclient.h b/src/coap/qcoapclient.h index b64905b..7171b59 100644 --- a/src/coap/qcoapclient.h +++ b/src/coap/qcoapclient.h @@ -85,10 +85,10 @@ public: void setSecurityConfiguration(const QCoapSecurityConfiguration &configuration); void setBlockSize(quint16 blockSize); void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value); - void setMaxServerResponseDelay(uint responseDelay); + void setMaximumServerResponseDelay(uint responseDelay); void setAckTimeout(uint ackTimeout); void setAckRandomFactor(double ackRandomFactor); - void setMaxRetransmit(uint maxRetransmit); + void setMaximumRetransmitCount(uint maximumRetransmitCount); Q_SIGNALS: void finished(QCoapReply *reply); diff --git a/src/coap/qcoapprotocol.cpp b/src/coap/qcoapprotocol.cpp index 28ee7e8..46f420c 100644 --- a/src/coap/qcoapprotocol.cpp +++ b/src/coap/qcoapprotocol.cpp @@ -145,7 +145,7 @@ void QCoapProtocol::sendRequest(QPointer reply, QCoapConnection *con }); auto internalRequest = QSharedPointer::create(reply->request(), this); - internalRequest->setMaxTransmissionWait(maxTransmitWait()); + internalRequest->setMaxTransmissionWait(maximumTransmitWait()); connect(reply, &QCoapReply::finished, this, &QCoapProtocol::finished); if (internalRequest->isMulticast()) { @@ -157,8 +157,8 @@ void QCoapProtocol::sendRequest(QPointer reply, QCoapConnection *con // The timeout interval is chosen based on // https://tools.ietf.org/html/rfc7390#section-2.5 internalRequest->setMulticastTimeout(nonConfirmLifetime() - + maxLatency() - + maxServerResponseDelay()); + + maximumLatency() + + maximumServerResponseDelay()); } // Set a unique Message Id and Token @@ -181,9 +181,9 @@ void QCoapProtocol::sendRequest(QPointer reply, QCoapConnection *con } if (requestMessage->type() == QCoapMessage::Type::Confirmable) - internalRequest->setTimeout(QtCoap::randomGenerator().bounded(minTimeout(), maxTimeout())); + internalRequest->setTimeout(QtCoap::randomGenerator().bounded(minimumTimeout(), maximumTimeout())); else - internalRequest->setTimeout(maxTimeout()); + internalRequest->setTimeout(maximumTimeout()); connect(internalRequest.data(), &QCoapInternalRequest::timeout, [this](QCoapInternalRequest *request) { @@ -242,7 +242,7 @@ void QCoapProtocolPrivate::onRequestTimeout(QCoapInternalRequest *request) return; if (request->message()->type() == QCoapMessage::Type::Confirmable - && request->retransmissionCounter() < maxRetransmit) { + && request->retransmissionCounter() < maximumRetransmitCount) { sendRequest(request); } else { onRequestError(request, QtCoap::Error::TimeOut); @@ -894,7 +894,7 @@ bool QCoapProtocolPrivate::isMessageIdRegistered(quint16 id) const Returns the ACK_TIMEOUT value in milliseconds. The default is 2000. - \sa minTimeout(), setAckTimeout() + \sa minimumTimeout(), setAckTimeout() */ uint QCoapProtocol::ackTimeout() const { @@ -923,18 +923,18 @@ double QCoapProtocol::ackRandomFactor() const retransmissions of a message, before notifying a timeout error. The default is 4. - \sa setMaxRetransmit() + \sa setMaximumRetransmitCount() */ -uint QCoapProtocol::maxRetransmit() const +uint QCoapProtocol::maximumRetransmitCount() const { Q_D(const QCoapProtocol); - return d->maxRetransmit; + return d->maximumRetransmitCount; } /*! \internal - Returns the max block size wanted. + Returns the maximum block size wanted. The default is 0, which invites the server to choose the block size. \sa setBlockSize() @@ -954,9 +954,9 @@ quint16 QCoapProtocol::blockSize() const It is the maximum time from the first transmission of a Confirmable message to its last retransmission. */ -uint QCoapProtocol::maxTransmitSpan() const +uint QCoapProtocol::maximumTransmitSpan() const { - return static_cast(ackTimeout() * (1u << (maxRetransmit() - 1)) * ackRandomFactor()); + return static_cast(ackTimeout() * (1u << (maximumRetransmitCount() - 1)) * ackRandomFactor()); } /*! @@ -969,9 +969,9 @@ uint QCoapProtocol::maxTransmitSpan() const message to the time when the sender gives up on receiving an acknowledgment or reset. */ -uint QCoapProtocol::maxTransmitWait() const +uint QCoapProtocol::maximumTransmitWait() const { - return static_cast(ackTimeout() * ((1u << (maxRetransmit() + 1)) - 1) + return static_cast(ackTimeout() * ((1u << (maximumRetransmitCount() + 1)) - 1) * ackRandomFactor()); } @@ -985,7 +985,7 @@ uint QCoapProtocol::maxTransmitWait() const It is the maximum time a datagram is expected to take from the start of its transmission to the completion of its reception. */ -constexpr uint QCoapProtocol::maxLatency() +uint QCoapProtocol::maximumLatency() const { return 100 * 1000; } @@ -994,12 +994,12 @@ constexpr uint QCoapProtocol::maxLatency() \internal Returns the minimum duration for messages timeout. The timeout is defined - as a random value between minTimeout() and maxTimeout(). This is a + as a random value between minimumTimeout() and maximumTimeout(). This is a convenience method identical to ackTimeout(). \sa ackTimeout(), setAckTimeout() */ -uint QCoapProtocol::minTimeout() const +uint QCoapProtocol::minimumTimeout() const { Q_D(const QCoapProtocol); return d->ackTimeout; @@ -1010,9 +1010,9 @@ uint QCoapProtocol::minTimeout() const Returns the maximum duration for messages timeout in milliseconds. - \sa maxTimeout(), setAckTimeout(), setAckRandomFactor() + \sa maximumTimeout(), setAckTimeout(), setAckRandomFactor() */ -uint QCoapProtocol::maxTimeout() const +uint QCoapProtocol::maximumTimeout() const { Q_D(const QCoapProtocol); return static_cast(d->ackTimeout * d->ackRandomFactor); @@ -1029,7 +1029,7 @@ uint QCoapProtocol::maxTimeout() const */ uint QCoapProtocol::nonConfirmLifetime() const { - return maxTransmitSpan() + maxLatency(); + return maximumTransmitSpan() + maximumLatency(); } /*! @@ -1041,12 +1041,12 @@ uint QCoapProtocol::nonConfirmLifetime() const It is the expected maximum response delay over all servers that the client can send a multicast request to. - \sa setMaxServerResponseDelay() + \sa setMaximumServerResponseDelay() */ -uint QCoapProtocol::maxServerResponseDelay() const +uint QCoapProtocol::maximumServerResponseDelay() const { Q_D(const QCoapProtocol); - return d->maxServerResponseDelay; + return d->maximumServerResponseDelay; } /*! @@ -1059,7 +1059,7 @@ uint QCoapProtocol::maxServerResponseDelay() const reliable transmissions is a random value between ackTimeout() and ackTimeout() * ackRandomFactor(). - \sa ackTimeout(), setAckRandomFactor(), minTimeout(), maxTimeout() + \sa ackTimeout(), setAckRandomFactor(), minimumTimeout(), maximumTimeout() */ void QCoapProtocol::setAckTimeout(uint ackTimeout) { @@ -1088,28 +1088,28 @@ void QCoapProtocol::setAckRandomFactor(double ackRandomFactor) /*! \internal - Sets the MAX_RETRANSMIT value to \a maxRetransmit, but never + Sets the MAX_RETRANSMIT value to \a maximumRetransmitCount, but never to more than 25. The default is 4. - \sa maxRetransmit() + \sa maximumRetransmitCount() */ -void QCoapProtocol::setMaxRetransmit(uint maxRetransmit) +void QCoapProtocol::setMaximumRetransmitCount(uint maximumRetransmitCount) { Q_D(QCoapProtocol); - if (maxRetransmit > 25) { - qCWarning(lcCoapProtocol, "Max retransmit count is capped at 25."); - maxRetransmit = 25; + if (maximumRetransmitCount > 25) { + qCWarning(lcCoapProtocol, "Maximum retransmit count is capped at 25."); + maximumRetransmitCount = 25; } - d->maxRetransmit = maxRetransmit; + d->maximumRetransmitCount = maximumRetransmitCount; } /*! \internal - Sets the max block size wanted to \a blockSize. + Sets the maximum block size wanted to \a blockSize. The \a blockSize should be zero, or range from 16 to 1024 and be a power of 2. A size of 0 invites the server to choose the block size. @@ -1143,12 +1143,12 @@ void QCoapProtocol::setBlockSize(quint16 blockSize) As defined in \l {RFC 7390 - Section 2.5}, \c MAX_SERVER_RESPONSE_DELAY is the expected maximum response delay over all servers that the client can send a multicast request to. - \sa maxServerResponseDelay() + \sa maximumServerResponseDelay() */ -void QCoapProtocol::setMaxServerResponseDelay(uint responseDelay) +void QCoapProtocol::setMaximumServerResponseDelay(uint responseDelay) { Q_D(QCoapProtocol); - d->maxServerResponseDelay = responseDelay; + d->maximumServerResponseDelay = responseDelay; } QT_END_NAMESPACE diff --git a/src/coap/qcoapprotocol_p.h b/src/coap/qcoapprotocol_p.h index b50453f..4bd61c8 100644 --- a/src/coap/qcoapprotocol_p.h +++ b/src/coap/qcoapprotocol_p.h @@ -66,17 +66,17 @@ public: uint ackTimeout() const; double ackRandomFactor() const; - uint maxRetransmit() const; + uint maximumRetransmitCount() const; quint16 blockSize() const; - uint maxTransmitSpan() const; - uint maxTransmitWait() const; - static constexpr uint maxLatency(); + uint maximumTransmitSpan() const; + uint maximumTransmitWait() const; + uint maximumLatency() const; - uint minTimeout() const; - uint maxTimeout() const; + uint minimumTimeout() const; + uint maximumTimeout() const; uint nonConfirmLifetime() const; - uint maxServerResponseDelay() const; + uint maximumServerResponseDelay() const; Q_SIGNALS: void finished(QCoapReply *reply); @@ -87,9 +87,9 @@ Q_SIGNALS: public: Q_INVOKABLE void setAckTimeout(uint ackTimeout); Q_INVOKABLE void setAckRandomFactor(double ackRandomFactor); - Q_INVOKABLE void setMaxRetransmit(uint maxRetransmit); + Q_INVOKABLE void setMaximumRetransmitCount(uint maximumRetransmitCount); Q_INVOKABLE void setBlockSize(quint16 blockSize); - Q_INVOKABLE void setMaxServerResponseDelay(uint responseDelay); + Q_INVOKABLE void setMaximumServerResponseDelay(uint responseDelay); private: Q_INVOKABLE void sendRequest(QPointer reply, QCoapConnection *connection); @@ -157,9 +157,9 @@ public: CoapExchangeMap exchangeMap; quint16 blockSize = 0; - uint maxRetransmit = 4; + uint maximumRetransmitCount = 4; uint ackTimeout = 2000; - uint maxServerResponseDelay = 250 * 1000; + uint maximumServerResponseDelay = 250 * 1000; double ackRandomFactor = 1.5; Q_DECLARE_PUBLIC(QCoapProtocol) diff --git a/tests/auto/qcoapclient/tst_qcoapclient.cpp b/tests/auto/qcoapclient/tst_qcoapclient.cpp index 2b914ab..6f66346 100644 --- a/tests/auto/qcoapclient/tst_qcoapclient.cpp +++ b/tests/auto/qcoapclient/tst_qcoapclient.cpp @@ -478,7 +478,7 @@ void tst_QCoapClient::socketError() void tst_QCoapClient::timeout_data() { QTest::addColumn("timeout"); - QTest::addColumn("maxRetransmit"); + QTest::addColumn("maximumRetransmitCount"); QTest::newRow("2000/0") << 2000 << 0; QTest::newRow("2000/2") << 2000 << 2; @@ -489,13 +489,13 @@ void tst_QCoapClient::timeout() { #ifdef QT_BUILD_INTERNAL QFETCH(int, timeout); - QFETCH(int, maxRetransmit); + QFETCH(int, maximumRetransmitCount); QCoapClientForTests client; // Trigger a network timeout client.protocol()->setAckTimeout(timeout); client.protocol()->setAckRandomFactor(1); - client.protocol()->setMaxRetransmit(maxRetransmit); + client.protocol()->setMaximumRetransmitCount(maximumRetransmitCount); QUrl url = QUrl("coap://192.0.2.0:5683/"); // Need an url that returns nothing QElapsedTimer timeoutTimer; @@ -508,20 +508,20 @@ void tst_QCoapClient::timeout() QSignalSpy spyReplyFinished(reply.data(), &QCoapReply::finished); // Check timeout upper limit - int transmissions = maxRetransmit + 1; + int transmissions = maximumRetransmitCount + 1; // 10% Precision expected at least, plus timer precision QTRY_COMPARE_WITH_TIMEOUT(spyReplyError.count(), 1, static_cast( - 1.1 * client.protocol()->maxTransmitWait() + 20 * transmissions)); + 1.1 * client.protocol()->maximumTransmitWait() + 20 * transmissions)); // Check timeout lower limit qint64 elapsedTime = timeoutTimer.elapsed(); QString errorMessage = QString("Timeout was triggered after %1ms, while expecting about %2ms") .arg(QString::number(elapsedTime), - QString::number(client.protocol()->maxTransmitWait())); + QString::number(client.protocol()->maximumTransmitWait())); // 10% Precision expected at least, minus timer precision - QVERIFY2(elapsedTime > 0.9 * client.protocol()->maxTransmitWait() - 20 * transmissions, + QVERIFY2(elapsedTime > 0.9 * client.protocol()->maximumTransmitWait() - 20 * transmissions, qPrintable(errorMessage)); QCOMPARE(qvariant_cast(spyReplyError.first().at(1)), -- cgit v1.2.3 From 596c407a3c5719d5369c602b51cbd3b31779b9da Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Tue, 14 May 2019 14:40:49 +0200 Subject: Replace Q_DECL_NOTHROW -> noexcept This change is based on the feedback from API review. Change-Id: I422f08cfc31d442893feefcacfa34271e8d7954b Reviewed-by: Simon Hausmann --- src/coap/qcoapmessage.cpp | 4 ++-- src/coap/qcoapmessage.h | 4 ++-- src/coap/qcoapoption.cpp | 4 ++-- src/coap/qcoapoption.h | 4 ++-- src/coap/qcoapresource.cpp | 2 +- src/coap/qcoapresource.h | 2 +- src/coap/qcoapsecurityconfiguration.cpp | 4 ++-- src/coap/qcoapsecurityconfiguration.h | 12 ++++++------ 8 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/coap/qcoapmessage.cpp b/src/coap/qcoapmessage.cpp index 21462f7..d1ad665 100644 --- a/src/coap/qcoapmessage.cpp +++ b/src/coap/qcoapmessage.cpp @@ -398,7 +398,7 @@ void QCoapMessage::setOptions(const QVector &options) d->options = options; } -void QCoapMessage::swap(QCoapMessage &other) Q_DECL_NOTHROW +void QCoapMessage::swap(QCoapMessage &other) noexcept { qSwap(d_ptr, other.d_ptr); } @@ -406,7 +406,7 @@ void QCoapMessage::swap(QCoapMessage &other) Q_DECL_NOTHROW /*! Move-assignment operator. */ -QCoapMessage &QCoapMessage::operator=(QCoapMessage &&other) Q_DECL_NOTHROW +QCoapMessage &QCoapMessage::operator=(QCoapMessage &&other) noexcept { swap(other); return *this; diff --git a/src/coap/qcoapmessage.h b/src/coap/qcoapmessage.h index b88a41a..200a44b 100644 --- a/src/coap/qcoapmessage.h +++ b/src/coap/qcoapmessage.h @@ -55,9 +55,9 @@ public: QCoapMessage(const QCoapMessage &other); ~QCoapMessage(); - void swap(QCoapMessage &other) Q_DECL_NOTHROW; + void swap(QCoapMessage &other) noexcept; QCoapMessage &operator=(const QCoapMessage &other); - QCoapMessage &operator=(QCoapMessage &&other) Q_DECL_NOTHROW; + QCoapMessage &operator=(QCoapMessage &&other) noexcept; quint8 version() const; Type type() const; diff --git a/src/coap/qcoapoption.cpp b/src/coap/qcoapoption.cpp index 4973091..ac5e809 100644 --- a/src/coap/qcoapoption.cpp +++ b/src/coap/qcoapoption.cpp @@ -172,7 +172,7 @@ QCoapOption &QCoapOption::operator=(const QCoapOption &other) /*! Move-assignment operator. */ -QCoapOption &QCoapOption::operator=(QCoapOption &&other) Q_DECL_NOTHROW +QCoapOption &QCoapOption::operator=(QCoapOption &&other) noexcept { swap(other); return *this; @@ -181,7 +181,7 @@ QCoapOption &QCoapOption::operator=(QCoapOption &&other) Q_DECL_NOTHROW /*! Swaps this option with \a other. This operation is very fast and never fails. */ -void QCoapOption::swap(QCoapOption &other) Q_DECL_NOTHROW +void QCoapOption::swap(QCoapOption &other) noexcept { qSwap(d_ptr, other.d_ptr); } diff --git a/src/coap/qcoapoption.h b/src/coap/qcoapoption.h index ed8ebaf..3b44450 100644 --- a/src/coap/qcoapoption.h +++ b/src/coap/qcoapoption.h @@ -72,8 +72,8 @@ public: ~QCoapOption(); QCoapOption &operator=(const QCoapOption &other); - QCoapOption &operator=(QCoapOption &&other) Q_DECL_NOTHROW; - void swap(QCoapOption &other) Q_DECL_NOTHROW; + QCoapOption &operator=(QCoapOption &&other) noexcept; + void swap(QCoapOption &other) noexcept; QByteArray opaqueValue() const; quint32 uintValue() const; diff --git a/src/coap/qcoapresource.cpp b/src/coap/qcoapresource.cpp index 3ce1eea..3e0dc15 100644 --- a/src/coap/qcoapresource.cpp +++ b/src/coap/qcoapresource.cpp @@ -84,7 +84,7 @@ QCoapResource &QCoapResource::operator=(const QCoapResource &other) /*! Swaps this resource with \a other. This operation is very fast and never fails. */ -void QCoapResource::swap(QCoapResource &other) Q_DECL_NOTHROW +void QCoapResource::swap(QCoapResource &other) noexcept { d.swap(other.d); } diff --git a/src/coap/qcoapresource.h b/src/coap/qcoapresource.h index 5a97e12..9b1e421 100644 --- a/src/coap/qcoapresource.h +++ b/src/coap/qcoapresource.h @@ -47,7 +47,7 @@ public: ~QCoapResource(); QCoapResource &operator =(const QCoapResource &other); - void swap(QCoapResource &other) Q_DECL_NOTHROW; + void swap(QCoapResource &other) noexcept; QHostAddress host() const; QString path() const; diff --git a/src/coap/qcoapsecurityconfiguration.cpp b/src/coap/qcoapsecurityconfiguration.cpp index 5fb8b26..c9dba57 100644 --- a/src/coap/qcoapsecurityconfiguration.cpp +++ b/src/coap/qcoapsecurityconfiguration.cpp @@ -123,7 +123,7 @@ QCoapPrivateKey::QCoapPrivateKey(const QCoapPrivateKey &other) Move-constructs a QCoapPrivateKey, making it point to the same object as \a other was pointing to. */ -QCoapPrivateKey::QCoapPrivateKey(QCoapPrivateKey &&other) Q_DECL_NOTHROW +QCoapPrivateKey::QCoapPrivateKey(QCoapPrivateKey &&other) noexcept : d(other.d) { other.d = nullptr; @@ -236,7 +236,7 @@ QCoapSecurityConfiguration::QCoapSecurityConfiguration(const QCoapSecurityConfig object as \a other was pointing to. */ QCoapSecurityConfiguration::QCoapSecurityConfiguration( - QCoapSecurityConfiguration &&other) Q_DECL_NOTHROW + QCoapSecurityConfiguration &&other) noexcept : d(other.d) { other.d = nullptr; diff --git a/src/coap/qcoapsecurityconfiguration.h b/src/coap/qcoapsecurityconfiguration.h index d91ac52..f935b5e 100644 --- a/src/coap/qcoapsecurityconfiguration.h +++ b/src/coap/qcoapsecurityconfiguration.h @@ -49,14 +49,14 @@ public: QCoapPrivateKey(const Qt::HANDLE &handle); QCoapPrivateKey(const QCoapPrivateKey& other); - QCoapPrivateKey(QCoapPrivateKey&& other) Q_DECL_NOTHROW; + QCoapPrivateKey(QCoapPrivateKey&& other) noexcept; ~QCoapPrivateKey(); - QCoapPrivateKey &operator=(QCoapPrivateKey &&other) Q_DECL_NOTHROW + QCoapPrivateKey &operator=(QCoapPrivateKey &&other) noexcept { swap(other); return *this; } QCoapPrivateKey &operator=(const QCoapPrivateKey &other); - void swap(QCoapPrivateKey &other) Q_DECL_NOTHROW + void swap(QCoapPrivateKey &other) noexcept { qSwap(d, other.d); } bool isNull() const; @@ -78,12 +78,12 @@ public: QCoapSecurityConfiguration(const QCoapSecurityConfiguration &other); ~QCoapSecurityConfiguration(); - QCoapSecurityConfiguration(QCoapSecurityConfiguration &&other) Q_DECL_NOTHROW; - QCoapSecurityConfiguration &operator=(QCoapSecurityConfiguration &&other) Q_DECL_NOTHROW + QCoapSecurityConfiguration(QCoapSecurityConfiguration &&other) noexcept; + QCoapSecurityConfiguration &operator=(QCoapSecurityConfiguration &&other) noexcept { swap(other); return *this; } QCoapSecurityConfiguration &operator=(const QCoapSecurityConfiguration &other); - void swap(QCoapSecurityConfiguration &other) Q_DECL_NOTHROW + void swap(QCoapSecurityConfiguration &other) noexcept { qSwap(d, other.d); } void setPreSharedKeyIdentity(const QByteArray &preSharedKeyIdentity); -- cgit v1.2.3 From fd94d8e5673a5d5228aa2d38b605fde7a881332b Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Tue, 14 May 2019 16:23:27 +0200 Subject: Conditionally enable subprojects for the private class tests Instead of using #ifdefs for the private class tests, conditionally enable/disable subprojects for them. Change-Id: I574dc34dd895a4fc2eeb311dfb91a149a6715e28 Reviewed-by: Simon Hausmann --- tests/auto/auto.pro | 13 ++++++++----- .../auto/qcoapinternalreply/tst_qcoapinternalreply.cpp | 17 ----------------- .../qcoapinternalrequest/tst_qcoapinternalrequest.cpp | 17 ----------------- .../qcoapqudpconnection/tst_qcoapqudpconnection.cpp | 17 ----------------- tests/auto/qcoapreply/tst_qcoapreply.cpp | 17 ----------------- 5 files changed, 8 insertions(+), 73 deletions(-) diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 82c8af8..5bfc66e 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -1,14 +1,17 @@ TEMPLATE = subdirs +# TODO: enable disabled tests, when CI is configured properly + SUBDIRS += \ cmake \ -# TODO: enable the tests below, when CI is configured properly # qcoapclient \ -# qcoapqudpconnection \ - qcoapinternalreply \ - qcoapinternalrequest \ qcoapmessage \ qcoapoption \ - qcoapreply \ qcoaprequest \ qcoapresource + +qtConfig(private_tests): SUBDIRS += \ +# qcoapqudpconnection \ + qcoapinternalrequest \ + qcoapinternalreply \ + qcoapreply diff --git a/tests/auto/qcoapinternalreply/tst_qcoapinternalreply.cpp b/tests/auto/qcoapinternalreply/tst_qcoapinternalreply.cpp index f9f46bd..c12592a 100644 --- a/tests/auto/qcoapinternalreply/tst_qcoapinternalreply.cpp +++ b/tests/auto/qcoapinternalreply/tst_qcoapinternalreply.cpp @@ -34,8 +34,6 @@ #include #include -#ifdef QT_BUILD_INTERNAL - class tst_QCoapInternalReply : public QObject { Q_OBJECT @@ -191,21 +189,6 @@ void tst_QCoapInternalReply::updateReply() QCOMPARE(reply->readAll(), data); } -#else - -class tst_QCoapInternalReply : public QObject -{ - Q_OBJECT - -private slots: - void initTestCase() - { - QSKIP("Not an internal build, nothing to test"); - } -}; - -#endif - QTEST_MAIN(tst_QCoapInternalReply) #include "tst_qcoapinternalreply.moc" diff --git a/tests/auto/qcoapinternalrequest/tst_qcoapinternalrequest.cpp b/tests/auto/qcoapinternalrequest/tst_qcoapinternalrequest.cpp index d3e367b..d14edc1 100644 --- a/tests/auto/qcoapinternalrequest/tst_qcoapinternalrequest.cpp +++ b/tests/auto/qcoapinternalrequest/tst_qcoapinternalrequest.cpp @@ -35,8 +35,6 @@ #include #include -#ifdef QT_BUILD_INTERNAL - class tst_QCoapInternalRequest : public QObject { Q_OBJECT @@ -315,21 +313,6 @@ void tst_QCoapInternalRequest::isMulticast() QCOMPARE(internalRequest.isMulticast(), result); } -#else - -class tst_QCoapInternalRequest : public QObject -{ - Q_OBJECT - -private slots: - void initTestCase() - { - QSKIP("Not an internal build, nothing to test"); - } -}; - -#endif - QTEST_APPLESS_MAIN(tst_QCoapInternalRequest) #include "tst_qcoapinternalrequest.moc" diff --git a/tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp b/tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp index ee8c295..8876214 100644 --- a/tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp +++ b/tests/auto/qcoapqudpconnection/tst_qcoapqudpconnection.cpp @@ -43,8 +43,6 @@ #include #include "../coapnetworksettings.h" -#ifdef QT_BUILD_INTERNAL - using namespace QtCoapNetworkSettings; class tst_QCoapQUdpConnection : public QObject @@ -197,21 +195,6 @@ void tst_QCoapQUdpConnection::sendRequest() QVERIFY(QString(data.toHex()).endsWith(dataHexaPayload)); } -#else - -class tst_QCoapQUdpConnection : public QObject -{ - Q_OBJECT - -private slots: - void initTestCase() - { - QSKIP("Not an internal build, nothing to test"); - } -}; - -#endif - QTEST_MAIN(tst_QCoapQUdpConnection) #include "tst_qcoapqudpconnection.moc" diff --git a/tests/auto/qcoapreply/tst_qcoapreply.cpp b/tests/auto/qcoapreply/tst_qcoapreply.cpp index f893840..c02c3ae 100644 --- a/tests/auto/qcoapreply/tst_qcoapreply.cpp +++ b/tests/auto/qcoapreply/tst_qcoapreply.cpp @@ -35,8 +35,6 @@ #include #include -#ifdef QT_BUILD_INTERNAL - class tst_QCoapReply : public QObject { Q_OBJECT @@ -145,21 +143,6 @@ void tst_QCoapReply::abortRequest() QVERIFY(arguments.at(0).toByteArray() == "token"); } -#else - -class tst_QCoapReply : public QObject -{ - Q_OBJECT - -private slots: - void initTestCase() - { - QSKIP("Not an internal build, nothing to test"); - } -}; - -#endif - QTEST_MAIN(tst_QCoapReply) #include "tst_qcoapreply.moc" -- cgit v1.2.3