forked from emsesp/EMS-ESP32
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPacket.h
155 lines (140 loc) · 4.69 KB
/
Packet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
Copyright (c) 2022 Bert Melis. All rights reserved.
This work is licensed under the terms of the MIT license.
For a copy, see <https://opensource.org/licenses/MIT> or
the LICENSE file.
*/
#pragma once
#include <stdint.h>
#include <stddef.h>
#include "Constants.h"
#include "../Config.h"
#include "../TypeDefs.h"
#include "../Helpers.h"
#include "../Logging.h"
#include "RemainingLength.h"
#include "String.h"
namespace espMqttClientInternals {
class Packet {
public:
~Packet();
size_t available(size_t index);
const uint8_t* data(size_t index) const;
size_t size() const;
void setDup();
uint16_t packetId() const;
MQTTPacketType packetType() const;
bool removable() const;
protected:
uint16_t _packetId; // save as separate variable: will be accessed frequently
uint8_t* _data;
size_t _size;
// variables for chunked payload handling
size_t _payloadIndex;
size_t _payloadStartIndex;
size_t _payloadEndIndex;
espMqttClientTypes::PayloadCallback _getPayload;
struct SubscribeItem {
const char* topic;
uint8_t qos;
};
public:
// CONNECT
Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references)
bool cleanSession,
const char* username,
const char* password,
const char* willTopic,
bool willRetain,
uint8_t willQos,
const uint8_t* willPayload,
uint16_t willPayloadLength,
uint16_t keepAlive,
const char* clientId);
// PUBLISH
Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references)
uint16_t packetId,
const char* topic,
const uint8_t* payload,
size_t payloadLength,
uint8_t qos,
bool retain);
Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references)
uint16_t packetId,
const char* topic,
espMqttClientTypes::PayloadCallback payloadCallback,
size_t payloadLength,
uint8_t qos,
bool retain);
// SUBSCRIBE
Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references)
uint16_t packetId,
const char* topic,
uint8_t qos);
template<typename ... Args>
Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references)
uint16_t packetId,
const char* topic1,
uint8_t qos1,
const char* topic2,
uint8_t qos2,
Args&& ... args)
: _packetId(packetId)
, _data(nullptr)
, _size(0)
, _payloadIndex(0)
, _payloadStartIndex(0)
, _payloadEndIndex(0)
, _getPayload(nullptr) {
static_assert(sizeof...(Args) % 2 == 0, "Subscribe should be in topic/qos pairs");
size_t numberTopics = 2 + (sizeof...(Args) / 2);
SubscribeItem list[numberTopics] = {topic1, qos1, topic2, qos2, args...};
_createSubscribe(error, list, numberTopics);
}
// UNSUBSCRIBE
Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references)
uint16_t packetId,
const char* topic);
template<typename ... Args>
Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references)
uint16_t packetId,
const char* topic1,
const char* topic2,
Args&& ... args)
: _packetId(packetId)
, _data(nullptr)
, _size(0)
, _payloadIndex(0)
, _payloadStartIndex(0)
, _payloadEndIndex(0)
, _getPayload(nullptr) {
size_t numberTopics = 2 + sizeof...(Args);
const char* list[numberTopics] = {topic1, topic2, args...};
_createUnsubscribe(error, list, numberTopics);
}
// PUBACK, PUBREC, PUBREL, PUBCOMP
Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references)
MQTTPacketType type,
uint16_t packetId);
// PING, DISCONN
explicit Packet(espMqttClientTypes::Error& error, // NOLINT(runtime/references)
MQTTPacketType type);
private:
// pass remainingLength = total size - header - remainingLengthLength!
bool _allocate(size_t remainingLength, bool check = true);
// fills header and returns index of next available byte in buffer
size_t _fillPublishHeader(uint16_t packetId,
const char* topic,
size_t remainingLength,
uint8_t qos,
bool retain);
void _createSubscribe(espMqttClientTypes::Error& error, // NOLINT(runtime/references)
SubscribeItem* list,
size_t numberTopics);
void _createUnsubscribe(espMqttClientTypes::Error& error, // NOLINT(runtime/references)
const char** list,
size_t numberTopics);
size_t _chunkedAvailable(size_t index);
const uint8_t* _chunkedData(size_t index) const;
};
} // end namespace espMqttClientInternals