1 | // Copyright (C) 2019 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #ifndef QCOAPCONNECTION_P_H |
5 | #define QCOAPCONNECTION_P_H |
6 | |
7 | #include <QtCoap/qcoapnamespace.h> |
8 | #include <QtCoap/qcoapsecurityconfiguration.h> |
9 | |
10 | #include <QtCore/qqueue.h> |
11 | #include <QtCore/qobject.h> |
12 | #include <QtNetwork/qabstractsocket.h> |
13 | #include <private/qobject_p.h> |
14 | |
15 | // |
16 | // W A R N I N G |
17 | // ------------- |
18 | // |
19 | // This file is not part of the Qt API. It exists purely as an |
20 | // implementation detail. This header file may change from version to |
21 | // version without notice, or even be removed. |
22 | // |
23 | // We mean it. |
24 | // |
25 | |
26 | QT_BEGIN_NAMESPACE |
27 | |
28 | class QCoapConnectionPrivate; |
29 | class Q_AUTOTEST_EXPORT QCoapConnection : public QObject |
30 | { |
31 | Q_OBJECT |
32 | public: |
33 | enum class ConnectionState : quint8 { |
34 | Unconnected, |
35 | Bound |
36 | }; |
37 | |
38 | explicit QCoapConnection(QtCoap::SecurityMode securityMode = QtCoap::SecurityMode::NoSecurity, |
39 | QObject *parent = nullptr); |
40 | virtual ~QCoapConnection(); |
41 | |
42 | bool isSecure() const; |
43 | QtCoap::SecurityMode securityMode() const; |
44 | ConnectionState state() const; |
45 | QCoapSecurityConfiguration securityConfiguration() const; |
46 | |
47 | Q_INVOKABLE void setSecurityConfiguration(const QCoapSecurityConfiguration &configuration); |
48 | Q_INVOKABLE void disconnect(); |
49 | |
50 | Q_SIGNALS: |
51 | void error(QAbstractSocket::SocketError error); |
52 | void readyRead(const QByteArray &data, const QHostAddress &sender); |
53 | void bound(); |
54 | void securityConfigurationChanged(); |
55 | |
56 | private: |
57 | void startToSendRequest(); |
58 | |
59 | protected: |
60 | QCoapConnection(QObjectPrivate &dd, QObject *parent = nullptr); |
61 | |
62 | virtual void bind(const QString &host, quint16 port) = 0; |
63 | virtual void writeData(const QByteArray &data, const QString &host, quint16 port) = 0; |
64 | virtual void close() = 0; |
65 | |
66 | private: |
67 | friend class QCoapProtocolPrivate; |
68 | |
69 | Q_DECLARE_PRIVATE(QCoapConnection) |
70 | }; |
71 | |
72 | struct CoapFrame { |
73 | QByteArray currentPdu; |
74 | QString host; |
75 | quint16 port = 0; |
76 | |
77 | CoapFrame(const QByteArray &pdu, const QString &hostName, quint16 portNumber) |
78 | : currentPdu(pdu), host(hostName), port(portNumber) {} |
79 | }; |
80 | |
81 | class Q_AUTOTEST_EXPORT QCoapConnectionPrivate : public QObjectPrivate |
82 | { |
83 | public: |
84 | QCoapConnectionPrivate(QtCoap::SecurityMode security = QtCoap::SecurityMode::NoSecurity); |
85 | |
86 | ~QCoapConnectionPrivate() override = default; |
87 | |
88 | void sendRequest(const QByteArray &request, const QString &host, quint16 port); |
89 | |
90 | QCoapSecurityConfiguration securityConfiguration; |
91 | QtCoap::SecurityMode securityMode; |
92 | QCoapConnection::ConnectionState state; |
93 | QQueue<CoapFrame> framesToSend; |
94 | |
95 | Q_DECLARE_PUBLIC(QCoapConnection) |
96 | }; |
97 | |
98 | QT_END_NAMESPACE |
99 | |
100 | #endif // QCOAPCONNECTION_P_H |
101 | |