Skip to content

Cleaning up mqtt client definition #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 20 additions & 60 deletions src/MqttClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ int MqttClient::messageRetain() const
return -1;
}

int MqttClient::beginMessage(const char* topic, unsigned long size, bool retain, uint8_t qos, bool dup)
int MqttClient::beginMessage(const String& topic, unsigned long size, bool retain, uint8_t qos, bool dup)
{
_txMessageTopic = topic;
_txMessageTopic = topic.c_str();
_txMessageRetain = retain;
_txMessageQoS = qos;
_txMessageDup = dup;
Expand All @@ -191,19 +191,9 @@ int MqttClient::beginMessage(const char* topic, unsigned long size, bool retain,
return 1;
}

int MqttClient::beginMessage(const String& topic, unsigned long size, bool retain, uint8_t qos, bool dup)
{
return beginMessage(topic.c_str(), size, retain, qos, dup);
}

int MqttClient::beginMessage(const char* topic, bool retain, uint8_t qos, bool dup)
{
return beginMessage(topic, 0xffffffffL, retain, qos, dup);
}

int MqttClient::beginMessage(const String& topic, bool retain, uint8_t qos, bool dup)
{
return beginMessage(topic.c_str(), retain, qos, dup);
return beginMessage(topic, 0xffffffffL, retain, qos, dup);
}

int MqttClient::endMessage()
Expand Down Expand Up @@ -259,9 +249,9 @@ int MqttClient::endMessage()
return 1;
}

int MqttClient::beginWill(const char* topic, unsigned short size, bool retain, uint8_t qos)
int MqttClient::beginWill(const String& topic, unsigned short size, bool retain, uint8_t qos)
{
int topicLength = strlen(topic);
int topicLength = topic.length();
size_t willLength = (2 + topicLength + 2 + size);

if (qos > 2) {
Expand All @@ -272,7 +262,7 @@ int MqttClient::beginWill(const char* topic, unsigned short size, bool retain, u

_txBuffer = _willBuffer;
_txBufferIndex = 0;
writeString(topic, topicLength);
writeString(topic.c_str(), topic.length());
write16(0); // dummy size for now
_willMessageIndex = _txBufferIndex;

Expand All @@ -284,19 +274,9 @@ int MqttClient::beginWill(const char* topic, unsigned short size, bool retain, u
return 0;
}

int MqttClient::beginWill(const String& topic, unsigned short size, bool retain, uint8_t qos)
{
return beginWill(topic.c_str(), size, retain, qos);
}

int MqttClient::beginWill(const char* topic, bool retain, uint8_t qos)
{
return beginWill(topic, _tx_payload_buffer_size, retain, qos);
}

int MqttClient::beginWill(const String& topic, bool retain, uint8_t qos)
{
return beginWill(topic.c_str(), retain, qos);
return beginWill(topic, _tx_payload_buffer_size, retain, qos);
}

int MqttClient::endWill()
Expand All @@ -314,9 +294,10 @@ int MqttClient::endWill()
return 1;
}

int MqttClient::subscribe(const char* topic, uint8_t qos)
int MqttClient::subscribe(const String& topic, uint8_t qos)

{
int topicLength = strlen(topic);
int topicLength = topic.length();
int remainingLength = topicLength + 5;

if (qos > 2) {
Expand All @@ -334,7 +315,7 @@ int MqttClient::subscribe(const char* topic, uint8_t qos)

beginPacket(MQTT_SUBSCRIBE, 0x02, remainingLength, packetBuffer);
write16(_txPacketId);
writeString(topic, topicLength);
writeString(topic.c_str(), topicLength);
write8(qos);

if (!endPacket()) {
Expand Down Expand Up @@ -362,14 +343,9 @@ int MqttClient::subscribe(const char* topic, uint8_t qos)
return 0;
}

int MqttClient::subscribe(const String& topic, uint8_t qos)
{
return subscribe(topic.c_str(), qos);
}

int MqttClient::unsubscribe(const char* topic)
int MqttClient::unsubscribe(const String& topic)
{
int topicLength = strlen(topic);
int topicLength = topic.length();
int remainingLength = topicLength + 4;

_txPacketId++;
Expand All @@ -382,7 +358,7 @@ int MqttClient::unsubscribe(const char* topic)

beginPacket(MQTT_UNSUBSCRIBE, 0x02, remainingLength, packetBuffer);
write16(_txPacketId);
writeString(topic, topicLength);
writeString(topic.c_str(), topicLength);

if (!endPacket()) {
stop();
Expand All @@ -406,11 +382,6 @@ int MqttClient::unsubscribe(const char* topic)
return 0;
}

int MqttClient::unsubscribe(const String& topic)
{
return unsubscribe(topic.c_str());
}

void MqttClient::poll()
{
if (clientAvailable() == 0 && !clientConnected()) {
Expand Down Expand Up @@ -448,17 +419,17 @@ void MqttClient::poll()
if ((b & 0x80) == 0) { // length done
bool malformedResponse = false;

if (_rxType == MQTT_CONNACK ||
if (_rxType == MQTT_CONNACK ||
_rxType == MQTT_PUBACK ||
_rxType == MQTT_PUBREC ||
_rxType == MQTT_PUBREC ||
_rxType == MQTT_PUBCOMP ||
_rxType == MQTT_UNSUBACK) {
malformedResponse = (_rxFlags != 0x00 || _rxLength != 2);
} else if (_rxType == MQTT_PUBLISH) {
malformedResponse = ((_rxFlags & 0x06) == 0x06);
} else if (_rxType == MQTT_PUBREL) {
malformedResponse = (_rxFlags != 0x02 || _rxLength != 2);
} else if (_rxType == MQTT_SUBACK) {
} else if (_rxType == MQTT_SUBACK) {
malformedResponse = (_rxFlags != 0x00 || _rxLength != 3);
} else if (_rxType == MQTT_PINGRESP) {
malformedResponse = (_rxFlags != 0x00 || _rxLength != 0);
Expand Down Expand Up @@ -531,7 +502,7 @@ void MqttClient::poll()
if (_rxMessageIndex == 2) {
_rxMessageTopicLength = (_rxMessageBuffer[0] << 8) | _rxMessageBuffer[1];
_rxLength -= 2;

_rxMessageTopic = "";
_rxMessageTopic.reserve(_rxMessageTopicLength);

Expand Down Expand Up @@ -722,7 +693,7 @@ int MqttClient::read(uint8_t *buf, size_t size)

if (b == -1) {
break;
}
}

result++;
*buf++ = b;
Expand Down Expand Up @@ -775,22 +746,11 @@ MqttClient::operator bool()
return true;
}

void MqttClient::setId(const char* id)
{
_id = id;
}

void MqttClient::setId(const String& id)
{
_id = id;
}

void MqttClient::setUsernamePassword(const char* username, const char* password)
{
_username = username;
_password = password;
}

void MqttClient::setUsernamePassword(const String& username, const String& password)
{
_username = username;
Expand Down Expand Up @@ -819,7 +779,7 @@ void MqttClient::setTxPayloadSize(unsigned short size)
_txPayloadBuffer = NULL;
_txPayloadBufferIndex = 0;
}

_tx_payload_buffer_size = size;
}

Expand Down
8 changes: 0 additions & 8 deletions src/MqttClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,15 @@ class MqttClient : public Client {
int messageQoS() const;
int messageRetain() const;

int beginMessage(const char* topic, unsigned long size, bool retain = false, uint8_t qos = 0, bool dup = false);
int beginMessage(const String& topic, unsigned long size, bool retain = false, uint8_t qos = 0, bool dup = false);
int beginMessage(const char* topic, bool retain = false, uint8_t qos = 0, bool dup = false);
int beginMessage(const String& topic, bool retain = false, uint8_t qos = 0, bool dup = false);
int endMessage();

int beginWill(const char* topic, unsigned short size, bool retain, uint8_t qos);
int beginWill(const String& topic, unsigned short size, bool retain, uint8_t qos);
int beginWill(const char* topic, bool retain, uint8_t qos);
int beginWill(const String& topic, bool retain, uint8_t qos);
int endWill();

int subscribe(const char* topic, uint8_t qos = 0);
int subscribe(const String& topic, uint8_t qos = 0);
int unsubscribe(const char* topic);
int unsubscribe(const String& topic);

void poll();
Expand All @@ -95,10 +89,8 @@ class MqttClient : public Client {
virtual uint8_t connected();
virtual operator bool();

void setId(const char* id);
void setId(const String& id);

void setUsernamePassword(const char* username, const char* password);
void setUsernamePassword(const String& username, const String& password);

void setCleanSession(bool cleanSession);
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy