SNode.C
Loading...
Searching...
No Matches
ControlPacket.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-fast/ControlPacket.h"
21
22#include "iot/mqtt-fast/ControlPacketFactory.h"
23
24#ifndef DOXYGEN_SHOULD_SKIP_THIS
25
26#include <utility>
27
28#endif // DOXYGEN_SHOULD_SKIP_THIS
29
30namespace iot::mqtt_fast {
31
32 ControlPacket::ControlPacket(uint8_t type, uint8_t reserved)
33 : type(type)
34 , reserved(reserved) {
35 }
36
38 : type(controlPacketFactory.getPacketType())
39 , reserved(controlPacketFactory.getPacketFlags())
41 }
42
43 uint8_t ControlPacket::getType() const {
44 return type;
45 }
46
47 uint8_t ControlPacket::getReserved() const {
48 return reserved;
49 }
50
52 return data.getLength();
53 }
54
56 std::vector<char> packet;
57
58 packet.push_back(static_cast<char>((type << 0x04) | (reserved & 0x0F)));
59
60 uint64_t remainingLength = data.getLength();
61 do {
62 uint8_t encodedByte = static_cast<uint8_t>(remainingLength % 0x80);
63 remainingLength /= 0x80;
64 if (remainingLength > 0) {
65 encodedByte |= 0x80;
66 }
67 packet.push_back(static_cast<char>(encodedByte));
68 } while (remainingLength > 0);
69
70 packet.insert(packet.end(), data.getValue().begin(), data.getValue().end());
71
72 return packet;
73 }
74
75 bool ControlPacket::isError() const {
76 return error;
77 }
78
80 return data.getValue();
81 }
82
84 return data.getInt8();
85 }
86
87 uint16_t ControlPacket::getInt16() {
88 return data.getInt16();
89 }
90
91 uint32_t ControlPacket::getInt32() {
92 return data.getInt32();
93 }
94
95 uint64_t ControlPacket::getInt64() {
96 return data.getInt64();
97 }
98
99 uint32_t ControlPacket::getIntV() {
100 return data.getIntV();
101 }
102
103 std::string ControlPacket::getString() {
104 return data.getString();
105 }
106
108 return data.getStringRaw();
109 }
110
112 return data.getUint8ListRaw();
113 }
114
115 void ControlPacket::putInt8(uint8_t value) {
116 data.putInt8(value);
117 }
118
119 void ControlPacket::putInt16(uint16_t value) {
120 data.putInt16(value);
121 }
122
123 void ControlPacket::putInt32(uint32_t value) {
124 data.putInt32(value);
125 }
126
127 void ControlPacket::putInt64(uint64_t value) {
128 data.putInt64(value);
129 }
130
131 void ControlPacket::putIntV(uint32_t value) {
132 data.putIntV(value);
133 }
134
135 void ControlPacket::putString(const std::string& value) {
136 data.putString(value);
137 }
138
139 void ControlPacket::putStringRaw(const std::string& value) {
140 data.putStringRaw(value);
141 }
142
143 void ControlPacket::putUint8ListRaw(const std::list<uint8_t>& value) {
144 data.putUint8ListRaw(value);
145 }
146
148 return data.isError();
149 }
150
151} // namespace iot::mqtt_fast
std::vector< char > getPacket()
ControlPacket(iot::mqtt_fast::ControlPacketFactory &controlPacketFactory)
void putInt16(uint16_t value)
void putString(const std::string &value)
ControlPacket(uint8_t type, uint8_t reserved=0)
void putStringRaw(const std::string &value)
uint64_t getRemainingLength() const
void putInt32(uint32_t value)
std::list< uint8_t > getUint8ListRaw()
void putUint8ListRaw(const std::list< uint8_t > &value)
void putInt64(uint64_t value)
std::vector< char > & getValue()