From 9dc484ea64dd86ba8be15632c4876152ac093d08 Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Wed, 21 Feb 2018 20:27:11 +0000 Subject: [PATCH] Start splitting YSF and FCS functionality. --- YSFGateway/DTMF.cpp | 6 +- YSFGateway/WiresX.cpp | 2 +- YSFGateway/WiresX.h | 3 +- YSFGateway/YSFGateway.cpp | 308 +++++++++++++++++++++----------------- YSFGateway/YSFGateway.h | 16 +- 5 files changed, 191 insertions(+), 144 deletions(-) diff --git a/YSFGateway/DTMF.cpp b/YSFGateway/DTMF.cpp index fcc783c..c475f41 100644 --- a/YSFGateway/DTMF.cpp +++ b/YSFGateway/DTMF.cpp @@ -173,6 +173,8 @@ WX_STATUS CDTMF::validate() const if (m_command.at(1U) < '0' || m_command.at(1U) > '9') return WXS_NONE; } + + return WXS_CONNECT_FCS; } else { for (unsigned int i = 1U; i <= 6U; i++) { if (m_command.at(1U) < '0' || m_command.at(1U) > '9') @@ -181,9 +183,9 @@ WX_STATUS CDTMF::validate() const if (m_command == "#99999") return WXS_DISCONNECT; - } - return WXS_CONNECT; + return WXS_CONNECT_YSF; + } } std::string CDTMF::getReflector() diff --git a/YSFGateway/WiresX.cpp b/YSFGateway/WiresX.cpp index 72d9c4f..dce4b25 100644 --- a/YSFGateway/WiresX.cpp +++ b/YSFGateway/WiresX.cpp @@ -294,7 +294,7 @@ WX_STATUS CWiresX::processConnect(const unsigned char* source, const unsigned ch m_status = WXSI_CONNECT; m_timer.start(); - return WXS_CONNECT; + return WXS_CONNECT_YSF; } void CWiresX::processConnect(CYSFReflector* reflector) diff --git a/YSFGateway/WiresX.h b/YSFGateway/WiresX.h index 847e858..e1128cf 100644 --- a/YSFGateway/WiresX.h +++ b/YSFGateway/WiresX.h @@ -27,7 +27,8 @@ enum WX_STATUS { WXS_NONE, - WXS_CONNECT, + WXS_CONNECT_YSF, + WXS_CONNECT_FCS, WXS_DISCONNECT }; diff --git a/YSFGateway/YSFGateway.cpp b/YSFGateway/YSFGateway.cpp index 8b1a79e..168572f 100644 --- a/YSFGateway/YSFGateway.cpp +++ b/YSFGateway/YSFGateway.cpp @@ -80,11 +80,14 @@ m_suffix(), m_conf(configFile), m_gps(NULL), m_wiresX(NULL), -m_dtmf(NULL), +m_dtmf(), m_ysfNetwork(NULL), m_fcsNetwork(NULL), -m_linked(false), -m_exclude(false) +m_linkType(LINK_NONE), +m_exclude(false), +m_inactivityTimer(1000U), +m_lostTimer(1000U, 120U), +m_ysfPollTimer(1000U, 5U) { } @@ -205,20 +208,18 @@ int CYSFGateway::run() return 1; } - CTimer inactivityTimer(1000U, m_conf.getYSFNetworkInactivityTimeout() * 60U); - CTimer lostTimer(1000U, 120U); - CTimer pollTimer(1000U, 5U); + m_inactivityTimer.setTimeout(m_conf.getYSFNetworkInactivityTimeout() * 60U); bool revert = m_conf.getYSFNetworkRevert(); std::string startup = m_conf.getYSFNetworkStartup(); + bool fcsNetworkEnabled = m_conf.getFCSNetworkEnabled(); bool ysfNetworkEnabled = m_conf.getYSFNetworkEnabled(); if (ysfNetworkEnabled) { std::string fileName = m_conf.getYSFNetworkHosts(); unsigned int reloadTime = m_conf.getYSFNetworkReloadTime(); m_wiresX = new CWiresX(m_callsign, m_suffix, &rptNetwork, fileName, reloadTime); - m_dtmf = new CDTMF; std::string name = m_conf.getName(); unsigned int txFrequency = m_conf.getTxFrequency(); @@ -251,12 +252,12 @@ int CYSFGateway::run() m_ysfNetwork->writePoll(); if (!revert) - inactivityTimer.start(); + m_inactivityTimer.start(); - lostTimer.start(); - pollTimer.start(); + m_lostTimer.start(); + m_ysfPollTimer.start(); - m_linked = true; + m_linkType = LINK_YSF; } } } @@ -283,133 +284,52 @@ int CYSFGateway::run() // Don't send out control data m_exclude = (dt == YSF_DT_DATA_FR_MODE); - if (m_wiresX != NULL) { - WX_STATUS status = m_wiresX->process(buffer + 35U, buffer + 14U, fi, dt, fn, ft); - switch (status) { - case WXS_CONNECT: { - m_ysfNetwork->writeUnlink(); - m_ysfNetwork->writeUnlink(); - m_ysfNetwork->writeUnlink(); - - CYSFReflector* reflector = m_wiresX->getReflector(); - LogMessage("Connect to %5.5s - \"%s\" has been requested by %10.10s", reflector->m_id.c_str(), reflector->m_name.c_str(), buffer + 14U); - - m_ysfNetwork->setDestination(reflector->m_address, reflector->m_port); - m_ysfNetwork->writePoll(); - m_ysfNetwork->writePoll(); - m_ysfNetwork->writePoll(); - - inactivityTimer.start(); - lostTimer.start(); - pollTimer.start(); - - m_linked = true; - } - break; - case WXS_DISCONNECT: - LogMessage("Disconnect has been requested by %10.10s", buffer + 14U); - - m_ysfNetwork->writeUnlink(); - m_ysfNetwork->writeUnlink(); - m_ysfNetwork->writeUnlink(); - m_ysfNetwork->clearDestination(); - - inactivityTimer.stop(); - lostTimer.stop(); - pollTimer.stop(); - - m_linked = false; - break; - default: - break; - } - - status = WXS_NONE; - switch (dt) { - case YSF_DT_VD_MODE2: - status = m_dtmf->decodeVDMode2(buffer + 35U, (buffer[34U] & 0x01U) == 0x01U); - break; - default: - break; - } - - switch (status) { - case WXS_CONNECT: { - std::string id = m_dtmf->getReflector(); - CYSFReflector* reflector = m_wiresX->getReflector(id); - if (reflector != NULL) { - m_wiresX->processConnect(reflector); - - if (m_linked) { - m_ysfNetwork->writeUnlink(); - m_ysfNetwork->writeUnlink(); - m_ysfNetwork->writeUnlink(); - } - - LogMessage("Connect via DTMF to %5.5s - \"%s\" has been requested by %10.10s", reflector->m_id.c_str(), reflector->m_name.c_str(), buffer + 14U); - - m_ysfNetwork->setDestination(reflector->m_address, reflector->m_port); - m_ysfNetwork->writePoll(); - m_ysfNetwork->writePoll(); - m_ysfNetwork->writePoll(); - - inactivityTimer.start(); - lostTimer.start(); - pollTimer.start(); - - m_linked = true; - } - } - break; - case WXS_DISCONNECT: - if (m_linked) { - m_wiresX->processDisconnect(); - - LogMessage("Disconnect via DTMF has been requested by %10.10s", buffer + 14U); - - m_ysfNetwork->writeUnlink(); - m_ysfNetwork->writeUnlink(); - m_ysfNetwork->writeUnlink(); - m_ysfNetwork->clearDestination(); - - inactivityTimer.stop(); - lostTimer.stop(); - pollTimer.stop(); - - m_linked = false; - } - break; - default: - break; - } - } + if (m_wiresX != NULL) + processWiresX(buffer, fi, dt, fn, ft); + + processDTMF(buffer, dt); if (m_gps != NULL) m_gps->data(buffer + 14U, buffer + 35U, fi, dt, fn, ft); } - if (ysfNetworkEnabled && m_linked && !m_exclude) { + if (ysfNetworkEnabled && m_linkType == LINK_YSF && !m_exclude) { m_ysfNetwork->write(buffer); if (::memcmp(buffer + 0U, "YSFD", 4U) == 0) - inactivityTimer.start(); + m_inactivityTimer.start(); + } + + if (fcsNetworkEnabled && m_linkType == LINK_FCS && !m_exclude) { + m_fcsNetwork->write(buffer); + if (::memcmp(buffer + 0U, "YSFD", 4U) == 0) + m_inactivityTimer.start(); } if ((buffer[34U] & 0x01U) == 0x01U) { if (m_gps != NULL) m_gps->reset(); - if (m_dtmf != NULL) - m_dtmf->reset(); + m_dtmf.reset(); m_exclude = false; } } while (m_ysfNetwork->read(buffer) > 0U) { - if (ysfNetworkEnabled && m_linked) { + if (ysfNetworkEnabled && m_linkType == LINK_YSF) { // Only pass through YSF data packets if (::memcmp(buffer + 0U, "YSFD", 4U) == 0) rptNetwork.write(buffer); - lostTimer.start(); + m_lostTimer.start(); + } + } + + while (m_fcsNetwork->read(buffer) > 0U) { + if (fcsNetworkEnabled && m_linkType == LINK_FCS) { + // Only pass through YSF data packets + if (::memcmp(buffer + 0U, "YSFD", 4U) == 0) + rptNetwork.write(buffer); + + m_lostTimer.start(); } } @@ -424,9 +344,9 @@ int CYSFGateway::run() if (m_wiresX != NULL) m_wiresX->clock(ms); - inactivityTimer.clock(ms); - if (inactivityTimer.isRunning() && inactivityTimer.hasExpired()) { - if (m_linked) { + m_inactivityTimer.clock(ms); + if (m_inactivityTimer.isRunning() && m_inactivityTimer.hasExpired()) { + if (m_linkType == LINK_YSF) { CYSFReflector* reflector = NULL; if (revert && !startup.empty() && m_wiresX != NULL) reflector = m_wiresX->getReflector(startup); @@ -434,7 +354,8 @@ int CYSFGateway::run() if (reflector != NULL) { LogMessage("Reverting connection to %5.5s - \"%s\"", reflector->m_id.c_str(), reflector->m_name.c_str()); - m_wiresX->processConnect(reflector); + if (m_wiresX != NULL) + m_wiresX->processConnect(reflector); m_ysfNetwork->writeUnlink(); m_ysfNetwork->writeUnlink(); @@ -445,8 +366,8 @@ int CYSFGateway::run() m_ysfNetwork->writePoll(); m_ysfNetwork->writePoll(); - lostTimer.start(); - pollTimer.start(); + m_lostTimer.start(); + m_ysfPollTimer.start(); } else { LogMessage("Disconnecting due to inactivity"); @@ -458,36 +379,37 @@ int CYSFGateway::run() m_ysfNetwork->writeUnlink(); m_ysfNetwork->clearDestination(); - lostTimer.stop(); - pollTimer.stop(); + m_lostTimer.stop(); + m_ysfPollTimer.stop(); - m_linked = false; + m_linkType = LINK_NONE; } } - inactivityTimer.stop(); + m_inactivityTimer.stop(); } - lostTimer.clock(ms); - if (lostTimer.isRunning() && lostTimer.hasExpired()) { + m_lostTimer.clock(ms); + if (m_lostTimer.isRunning() && m_lostTimer.hasExpired()) { LogWarning("Link has failed, polls lost"); if (m_wiresX != NULL) m_wiresX->processDisconnect(); - m_ysfNetwork->clearDestination(); + if (m_ysfNetwork != NULL) + m_ysfNetwork->clearDestination(); - inactivityTimer.stop(); - lostTimer.stop(); - pollTimer.stop(); + m_inactivityTimer.stop(); + m_lostTimer.stop(); + m_ysfPollTimer.stop(); - m_linked = false; + m_linkType = LINK_NONE; } - pollTimer.clock(ms); - if (pollTimer.isRunning() && pollTimer.hasExpired()) { + m_ysfPollTimer.clock(ms); + if (m_ysfPollTimer.isRunning() && m_ysfPollTimer.hasExpired()) { m_ysfNetwork->writePoll(); - pollTimer.start(); + m_ysfPollTimer.start(); } if (ms < 5U) @@ -506,7 +428,6 @@ int CYSFGateway::run() delete m_ysfNetwork; delete m_fcsNetwork; delete m_wiresX; - delete m_dtmf; ::LogFinalise(); @@ -539,3 +460,114 @@ void CYSFGateway::createGPS() m_gps = NULL; } } + +void CYSFGateway::processWiresX(const unsigned char* buffer, unsigned char fi, unsigned char dt, unsigned char fn, unsigned char ft) +{ + assert(buffer != NULL); + + WX_STATUS status = m_wiresX->process(buffer + 35U, buffer + 14U, fi, dt, fn, ft); + switch (status) { + case WXS_CONNECT_YSF: { + m_ysfNetwork->writeUnlink(); + m_ysfNetwork->writeUnlink(); + m_ysfNetwork->writeUnlink(); + + CYSFReflector* reflector = m_wiresX->getReflector(); + LogMessage("Connect to %5.5s - \"%s\" has been requested by %10.10s", reflector->m_id.c_str(), reflector->m_name.c_str(), buffer + 14U); + + m_ysfNetwork->setDestination(reflector->m_address, reflector->m_port); + m_ysfNetwork->writePoll(); + m_ysfNetwork->writePoll(); + m_ysfNetwork->writePoll(); + + m_inactivityTimer.start(); + m_lostTimer.start(); + m_ysfPollTimer.start(); + + m_linkType = LINK_YSF; + } + break; + case WXS_DISCONNECT: + LogMessage("Disconnect has been requested by %10.10s", buffer + 14U); + + m_ysfNetwork->writeUnlink(); + m_ysfNetwork->writeUnlink(); + m_ysfNetwork->writeUnlink(); + m_ysfNetwork->clearDestination(); + + m_inactivityTimer.stop(); + m_lostTimer.stop(); + m_ysfPollTimer.stop(); + + m_linkType = LINK_NONE; + break; + default: + break; + } +} + +void CYSFGateway::processDTMF(const unsigned char* buffer, unsigned char dt) +{ + assert(buffer != NULL); + + WX_STATUS status = WXS_NONE; + switch (dt) { + case YSF_DT_VD_MODE2: + status = m_dtmf.decodeVDMode2(buffer + 35U, (buffer[34U] & 0x01U) == 0x01U); + break; + default: + break; + } + + switch (status) { + case WXS_CONNECT_YSF: { + std::string id = m_dtmf.getReflector(); + CYSFReflector* reflector = m_wiresX->getReflector(id); + if (reflector != NULL) { + if (m_wiresX != NULL) + m_wiresX->processConnect(reflector); + + if (m_linkType == LINK_YSF) { + m_ysfNetwork->writeUnlink(); + m_ysfNetwork->writeUnlink(); + m_ysfNetwork->writeUnlink(); + } + + LogMessage("Connect via DTMF to %5.5s - \"%s\" has been requested by %10.10s", reflector->m_id.c_str(), reflector->m_name.c_str(), buffer + 14U); + + m_ysfNetwork->setDestination(reflector->m_address, reflector->m_port); + m_ysfNetwork->writePoll(); + m_ysfNetwork->writePoll(); + m_ysfNetwork->writePoll(); + + m_inactivityTimer.start(); + m_lostTimer.start(); + m_ysfPollTimer.start(); + + m_linkType = LINK_YSF; + } + } + break; + case WXS_DISCONNECT: + if (m_linkType == LINK_YSF) { + if (m_wiresX != NULL) + m_wiresX->processDisconnect(); + + LogMessage("Disconnect via DTMF has been requested by %10.10s", buffer + 14U); + + m_ysfNetwork->writeUnlink(); + m_ysfNetwork->writeUnlink(); + m_ysfNetwork->writeUnlink(); + m_ysfNetwork->clearDestination(); + + m_inactivityTimer.stop(); + m_lostTimer.stop(); + m_ysfPollTimer.stop(); + + m_linkType = LINK_NONE; + } + break; + default: + break; + } +} diff --git a/YSFGateway/YSFGateway.h b/YSFGateway/YSFGateway.h index fabc41d..1aebf76 100644 --- a/YSFGateway/YSFGateway.h +++ b/YSFGateway/YSFGateway.h @@ -22,12 +22,19 @@ #include "YSFNetwork.h" #include "FCSNetwork.h" #include "WiresX.h" +#include "Timer.h" #include "Conf.h" #include "DTMF.h" #include "GPS.h" #include +enum LINK_TYPE { + LINK_NONE, + LINK_YSF, + LINK_FCS +}; + class CYSFGateway { public: @@ -42,12 +49,17 @@ private: CConf m_conf; CGPS* m_gps; CWiresX* m_wiresX; - CDTMF* m_dtmf; + CDTMF m_dtmf; CYSFNetwork* m_ysfNetwork; CFCSNetwork* m_fcsNetwork; - bool m_linked; + LINK_TYPE m_linkType; bool m_exclude; + CTimer m_inactivityTimer; + CTimer m_lostTimer; + CTimer m_ysfPollTimer; + void processWiresX(const unsigned char* buffer, unsigned char fi, unsigned char dt, unsigned char fn, unsigned char ft); + void processDTMF(const unsigned char* buffer, unsigned char dt); void createGPS(); };