SNode.C
Loading...
Searching...
No Matches
FixedHeader.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/FixedHeader.h"
21
22#ifndef DOXYGEN_SHOULD_SKIP_THIS
23
24#endif // DOXYGEN_SHOULD_SKIP_THIS
25
26namespace iot::mqtt {
27
30
31 FixedHeader::FixedHeader(uint8_t type, uint8_t flags, uint32_t remainingLength) {
32 this->typeFlags = static_cast<uint8_t>((type << 4) | (flags & 0x0F));
33 this->remainingLength = remainingLength;
34 }
35
38
39 std::size_t FixedHeader::deserialize(MqttContext* mqttContext) {
40 std::size_t consumed = 0;
41
42 switch (state) {
43 case 0:
44 consumed += typeFlags.deserialize(mqttContext);
45
46 if (!typeFlags.isComplete()) {
47 break;
48 }
49 state++;
50 [[fallthrough]];
51 case 1:
52 consumed += remainingLength.deserialize(mqttContext);
53
54 complete = remainingLength.isComplete();
55 error = remainingLength.isError();
56
57 break;
58 }
59
60 return consumed;
61 }
62
63 uint8_t FixedHeader::getType() const {
64 return static_cast<uint8_t>(typeFlags >> 0x04);
65 }
66
67 uint8_t FixedHeader::getFlags() const {
68 return static_cast<uint8_t>(typeFlags & 0x0F);
69 }
70
71 void FixedHeader::setRemainingLength(uint32_t remainingLength) {
72 this->remainingLength = remainingLength;
73 }
74
75 uint32_t FixedHeader::getRemainingLength() const {
76 return remainingLength;
77 }
78
79 bool FixedHeader::isComplete() const {
80 return complete;
81 }
82
83 bool FixedHeader::isError() const {
84 return error;
85 }
86
87 std::vector<char> FixedHeader::serialize() const {
88 std::vector<char> packet = typeFlags.serialize();
89
90 std::vector<char> tmpVector = remainingLength.serialize();
91 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
92
93 return packet;
94 }
95
97 typeFlags.reset();
98 remainingLength.reset();
99
100 complete = false;
101 error = false;
102
103 state = 0;
104 }
105
106} // namespace iot::mqtt
uint8_t getType() const
FixedHeader(uint8_t type, uint8_t flags, uint32_t remainingLength=0)
uint32_t getRemainingLength() const
uint8_t getFlags() const
void setRemainingLength(uint32_t remainingLength)
std::vector< char > serialize() const
std::size_t deserialize(iot::mqtt::MqttContext *mqttContext)