SNode.C
Loading...
Searching...
No Matches
Publish.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "iot/mqtt/packets/Publish.h"
21
22#ifndef DOXYGEN_SHOULD_SKIP_THIS
23
24#include <vector>
25
26#endif // DOXYGEN_SHOULD_SKIP_THIS
27
28namespace iot::mqtt::packets {
29
31 : iot::mqtt::ControlPacket(MQTT_PUBLISH, "PUBLISH") {
32 }
33
34 Publish::Publish(uint16_t packetIdentifier, const std::string& topic, const std::string& message, uint8_t qoS, bool dup, bool retain)
35 : Publish() {
36 this->flags = static_cast<uint8_t>((dup ? 0x08 : 0x00) | ((qoS << 1) & 0x06) | (retain ? 0x01 : 0x00));
37 this->packetIdentifier = packetIdentifier;
38 this->topic = topic;
39 this->message = message;
40 this->dup = dup;
41 this->qoS = qoS;
42 this->retain = retain;
43 }
44
45 std::vector<char> Publish::serializeVP() const {
46 std::vector<char> packet(topic.serialize());
47
48 if (qoS > 0) {
49 std::vector<char> tmpVector = packetIdentifier.serialize();
50 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
51 }
52
53 std::vector<char> tmpVector = message.serialize();
54 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
55
56 return packet;
57 }
58
59 bool Publish::getDup() const {
60 return dup;
61 }
62
63 uint8_t Publish::getQoS() const {
64 return qoS;
65 }
66
67 uint16_t Publish::getPacketIdentifier() const {
68 return packetIdentifier;
69 }
70
71 std::string Publish::getTopic() const {
72 return topic;
73 }
74
75 std::string Publish::getMessage() const {
76 return message;
77 }
78
79 bool Publish::getRetain() const {
80 return retain;
81 }
82
83} // namespace iot::mqtt::packets
uint8_t getQoS() const
Definition Publish.cpp:63
std::string getTopic() const
Definition Publish.cpp:71
std::string getMessage() const
Definition Publish.cpp:75
Publish(uint16_t packetIdentifier, const std::string &topic, const std::string &message, uint8_t qoS, bool dup, bool retain)
Definition Publish.cpp:34
uint16_t getPacketIdentifier() const
Definition Publish.cpp:67
std::vector< char > serializeVP() const override
Definition Publish.cpp:45
#define MQTT_PUBLISH
Definition Publish.h:36