diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index 221b230..f832d60 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -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; @@ -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() @@ -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) { @@ -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; @@ -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() @@ -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) { @@ -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()) { @@ -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++; @@ -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(); @@ -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()) { @@ -448,9 +419,9 @@ 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); @@ -458,7 +429,7 @@ void MqttClient::poll() 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); @@ -531,7 +502,7 @@ void MqttClient::poll() if (_rxMessageIndex == 2) { _rxMessageTopicLength = (_rxMessageBuffer[0] << 8) | _rxMessageBuffer[1]; _rxLength -= 2; - + _rxMessageTopic = ""; _rxMessageTopic.reserve(_rxMessageTopicLength); @@ -722,7 +693,7 @@ int MqttClient::read(uint8_t *buf, size_t size) if (b == -1) { break; - } + } result++; *buf++ = b; @@ -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; @@ -819,7 +779,7 @@ void MqttClient::setTxPayloadSize(unsigned short size) _txPayloadBuffer = NULL; _txPayloadBufferIndex = 0; } - + _tx_payload_buffer_size = size; } diff --git a/src/MqttClient.h b/src/MqttClient.h index 522f023..5b457a9 100644 --- a/src/MqttClient.h +++ b/src/MqttClient.h @@ -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(); @@ -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);