• Coap
  • Coap Class

    The client connection class to a CoAP server. More...

    Header: #include <Coap>
    Inherits:

    Public Functions

    Coap(QObject * parent = nullptr, const quint16 & port = 5683)
    CoapReply * deleteResource(const CoapRequest & request)
    CoapReply * disableNotifications(const CoapRequest & request)
    CoapReply * enableResourceNotifications(const CoapRequest & request)
    CoapReply * get(const CoapRequest & request)
    CoapReply * ping(const CoapRequest & request)
    CoapReply * post(const CoapRequest & request, const QByteArray & data = QByteArray())
    CoapReply * put(const CoapRequest & request, const QByteArray & data = QByteArray())

    Signals

    void notificationReceived(const CoapObserveResource & resource, const int & notificationNumber, const QByteArray & payload)
    void replyFinished(CoapReply * reply)

    Detailed Description

    The client connection class to a CoAP server.

    The Coap class provides a signal solt based communication with a CoAP (Constrained Application Protocol) server. The API of this class was inspired by the QNetworkAccessManager and was written according to the RFC7252. This class supports also blockwise transfere according to the IETF V18 specifications and observing resources according to the RFC7641.

    Example

    MyClass::MyClass(QObject *parent) :
      QObject(parent)
    {
      Coap *coap = new Coap(this);
      connect(coap, SIGNAL(replyFinished(CoapReply*)), this, SLOT(onReplyFinished(CoapReply*)));
    
      CoapRequest request(QUrl("coap://coap.me/hello"));
      coap->get(request);
    }
    void MyClass::onReplyFinished(CoapReply *reply)
    {
      if (reply->error() != CoapReply::NoError) {
        qWarning() << "Reply finished with error" << reply->errorString();
        reply->deleteLater();
        return;
      }
    
      qDebug() << "Reply finished" << reply;
      reply->deleteLater();
    }

    See also CoapReply and CoapRequest.

    Member Function Documentation

    Coap::Coap(QObject * parent = nullptr, const quint16 & port = 5683)

    Constructs a Coap access manager with the given parent and port.

    CoapReply * Coap::deleteResource(const CoapRequest & request)

    Performs a DELETE request to the CoAP server specified in the given request. Returns a CoapReply to match the response with the request.

    CoapReply * Coap::disableNotifications(const CoapRequest & request)

    Disables notifications (observing) on the CoAP server for the resource specified in the given request. Returns a CoapReply to match the response with the request.

    CoapReply * Coap::enableResourceNotifications(const CoapRequest & request)

    Enables notifications (observing) on the CoAP server for the resource specified in the given request. Returns a CoapReply to match the response with the request.

    CoapReply * Coap::get(const CoapRequest & request)

    Performs a GET request to the CoAP server specified in the given request. Returns a CoapReply to match the response with the request.

    [signal] void Coap::notificationReceived(const CoapObserveResource & resource, const int & notificationNumber, const QByteArray & payload)

    This signal is emitted when a value of an observed resource changed. The notificationNumber specifies the count of the notification to keep the correct order. The value can be parsed from the payload parameter.

    CoapReply * Coap::ping(const CoapRequest & request)

    Performs a ping request to the CoAP server specified in the given request. Returns a CoapReply to match the response with the request.

    CoapReply * Coap::post(const CoapRequest & request, const QByteArray & data = QByteArray())

    Performs a POST request to the CoAP server specified in the given request and data. Returns a CoapReply to match the response with the request.

    CoapReply * Coap::put(const CoapRequest & request, const QByteArray & data = QByteArray())

    Performs a PUT request to the CoAP server specified in the given request and data. Returns a CoapReply to match the response with the request.

    [signal] void Coap::replyFinished(CoapReply * reply)

    This signal is emitted when the given reply is finished.