Qt CoAP client module
Go to file
Tarja Sundqvist d7e7e6b041 Merge tag 'v6.2.12-lts' into tqtc/lts-6.2-opensource
Qt 6.2.12-lts release

Conflicts solved:
	dependencies.yaml

Change-Id: Ia8fbe6cd12a777c2a999d64d09417c8fdf89fe65
2025-01-28 17:40:14 +02:00
coin Coin: Add alias to non tqtc module 2022-11-23 10:24:07 +02:00
dist Add changes file for Qt 5.15.1 2020-08-26 05:40:24 +00:00
examples Switch examples to build as isolated sub-builds part 2 2022-02-05 03:35:20 +00:00
src Merge tag 'v6.2.10-lts' into tqtc/lts-6.2-opensource 2024-10-04 14:08:19 +03:00
tests CMake: Bump almost all cmake_minimum_required calls to 3.16 2021-09-22 19:10:06 +00:00
.cmake.conf Bump version to 6.2.12 2024-01-17 22:05:53 +02:00
.gitattributes Add missing .gitattributes and .tag files 2021-03-31 10:45:59 +03:00
.gitignore Add the CoAP module, providing a client for Qt 2018-12-14 12:20:26 +00:00
.qmake.conf Bump version to 6.2.12 2024-01-17 22:05:53 +02:00
.tag Add missing .gitattributes and .tag files 2021-03-31 10:45:59 +03:00
CMakeLists.txt CMake: Bump almost all cmake_minimum_required calls to 3.16 2021-09-22 19:10:06 +00:00
LICENSE.FDL Add docs license file 2019-03-27 11:26:29 +00:00
LICENSE.GPL3 Add license file 2019-03-19 19:50:45 +00:00
LICENSE.GPL3-EXCEPT Add missing license file 2021-12-02 17:54:01 +00:00
README.md Rename QCoapDiscoveryReply -> QCoapResourceDiscoveryReply 2019-05-14 15:15:05 +00:00
conanfile.py Conan: Inherit recipe class from QtLeafModule for common functionality 2021-08-26 06:54:25 +00:00
dependencies.yaml Update dependencies on 'tqtc/lts-6.2-opensource' in qt/tqtc-qtcoap 2025-01-28 09:35:50 +00:00
sync.profile Add the CoAP module, providing a client for Qt 2018-12-14 12:20:26 +00:00

README.md

Introduction

This is the Qt CoAP module repository. CoAP is a protocol for IoT devices, and machine to machine communication. The full specification can be found in RFC 7252.

Supported features

  • CoAP Client
  • Send GET/POST/PUT/DELETE requests
  • Discover resources (single server)
  • Observe resources and cancel the observation
  • Blockwise requests and replies
  • Confirmable and non-confirmable messages
  • Some options can be added to the request
  • Replies can be received in a separate or piggybacked message

Unsupported yet

  • CoAP Server

How to use the library

GET/POST/PUT/DELETE requests

QCoapClient* client = new QCoapClient(this);
connect(client, &QCoapClient::finished, this, &MyClass::onFinished);
client->get(QUrl("coap://coap.me/test"));
client->put(QUrl("coap://coap.me/test"), QByteArray("payload"));

or

QCoapReply* reply = client->get(QCoapRequest("coap://coap.me/test"));
connect(reply, &QCoapReply::finished, this, &MyClass::onFinished);

The slot connected to the QCoapReply::finished(QCoapReply *) signal can use the QCoapReply object like a QIODevice object.

OBSERVE requests

Observe requests are used to receive automatic server notifications for a resource. For Observe requests specifically, you can use the QCoapReply::notified(QCoapReply *, QCoapMessage) signal to handle notifications from the CoAP server.

QCoapRequest request = QCoapRequest("coap://coap.me/obs");
QCoapReply* reply = client->observe(request);
connect(reply, &QCoapReply::notified, this, &MyClass::onNotified);

You can then stop the observation with

client->cancelObserve(reply);

The notified signal will provide the QCoapReply and most recent message.

DISCOVERY requests

For machine to machine communication, CoAP Discovery requests is used to query the resources available to an endpoint, or to the complete network.

QCoapResourceDiscoveryReply *reply = client->discover("coap://coap.me/");
connect(reply, &QCoapReply::discovered, this, &MyClass::onDiscovered);

For multicast discovery use one of the groups from the QtCoap::MulticastGroup enum, instead of specifying the discovery path:

QCoapResourceDiscoveryReply *reply = client->discover(QtCoap::AllCoapNodesIPv6LinkLocal);

If no group is specified, QtCoap::AllCoapNodesIPv4 will be used by default.

The signal discovered can be triggered multiple times, and will provide the list of resources returned by the server(s).

Using security

The following example code can be used to secure CoAP communication:

QCoapClient* client = new QCoapClient(this, QtCoap::PreSharedKey);
QCoapSecurityConfiguration config;
config.setPreSharedKey("secretPSK");
config.setIdentity("Client_identity");
client->setSecurityConfiguration(config);

To use X.509 certificate-based security use QtCoap::Certificate for the security mode. QtCoap::RawPublicKey mode is not supported yet.

Automated tests

Automated tests require a Californium plugtest server. Plugtest is a CoAP server used to test the main features of the CoAP protocol. The following command starts a plugtest server using Docker.

docker run --name coap-test-server -d --rm -p 5683:5683/udp aleravat/coap-test-server:latest

Automated tests require COAP_TEST_SERVER_IP environment variable to be set, containing Plugtest server IP address. This address will be used to connect to the Plugtest server on port 5683.

The IP address of the docker container can found identified by:

  1. Retrieve the container id with docker ps
$ docker ps
CONTAINER ID        IMAGE                                           [...]
826073e84e7f        aleravat/coap-test-server:latest                [...]
  1. Get the IP address with docker inspect <container_id> | grep IPAddress
$ docker inspect 826073e84e7f | grep IPAddress
[...]
        "IPAddress": "172.17.0.3",
[...]
  1. Set the environment variable in QtCreator, or in a the terminal used export COAP_TEST_SERVER_IP="172.17.0.3"