Use MQTT for the remote command handling.

This commit is contained in:
Jonathan Naylor
2023-06-20 15:27:56 +01:00
parent 0615a4218d
commit 1afe34f514
11 changed files with 132 additions and 353 deletions

View File

@@ -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 <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
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 <mode> [<timeout>|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 <ric> <message>"
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 <ric> <bcd message>"
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 <ric>"
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 <ric> <message>"
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 <message>"
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 <mode> [<timeout>|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 <ric> <message>"
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 <ric> <bcd message>"
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 <ric>"
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 <ric> <message>"
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 <message>"
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();
}