From 9b9b70eec7fa9815d6c5dacb0820f6e5e6aac099 Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Wed, 5 Jul 2023 11:37:33 +0100 Subject: [PATCH] Handle changes to the MQTT class API. --- MMDVMHost.cpp | 17 ++++++++++------- MMDVMHost.h | 6 +++--- MQTTConnection.cpp | 28 ++++++++++++++++++---------- MQTTConnection.h | 4 ++-- 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/MMDVMHost.cpp b/MMDVMHost.cpp index d04765b..0c16fca 100644 --- a/MMDVMHost.cpp +++ b/MMDVMHost.cpp @@ -355,7 +355,7 @@ int CMMDVMHost::run() ::LogInitialise(m_conf.getLogDisplayLevel(), m_conf.getLogMQTTLevel()); - std::vector> subscriptions; + std::vector> subscriptions; subscriptions.push_back(std::make_pair("display", CMMDVMHost::onDisplay)); subscriptions.push_back(std::make_pair("command", CMMDVMHost::onCommand)); @@ -3621,24 +3621,27 @@ void CMMDVMHost::writeJSONMessage(const std::string& message) WriteJSON("MMDVM", json); } -void CMMDVMHost::writeSerial(const std::string& message) +void CMMDVMHost::writeSerial(const unsigned char* message, unsigned int length) { assert(m_modem != NULL); + assert(message != NULL); - m_modem->writeSerialData((unsigned char*)message.c_str(), message.length()); + m_modem->writeSerialData(message, length); } -void CMMDVMHost::onCommand(const std::string& command) +void CMMDVMHost::onCommand(const unsigned char* command, unsigned int length) { assert(host != NULL); + assert(command != NULL); - host->remoteControl(command); + host->remoteControl(std::string((char*)command, length)); } -void CMMDVMHost::onDisplay(const std::string& message) +void CMMDVMHost::onDisplay(const unsigned char* message, unsigned int length) { assert(host != NULL); + assert(message != NULL); - host->writeSerial(message); + host->writeSerial(message, length); } diff --git a/MMDVMHost.h b/MMDVMHost.h index fdb8b47..1101bf6 100644 --- a/MMDVMHost.h +++ b/MMDVMHost.h @@ -221,7 +221,7 @@ private: #if defined(USE_AX25) bool createAX25Network(); #endif - void writeSerial(const std::string& message); + void writeSerial(const unsigned char* message, unsigned int length); void remoteControl(const std::string& commandString); void processModeCommand(unsigned char mode, unsigned int timeout); @@ -235,8 +235,8 @@ private: void writeJSONMode(const std::string& mode); void writeJSONMessage(const std::string& message); - static void onDisplay(const std::string& message); - static void onCommand(const std::string& command); + static void onDisplay(const unsigned char* message, unsigned int length); + static void onCommand(const unsigned char* command, unsigned int length); }; #endif diff --git a/MQTTConnection.cpp b/MQTTConnection.cpp index 7af8192..fa951e5 100644 --- a/MQTTConnection.cpp +++ b/MQTTConnection.cpp @@ -23,7 +23,7 @@ #include -CMQTTConnection::CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const std::vector>& subs, unsigned int keepalive, MQTT_QOS qos) : +CMQTTConnection::CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const std::vector>& subs, unsigned int keepalive, MQTT_QOS qos) : m_host(host), m_port(port), m_name(name), @@ -145,16 +145,24 @@ void CMQTTConnection::onConnect(mosquitto* mosq, void* obj, int rc) CMQTTConnection* p = static_cast(obj); p->m_connected = true; - for (std::vector>::const_iterator it = p->m_subs.cbegin(); it != p->m_subs.cend(); ++it) { + for (std::vector>::const_iterator it = p->m_subs.cbegin(); it != p->m_subs.cend(); ++it) { std::string topic = (*it).first; - char topicEx[100U]; - ::sprintf(topicEx, "%s/%s", p->m_name.c_str(), topic.c_str()); + if (topic.find_first_of('/') == std::string::npos) { + char topicEx[100U]; + ::sprintf(topicEx, "%s/%s", p->m_name.c_str(), topic.c_str()); - rc = ::mosquitto_subscribe(mosq, NULL, topicEx, MQTT_QOS_AT_LEAST_ONCE); - if (rc != MOSQ_ERR_SUCCESS) { - ::fprintf(stderr, "MQTT: error subscribing to %s - %s\n", topicEx, ::mosquitto_strerror(rc)); - ::mosquitto_disconnect(mosq); + rc = ::mosquitto_subscribe(mosq, NULL, topicEx, static_cast(p->m_qos)); + if (rc != MOSQ_ERR_SUCCESS) { + ::fprintf(stderr, "MQTT: error subscribing to %s - %s\n", topicEx, ::mosquitto_strerror(rc)); + ::mosquitto_disconnect(mosq); + } + } else { + rc = ::mosquitto_subscribe(mosq, NULL, topic.c_str(), static_cast(p->m_qos)); + if (rc != MOSQ_ERR_SUCCESS) { + ::fprintf(stderr, "MQTT: error subscribing to %s - %s\n", topic.c_str(), ::mosquitto_strerror(rc)); + ::mosquitto_disconnect(mosq); + } } } } @@ -177,14 +185,14 @@ void CMQTTConnection::onMessage(mosquitto* mosq, void* obj, const mosquitto_mess CMQTTConnection* p = static_cast(obj); - for (std::vector>::const_iterator it = p->m_subs.cbegin(); it != p->m_subs.cend(); ++it) { + for (std::vector>::const_iterator it = p->m_subs.cbegin(); it != p->m_subs.cend(); ++it) { std::string topic = (*it).first; char topicEx[100U]; ::sprintf(topicEx, "%s/%s", p->m_name.c_str(), topic.c_str()); if (::strcmp(topicEx, message->topic) == 0) { - (*it).second(std::string((char*)message->payload, message->payloadlen)); + (*it).second((unsigned char*)message->payload, message->payloadlen); break; } } diff --git a/MQTTConnection.h b/MQTTConnection.h index 5481815..8fc98ce 100644 --- a/MQTTConnection.h +++ b/MQTTConnection.h @@ -32,7 +32,7 @@ enum MQTT_QOS { class CMQTTConnection { public: - CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const std::vector>& subs, unsigned int keepalive, MQTT_QOS qos = MQTT_QOS_EXACTLY_ONCE); + CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const std::vector>& subs, unsigned int keepalive, MQTT_QOS qos = MQTT_QOS_EXACTLY_ONCE); ~CMQTTConnection(); bool open(); @@ -47,7 +47,7 @@ private: std::string m_host; unsigned short m_port; std::string m_name; - std::vector> m_subs; + std::vector> m_subs; unsigned int m_keepalive; MQTT_QOS m_qos; mosquitto* m_mosq;