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/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#include "iot/mqtt-fast/ControlPacket.h"
43
44#include "iot/mqtt-fast/ControlPacketFactory.h"
45
46#ifndef DOXYGEN_SHOULD_SKIP_THIS
47
48#include <utility>
49
50#endif // DOXYGEN_SHOULD_SKIP_THIS
51
52namespace iot::mqtt_fast {
53
54 ControlPacket::ControlPacket(uint8_t type, uint8_t reserved)
55 : type(type)
56 , reserved(reserved) {
57 }
58
60 : type(controlPacketFactory.getPacketType())
61 , reserved(controlPacketFactory.getPacketFlags())
62 , data(std::move(controlPacketFactory.getPacket())) {
63 }
64
65 uint8_t ControlPacket::getType() const {
66 return type;
67 }
68
69 uint8_t ControlPacket::getReserved() const {
70 return reserved;
71 }
72
74 return data.getLength();
75 }
76
77 std::vector<char> ControlPacket::getPacket() {
78 std::vector<char> packet;
79
80 packet.push_back(static_cast<char>((type << 0x04) | (reserved & 0x0F)));
81
82 uint64_t remainingLength = data.getLength();
83 do {
84 uint8_t encodedByte = static_cast<uint8_t>(remainingLength % 0x80);
85 remainingLength /= 0x80;
86 if (remainingLength > 0) {
87 encodedByte |= 0x80;
88 }
89 packet.push_back(static_cast<char>(encodedByte));
90 } while (remainingLength > 0);
91
92 packet.insert(packet.end(), data.getValue().begin(), data.getValue().end());
93
94 return packet;
95 }
96
97 bool ControlPacket::isError() const {
98 return error;
99 }
100
101 std::vector<char>& ControlPacket::getValue() {
102 return data.getValue();
103 }
104
106 return data.getInt8();
107 }
108
110 return data.getInt16();
111 }
112
114 return data.getInt32();
115 }
116
118 return data.getInt64();
119 }
120
121 uint32_t ControlPacket::getIntV() {
122 return data.getIntV();
123 }
124
125 std::string ControlPacket::getString() {
126 return data.getString();
127 }
128
130 return data.getStringRaw();
131 }
132
133 std::list<uint8_t> ControlPacket::getUint8ListRaw() {
135 }
136
137 void ControlPacket::putInt8(uint8_t value) {
138 data.putInt8(value);
139 }
140
141 void ControlPacket::putInt16(uint16_t value) {
142 data.putInt16(value);
143 }
144
145 void ControlPacket::putInt32(uint32_t value) {
146 data.putInt32(value);
147 }
148
149 void ControlPacket::putInt64(uint64_t value) {
150 data.putInt64(value);
151 }
152
153 void ControlPacket::putIntV(uint32_t value) {
154 data.putIntV(value);
155 }
156
157 void ControlPacket::putString(const std::string& value) {
158 data.putString(value);
159 }
160
161 void ControlPacket::putStringRaw(const std::string& value) {
163 }
164
165 void ControlPacket::putUint8ListRaw(const std::list<uint8_t>& value) {
167 }
168
170 return data.isError();
171 }
172
173} // namespace iot::mqtt_fast
iot::mqtt_fast::types::Binary & getPacket()
std::vector< char > getPacket()
ControlPacket(iot::mqtt_fast::ControlPacketFactory &controlPacketFactory)
void putInt16(uint16_t value)
void putString(const std::string &value)
iot::mqtt_fast::types::Binary data
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()
void putUint8ListRaw(const std::list< uint8_t > &values)
Definition Binary.cpp:269
void putInt64(uint64_t value)
Definition Binary.cpp:230
std::list< uint8_t > getUint8ListRaw()
Definition Binary.cpp:194
void putStringRaw(const std::string &string)
Definition Binary.cpp:263
void putInt8(uint8_t value)
Definition Binary.cpp:208
void putInt32(uint32_t value)
Definition Binary.cpp:221
std::vector< char > & getValue()
Definition Binary.cpp:76
void putString(const std::string &string)
Definition Binary.cpp:256
std::vector< char >::size_type getLength() const
Definition Binary.cpp:62
void putIntV(uint32_t value)
Definition Binary.cpp:243
void putInt16(uint16_t value)
Definition Binary.cpp:214