From 785faa7ad9173cda54f83a4e43683e5b96c4972f Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Fri, 3 Apr 2020 17:59:40 +0100 Subject: [PATCH] First version of the remote commands handler. --- NXDNGateway/Conf.cpp | 25 ++++++++-- NXDNGateway/Conf.h | 15 ++++-- NXDNGateway/NXDNGateway.cpp | 91 ++++++++++++++++++++++++++++++++++++- NXDNGateway/NXDNGateway.ini | 3 ++ NXDNGateway/Version.h | 4 +- 5 files changed, 128 insertions(+), 10 deletions(-) diff --git a/NXDNGateway/Conf.cpp b/NXDNGateway/Conf.cpp index 92cba3c..0056c27 100644 --- a/NXDNGateway/Conf.cpp +++ b/NXDNGateway/Conf.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015,2016,2017,2018 by Jonathan Naylor G4KLX + * Copyright (C) 2015,2016,2017,2018,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 @@ -35,7 +35,8 @@ enum SECTION { SECTION_LOG, SECTION_APRS_FI, SECTION_NETWORK, - SECTION_MOBILE_GPS + SECTION_MOBILE_GPS, + SECTION_REMOTE_COMMANDS }; CConf::CConf(const std::string& file) : @@ -81,7 +82,9 @@ m_networkInactivityTimeout(0U), m_networkDebug(false), m_mobileGPSEnabled(false), m_mobileGPSAddress(), -m_mobileGPSPort(0U) +m_mobileGPSPort(0U), +m_remoteCommandsEnabled(false), +m_remoteCommandsPort(6075U) { } @@ -121,6 +124,8 @@ bool CConf::read() section = SECTION_NETWORK; else if (::strncmp(buffer, "[Mobile GPS]", 12U) == 0) section = SECTION_MOBILE_GPS; + else if (::strncmp(buffer, "[Remote Commands]", 17U) == 0) + section = SECTION_REMOTE_COMMANDS; else section = SECTION_NONE; @@ -230,6 +235,11 @@ bool CConf::read() m_mobileGPSAddress = value; else if (::strcmp(key, "Port") == 0) m_mobileGPSPort = (unsigned int)::atoi(value); + } else if (section == SECTION_REMOTE_COMMANDS) { + if (::strcmp(key, "Enable") == 0) + m_remoteCommandsEnabled = ::atoi(value) == 1; + else if (::strcmp(key, "Port") == 0) + m_remoteCommandsPort = (unsigned int)::atoi(value); } } @@ -448,3 +458,12 @@ unsigned int CConf::getMobileGPSPort() const return m_mobileGPSPort; } +bool CConf::getRemoteCommandsEnabled() const +{ + return m_remoteCommandsEnabled; +} + +unsigned int CConf::getRemoteCommandsPort() const +{ + return m_remoteCommandsPort; +} diff --git a/NXDNGateway/Conf.h b/NXDNGateway/Conf.h index 29dce87..d11307b 100644 --- a/NXDNGateway/Conf.h +++ b/NXDNGateway/Conf.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015,2016,2017,2018 by Jonathan Naylor G4KLX + * Copyright (C) 2015,2016,2017,2018,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 @@ -84,9 +84,13 @@ public: bool getNetworkDebug() const; // The Mobile GPS section - bool getMobileGPSEnabled() const; - std::string getMobileGPSAddress() const; - unsigned int getMobileGPSPort() const; + bool getMobileGPSEnabled() const; + std::string getMobileGPSAddress() const; + unsigned int getMobileGPSPort() const; + + // The Remote Commands section + bool getRemoteCommandsEnabled() const; + unsigned int getRemoteCommandsPort() const; private: std::string m_file; @@ -139,6 +143,9 @@ private: bool m_mobileGPSEnabled; std::string m_mobileGPSAddress; unsigned int m_mobileGPSPort; + + bool m_remoteCommandsEnabled; + unsigned int m_remoteCommandsPort; }; #endif diff --git a/NXDNGateway/NXDNGateway.cpp b/NXDNGateway/NXDNGateway.cpp index b86fb9e..97cdb0b 100644 --- a/NXDNGateway/NXDNGateway.cpp +++ b/NXDNGateway/NXDNGateway.cpp @@ -1,5 +1,5 @@ /* -* Copyright (C) 2016,2017,2018 by Jonathan Naylor G4KLX +* Copyright (C) 2016,2017,2018,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 @@ -27,6 +27,7 @@ #include "Thread.h" #include "Voice.h" #include "Timer.h" +#include "Utils.h" #include "Log.h" #if defined(_WIN32) || defined(_WIN64) @@ -179,6 +180,16 @@ void CNXDNGateway::run() createGPS(); + CUDPSocket* remoteSocket = NULL; + if (m_conf.getRemoteCommandsEnabled()) { + remoteSocket = new CUDPSocket(m_conf.getRemoteCommandsPort()); + ret = remoteSocket->open(); + if (!ret) { + delete remoteSocket; + remoteSocket = NULL; + } + } + CIcomNetwork localNetwork(m_conf.getMyPort(), m_conf.getRptDebug()); ret = localNetwork.open(); if (!ret) { @@ -389,6 +400,79 @@ startupId = 9999U; reflectors.clock(ms); + if (remoteSocket != NULL) { + int res = remoteSocket->read(buffer, 200U, address, port); + if (res > 0) { + buffer[res] = '\0'; + if (::memcmp(buffer + 0U, "TalkGroup", 9U) == 0) { + unsigned int tg = (unsigned int)::atoi((char*)(buffer + 9U)); + + CNXDNReflector* reflector = NULL; + if (tg != 9999U) + reflector = reflectors.find(tg); + + if (reflector == NULL && currentId != 9999U) { + LogMessage("Unlinked from reflector %u by remote command", currentId); + + if (voice != NULL) + voice->unlinked(); + + remoteNetwork.writeUnlink(currentAddr, currentPort, currentId); + remoteNetwork.writeUnlink(currentAddr, currentPort, currentId); + remoteNetwork.writeUnlink(currentAddr, currentPort, currentId); + + inactivityTimer.stop(); + pollTimer.stop(); + lostTimer.stop(); + + currentId = 9999U; + } else if (reflector != NULL && currentId == 9999U) { + currentId = tg; + currentAddr = reflector->m_address; + currentPort = reflector->m_port; + + LogMessage("Linked to reflector %u by remote command", currentId); + + if (voice != NULL) + voice->linkedTo(currentId); + + remoteNetwork.writePoll(currentAddr, currentPort, currentId); + remoteNetwork.writePoll(currentAddr, currentPort, currentId); + remoteNetwork.writePoll(currentAddr, currentPort, currentId); + + inactivityTimer.start(); + pollTimer.start(); + lostTimer.start(); + } else if (reflector != NULL && currentId != 9999U) { + LogMessage("Unlinked from reflector %u by remote command", currentId); + + remoteNetwork.writeUnlink(currentAddr, currentPort, currentId); + remoteNetwork.writeUnlink(currentAddr, currentPort, currentId); + remoteNetwork.writeUnlink(currentAddr, currentPort, currentId); + + currentId = tg; + currentAddr = reflector->m_address; + currentPort = reflector->m_port; + + LogMessage("Linked to reflector %u by remote command", currentId); + + if (voice != NULL) + voice->linkedTo(currentId); + + remoteNetwork.writePoll(currentAddr, currentPort, currentId); + remoteNetwork.writePoll(currentAddr, currentPort, currentId); + remoteNetwork.writePoll(currentAddr, currentPort, currentId); + + inactivityTimer.start(); + pollTimer.start(); + lostTimer.start(); + } + } else { + CUtils::dump("Invalid remote command received", buffer, res); + } + } + } + if (voice != NULL) voice->clock(ms); @@ -469,6 +553,11 @@ startupId = 9999U; delete voice; + if (remoteSocket != NULL) { + remoteSocket->close(); + delete remoteSocket; + } + localNetwork.close(); remoteNetwork.close(); diff --git a/NXDNGateway/NXDNGateway.ini b/NXDNGateway/NXDNGateway.ini index 884d8bd..3aff870 100644 --- a/NXDNGateway/NXDNGateway.ini +++ b/NXDNGateway/NXDNGateway.ini @@ -57,3 +57,6 @@ Enable=0 Address=127.0.0.1 Port=7834 +[Remote Commands] +Enable=0 +Port=6075 diff --git a/NXDNGateway/Version.h b/NXDNGateway/Version.h index 53badbe..b366ff5 100644 --- a/NXDNGateway/Version.h +++ b/NXDNGateway/Version.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015,2016,2017,2018 by Jonathan Naylor G4KLX + * Copyright (C) 2015,2016,2017,2018,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 @@ -19,6 +19,6 @@ #if !defined(VERSION_H) #define VERSION_H -const char* VERSION = "20180524"; +const char* VERSION = "20200403"; #endif