Skip to content

Commit

Permalink
Revert the possibility to pass char array to subscribe, publish and u…
Browse files Browse the repository at this point in the history
…nsubscribe

It was a bad idea as the buffer stored in the callback list get deleted if it come from a String object that is deleted after the call of subscribe.
  • Loading branch information
plapointe6 committed Feb 21, 2019
1 parent 71c7e4b commit 191549e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 34 deletions.
45 changes: 15 additions & 30 deletions src/EspMQTTClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,20 @@ bool EspMQTTClient::isConnected() const
return mWifiConnected && mMqttConnected;
}

void EspMQTTClient::publish(const char topic[], const char payload[], bool retain)
void EspMQTTClient::publish(const String &topic, const String &payload, bool retain)
{
mMqttClient->publish(topic, payload, retain);
mMqttClient->publish(topic.c_str(), payload.c_str(), retain);

if (mEnableSerialLogs)
Serial.printf("MQTT - Message sent [%s] %s \n", topic, payload);
}

void EspMQTTClient::publish(const String &topic, const String &payload, bool retain)
{
publish(topic.c_str(), payload.c_str(), retain);
Serial.printf("MQTT - Message sent [%s] %s \n", topic.c_str(), payload.c_str());
}

void EspMQTTClient::subscribe(const char topic[], MessageReceivedCallback messageReceivedCallback)
void EspMQTTClient::subscribe(const String &topic, MessageReceivedCallback messageReceivedCallback)
{
mMqttClient->subscribe(topic);
mMqttClient->subscribe(topic.c_str());

if (mEnableSerialLogs)
Serial.printf("MQTT - subscribed to %s \n", topic);
Serial.printf("MQTT - subscribed to %s \n", topic.c_str());

if (mTopicSubscriptionListSize < MAX_TOPIC_SUBSCRIPTION_LIST_SIZE)
{
Expand All @@ -159,46 +154,36 @@ void EspMQTTClient::subscribe(const char topic[], MessageReceivedCallback messag
Serial.println("ERROR - EspMQTTClient::subscribe - Max callback size reached.");
}

void EspMQTTClient::subscribe(const String &topic, MessageReceivedCallback messageReceivedCallback)
{
subscribe(topic.c_str(), messageReceivedCallback);
}

void EspMQTTClient::unsubscribe(const char topic[])
void EspMQTTClient::unsubscribe(const String &topic)
{
bool found = false;

for (int i = 0; i < mTopicSubscriptionListSize; i++)
for (int i = 0; i < mTopicSubscriptionListSize; i++)
{
if (!found)
if (!found)
{
if (strcmp(mTopicSubscriptionList[i].topic, topic) == 0)
if (mTopicSubscriptionList[i].topic.equals(topic))
{
found = true;
mMqttClient->unsubscribe(topic);
mMqttClient->unsubscribe(topic.c_str());
if (mEnableSerialLogs)
Serial.printf("MQTT - unsubscribed to %s \n", topic);
Serial.printf("MQTT - unsubscribed to %s \n", topic.c_str());
}
}

if (found)
if (found)
{
if ((i + 1) < MAX_TOPIC_SUBSCRIPTION_LIST_SIZE)
mTopicSubscriptionList[i] = mTopicSubscriptionList[i + 1];
}
}

if (found)
if (found)
mTopicSubscriptionListSize--;
else if (mEnableSerialLogs)
Serial.println("ERROR - EspMQTTClient::unsubscribe - topic not found.");
}

void EspMQTTClient::unsubscribe(const String &topic)
{
unsubscribe(topic.c_str());
}

void EspMQTTClient::executeDelayed(const long delay, DelayedExecutionCallback callback)
{
if(mDelayedExecutionListSize < MAX_DELAYED_EXECUTION_LIST_SIZE)
Expand Down Expand Up @@ -300,7 +285,7 @@ void EspMQTTClient::mqttMessageReceivedCallback(char* topic, byte* payload, unsi
// Send the message to subscribers
for (int i = 0 ; i < mTopicSubscriptionListSize ; i++)
{
if (strcmp(mTopicSubscriptionList[i].topic, topic) == 0)
if (mTopicSubscriptionList[i].topic.equals(topic))
(*mTopicSubscriptionList[i].callback)(payloadStr); // Call the callback
}
}
5 changes: 1 addition & 4 deletions src/EspMQTTClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class EspMQTTClient
PubSubClient* mMqttClient;

struct TopicSubscription {
const char* topic;
String topic;
MessageReceivedCallback callback;
};
TopicSubscription mTopicSubscriptionList[MAX_TOPIC_SUBSCRIPTION_LIST_SIZE];
Expand All @@ -69,13 +69,10 @@ class EspMQTTClient
void loop();
bool isConnected() const;

void publish(const char topic[], const char payload[], bool retain = false);
void publish(const String &topic, const String &payload, bool retain = false);
void subscribe(const char topic[], MessageReceivedCallback messageReceivedCallback);
void subscribe(const String &topic, MessageReceivedCallback messageReceivedCallback);

//Unsubscribes from the topic, if it exists, and removes it from the CallbackList.
void unsubscribe(const char topic[]);
void unsubscribe(const String &topic);

void executeDelayed(const long delay, DelayedExecutionCallback callback);
Expand Down

0 comments on commit 191549e

Please sign in to comment.