From 7f9e1930e8ca3b57d116d1efdd1916bdb21babef Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Fri, 4 Feb 2022 20:52:55 +0100 Subject: [PATCH] #17 add mising parts, still nedd to glue it all together --- Common/RemoteProtocolHandler.cpp | 6 +- Common/RepeaterHandler.cpp | 8 +- DGWRemoteControl/DGWRemoteControlApp.cpp | 2 +- DGWRemoteControl/DGWRemoteControlConfig.cpp | 38 ++ DGWRemoteControl/DGWRemoteControlConfig.h | 41 ++ .../RemoteControlCallsignData.cpp | 59 ++ DGWRemoteControl/RemoteControlCallsignData.h | 44 ++ DGWRemoteControl/RemoteControlDefs.h | 28 + DGWRemoteControl/RemoteControlLinkData.cpp | 59 ++ DGWRemoteControl/RemoteControlLinkData.h | 46 ++ .../RemoteControlRemoteControlHandler.cpp | 561 ++++++++++++++++++ .../RemoteControlRemoteControlHandler.h | 84 +++ .../RemoteControlRepeaterData.cpp | 68 +++ DGWRemoteControl/RemoteControlRepeaterData.h | 48 ++ .../RemoteControlStarNetGroup.cpp | 76 +++ DGWRemoteControl/RemoteControlStarNetGroup.h | 51 ++ DGWRemoteControl/RemoteControlStarNetUser.cpp | 45 ++ DGWRemoteControl/RemoteControlStarNetUser.h | 39 ++ DGWRemoteControl/dgwremotecontrol_example.cfg | 12 + 19 files changed, 1307 insertions(+), 8 deletions(-) create mode 100644 DGWRemoteControl/DGWRemoteControlConfig.cpp create mode 100644 DGWRemoteControl/DGWRemoteControlConfig.h create mode 100644 DGWRemoteControl/RemoteControlCallsignData.cpp create mode 100644 DGWRemoteControl/RemoteControlCallsignData.h create mode 100644 DGWRemoteControl/RemoteControlDefs.h create mode 100644 DGWRemoteControl/RemoteControlLinkData.cpp create mode 100644 DGWRemoteControl/RemoteControlLinkData.h create mode 100644 DGWRemoteControl/RemoteControlRemoteControlHandler.cpp create mode 100644 DGWRemoteControl/RemoteControlRemoteControlHandler.h create mode 100644 DGWRemoteControl/RemoteControlRepeaterData.cpp create mode 100644 DGWRemoteControl/RemoteControlRepeaterData.h create mode 100644 DGWRemoteControl/RemoteControlStarNetGroup.cpp create mode 100644 DGWRemoteControl/RemoteControlStarNetGroup.h create mode 100644 DGWRemoteControl/RemoteControlStarNetUser.cpp create mode 100644 DGWRemoteControl/RemoteControlStarNetUser.h create mode 100644 DGWRemoteControl/dgwremotecontrol_example.cfg diff --git a/Common/RemoteProtocolHandler.cpp b/Common/RemoteProtocolHandler.cpp index 0c6a026..fd38c24 100644 --- a/Common/RemoteProtocolHandler.cpp +++ b/Common/RemoteProtocolHandler.cpp @@ -365,12 +365,12 @@ bool CRemoteProtocolHandler::sendStarNetGroup(const CRemoteStarNetGroup& data) ::memset(p, ' ', LONG_CALLSIGN_LENGTH); for (unsigned int i = 0U; i < data.getCallsign().length(); i++) - p[i] = data.getCallsign().GetChar(i); + p[i] = data.getCallsign().at(i); p += LONG_CALLSIGN_LENGTH; ::memset(p, ' ', LONG_CALLSIGN_LENGTH); for (unsigned int i = 0U; i < data.getLogoff().length(); i++) - p[i] = data.getLogoff().GetChar(i); + p[i] = data.getLogoff().at(i); p += LONG_CALLSIGN_LENGTH; uint32_t timer = wxUINT32_SWAP_ON_BE(data.getTimer()); @@ -386,7 +386,7 @@ bool CRemoteProtocolHandler::sendStarNetGroup(const CRemoteStarNetGroup& data) ::memset(p, ' ', LONG_CALLSIGN_LENGTH); for (unsigned int i = 0U; i < user.getCallsign().length(); i++) - p[i] = user.getCallsign().GetChar(i); + p[i] = user.getCallsign().at(i); p += LONG_CALLSIGN_LENGTH; timer = wxUINT32_SWAP_ON_BE(user.getTimer()); diff --git a/Common/RepeaterHandler.cpp b/Common/RepeaterHandler.cpp index c55ca20..7c486ee 100644 --- a/Common/RepeaterHandler.cpp +++ b/Common/RepeaterHandler.cpp @@ -3106,19 +3106,19 @@ bool CRepeaterHandler::isCCSCommand(const std::string& command) const if (command.IsSameAs("CA "))) return true; - wxChar c = command.GetChar(0U); + wxChar c = command.at(0U); if (c != wxT('C')) return false; - c = command.GetChar(1U); + c = command.at(1U); if (c < wxT('0') || c > wxT('9')) return false; - c = command.GetChar(2U); + c = command.at(2U); if (c < wxT('0') || c > wxT('9')) return false; - c = command.GetChar(3U); + c = command.at(3U); if (c < wxT('0') || c > wxT('9')) return false; diff --git a/DGWRemoteControl/DGWRemoteControlApp.cpp b/DGWRemoteControl/DGWRemoteControlApp.cpp index 0ed500b..d46cc0a 100644 --- a/DGWRemoteControl/DGWRemoteControlApp.cpp +++ b/DGWRemoteControl/DGWRemoteControlApp.cpp @@ -1,6 +1,6 @@ /* * Copyright (C) 2010,2011 by Jonathan Naylor G4KLX - * Copyright (c) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA + * Copyright (c) 2022 by Geoffrey Merck F4FXL / KC3FRA * * 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 diff --git a/DGWRemoteControl/DGWRemoteControlConfig.cpp b/DGWRemoteControl/DGWRemoteControlConfig.cpp new file mode 100644 index 0000000..abce0c1 --- /dev/null +++ b/DGWRemoteControl/DGWRemoteControlConfig.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2010,2011 by Jonathan Naylor G4KLX + * Copyright (c) 2022 by Geoffrey Merck F4FXL / KC3FRA + * + * 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 "DGWRemoteControlConfig.h" +#include "Log.h" + +bool load() +{ + return false; +} + +bool CDGWRemoteControlConfig::open(CConfig & cfg) +{ + try { + return cfg.load(); + } + catch(...) { + CLog::logError("Can't read %s\n", m_fileName.c_str()); + return false; + } + return true; +} \ No newline at end of file diff --git a/DGWRemoteControl/DGWRemoteControlConfig.h b/DGWRemoteControl/DGWRemoteControlConfig.h new file mode 100644 index 0000000..e587253 --- /dev/null +++ b/DGWRemoteControl/DGWRemoteControlConfig.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2010,2011 by Jonathan Naylor G4KLX + * Copyright (c) 2022 by Geoffrey Merck F4FXL / KC3FRA + * + * 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. + */ + +#pragma once + +#include + +#include "Config.h" + +typedef struct { + std::string callsign; + std::string address; +} TRemoteGateway; + +class CDGWRemoteControlConfig +{ +public: + CDGWRemoteControlConfig(const std::string& pathName); + bool load(); + +private: + bool open(CConfig& config); + + std::string m_fileName; +}; \ No newline at end of file diff --git a/DGWRemoteControl/RemoteControlCallsignData.cpp b/DGWRemoteControl/RemoteControlCallsignData.cpp new file mode 100644 index 0000000..710811c --- /dev/null +++ b/DGWRemoteControl/RemoteControlCallsignData.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2011 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 "RemoteControlCallsignData.h" + +CRemoteControlCallsignData::CRemoteControlCallsignData() : +m_repeaters(), +m_starNets() +{ +} + +CRemoteControlCallsignData::~CRemoteControlCallsignData() +{ +} + +void CRemoteControlCallsignData::addRepeater(const std::string& callsign) +{ + m_repeaters.push_back(callsign); +} + +void CRemoteControlCallsignData::addStarNet(const std::string& callsign) +{ + m_starNets.push_back(callsign); +} + +unsigned int CRemoteControlCallsignData::getRepeaterCount() const +{ + return m_repeaters.size(); +} + +std::string CRemoteControlCallsignData::getRepeater(unsigned int n) const +{ + return m_repeaters[n]; +} + +unsigned int CRemoteControlCallsignData::getStarNetCount() const +{ + return m_starNets.size(); +} + +std::string CRemoteControlCallsignData::getStarNet(unsigned int n) const +{ + return m_starNets[n]; +} diff --git a/DGWRemoteControl/RemoteControlCallsignData.h b/DGWRemoteControl/RemoteControlCallsignData.h new file mode 100644 index 0000000..c21ac6a --- /dev/null +++ b/DGWRemoteControl/RemoteControlCallsignData.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2011 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 RemoteControlCallsignData_H +#define RemoteControlCallsignData_H + +#include +#include + +class CRemoteControlCallsignData { +public: + CRemoteControlCallsignData(); + ~CRemoteControlCallsignData(); + + void addRepeater(const std::string& callsign); + void addStarNet(const std::string& callsign); + + unsigned int getRepeaterCount() const; + unsigned int getStarNetCount() const; + + std::string getRepeater(unsigned int n) const; + std::string getStarNet(unsigned int n) const; + +private: + std::vector m_repeaters; + std::vector m_starNets; +}; + +#endif diff --git a/DGWRemoteControl/RemoteControlDefs.h b/DGWRemoteControl/RemoteControlDefs.h new file mode 100644 index 0000000..5e41383 --- /dev/null +++ b/DGWRemoteControl/RemoteControlDefs.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2011 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 RemoteControlDefs_H +#define RemoteControlDefs_H + +#include + +const std::string APPLICATION_NAME = wxT("Remote Control"); + +const std::string CONFIG_FILE_NAME = wxT(".Remote Control"); + +#endif diff --git a/DGWRemoteControl/RemoteControlLinkData.cpp b/DGWRemoteControl/RemoteControlLinkData.cpp new file mode 100644 index 0000000..c869595 --- /dev/null +++ b/DGWRemoteControl/RemoteControlLinkData.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2011 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 "RemoteControlLinkData.h" + +CRemoteControlLinkData::CRemoteControlLinkData(const std::string& callsign, int32_t protocol, int32_t linked, int32_t direction, int32_t dongle) : +m_callsign(callsign), +m_protocol(PROTOCOL(protocol)), +m_linked(false), +m_direction(DIRECTION(direction)), +m_dongle(false) +{ + m_linked = linked == 1; + m_dongle = dongle == 1; +} + +CRemoteControlLinkData::~CRemoteControlLinkData() +{ +} + +std::string CRemoteControlLinkData::getCallsign() const +{ + return m_callsign; +} + +PROTOCOL CRemoteControlLinkData::getProtocol() const +{ + return m_protocol; +} + +bool CRemoteControlLinkData::isLinked() const +{ + return m_linked; +} + +DIRECTION CRemoteControlLinkData::getDirection() const +{ + return m_direction; +} + +bool CRemoteControlLinkData::isDongle() const +{ + return m_dongle; +} diff --git a/DGWRemoteControl/RemoteControlLinkData.h b/DGWRemoteControl/RemoteControlLinkData.h new file mode 100644 index 0000000..dd6aa1c --- /dev/null +++ b/DGWRemoteControl/RemoteControlLinkData.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2011 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 RemoteControlLinkData_H +#define RemoteControlLinkData_H + +#include + +#include "Defs.h" + + +class CRemoteControlLinkData { +public: + CRemoteControlLinkData(const std::string& callsign, int32_t protocol, int32_t linked, int32_t direction, int32_t dongle); + ~CRemoteControlLinkData(); + + std::string getCallsign() const; + PROTOCOL getProtocol() const; + bool isLinked() const; + DIRECTION getDirection() const; + bool isDongle() const; + +private: + std::string m_callsign; + PROTOCOL m_protocol; + bool m_linked; + DIRECTION m_direction; + bool m_dongle; +}; + +#endif diff --git a/DGWRemoteControl/RemoteControlRemoteControlHandler.cpp b/DGWRemoteControl/RemoteControlRemoteControlHandler.cpp new file mode 100644 index 0000000..f2e9d8d --- /dev/null +++ b/DGWRemoteControl/RemoteControlRemoteControlHandler.cpp @@ -0,0 +1,561 @@ +/* + * Copyright (C) 2011,2013 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 +#include + +#include "Utils.h" +#include "RemoteControlRemoteControlHandler.h" +#include "DStarDefines.h" + +const unsigned int BUFFER_LENGTH = 2000U; + +const unsigned int MAX_RETRIES = 3U; + +CRemoteControlRemoteControlHandler::CRemoteControlRemoteControlHandler(const std::string& address, unsigned int port) : +m_socket("", 0U), +m_address(), +m_port(port), +m_loggedIn(false), +m_retryCount(0U), +m_type(RCT_NONE), +m_inBuffer(NULL), +m_inLength(0U), +m_outBuffer(NULL), +m_outLength(0U) +{ + assert(!address.empty()); + assert(port > 0U); + + m_address = CUDPReaderWriter::lookup(address); + + m_inBuffer = new unsigned char[BUFFER_LENGTH]; + m_outBuffer = new unsigned char[BUFFER_LENGTH]; +} + +CRemoteControlRemoteControlHandler::~CRemoteControlRemoteControlHandler() +{ + delete[] m_inBuffer; + delete[] m_outBuffer; +} + +bool CRemoteControlRemoteControlHandler::open() +{ + return m_socket.open(); +} + +RC_TYPE CRemoteControlRemoteControlHandler::readType() +{ + m_type = RCT_NONE; + + in_addr address; + unsigned int port; + + int length = m_socket.read(m_inBuffer, BUFFER_LENGTH, address, port); + if (length <= 0) + return m_type; + + m_inLength = length; + + if (::memcmp(m_inBuffer, "ACK", 3U) == 0) { + m_retryCount = 0U; + m_type = RCT_ACK; + return m_type; + } else if (::memcmp(m_inBuffer, "NAK", 3U) == 0) { + m_retryCount = 0U; + m_type = RCT_NAK; + return m_type; + } else if (::memcmp(m_inBuffer, "RND", 3U) == 0) { + m_retryCount = 0U; + m_type = RCT_RANDOM; + return m_type; + } else if (::memcmp(m_inBuffer, "CAL", 3U) == 0) { + m_retryCount = 0U; + m_type = RCT_CALLSIGNS; + return m_type; + } else if (::memcmp(m_inBuffer, "RPT", 3U) == 0) { + m_retryCount = 0U; + m_type = RCT_REPEATER; + return m_type; + } else if (::memcmp(m_inBuffer, "SNT", 3U) == 0) { + m_retryCount = 0U; + m_type = RCT_STARNET; + return m_type; + } + + return m_type; +} + +std::string CRemoteControlRemoteControlHandler::readNAK() +{ + if (m_type != RCT_NAK) + return ""; + + std::string text((char*)(m_inBuffer + 3U)); + + return text; +} + +unsigned int CRemoteControlRemoteControlHandler::readRandom() +{ + if (m_type != RCT_RANDOM) + return 0U; + + uint32_t random; + ::memcpy(&random, m_inBuffer + 3U, sizeof(uint32_t)); + + return CUtils::swap_endian_be(random); +} + +CRemoteControlCallsignData* CRemoteControlRemoteControlHandler::readCallsigns() +{ + if (m_type != RCT_CALLSIGNS) + return NULL; + + CRemoteControlCallsignData* data = new CRemoteControlCallsignData; + + unsigned char* p = m_inBuffer + 3U; + unsigned int pos = 3U; + + while (pos < m_inLength) { + unsigned char type = *p; + pos += 1U; + p += 1U; + + std::string callsign((char*)p, LONG_CALLSIGN_LENGTH); + pos += LONG_CALLSIGN_LENGTH; + p += LONG_CALLSIGN_LENGTH; + + switch (type) { + case 'R': + data->addRepeater(callsign); + break; + case 'S': + data->addStarNet(callsign); + break; + default: // ???? + break; + } + } + + return data; +} + +CRemoteControlRepeaterData* CRemoteControlRemoteControlHandler::readRepeater() +{ + if (m_type != RCT_REPEATER) + return NULL; + + unsigned char* p = m_inBuffer + 3U; + unsigned int pos = 3U; + + std::string callsign((char*)p, LONG_CALLSIGN_LENGTH); + pos += LONG_CALLSIGN_LENGTH; + p += LONG_CALLSIGN_LENGTH; + + int32_t reconnect; + ::memcpy(&reconnect, p, sizeof(int32_t)); + pos += sizeof(int32_t); + p += sizeof(int32_t); + + std::string reflector((char*)p, LONG_CALLSIGN_LENGTH); + pos += LONG_CALLSIGN_LENGTH; + p += LONG_CALLSIGN_LENGTH; + + CRemoteControlRepeaterData* data = new CRemoteControlRepeaterData(callsign, CUtils::swap_endian_be(reconnect), reflector); + + while (pos < m_inLength) { + std::string callsign((char*)p, LONG_CALLSIGN_LENGTH); + pos += LONG_CALLSIGN_LENGTH; + p += LONG_CALLSIGN_LENGTH; + + int32_t protocol; + ::memcpy(&protocol, p, sizeof(int32_t)); + pos += sizeof(int32_t); + p += sizeof(int32_t); + + int32_t linked; + ::memcpy(&linked, p, sizeof(int32_t)); + pos += sizeof(int32_t); + p += sizeof(int32_t); + + int32_t direction; + ::memcpy(&direction, p, sizeof(int32_t)); + pos += sizeof(int32_t); + p += sizeof(int32_t); + + int32_t dongle; + ::memcpy(&dongle, p, sizeof(int32_t)); + pos += sizeof(int32_t); + p += sizeof(int32_t); + + data->addLink(callsign, CUtils::swap_endian_be(protocol), CUtils::swap_endian_be(linked), CUtils::swap_endian_be(direction), CUtils::swap_endian_be(dongle)); + } + + return data; +} + +CRemoteControlStarNetGroup* CRemoteControlRemoteControlHandler::readStarNetGroup() +{ + if (m_type != RCT_STARNET) + return NULL; + + unsigned char* p = m_inBuffer + 3U; + unsigned int pos = 3U; + + std::string callsign((char*)p, LONG_CALLSIGN_LENGTH); + pos += LONG_CALLSIGN_LENGTH; + p += LONG_CALLSIGN_LENGTH; + + std::string logoff((char*)p, LONG_CALLSIGN_LENGTH); + pos += LONG_CALLSIGN_LENGTH; + p += LONG_CALLSIGN_LENGTH; + + uint32_t timer; + ::memcpy(&timer, p, sizeof(uint32_t)); + pos += sizeof(uint32_t); + p += sizeof(uint32_t); + + uint32_t timeout; + ::memcpy(&timeout, p, sizeof(uint32_t)); + pos += sizeof(uint32_t); + p += sizeof(uint32_t); + + CRemoteControlStarNetGroup* group = new CRemoteControlStarNetGroup(callsign, logoff, CUtils::swap_endian_be(timer), CUtils::swap_endian_be(timeout)); + + while (pos < m_inLength) { + std::string callsign((char*)p, LONG_CALLSIGN_LENGTH); + pos += LONG_CALLSIGN_LENGTH; + p += LONG_CALLSIGN_LENGTH; + + ::memcpy(&timer, p, sizeof(uint32_t)); + pos += sizeof(uint32_t); + p += sizeof(uint32_t); + + ::memcpy(&timeout, p, sizeof(uint32_t)); + pos += sizeof(uint32_t); + p += sizeof(uint32_t); + + group->addUser(callsign, CUtils::swap_endian_be(timer), CUtils::swap_endian_be(timeout)); + } + + return group; +} + +bool CRemoteControlRemoteControlHandler::login() +{ + if (m_loggedIn) + return false; + + if (m_address.s_addr == INADDR_NONE) + return false; + + ::memcpy(m_outBuffer, "LIN", 3U); + m_outLength = 3U; + + bool ret = m_socket.write(m_outBuffer, m_outLength, m_address, m_port); + if (!ret) { + m_retryCount = 0U; + return false; + } else { + m_retryCount = 1U; + return true; + } +} + +void CRemoteControlRemoteControlHandler::setLoggedIn(bool set) +{ + m_loggedIn = set; +} + +bool CRemoteControlRemoteControlHandler::getCallsigns() +{ + if (!m_loggedIn || m_retryCount > 0U) + return false; + + ::memcpy(m_outBuffer, "GCS", 3U); + m_outLength = 3U; + + bool ret = m_socket.write(m_outBuffer, m_outLength, m_address, m_port); + if (!ret) { + m_retryCount = 0U; + return false; + } else { + m_retryCount = 1U; + return true; + } +} + +bool CRemoteControlRemoteControlHandler::sendHash(const unsigned char* hash, unsigned int length) +{ + assert(hash != NULL); + assert(length > 0U); + + if (m_loggedIn || m_retryCount > 0U) + return false; + + unsigned char* p = m_outBuffer; + m_outLength = 0U; + + ::memcpy(p, "SHA", 3U); + m_outLength += 3U; + p += 3U; + + ::memcpy(p, hash, length); + m_outLength += length; + p += length; + + bool ret = m_socket.write(m_outBuffer, m_outLength, m_address, m_port); + if (!ret) { + m_retryCount = 0U; + return false; + } else { + m_retryCount = 1U; + return true; + } +} + +bool CRemoteControlRemoteControlHandler::getRepeater(const std::string& callsign) +{ + assert(!callsign.empty()); + + if (!m_loggedIn || m_retryCount > 0U) + return false; + + unsigned char* p = m_outBuffer; + m_outLength = 0U; + + ::memcpy(p, "GRP", 3U); + m_outLength += 3U; + p += 3U; + + ::memset(p, ' ', LONG_CALLSIGN_LENGTH); + for (unsigned int i = 0U; i < callsign.length(); i++) + p[i] = callsign.at(i); + + m_outLength += LONG_CALLSIGN_LENGTH; + p += LONG_CALLSIGN_LENGTH; + + bool ret = m_socket.write(m_outBuffer, m_outLength, m_address, m_port); + if (!ret) { + m_retryCount = 0U; + return false; + } else { + m_retryCount = 1U; + return true; + } +} + +bool CRemoteControlRemoteControlHandler::getStarNet(const std::string& callsign) +{ + assert(!callsign.empty()); + + if (!m_loggedIn || m_retryCount > 0U) + return false; + + unsigned char* p = m_outBuffer; + m_outLength = 0U; + + ::memcpy(p, "GSN", 3U); + m_outLength += 3U; + p += 3U; + + ::memset(p, ' ', LONG_CALLSIGN_LENGTH); + for (unsigned int i = 0U; i < callsign.length(); i++) + p[i] = callsign.at(i); + + m_outLength += LONG_CALLSIGN_LENGTH; + p += LONG_CALLSIGN_LENGTH; + + bool ret = m_socket.write(m_outBuffer, m_outLength, m_address, m_port); + if (!ret) { + m_retryCount = 0U; + return false; + } else { + m_retryCount = 1U; + return true; + } +} + +bool CRemoteControlRemoteControlHandler::link(const std::string& callsign, RECONNECT reconnect, const std::string& reflector) +{ + assert(!callsign.empty()); + + if (!m_loggedIn || m_retryCount > 0U) + return false; + + unsigned char* p = m_outBuffer; + m_outLength = 0U; + + ::memcpy(p, "LNK", 3U); + m_outLength += 3U; + p += 3U; + + ::memset(p, ' ', LONG_CALLSIGN_LENGTH); + for (unsigned int i = 0U; i < callsign.length(); i++) + p[i] = callsign.at(i); + + m_outLength += LONG_CALLSIGN_LENGTH; + p += LONG_CALLSIGN_LENGTH; + + int32_t temp1 = int32_t(reconnect); + int32_t temp2 = CUtils::swap_endian_be(temp1); + ::memcpy(p, &temp2, sizeof(int32_t)); + m_outLength += sizeof(int32_t); + p += sizeof(int32_t); + + ::memset(p, ' ', LONG_CALLSIGN_LENGTH); + for (unsigned int i = 0U; i < reflector.length(); i++) + p[i] = reflector.at(i); + + m_outLength += LONG_CALLSIGN_LENGTH; + p += LONG_CALLSIGN_LENGTH; + + bool ret = m_socket.write(m_outBuffer, m_outLength, m_address, m_port); + if (!ret) { + m_retryCount = 0U; + return false; + } else { + m_retryCount = 1U; + return true; + } +} + +bool CRemoteControlRemoteControlHandler::unlink(const std::string& callsign, PROTOCOL protocol, const std::string& reflector) +{ + assert(!callsign.empty()); + + if (!m_loggedIn || m_retryCount > 0U) + return false; + + unsigned char* p = m_outBuffer; + m_outLength = 0U; + + ::memcpy(p, "UNL", 3U); + m_outLength += 3U; + p += 3U; + + ::memset(p, ' ', LONG_CALLSIGN_LENGTH); + for (unsigned int i = 0U; i < callsign.length(); i++) + p[i] = callsign.at(i); + + m_outLength += LONG_CALLSIGN_LENGTH; + p += LONG_CALLSIGN_LENGTH; + + int32_t temp1 = int32_t(protocol); + int32_t temp2 = CUtils::swap_endian_be(temp1); + ::memcpy(p, &temp2, sizeof(int32_t)); + m_outLength += sizeof(int32_t); + p += sizeof(int32_t); + + ::memset(p, ' ', LONG_CALLSIGN_LENGTH); + for (unsigned int i = 0U; i < reflector.length(); i++) + p[i] = reflector.at(i); + + m_outLength += LONG_CALLSIGN_LENGTH; + p += LONG_CALLSIGN_LENGTH; + + bool ret = m_socket.write(m_outBuffer, m_outLength, m_address, m_port); + if (!ret) { + m_retryCount = 0U; + return false; + } else { + m_retryCount = 1U; + return true; + } +} + +bool CRemoteControlRemoteControlHandler::logoff(const std::string& callsign, const std::string& user) +{ + assert(!callsign.empty()); + assert(!user.empty()); + + if (!m_loggedIn || m_retryCount > 0U) + return false; + + unsigned char* p = m_outBuffer; + m_outLength = 0U; + + ::memcpy(p, "LGO", 3U); + m_outLength += 3U; + p += 3U; + + ::memset(p, ' ', LONG_CALLSIGN_LENGTH); + for (unsigned int i = 0U; i < callsign.length(); i++) + p[i] = callsign.at(i); + + m_outLength += LONG_CALLSIGN_LENGTH; + p += LONG_CALLSIGN_LENGTH; + + ::memset(p, ' ', LONG_CALLSIGN_LENGTH); + for (unsigned int i = 0U; i < user.length(); i++) + p[i] = user.at(i); + + m_outLength += LONG_CALLSIGN_LENGTH; + p += LONG_CALLSIGN_LENGTH; + + bool ret = m_socket.write(m_outBuffer, m_outLength, m_address, m_port); + if (!ret) { + m_retryCount = 0U; + return false; + } else { + m_retryCount = 1U; + return true; + } +} + +bool CRemoteControlRemoteControlHandler::logout() +{ + if (!m_loggedIn || m_retryCount > 0U) + return false; + + ::memcpy(m_outBuffer, "LOG", 3U); + m_outLength = 3U; + + for (unsigned int i = 0U; i < 5U; i++) { + bool ret = m_socket.write(m_outBuffer, m_outLength, m_address, m_port); + if (!ret) { + m_retryCount = 0U; + return false; + } + } + + m_retryCount = 1U; + + return true; +} + +bool CRemoteControlRemoteControlHandler::retry() +{ + if (m_retryCount > 0U) { + m_retryCount++; + if (m_retryCount >= MAX_RETRIES) { + m_retryCount = 0U; + return false; + } + + m_socket.write(m_outBuffer, m_outLength, m_address, m_port); + } + + return true; +} + +void CRemoteControlRemoteControlHandler::close() +{ + m_socket.close(); +} diff --git a/DGWRemoteControl/RemoteControlRemoteControlHandler.h b/DGWRemoteControl/RemoteControlRemoteControlHandler.h new file mode 100644 index 0000000..dd434e1 --- /dev/null +++ b/DGWRemoteControl/RemoteControlRemoteControlHandler.h @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2011,2013 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 RemoteControlRemoteControlHandler_H +#define RemoteControlRemoteControlHandler_H + +#include "RemoteControlRepeaterData.h" +#include "RemoteControlStarNetGroup.h" +#include "RemoteControlCallsignData.h" +#include "UDPReaderWriter.h" + +enum RC_TYPE { + RCT_NONE, + RCT_ACK, + RCT_NAK, + RCT_RANDOM, + RCT_CALLSIGNS, + RCT_REPEATER, + RCT_STARNET +}; + +class CRemoteControlRemoteControlHandler { +public: + CRemoteControlRemoteControlHandler(const std::string& address, unsigned int port); + ~CRemoteControlRemoteControlHandler(); + + bool open(); + + RC_TYPE readType(); + + std::string readNAK(); + unsigned int readRandom(); + CRemoteControlCallsignData* readCallsigns(); + CRemoteControlRepeaterData* readRepeater(); + CRemoteControlStarNetGroup* readStarNetGroup(); + + bool login(); + bool sendHash(const unsigned char* hash, unsigned int length); + + void setLoggedIn(bool set); + + bool getCallsigns(); + bool getRepeater(const std::string& callsign); + bool getStarNet(const std::string& callsign); + + bool link(const std::string& callsign, RECONNECT reconnect, const std::string& reflector); + bool unlink(const std::string& callsign, PROTOCOL protocol, const std::string& reflector); + bool logoff(const std::string& callsign, const std::string& user); + + bool logout(); + + bool retry(); + + void close(); + +private: + CUDPReaderWriter m_socket; + in_addr m_address; + unsigned int m_port; + bool m_loggedIn; + unsigned int m_retryCount; + RC_TYPE m_type; + unsigned char* m_inBuffer; + unsigned int m_inLength; + unsigned char* m_outBuffer; + unsigned int m_outLength; +}; + +#endif diff --git a/DGWRemoteControl/RemoteControlRepeaterData.cpp b/DGWRemoteControl/RemoteControlRepeaterData.cpp new file mode 100644 index 0000000..241bf45 --- /dev/null +++ b/DGWRemoteControl/RemoteControlRepeaterData.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2011 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 "RemoteControlRepeaterData.h" + + +CRemoteControlRepeaterData::CRemoteControlRepeaterData(const std::string& callsign, int32_t reconnect, const std::string& reflector) : +m_callsign(callsign), +m_reconnect(RECONNECT(reconnect)), +m_reflector(reflector), +m_links() +{ +} + +CRemoteControlRepeaterData::~CRemoteControlRepeaterData() +{ + for(auto data : m_links) { + delete data; + } + m_links.clear(); +} + +void CRemoteControlRepeaterData::addLink(const std::string& callsign, int32_t protocol, int32_t linked, int32_t direction, int32_t dongle) +{ + CRemoteControlLinkData * data = new CRemoteControlLinkData(callsign, protocol, linked, direction, dongle); + + m_links.push_back(data); +} + +std::string CRemoteControlRepeaterData::getCallsign() const +{ + return m_callsign; +} + +RECONNECT CRemoteControlRepeaterData::getReconnect() const +{ + return m_reconnect; +} + +std::string CRemoteControlRepeaterData::getReflector() const +{ + return m_reflector; +} + +unsigned int CRemoteControlRepeaterData::getLinkCount() const +{ + return m_links.size(); +} + +CRemoteControlLinkData* CRemoteControlRepeaterData::getLink(unsigned int n) const +{ + return m_links[n]; +} diff --git a/DGWRemoteControl/RemoteControlRepeaterData.h b/DGWRemoteControl/RemoteControlRepeaterData.h new file mode 100644 index 0000000..1025328 --- /dev/null +++ b/DGWRemoteControl/RemoteControlRepeaterData.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2011 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 RemoteControlRepeaterData_H +#define RemoteControlRepeaterData_H + +#include + +#include "RemoteControlLinkData.h" + + +class CRemoteControlRepeaterData { +public: + CRemoteControlRepeaterData(const std::string& callsign, int32_t reconnect, const std::string& reflector); + ~CRemoteControlRepeaterData(); + + void addLink(const std::string& callsign, int32_t protocol, int32_t linked, int32_t direction, int32_t dongle); + + std::string getCallsign() const; + RECONNECT getReconnect() const; + std::string getReflector() const; + + unsigned int getLinkCount() const; + CRemoteControlLinkData * getLink(unsigned int n) const; + +private: + std::string m_callsign; + RECONNECT m_reconnect; + std::string m_reflector; + std::vector m_links; +}; + +#endif diff --git a/DGWRemoteControl/RemoteControlStarNetGroup.cpp b/DGWRemoteControl/RemoteControlStarNetGroup.cpp new file mode 100644 index 0000000..9fa7a98 --- /dev/null +++ b/DGWRemoteControl/RemoteControlStarNetGroup.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2011 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 "RemoteControlStarNetGroup.h" + + +CRemoteControlStarNetGroup::CRemoteControlStarNetGroup(const std::string& callsign, const std::string& logoff, uint32_t timer, uint32_t timeout) : +m_callsign(callsign), +m_logoff(logoff), +m_timer((unsigned int)timer), +m_timeout((unsigned int)timeout), +m_users() +{ + if (m_logoff == " ") + m_logoff.clear(); +} + +CRemoteControlStarNetGroup::~CRemoteControlStarNetGroup() +{ + for(auto user : m_users) { + delete user; + } + m_users.clear(); +} + +void CRemoteControlStarNetGroup::addUser(const std::string& callsign, uint32_t timer, uint32_t timeout) +{ + CRemoteControlStarNetUser * user = new CRemoteControlStarNetUser(callsign, timer, timeout); + + m_users.push_back(user); +} + +std::string CRemoteControlStarNetGroup::getCallsign() const +{ + return m_callsign; +} + +std::string CRemoteControlStarNetGroup::getLogoff() const +{ + return m_logoff; +} + +unsigned int CRemoteControlStarNetGroup::getTimer() const +{ + return m_timer; +} + +unsigned int CRemoteControlStarNetGroup::getTimeout() const +{ + return m_timeout; +} + +unsigned int CRemoteControlStarNetGroup::getUserCount() const +{ + return m_users.size(); +} + +CRemoteControlStarNetUser * CRemoteControlStarNetGroup::getUser(unsigned int n) const +{ + return m_users[n]; +} diff --git a/DGWRemoteControl/RemoteControlStarNetGroup.h b/DGWRemoteControl/RemoteControlStarNetGroup.h new file mode 100644 index 0000000..351aea3 --- /dev/null +++ b/DGWRemoteControl/RemoteControlStarNetGroup.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2011 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 RemoteControlStarNetGroup_H +#define RemoteControlStarNetGroup_H + +#include + +#include "RemoteControlStarNetUser.h" + + + +class CRemoteControlStarNetGroup { +public: + CRemoteControlStarNetGroup(const std::string& callsign, const std::string& logoff, uint32_t timer, uint32_t timeout); + ~CRemoteControlStarNetGroup(); + + void addUser(const std::string& callsign, uint32_t timer, uint32_t timeout); + + std::string getCallsign() const; + std::string getLogoff() const; + unsigned int getTimer() const; + unsigned int getTimeout() const; + + unsigned int getUserCount() const; + CRemoteControlStarNetUser * getUser(unsigned int n) const; + +private: + std::string m_callsign; + std::string m_logoff; + unsigned int m_timer; + unsigned int m_timeout; + std::vector m_users; +}; + +#endif diff --git a/DGWRemoteControl/RemoteControlStarNetUser.cpp b/DGWRemoteControl/RemoteControlStarNetUser.cpp new file mode 100644 index 0000000..e692901 --- /dev/null +++ b/DGWRemoteControl/RemoteControlStarNetUser.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2011 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 "RemoteControlStarNetUser.h" + +CRemoteControlStarNetUser::CRemoteControlStarNetUser(const std::string& callsign, uint32_t timer, uint32_t timeout) : +m_callsign(callsign), +m_timer((unsigned int)timer), +m_timeout((unsigned int)timeout) +{ +} + +CRemoteControlStarNetUser::~CRemoteControlStarNetUser() +{ +} + +std::string CRemoteControlStarNetUser::getCallsign() const +{ + return m_callsign; +} + +unsigned int CRemoteControlStarNetUser::getTimer() const +{ + return m_timer; +} + +unsigned int CRemoteControlStarNetUser::getTimeout() const +{ + return m_timeout; +} diff --git a/DGWRemoteControl/RemoteControlStarNetUser.h b/DGWRemoteControl/RemoteControlStarNetUser.h new file mode 100644 index 0000000..df42db7 --- /dev/null +++ b/DGWRemoteControl/RemoteControlStarNetUser.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2011 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 RemoteControlStarNetUser_H +#define RemoteControlStarNetUser_H + +#include + +class CRemoteControlStarNetUser { +public: + CRemoteControlStarNetUser(const std::string& callsign, uint32_t timer, uint32_t timeout); + ~CRemoteControlStarNetUser(); + + std::string getCallsign() const; + unsigned int getTimer() const; + unsigned int getTimeout() const; + +private: + std::string m_callsign; + unsigned int m_timer; + unsigned int m_timeout; +}; + +#endif diff --git a/DGWRemoteControl/dgwremotecontrol_example.cfg b/DGWRemoteControl/dgwremotecontrol_example.cfg new file mode 100644 index 0000000..40e1395 --- /dev/null +++ b/DGWRemoteControl/dgwremotecontrol_example.cfg @@ -0,0 +1,12 @@ +# You can specifiy up to 4 sections with different target gateways +# When invoking dgwremotecontrol without name parameter, the first gateway of the config file is used + +[gateway_1] +address=127.0.0.1 +port=4242 +password=CHANGE_ME + +[gateway_2] +address=127.0.0.1 +port=4242 +password=CHANGE_ME