aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorAdrien Leravat <aleravat@witekio.com>2018-08-24 04:42:50 +0000
committerSona Kurazyan <sona.kurazyan@qt.io>2018-12-14 12:20:26 +0000
commit2b3755c8e6587c6a720dd8dcfaafca6e2ba755a9 (patch)
tree12c2287fba0edee83e5153ca6ab4574d14d78f23 /examples
parentaeb607e972af3be78cc25435a52a1fbd900fb8af (diff)
Add the CoAP module, providing a client for Qt
Features: - Send GET/POST/PUT/DELETE requests - Discover resources (single server) - Observe resources and cancel the observation - Blockwise requests and replies - Requests (and replies) can be confirmable or non-confirmable - Some options can be added to the request - Replies can be received in a separate or piggybacked message Change-Id: I31e0e20a4f19bdc6d6489281fde73a4ff848eda4 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'examples')
-rw-r--r--examples/examples.pro5
-rw-r--r--examples/testapp/coaphandler.cpp117
-rw-r--r--examples/testapp/coaphandler.h71
-rw-r--r--examples/testapp/main.cpp75
-rw-r--r--examples/testapp/testapp.pro14
5 files changed, 282 insertions, 0 deletions
diff --git a/examples/examples.pro b/examples/examples.pro
new file mode 100644
index 0000000..0490212
--- /dev/null
+++ b/examples/examples.pro
@@ -0,0 +1,5 @@
+TEMPLATE = subdirs
+CONFIG += no_docs_target
+
+SUBDIRS += \
+ testapp
diff --git a/examples/testapp/coaphandler.cpp b/examples/testapp/coaphandler.cpp
new file mode 100644
index 0000000..1e50117
--- /dev/null
+++ b/examples/testapp/coaphandler.cpp
@@ -0,0 +1,117 @@
+/****************************************************************************
+**
+** 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 "coaphandler.h"
+
+#include <QDebug>
+#include <QCoapClient>
+#include <QCoapReply>
+#include <QCoapDiscoveryReply>
+
+CoapHandler::CoapHandler(QObject *parent) : QObject(parent)
+{
+ connect(&m_coapClient, &QCoapClient::finished, this, &CoapHandler::onFinished);
+ connect(&m_coapClient, &QCoapClient::error, this, &CoapHandler::onError);
+}
+
+bool CoapHandler::runGet(const QUrl &url)
+{
+ return m_coapClient.get(url);
+}
+
+bool CoapHandler::runPost(const QUrl &url)
+{
+ return m_coapClient.post(url);
+}
+
+bool CoapHandler::runPut(const QUrl &url)
+{
+ return m_coapClient.put(url);
+}
+
+bool CoapHandler::runDelete(const QUrl &url)
+{
+ return m_coapClient.deleteResource(url);
+}
+
+bool CoapHandler::runObserve(const QUrl &url)
+{
+ QCoapReply *observeReply = m_coapClient.observe(url);
+ if (!observeReply)
+ return false;
+
+ connect(observeReply, &QCoapReply::notified, this, &CoapHandler::onNotified);
+ return true;
+}
+
+bool CoapHandler::runDiscover(const QUrl &url)
+{
+ QCoapDiscoveryReply *discoverReply = m_coapClient.discover(url);
+ if (discoverReply)
+ return false;
+
+ connect(discoverReply, &QCoapDiscoveryReply::discovered, this, &CoapHandler::onDiscovered);
+ return true;
+}
+
+void CoapHandler::onFinished(QCoapReply *reply)
+{
+ if (reply->errorReceived() == QtCoap::NoError)
+ qDebug() << "Request finished with payload: " << reply->readAll();
+ else
+ qDebug() << "Request failed";
+
+ // Don't forget to remove the reply
+ reply->deleteLater();
+}
+
+void CoapHandler::onNotified(QCoapReply *reply, QCoapMessage message)
+{
+ Q_UNUSED(message)
+
+ // You can alternatively use `message.payload();`
+ qDebug() << "Received Observe notification with payload: " << reply->readAll();
+}
+
+void CoapHandler::onDiscovered(QCoapDiscoveryReply *reply, QVector<QCoapResource> resources)
+{
+ Q_UNUSED(reply)
+
+ for (const QCoapResource &res : qAsConst(resources))
+ qDebug() << "Discovered resource: " << res.path() << res.title();
+}
+
+void CoapHandler::onError(QCoapReply *reply, QtCoap::Error error)
+{
+ if (reply)
+ qDebug() << "CoAP reply error: " << reply->errorString();
+ else
+ qDebug() << "CoAP error: " << error;
+}
diff --git a/examples/testapp/coaphandler.h b/examples/testapp/coaphandler.h
new file mode 100644
index 0000000..fd64eb6
--- /dev/null
+++ b/examples/testapp/coaphandler.h
@@ -0,0 +1,71 @@
+/****************************************************************************
+**
+** 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 COAPHANDLER_H
+#define COAPHANDLER_H
+
+#include <QObject>
+#include <QCoapClient>
+#include <QCoapMessage>
+#include <QCoapResource>
+#include <qcoapnamespace.h>
+
+QT_BEGIN_NAMESPACE
+
+class QCoapReply;
+class QCoapDiscoveryReply;
+class QCoapResource;
+
+QT_END_NAMESPACE
+
+class CoapHandler : public QObject
+{
+ Q_OBJECT
+public:
+ explicit CoapHandler(QObject *parent = nullptr);
+
+ bool runGet(const QUrl &url);
+ bool runPost(const QUrl &url);
+ bool runPut(const QUrl &url);
+ bool runDelete(const QUrl &url);
+ bool runObserve(const QUrl &url);
+ bool runDiscover(const QUrl &url);
+
+public Q_SLOTS:
+ void onFinished(QCoapReply *reply);
+ void onNotified(QCoapReply *reply, QCoapMessage message);
+ void onDiscovered(QCoapDiscoveryReply *reply, QVector<QCoapResource> resources);
+ void onError(QCoapReply *reply, QtCoap::Error error);
+
+private:
+ QCoapClient m_coapClient;
+};
+
+#endif // COAPHANDLER_H
diff --git a/examples/testapp/main.cpp b/examples/testapp/main.cpp
new file mode 100644
index 0000000..096277c
--- /dev/null
+++ b/examples/testapp/main.cpp
@@ -0,0 +1,75 @@
+/****************************************************************************
+**
+** 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 "coaphandler.h"
+
+#include <QCoreApplication>
+#include <QCommandLineParser>
+#include <QUrl>
+
+int main(int argc, char *argv[])
+{
+ QCoreApplication a(argc, argv);
+
+ QCommandLineParser parser;
+ parser.setApplicationDescription("Qt CoAP test application");
+ parser.addHelpOption();
+ parser.addPositionalArgument("url",
+ "The targeted coap resource URL, e.g. 'coap://10.10.10.10/myresource'");
+ parser.addOption({"get", "Use GET method for the request. This is the default method"});
+ parser.addOption({"post", "Use POST method for the request"});
+ parser.addOption({"put", "Use PUT method for the request"});
+ parser.addOption({"delete", "Use DELETE method for the request"});
+ parser.addOption({"observe", "Observe a resource"});
+ parser.addOption({"discover", "Discover available resources"});
+
+ parser.process(a);
+
+ if (parser.positionalArguments().isEmpty()) {
+ qWarning("Please provide an url for the request");
+ parser.showHelp(1);
+ }
+
+ QUrl url = parser.positionalArguments().first();
+ if (!url.isValid()) {
+ qWarning("The url provided is not valid.");
+ parser.showHelp(1);
+ }
+
+ CoapHandler handler;
+ if (parser.isSet("get")) handler.runGet(url);
+ if (parser.isSet("post")) handler.runPost(url);
+ if (parser.isSet("put")) handler.runPut(url);
+ if (parser.isSet("delete")) handler.runDelete(url);
+ if (parser.isSet("observe")) handler.runObserve(url);
+ if (parser.isSet("discover")) handler.runDiscover(url);
+
+ return a.exec();
+}
diff --git a/examples/testapp/testapp.pro b/examples/testapp/testapp.pro
new file mode 100644
index 0000000..d41641b
--- /dev/null
+++ b/examples/testapp/testapp.pro
@@ -0,0 +1,14 @@
+QT -= gui
+QT += network coap
+
+TARGET = testapp
+
+HEADERS += \
+ coaphandler.h
+
+SOURCES += main.cpp \
+ coaphandler.cpp
+
+# install
+target.path = $$[QT_INSTALL_EXAMPLES]/testapp
+INSTALLS += target