From 1afe34f5143e0ec003fe0fb9428e809fce1b821e Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Tue, 20 Jun 2023 15:27:56 +0100 Subject: [PATCH] Use MQTT for the remote command handling. --- Conf.cpp | 15 +-- Conf.h | 4 - MMDVM.ini | 3 +- MMDVMHost.cpp | 32 ++---- MMDVMHost.h | 2 +- MMDVMHost.sln | 10 -- Makefile | 8 +- RemoteCommand.cpp | 111 --------------------- RemoteCommand.h | 36 ------- RemoteControl.cpp | 248 +++++++++++++++++++++------------------------- RemoteControl.h | 16 ++- 11 files changed, 132 insertions(+), 353 deletions(-) delete mode 100644 RemoteCommand.cpp delete mode 100644 RemoteCommand.h diff --git a/Conf.cpp b/Conf.cpp index 0a7067d..f6c7678 100644 --- a/Conf.cpp +++ b/Conf.cpp @@ -303,9 +303,7 @@ m_ax25NetworkSpeed(9600U), m_ax25NetworkDebug(false), m_lockFileEnabled(false), m_lockFileName(), -m_remoteControlEnabled(false), -m_remoteControlAddress("127.0.0.1"), -m_remoteControlPort(0U) +m_remoteControlEnabled(false) { } @@ -1031,8 +1029,6 @@ bool CConf::read() } else if (section == SECTION_REMOTE_CONTROL) { if (::strcmp(key, "Enable") == 0) m_remoteControlEnabled = ::atoi(value) == 1; - else if (::strcmp(key, "Port") == 0) - m_remoteControlPort = (unsigned short)::atoi(value); } } @@ -2251,12 +2247,3 @@ bool CConf::getRemoteControlEnabled() const return m_remoteControlEnabled; } -std::string CConf::getRemoteControlAddress() const -{ - return m_remoteControlAddress; -} - -unsigned short CConf::getRemoteControlPort() const -{ - return m_remoteControlPort; -} diff --git a/Conf.h b/Conf.h index 0763386..4be509b 100644 --- a/Conf.h +++ b/Conf.h @@ -329,8 +329,6 @@ public: // The Remote Control section bool getRemoteControlEnabled() const; - std::string getRemoteControlAddress() const; - unsigned short getRemoteControlPort() const; private: std::string m_file; @@ -604,8 +602,6 @@ private: std::string m_lockFileName; bool m_remoteControlEnabled; - std::string m_remoteControlAddress; - unsigned short m_remoteControlPort; }; #endif diff --git a/MMDVM.ini b/MMDVM.ini index 9989fd2..4bb07f5 100644 --- a/MMDVM.ini +++ b/MMDVM.ini @@ -312,5 +312,4 @@ File=/tmp/MMDVM_Active.lck [Remote Control] Enable=0 -Address=127.0.0.1 -Port=7642 + diff --git a/MMDVMHost.cpp b/MMDVMHost.cpp index 3f98358..9d02059 100644 --- a/MMDVMHost.cpp +++ b/MMDVMHost.cpp @@ -769,22 +769,8 @@ int CMMDVMHost::run() } bool remoteControlEnabled = m_conf.getRemoteControlEnabled(); - if (remoteControlEnabled) { - std::string address = m_conf.getRemoteControlAddress(); - unsigned short port = m_conf.getRemoteControlPort(); - - LogInfo("Remote Control Parameters"); - LogInfo(" Address: %s", address.c_str()); - LogInfo(" Port: %hu", port); - - m_remoteControl = new CRemoteControl(this, address, port); - - ret = m_remoteControl->open(); - if (!ret) { - delete m_remoteControl; - m_remoteControl = NULL; - } - } + if (remoteControlEnabled) + m_remoteControl = new CRemoteControl(this, m_mqtt); setMode(MODE_IDLE); @@ -1205,8 +1191,6 @@ int CMMDVMHost::run() m_modem->writeTransparentData(data, len); } - remoteControl(); - unsigned int ms = stopWatch.elapsed(); stopWatch.start(); @@ -1387,10 +1371,7 @@ int CMMDVMHost::run() delete transparentSocket; } - if (m_remoteControl != NULL) { - m_remoteControl->close(); - delete m_remoteControl; - } + delete m_remoteControl; LogInfo("Stopping protocol handlers"); writeJSONMessage("Stopping protocol handlers"); @@ -2515,12 +2496,12 @@ void CMMDVMHost::removeLockFile() const ::remove(m_lockFileName.c_str()); } -void CMMDVMHost::remoteControl() +void CMMDVMHost::remoteControl(const std::string& commandString) { if (m_remoteControl == NULL) return; - REMOTE_COMMAND command = m_remoteControl->getCommand(); + REMOTE_COMMAND command = m_remoteControl->getCommand(commandString); switch (command) { case RCD_MODE_IDLE: m_fixedMode = false; @@ -2805,6 +2786,9 @@ void CMMDVMHost::writeJSONMessage(const std::string& message) void CMMDVMHost::onCommand(const std::string& command) { + assert(host != NULL); + + host->remoteControl(command); } void CMMDVMHost::onDisplay(const std::string& message) diff --git a/MMDVMHost.h b/MMDVMHost.h index b454314..cff69cc 100644 --- a/MMDVMHost.h +++ b/MMDVMHost.h @@ -131,7 +131,7 @@ private: bool createFMNetwork(); bool createAX25Network(); - void remoteControl(); + void remoteControl(const std::string& commandString); void processModeCommand(unsigned char mode, unsigned int timeout); void processEnableCommand(bool& mode, bool enabled); diff --git a/MMDVMHost.sln b/MMDVMHost.sln index 4d9518c..fec801b 100644 --- a/MMDVMHost.sln +++ b/MMDVMHost.sln @@ -5,8 +5,6 @@ VisualStudioVersion = 15.0.28307.271 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MMDVMHost", "MMDVMHost.vcxproj", "{1D34E8C1-CFA5-4D60-B509-9DB58DC4AE92}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "RemoteCommand", "RemoteCommand.vcxproj", "{5A61AB93-58BB-413A-BBD9-A26284CB37AE}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -23,14 +21,6 @@ Global {1D34E8C1-CFA5-4D60-B509-9DB58DC4AE92}.Release|x64.Build.0 = Release|x64 {1D34E8C1-CFA5-4D60-B509-9DB58DC4AE92}.Release|x86.ActiveCfg = Release|Win32 {1D34E8C1-CFA5-4D60-B509-9DB58DC4AE92}.Release|x86.Build.0 = Release|Win32 - {5A61AB93-58BB-413A-BBD9-A26284CB37AE}.Debug|x64.ActiveCfg = Debug|x64 - {5A61AB93-58BB-413A-BBD9-A26284CB37AE}.Debug|x64.Build.0 = Debug|x64 - {5A61AB93-58BB-413A-BBD9-A26284CB37AE}.Debug|x86.ActiveCfg = Debug|Win32 - {5A61AB93-58BB-413A-BBD9-A26284CB37AE}.Debug|x86.Build.0 = Debug|Win32 - {5A61AB93-58BB-413A-BBD9-A26284CB37AE}.Release|x64.ActiveCfg = Release|x64 - {5A61AB93-58BB-413A-BBD9-A26284CB37AE}.Release|x64.Build.0 = Release|x64 - {5A61AB93-58BB-413A-BBD9-A26284CB37AE}.Release|x86.ActiveCfg = Release|Win32 - {5A61AB93-58BB-413A-BBD9-A26284CB37AE}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Makefile b/Makefile index 622e0ef..41c2482 100644 --- a/Makefile +++ b/Makefile @@ -17,21 +17,17 @@ OBJECTS = \ POCSAGNetwork.o QR1676.o RemoteControl.o RS129.o RS241213.o RSSIInterpolator.o SerialPort.o StopWatch.o Sync.o SHA256.o Thread.o \ Timer.o UARTController.o UDPController.o UDPSocket.o UserDB.o UserDBentry.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFNetwork.o YSFPayload.o -all: MMDVMHost RemoteCommand +all: MMDVMHost MMDVMHost: GitVersion.h $(OBJECTS) $(CXX) $(OBJECTS) $(CFLAGS) $(LIBS) -o MMDVMHost -RemoteCommand: Log.o MQTTConnection.o RemoteCommand.o UDPSocket.o - $(CXX) Log.o MQTTConnection.o RemoteCommand.o UDPSocket.o $(CFLAGS) $(LIBS) -o RemoteCommand - %.o: %.cpp $(CXX) $(CFLAGS) -c -o $@ $< .PHONY install: install: all install -m 755 MMDVMHost /usr/local/bin/ - install -m 755 RemoteCommand /usr/local/bin/ .PHONY install-service: install-service: install /etc/MMDVM.ini @@ -56,7 +52,7 @@ uninstall-service: @rm -f /lib/systemd/system/mmdvmhost.service || true clean: - $(RM) MMDVMHost RemoteCommand *.o *.d *.bak *~ GitVersion.h + $(RM) MMDVMHost *.o *.d *.bak *~ GitVersion.h # Export the current git version if the index file exists, else 000... GitVersion.h: diff --git a/RemoteCommand.cpp b/RemoteCommand.cpp deleted file mode 100644 index 7443a0e..0000000 --- a/RemoteCommand.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright (C) 2019,2020 by Jonathan Naylor G4KLX - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ - -#include "RemoteCommand.h" - -#include "UDPSocket.h" -#include "Log.h" - -#include -#include -#include - -const unsigned int BUFFER_LENGTH = 1024U; - -int main(int argc, char** argv) -{ - if (argc < 3) { - ::fprintf(stderr, "Usage: RemoteCommand [argument]\n"); - return 1; - } - - unsigned int port = (unsigned int)::atoi(argv[1]); - std::string cmd = std::string(argv[2]); - - for (int i = 3; i < argc; i++) { - cmd += " "; - cmd += std::string(argv[i]); - } - - if (port == 0U) { - ::fprintf(stderr, "RemoteCommand: invalid port number - %s\n", argv[1]); - return 1; - } - - CRemoteCommand* command = new CRemoteCommand(port); - - return command->send(cmd); -} - -CRemoteCommand::CRemoteCommand(unsigned int port) : -m_port(port) -{ - CUDPSocket::startup(); - - ::LogInitialise(false, ".", "RemoteCommand", 2U, 2U, 2U, false); -} - -CRemoteCommand::~CRemoteCommand() -{ - ::LogFinalise(); - - CUDPSocket::shutdown(); -} - -int CRemoteCommand::send(const std::string& command) -{ - sockaddr_storage addr; - unsigned int addrLen; - char buffer[BUFFER_LENGTH]; - int retStatus = 0; - - if (CUDPSocket::lookup("127.0.0.1", m_port, addr, addrLen) != 0) { - ::fprintf(stderr, "Unable to resolve the address of the host\n"); - return 1; - } - - CUDPSocket socket(0U); - - bool ret = socket.open(addr); - if (!ret) - return 1; - - ret = socket.write((unsigned char*)command.c_str(), command.length(), addr, addrLen); - if (!ret) { - socket.close(); - return 1; - } - - ::fprintf(stdout, "Command sent: \"%s\" to port: %u\n", command.c_str(), m_port); - - std::this_thread::sleep_for(std::chrono::milliseconds(50)); - - int len = socket.read((unsigned char*)buffer, BUFFER_LENGTH, addr, addrLen); - if (len > 0) { - buffer[len] = '\0'; - ::fprintf(stdout, "%s\n", buffer); - } - else - { - retStatus = 1; - } - - socket.close(); - - return retStatus; -} diff --git a/RemoteCommand.h b/RemoteCommand.h deleted file mode 100644 index afefce7..0000000 --- a/RemoteCommand.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (C) 2019 by Jonathan Naylor G4KLX - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ - -#ifndef RemoteCommand_H -#define RemoteCommand_H - -#include - -class CRemoteCommand -{ -public: - CRemoteCommand(unsigned int port); - ~CRemoteCommand(); - - int send(const std::string& command); - -private: - unsigned int m_port; -}; - -#endif diff --git a/RemoteControl.cpp b/RemoteControl.cpp index e9f5429..4928e7f 100644 --- a/RemoteControl.cpp +++ b/RemoteControl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019,2020,2021 by Jonathan Naylor G4KLX + * Copyright (C) 2019,2020,2021,2023 by Jonathan Naylor G4KLX * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,7 +23,6 @@ #include #include #include -#include const unsigned int SET_MODE_ARGS = 2U; const unsigned int ENABLE_ARGS = 2U; @@ -31,159 +30,142 @@ const unsigned int DISABLE_ARGS = 2U; const unsigned int PAGE_ARGS = 3U; const unsigned int CW_ARGS = 2U; -const unsigned int BUFFER_LENGTH = 100U; -CRemoteControl::CRemoteControl(CMMDVMHost *host, const std::string address, unsigned int port) : +CRemoteControl::CRemoteControl(CMMDVMHost *host, CMQTTConnection* mqtt) : m_host(host), -m_socket(address, port), +m_mqtt(mqtt), m_command(RCD_NONE), m_args() { - assert(port > 0U); + assert(host != NULL); + assert(mqtt != NULL); } CRemoteControl::~CRemoteControl() { } -bool CRemoteControl::open() -{ - return m_socket.open(); -} - -REMOTE_COMMAND CRemoteControl::getCommand() +REMOTE_COMMAND CRemoteControl::getCommand(const std::string& command) { m_command = RCD_NONE; m_args.clear(); - char command[BUFFER_LENGTH]; - char buffer[BUFFER_LENGTH * 2]; - std::string replyStr = "OK"; - sockaddr_storage address; - unsigned int addrlen; - int ret = m_socket.read((unsigned char*)buffer, BUFFER_LENGTH, address, addrlen); - if (ret > 0) { - buffer[ret] = '\0'; + std::string reply = "OK"; - // Make a copy of the original command for logging. - ::strcpy(command, buffer); + std::stringstream tokeniser(command); - // Parse the original command into a vector of strings. - char* b = buffer; - char* p = NULL; - while ((p = ::strtok(b, " ")) != NULL) { - b = NULL; - m_args.push_back(std::string(p)); - } + // Parse the original command into a vector of strings. + std::string token; + while (std::getline(tokeniser, token, ' ')) + m_args.push_back(token); - if (m_args.at(0U) == "mode" && m_args.size() >= SET_MODE_ARGS) { - // Mode command is in the form of "mode [|fixed]" - if (m_args.at(1U) == "idle") - m_command = RCD_MODE_IDLE; - else if (m_args.at(1U) == "lockout") - m_command = RCD_MODE_LOCKOUT; - else if (m_args.at(1U) == "d-star") - m_command = RCD_MODE_DSTAR; - else if (m_args.at(1U) == "dmr") - m_command = RCD_MODE_DMR; - else if (m_args.at(1U) == "ysf") - m_command = RCD_MODE_YSF; - else if (m_args.at(1U) == "p25") - m_command = RCD_MODE_P25; - else if (m_args.at(1U) == "nxdn") - m_command = RCD_MODE_NXDN; - else if (m_args.at(1U) == "m17") - m_command = RCD_MODE_M17; - else - replyStr = "KO"; - } else if (m_args.at(0U) == "enable" && m_args.size() >= ENABLE_ARGS) { - if (m_args.at(1U) == "dstar") - m_command = RCD_ENABLE_DSTAR; - else if (m_args.at(1U) == "dmr") - m_command = RCD_ENABLE_DMR; - else if (m_args.at(1U) == "ysf") - m_command = RCD_ENABLE_YSF; - else if (m_args.at(1U) == "p25") - m_command = RCD_ENABLE_P25; - else if (m_args.at(1U) == "nxdn") - m_command = RCD_ENABLE_NXDN; - else if (m_args.at(1U) == "m17") - m_command = RCD_ENABLE_M17; - else if (m_args.at(1U) == "fm") - m_command = RCD_ENABLE_FM; - else if (m_args.at(1U) == "ax25") - m_command = RCD_ENABLE_AX25; - else - replyStr = "KO"; - } else if (m_args.at(0U) == "disable" && m_args.size() >= DISABLE_ARGS) { - if (m_args.at(1U) == "dstar") - m_command = RCD_DISABLE_DSTAR; - else if (m_args.at(1U) == "dmr") - m_command = RCD_DISABLE_DMR; - else if (m_args.at(1U) == "ysf") - m_command = RCD_DISABLE_YSF; - else if (m_args.at(1U) == "p25") - m_command = RCD_DISABLE_P25; - else if (m_args.at(1U) == "nxdn") - m_command = RCD_DISABLE_NXDN; - else if (m_args.at(1U) == "m17") - m_command = RCD_DISABLE_M17; - else if (m_args.at(1U) == "fm") - m_command = RCD_DISABLE_FM; - else if (m_args.at(1U) == "ax25") - m_command = RCD_DISABLE_AX25; - else - replyStr = "KO"; - } else if (m_args.at(0U) == "page" && m_args.size() >= PAGE_ARGS) { - // Page command is in the form of "page " - m_command = RCD_PAGE; - } else if (m_args.at(0U) == "page_bcd" && m_args.size() >= PAGE_ARGS) { - // BCD page command is in the form of "page_bcd " - m_command = RCD_PAGE_BCD; - } else if (m_args.at(0U) == "page_a1" && m_args.size() == 2) { - // Alert1 page command is in the form of "page_a1 " - m_command = RCD_PAGE_A1; - } else if (m_args.at(0U) == "page_a2" && m_args.size() >= PAGE_ARGS) { - // Alert2 page command is in the form of "page_a2 " - m_command = RCD_PAGE_A2; - } else if (m_args.at(0U) == "cw" && m_args.size() >= CW_ARGS) { - // CW command is in the form of "cw " - m_command = RCD_CW; - } else if (m_args.at(0U) == "reload") { - // Reload command is in the form of "reload" - m_command = RCD_RELOAD; - } else if (m_args.at(0U) == "status") { - if (m_host != NULL) { - m_host->buildNetworkStatusString(replyStr); - } else { - replyStr = "KO"; - } - - m_command = RCD_CONNECTION_STATUS; - } else if (m_args.at(0U) == "hosts") { - if (m_host != NULL) { - m_host->buildNetworkHostsString(replyStr); - } else { - replyStr = "KO"; - } - - m_command = RCD_CONFIG_HOSTS; + if (m_args.at(0U) == "mode" && m_args.size() >= SET_MODE_ARGS) { + // Mode command is in the form of "mode [|fixed]" + if (m_args.at(1U) == "idle") + m_command = RCD_MODE_IDLE; + else if (m_args.at(1U) == "lockout") + m_command = RCD_MODE_LOCKOUT; + else if (m_args.at(1U) == "d-star") + m_command = RCD_MODE_DSTAR; + else if (m_args.at(1U) == "dmr") + m_command = RCD_MODE_DMR; + else if (m_args.at(1U) == "ysf") + m_command = RCD_MODE_YSF; + else if (m_args.at(1U) == "p25") + m_command = RCD_MODE_P25; + else if (m_args.at(1U) == "nxdn") + m_command = RCD_MODE_NXDN; + else if (m_args.at(1U) == "m17") + m_command = RCD_MODE_M17; + else + reply = "KO"; + } else if (m_args.at(0U) == "enable" && m_args.size() >= ENABLE_ARGS) { + if (m_args.at(1U) == "dstar") + m_command = RCD_ENABLE_DSTAR; + else if (m_args.at(1U) == "dmr") + m_command = RCD_ENABLE_DMR; + else if (m_args.at(1U) == "ysf") + m_command = RCD_ENABLE_YSF; + else if (m_args.at(1U) == "p25") + m_command = RCD_ENABLE_P25; + else if (m_args.at(1U) == "nxdn") + m_command = RCD_ENABLE_NXDN; + else if (m_args.at(1U) == "m17") + m_command = RCD_ENABLE_M17; + else if (m_args.at(1U) == "fm") + m_command = RCD_ENABLE_FM; + else if (m_args.at(1U) == "ax25") + m_command = RCD_ENABLE_AX25; + else + reply = "KO"; + } else if (m_args.at(0U) == "disable" && m_args.size() >= DISABLE_ARGS) { + if (m_args.at(1U) == "dstar") + m_command = RCD_DISABLE_DSTAR; + else if (m_args.at(1U) == "dmr") + m_command = RCD_DISABLE_DMR; + else if (m_args.at(1U) == "ysf") + m_command = RCD_DISABLE_YSF; + else if (m_args.at(1U) == "p25") + m_command = RCD_DISABLE_P25; + else if (m_args.at(1U) == "nxdn") + m_command = RCD_DISABLE_NXDN; + else if (m_args.at(1U) == "m17") + m_command = RCD_DISABLE_M17; + else if (m_args.at(1U) == "fm") + m_command = RCD_DISABLE_FM; + else if (m_args.at(1U) == "ax25") + m_command = RCD_DISABLE_AX25; + else + reply = "KO"; + } else if (m_args.at(0U) == "page" && m_args.size() >= PAGE_ARGS) { + // Page command is in the form of "page " + m_command = RCD_PAGE; + } else if (m_args.at(0U) == "page_bcd" && m_args.size() >= PAGE_ARGS) { + // BCD page command is in the form of "page_bcd " + m_command = RCD_PAGE_BCD; + } else if (m_args.at(0U) == "page_a1" && m_args.size() == 2) { + // Alert1 page command is in the form of "page_a1 " + m_command = RCD_PAGE_A1; + } else if (m_args.at(0U) == "page_a2" && m_args.size() >= PAGE_ARGS) { + // Alert2 page command is in the form of "page_a2 " + m_command = RCD_PAGE_A2; + } else if (m_args.at(0U) == "cw" && m_args.size() >= CW_ARGS) { + // CW command is in the form of "cw " + m_command = RCD_CW; + } else if (m_args.at(0U) == "reload") { + // Reload command is in the form of "reload" + m_command = RCD_RELOAD; + } else if (m_args.at(0U) == "status") { + if (m_host != NULL) { + m_host->buildNetworkStatusString(reply); } else { - replyStr = "KO"; + reply = "KO"; } - ::snprintf(buffer, BUFFER_LENGTH * 2, "%s remote command of \"%s\" received", ((m_command == RCD_NONE) ? "Invalid" : "Valid"), command); - if (m_command == RCD_NONE) { - m_args.clear(); - LogWarning(buffer); + m_command = RCD_CONNECTION_STATUS; + } else if (m_args.at(0U) == "hosts") { + if (m_host != NULL) { + m_host->buildNetworkHostsString(reply); } else { -#if !defined(REMOTE_COMMAND_NO_LOG) - LogMessage(buffer); -#endif + reply = "KO"; } - - m_socket.write((unsigned char*)replyStr.c_str(), replyStr.length(), address, addrlen); + + m_command = RCD_CONFIG_HOSTS; + } else { + reply = "KO"; } + + char buffer[200U]; + ::snprintf(buffer, 200, "%s remote command of \"%s\" received", ((m_command == RCD_NONE) ? "Invalid" : "Valid"), command.c_str()); + + if (m_command == RCD_NONE) { + m_args.clear(); + LogWarning(buffer); + } else { + LogMessage(buffer); + } + + m_mqtt->publish("command", reply.c_str()); return m_command; } @@ -254,7 +236,3 @@ int CRemoteControl::getArgInt(unsigned int n) const return ::atoi(getArgString(n).c_str()); } -void CRemoteControl::close() -{ - m_socket.close(); -} diff --git a/RemoteControl.h b/RemoteControl.h index a4b2885..4712ff1 100644 --- a/RemoteControl.h +++ b/RemoteControl.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019,2020,2021 by Jonathan Naylor G4KLX + * Copyright (C) 2019,2020,2021,2023 by Jonathan Naylor G4KLX * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,11 +19,11 @@ #ifndef RemoteControl_H #define RemoteControl_H -#include "UDPSocket.h" - #include #include +#include "MQTTConnection.h" + class CMMDVMHost; enum REMOTE_COMMAND { @@ -65,12 +65,10 @@ enum REMOTE_COMMAND { class CRemoteControl { public: - CRemoteControl(class CMMDVMHost *host, const std::string address, unsigned int port); + CRemoteControl(class CMMDVMHost *host, CMQTTConnection* mqtt); ~CRemoteControl(); - bool open(); - - REMOTE_COMMAND getCommand(); + REMOTE_COMMAND getCommand(const std::string& command); unsigned int getArgCount() const; @@ -78,11 +76,9 @@ public: unsigned int getArgUInt(unsigned int n) const; signed int getArgInt(unsigned int n) const; - void close(); - private: CMMDVMHost* m_host; - CUDPSocket m_socket; + CMQTTConnection* m_mqtt; REMOTE_COMMAND m_command; std::vector m_args; };