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.
* client = new QCoapClient(this);
QCoapClientconnect(client, &QCoapClient::finished, this, &MyClass::onFinished);
->get(QUrl("coap://coap.me/test"));
client->put(QUrl("coap://coap.me/test"), QByteArray("payload")); client
or
* reply = client->get(QCoapRequest("coap://coap.me/test"));
QCoapReplyconnect(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 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("coap://coap.me/obs");
QCoapRequest request * reply = client->observe(request);
QCoapReplyconnect(reply, &QCoapReply::notified, this, &MyClass::onNotified);
You can then stop the observation with
->cancelObserve(reply); client
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.
*reply = client->discover("coap://coap.me/");
QCoapResourceDiscoveryReply 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:
*reply = client->discover(QtCoap::AllCoapNodesIPv6LinkLocal); QCoapResourceDiscoveryReply
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).
The following example code can be used to secure CoAP communication:
* client = new QCoapClient(this, QtCoap::PreSharedKey);
QCoapClient;
QCoapSecurityConfiguration config.setPreSharedKey("secretPSK");
config.setIdentity("Client_identity");
config->setSecurityConfiguration(config); client
To use X.509 certificate-based security use
QtCoap::Certificate
for the security mode.
QtCoap::RawPublicKey
mode is not supported yet.
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 [...]
docker inspect <container_id> | grep IPAddress
$ docker inspect 826073e84e7f | grep IPAddress
[...]
"IPAddress": "172.17.0.3",
[...]
export COAP_TEST_SERVER_IP="172.17.0.3"