diff --git a/src/mqttclient.cpp b/src/mqttclient.cpp index a4d2564..42eafb8 100644 --- a/src/mqttclient.cpp +++ b/src/mqttclient.cpp @@ -215,9 +215,17 @@ void MqttClient::disconnect() Token MqttClient::publish(const MqttMessage& message, int qos) { - if (hasWildcard(message.topic()) || !isValidTopic(message.topic())) { + if (hasWildcard(message.topic())) + { + LogDebug("[MqttClient::publish]","Topic has wildcard : " + message.topic()); return Token(m_clientId, -1); } + else if(!isValidTopic(message.topic())) + { + LogDebug("[MqttClient::publish]","Topic is invalid : " + message.topic()); + return Token(m_clientId, -1); + } + OSDEV_COMPONENTS_LOCKGUARD(m_interfaceMutex); IMqttClientImpl* client(nullptr); { @@ -239,6 +247,13 @@ Token MqttClient::publish(const MqttMessage& message, int qos) } client = m_principalClient.get(); } + + if(!client) + { + LogDebug("[MqttClient::publish]", "Invalid pointer to IMqttClient retrieved."); + return Token(m_clientId, -1); + } + return Token(client->clientId(), client->publish(message, qos)); } diff --git a/src/mqttutil.cpp b/src/mqttutil.cpp index 4f22c15..a7b3376 100644 --- a/src/mqttutil.cpp +++ b/src/mqttutil.cpp @@ -55,7 +55,12 @@ bool isValidTopic( const std::string &topic ) bool hasWildcard( const std::string &topic ) { - return ( topic.size() > 0 && (topic.find( '+' ) != std::string::npos || topic.size() - 1 == '#' ) ); + return ( topic.size() > 0 && + ( + topic.find( '+' ) != std::string::npos || + topic.back() == '#' + ) + ); } bool testForOverlap( const std::string &existingTopic, const std::string &newTopic )