add extra {} to SubscribeItem list[1]

This commit is contained in:
proddy
2024-02-14 14:46:10 +01:00
parent a35486ec24
commit b6accb8d02

View File

@@ -15,15 +15,19 @@ Packet::~Packet() {
}
size_t Packet::available(size_t index) {
if (index >= _size) return 0;
if (!_getPayload) return _size - index;
if (index >= _size)
return 0;
if (!_getPayload)
return _size - index;
return _chunkedAvailable(index);
}
const uint8_t * Packet::data(size_t index) const {
if (!_getPayload) {
if (!_data) return nullptr;
if (index >= _size) return nullptr;
if (!_data)
return nullptr;
if (index >= _size)
return nullptr;
return &_data[index];
}
return _chunkedData(index);
@@ -34,9 +38,12 @@ size_t Packet::size() const {
}
void Packet::setDup() {
if (!_data) return;
if (packetType() != PacketType.PUBLISH) return;
if (_packetId == 0) return;
if (!_data)
return;
if (packetType() != PacketType.PUBLISH)
return;
if (_packetId == 0)
return;
_data[0] |= 0x08;
}
@@ -45,13 +52,16 @@ uint16_t Packet::packetId() const {
}
MQTTPacketType Packet::packetType() const {
if (_data) return static_cast<MQTTPacketType>(_data[0] & 0xF0);
if (_data)
return static_cast<MQTTPacketType>(_data[0] & 0xF0);
return static_cast<MQTTPacketType>(0);
}
bool Packet::removable() const {
if (_packetId == 0) return true;
if ((packetType() == PacketType.PUBACK) || (packetType() == PacketType.PUBCOMP)) return true;
if (_packetId == 0)
return true;
if ((packetType() == PacketType.PUBACK) || (packetType() == PacketType.PUBCOMP))
return true;
return false;
}
@@ -89,15 +99,12 @@ Packet::Packet(espMqttClientTypes::Error& error,
}
// Calculate size
size_t remainingLength =
6 + // protocol
size_t remainingLength = 6 + // protocol
1 + // protocol level
1 + // connect flags
2 + // keepalive
2 + strlen(clientId) +
(willTopic ? 2 + strlen(willTopic) + 2 + willPayloadLength : 0) +
(username ? 2 + strlen(username) : 0) +
(password ? 2 + strlen(password) : 0);
2 + strlen(clientId) + (willTopic ? 2 + strlen(willTopic) + 2 + willPayloadLength : 0) + (username ? 2 + strlen(username) : 0)
+ (password ? 2 + strlen(password) : 0);
// allocate memory
if (!_allocate(remainingLength, false)) {
@@ -114,12 +121,16 @@ Packet::Packet(espMqttClientTypes::Error& error,
pos += encodeString(PROTOCOL, &_data[pos]);
_data[pos++] = PROTOCOL_LEVEL;
uint8_t connectFlags = 0;
if (cleanSession) connectFlags |= espMqttClientInternals::ConnectFlag.CLEAN_SESSION;
if (username != nullptr) connectFlags |= espMqttClientInternals::ConnectFlag.USERNAME;
if (password != nullptr) connectFlags |= espMqttClientInternals::ConnectFlag.PASSWORD;
if (cleanSession)
connectFlags |= espMqttClientInternals::ConnectFlag.CLEAN_SESSION;
if (username != nullptr)
connectFlags |= espMqttClientInternals::ConnectFlag.USERNAME;
if (password != nullptr)
connectFlags |= espMqttClientInternals::ConnectFlag.PASSWORD;
if (willTopic != nullptr) {
connectFlags |= espMqttClientInternals::ConnectFlag.WILL;
if (willRetain) connectFlags |= espMqttClientInternals::ConnectFlag.WILL_RETAIN;
if (willRetain)
connectFlags |= espMqttClientInternals::ConnectFlag.WILL_RETAIN;
switch (willQos) {
case 0:
connectFlags |= espMqttClientInternals::ConnectFlag.WILL_QOS0;
@@ -148,19 +159,15 @@ Packet::Packet(espMqttClientTypes::Error& error,
pos += willPayloadLength;
}
// credentials
if (username != nullptr) pos += encodeString(username, &_data[pos]);
if (password != nullptr) encodeString(password, &_data[pos]);
if (username != nullptr)
pos += encodeString(username, &_data[pos]);
if (password != nullptr)
encodeString(password, &_data[pos]);
error = espMqttClientTypes::Error::SUCCESS;
}
Packet::Packet(espMqttClientTypes::Error& error,
uint16_t packetId,
const char* topic,
const uint8_t* payload,
size_t payloadLength,
uint8_t qos,
bool retain)
Packet::Packet(espMqttClientTypes::Error & error, uint16_t packetId, const char * topic, const uint8_t * payload, size_t payloadLength, uint8_t qos, bool retain)
: _packetId(packetId)
, _data(nullptr)
, _size(0)
@@ -168,8 +175,7 @@ Packet::Packet(espMqttClientTypes::Error& error,
, _payloadStartIndex(0)
, _payloadEndIndex(0)
, _getPayload(nullptr) {
size_t remainingLength =
2 + strlen(topic) + // topic length + topic
size_t remainingLength = 2 + strlen(topic) + // topic length + topic
2 + // packet ID
payloadLength;
@@ -205,8 +211,7 @@ Packet::Packet(espMqttClientTypes::Error& error,
, _payloadStartIndex(0)
, _payloadEndIndex(0)
, _getPayload(payloadCallback) {
size_t remainingLength =
2 + strlen(topic) + // topic length + topic
size_t remainingLength = 2 + strlen(topic) + // topic length + topic
2 + // packet ID
payloadLength;
@@ -239,7 +244,7 @@ Packet::Packet(espMqttClientTypes::Error& error, uint16_t packetId, const char*
, _payloadStartIndex(0)
, _payloadEndIndex(0)
, _getPayload(nullptr) {
SubscribeItem list[1] = {topic, qos};
SubscribeItem list[1] = {{topic, qos}};
_createSubscribe(error, list, 1);
}
@@ -317,16 +322,13 @@ bool Packet::_allocate(size_t remainingLength, bool check) {
return true;
}
size_t Packet::_fillPublishHeader(uint16_t packetId,
const char* topic,
size_t remainingLength,
uint8_t qos,
bool retain) {
size_t Packet::_fillPublishHeader(uint16_t packetId, const char * topic, size_t remainingLength, uint8_t qos, bool retain) {
size_t index = 0;
// FIXED HEADER
_data[index] = PacketType.PUBLISH;
if (retain) _data[index] |= HeaderFlag.PUBLISH_RETAIN;
if (retain)
_data[index] |= HeaderFlag.PUBLISH_RETAIN;
if (qos == 0) {
_data[index++] |= HeaderFlag.PUBLISH_QOS0;
} else if (qos == 1) {
@@ -346,9 +348,7 @@ size_t Packet::_fillPublishHeader(uint16_t packetId,
return index;
}
void Packet::_createSubscribe(espMqttClientTypes::Error& error,
SubscribeItem* list,
size_t numberTopics) {
void Packet::_createSubscribe(espMqttClientTypes::Error & error, SubscribeItem * list, size_t numberTopics) {
// Calculate size
size_t payload = 0;
for (size_t i = 0; i < numberTopics; ++i) {
@@ -376,9 +376,7 @@ void Packet::_createSubscribe(espMqttClientTypes::Error& error,
error = espMqttClientTypes::Error::SUCCESS;
}
void Packet::_createUnsubscribe(espMqttClientTypes::Error& error,
const char** list,
size_t numberTopics) {
void Packet::_createUnsubscribe(espMqttClientTypes::Error & error, const char ** list, size_t numberTopics) {
// Calculate size
size_t payload = 0;
for (size_t i = 0; i < numberTopics; ++i) {