Handle changes to the MQTT class API.

This commit is contained in:
Jonathan Naylor
2023-07-05 11:37:33 +01:00
parent fddabee1b9
commit 9b9b70eec7
4 changed files with 33 additions and 22 deletions

View File

@@ -355,7 +355,7 @@ int CMMDVMHost::run()
::LogInitialise(m_conf.getLogDisplayLevel(), m_conf.getLogMQTTLevel()); ::LogInitialise(m_conf.getLogDisplayLevel(), m_conf.getLogMQTTLevel());
std::vector<std::pair<std::string, void (*)(const std::string&)>> subscriptions; std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>> subscriptions;
subscriptions.push_back(std::make_pair("display", CMMDVMHost::onDisplay)); subscriptions.push_back(std::make_pair("display", CMMDVMHost::onDisplay));
subscriptions.push_back(std::make_pair("command", CMMDVMHost::onCommand)); subscriptions.push_back(std::make_pair("command", CMMDVMHost::onCommand));
@@ -3621,24 +3621,27 @@ void CMMDVMHost::writeJSONMessage(const std::string& message)
WriteJSON("MMDVM", json); 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(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(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(host != NULL);
assert(message != NULL);
host->writeSerial(message); host->writeSerial(message, length);
} }

View File

@@ -221,7 +221,7 @@ private:
#if defined(USE_AX25) #if defined(USE_AX25)
bool createAX25Network(); bool createAX25Network();
#endif #endif
void writeSerial(const std::string& message); void writeSerial(const unsigned char* message, unsigned int length);
void remoteControl(const std::string& commandString); void remoteControl(const std::string& commandString);
void processModeCommand(unsigned char mode, unsigned int timeout); void processModeCommand(unsigned char mode, unsigned int timeout);
@@ -235,8 +235,8 @@ private:
void writeJSONMode(const std::string& mode); void writeJSONMode(const std::string& mode);
void writeJSONMessage(const std::string& message); void writeJSONMessage(const std::string& message);
static void onDisplay(const std::string& message); static void onDisplay(const unsigned char* message, unsigned int length);
static void onCommand(const std::string& command); static void onCommand(const unsigned char* command, unsigned int length);
}; };
#endif #endif

View File

@@ -23,7 +23,7 @@
#include <cstring> #include <cstring>
CMQTTConnection::CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const std::vector<std::pair<std::string, void (*)(const std::string&)>>& subs, unsigned int keepalive, MQTT_QOS qos) : CMQTTConnection::CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>>& subs, unsigned int keepalive, MQTT_QOS qos) :
m_host(host), m_host(host),
m_port(port), m_port(port),
m_name(name), m_name(name),
@@ -145,16 +145,24 @@ void CMQTTConnection::onConnect(mosquitto* mosq, void* obj, int rc)
CMQTTConnection* p = static_cast<CMQTTConnection*>(obj); CMQTTConnection* p = static_cast<CMQTTConnection*>(obj);
p->m_connected = true; p->m_connected = true;
for (std::vector<std::pair<std::string, void (*)(const std::string&)>>::const_iterator it = p->m_subs.cbegin(); it != p->m_subs.cend(); ++it) { for (std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>>::const_iterator it = p->m_subs.cbegin(); it != p->m_subs.cend(); ++it) {
std::string topic = (*it).first; std::string topic = (*it).first;
char topicEx[100U]; if (topic.find_first_of('/') == std::string::npos) {
::sprintf(topicEx, "%s/%s", p->m_name.c_str(), topic.c_str()); 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); rc = ::mosquitto_subscribe(mosq, NULL, topicEx, static_cast<int>(p->m_qos));
if (rc != MOSQ_ERR_SUCCESS) { if (rc != MOSQ_ERR_SUCCESS) {
::fprintf(stderr, "MQTT: error subscribing to %s - %s\n", topicEx, ::mosquitto_strerror(rc)); ::fprintf(stderr, "MQTT: error subscribing to %s - %s\n", topicEx, ::mosquitto_strerror(rc));
::mosquitto_disconnect(mosq); ::mosquitto_disconnect(mosq);
}
} else {
rc = ::mosquitto_subscribe(mosq, NULL, topic.c_str(), static_cast<int>(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<CMQTTConnection*>(obj); CMQTTConnection* p = static_cast<CMQTTConnection*>(obj);
for (std::vector<std::pair<std::string, void (*)(const std::string&)>>::const_iterator it = p->m_subs.cbegin(); it != p->m_subs.cend(); ++it) { for (std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>>::const_iterator it = p->m_subs.cbegin(); it != p->m_subs.cend(); ++it) {
std::string topic = (*it).first; std::string topic = (*it).first;
char topicEx[100U]; char topicEx[100U];
::sprintf(topicEx, "%s/%s", p->m_name.c_str(), topic.c_str()); ::sprintf(topicEx, "%s/%s", p->m_name.c_str(), topic.c_str());
if (::strcmp(topicEx, message->topic) == 0) { if (::strcmp(topicEx, message->topic) == 0) {
(*it).second(std::string((char*)message->payload, message->payloadlen)); (*it).second((unsigned char*)message->payload, message->payloadlen);
break; break;
} }
} }

View File

@@ -32,7 +32,7 @@ enum MQTT_QOS {
class CMQTTConnection { class CMQTTConnection {
public: public:
CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const std::vector<std::pair<std::string, void (*)(const std::string&)>>& 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<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>>& subs, unsigned int keepalive, MQTT_QOS qos = MQTT_QOS_EXACTLY_ONCE);
~CMQTTConnection(); ~CMQTTConnection();
bool open(); bool open();
@@ -47,7 +47,7 @@ private:
std::string m_host; std::string m_host;
unsigned short m_port; unsigned short m_port;
std::string m_name; std::string m_name;
std::vector<std::pair<std::string, void (*)(const std::string&)>> m_subs; std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>> m_subs;
unsigned int m_keepalive; unsigned int m_keepalive;
MQTT_QOS m_qos; MQTT_QOS m_qos;
mosquitto* m_mosq; mosquitto* m_mosq;