1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qmqttmessage.h"
5#include "qmqttmessage_p.h"
6
7QT_BEGIN_NAMESPACE
8
9/*!
10 \class QMqttMessage
11
12 \inmodule QtMqtt
13 \brief The QMqttMessage class provides information about a message received
14 from a message broker based on a subscription.
15
16 An MQTT message is created inside the module and returned via the
17 \l QMqttSubscription::messageReceived() signal.
18*/
19
20/*!
21 \property QMqttMessage::topic
22 \brief This property holds the topic of a message.
23
24 In case a wildcard has been used for a subscription, describes the topic
25 matching this subscription. This property never contains wildcards.
26*/
27
28/*!
29 \property QMqttMessage::payload
30 \brief This property holds the payload of a message.
31*/
32
33/*!
34 \property QMqttMessage::id
35 \brief This property holds the ID of the message.
36
37 IDs are used for messages with a QoS level above zero.
38*/
39
40/*!
41 \property QMqttMessage::qos
42 \brief This property holds the QoS level of a message.
43*/
44
45/*!
46 \property QMqttMessage::duplicate
47 \brief This property holds whether the message is a duplicate.
48
49 Duplicate messages indicate that the message has been sent earlier, but it
50 has not been confirmed yet. Hence, the broker assumes that it needs to
51 resend to verify the transport of the message itself. Duplicate messages
52 can only occur if the QoS level is one or two.
53*/
54
55/*!
56 \property QMqttMessage::retain
57 \brief This property holds whether the message has been retained.
58
59 A retained message is kept on the broker for future clients to subscribe.
60 Consequently, a retained message has been created previously and is not a
61 live update. A broker can store only one retained message per topic.
62*/
63
64/*!
65 Creates a new MQTT message.
66*/
67QMqttMessage::QMqttMessage()
68 : d(new QMqttMessagePrivate())
69{
70}
71
72/*!
73 Constructs a new MQTT message that is a copy of \a other.
74*/
75QMqttMessage::QMqttMessage(const QMqttMessage &other)
76 : d(other.d)
77{
78}
79
80QMqttMessage::~QMqttMessage()
81{
82}
83
84/*!
85 Makes this object a copy of \a other and returns the new value of this object.
86*/
87QMqttMessage &QMqttMessage::operator=(const QMqttMessage &other)
88{
89 d = other.d;
90 return *this;
91}
92
93/*!
94 Returns \c true if the message and \a other are equal, otherwise returns \c false.
95*/
96bool QMqttMessage::operator==(const QMqttMessage &other) const
97{
98 return d == other.d;
99}
100
101/*!
102 Returns \c true if the message and \a other are not equal, otherwise returns \c false.
103*/
104bool QMqttMessage::operator!=(const QMqttMessage &other) const
105{
106 return !(*this == other);
107}
108
109const QByteArray &QMqttMessage::payload() const
110{
111 return d->m_payload;
112}
113
114quint8 QMqttMessage::qos() const
115{
116 return d->m_qos;
117}
118
119quint16 QMqttMessage::id() const
120{
121 return d->m_id;
122}
123
124QMqttTopicName QMqttMessage::topic() const
125{
126 return d->m_topic;
127}
128
129bool QMqttMessage::duplicate() const
130{
131 return d->m_duplicate;
132}
133
134bool QMqttMessage::retain() const
135{
136 return d->m_retain;
137}
138
139/*!
140 \since 5.12
141
142 Returns the publish properties received as part of the message.
143
144 \note This function only specifies the properties when a
145 publish message is received. Messages with a QoS value of
146 1 or 2 can contain additional properties when a message is released.
147 Those can be obtained by the QMqttClient::messageStatusChanged signal.
148
149 \note This function will only provide valid data when the client
150 specifies QMqttClient::MQTT_5_0 as QMqttClient::ProtocolVersion.
151*/
152QMqttPublishProperties QMqttMessage::publishProperties() const
153{
154 return d->m_publishProperties;
155}
156
157/*!
158 \internal
159 Constructs a new MQTT message with \a content on the topic \a topic.
160 Furthermore the properties \a id, \a qos, \a dup, \a retain must be specified.
161
162 This constructor is mostly used internally to construct a QMqttMessage at message
163 receival.
164*/
165QMqttMessage::QMqttMessage(const QMqttTopicName &topic, const QByteArray &content, quint16 id, quint8 qos, bool dup, bool retain)
166 : d(new QMqttMessagePrivate)
167{
168 d->m_topic = topic;
169 d->m_payload = content;
170 d->m_id = id;
171 d->m_qos = qos;
172 d->m_duplicate = dup;
173 d->m_retain = retain;
174}
175
176QT_END_NAMESPACE
177

source code of qtmqtt/src/mqtt/qmqttmessage.cpp