This repository has been archived on 2026-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
Michael Zanetti 57048dd6e9 Add support for CoIoT
CoIoT is a shelly specific extensions to CoAP:
It adds a new Request code which is not part of the CoAP spec
as well as using CoAP multicast (which is part of CoAP).

This commit
* renames "statusCode" to "reqRspCode" which describes
the actual field more precisely as it is in fact a Request or Response
code, not a status code.
* Allows joining multicast groups, by default using the CoAP specified
multicast address.
* Allows setting custom Request codes in requests, as well as
processing the PDUs Request/response code by the client.
2022-06-30 10:16:20 +02:00

122 lines
4.8 KiB
C++

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2013 - 2020, nymea GmbH
* Contact: contact@nymea.io
*
* This file is part of nymea.
* This project including source code and documentation is protected by
* copyright law, and remains the property of nymea GmbH. All rights, including
* reproduction, publication, editing and translation, are reserved. The use of
* this project is subject to the terms of a license agreement to be concluded
* with nymea GmbH in accordance with the terms of use of nymea GmbH, available
* under https://nymea.io/license
*
* GNU Lesser General Public License Usage
* Alternatively, this project may be redistributed and/or modified under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; version 3. This project is distributed in the hope that
* it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this project. If not, see <https://www.gnu.org/licenses/>.
*
* For any further details and any questions please contact us under
* contact@nymea.io or see our FAQ/Licensing Information on
* https://nymea.io/license/faq
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef COAP_H
#define COAP_H
#include <QObject>
#include <QHostInfo>
#include <QUdpSocket>
#include <QHostAddress>
#include <QLoggingCategory>
#include <QPointer>
#include <QQueue>
#include "libnymea.h"
#include "coaprequest.h"
#include "coapreply.h"
#include "coapobserveresource.h"
/* Information about CoAP
*
* The Constrained Application Protocol (CoAP) : https://tools.ietf.org/html/rfc7252
* Blockwise transfers in CoAP : https://tools.ietf.org/html/draft-ietf-core-block-18
* Constrained RESTful Environments (CoRE) Link Format : http://tools.ietf.org/html/rfc6690
* Observing Resources in CoAP : https://tools.ietf.org/html/rfc7641
*
*/
class LIBNYMEA_EXPORT Coap : public QObject
{
Q_OBJECT
public:
Coap(QObject *parent = nullptr, const quint16 &port = 5683);
CoapReply *ping(const CoapRequest &request);
CoapReply *get(const CoapRequest &request);
CoapReply *put(const CoapRequest &request, const QByteArray &data = QByteArray());
CoapReply *post(const CoapRequest &request, const QByteArray &data = QByteArray());
CoapReply *deleteResource(const CoapRequest &request);
CoapReply *customRequest(CoapPdu::ReqRspCode requestCode, const CoapRequest &request, const QByteArray &data = QByteArray());
// Notifications for observable resources
CoapReply *enableResourceNotifications(const CoapRequest &request);
CoapReply *disableNotifications(const CoapRequest &request);
bool joinMulticastGroup(const QHostAddress &address = QHostAddress("224.0.1.187"));
bool leaveMulticastGroup(const QHostAddress &address = QHostAddress("224.0.1.187"));
private:
QUdpSocket *m_socket;
QPointer<CoapReply> m_reply;
QQueue<CoapReply *> m_replyQueue;
QHash<int, CoapReply *> m_runningHostLookups;
QHash<QByteArray, CoapObserveResource> m_observeResources; // token | resource
// Blockwise notifications
QPointer<CoapReply> m_observerReply;
QHash<CoapReply *, CoapObserveResource> m_observeReplyResource; // observe reply | resource
QHash<CoapReply *, int> m_observeBlockwise; // observe reply | observe nr.
void lookupHost();
void sendRequest(CoapReply *reply, const bool &lookedUp = false);
void sendData(const QHostAddress &hostAddress, const quint16 &port, const QByteArray &data);
void sendCoapPdu(const QHostAddress &address, const quint16 &port, const CoapPdu &pdu);
void processResponse(const CoapPdu &pdu, const QHostAddress &address, const quint16 &port);
void processIdBasedResponse(CoapReply *reply, const CoapPdu &pdu);
void processTokenBasedResponse(CoapReply *reply, const CoapPdu &pdu);
void processNotification(const CoapPdu &pdu, const QHostAddress &address, const quint16 &port);
void processBlock1Response(CoapReply *reply, const CoapPdu &pdu);
void processBlock2Response(CoapReply *reply, const CoapPdu &pdu);
void processBlock2Notification(CoapReply *reply, const CoapPdu &pdu);
signals:
void replyFinished(CoapReply *reply);
void notificationReceived(const CoapObserveResource &resource, const int &notificationNumber, const QByteArray &payload);
void multicastMessageReceived(const QHostAddress &source, const CoapPdu &pdu);
private slots:
void hostLookupFinished(const QHostInfo &hostInfo);
void onReadyRead();
void onReplyTimeout();
void onReplyFinished();
};
#endif // COAP_H