MMDVMHost-Private/YSFNetwork.cpp

207 lines
4.6 KiB
C++
Raw Normal View History

2016-05-16 20:57:32 +00:00
/*
2020-09-06 11:54:08 +00:00
* Copyright (C) 2009-2014,2016,2019,2020 by Jonathan Naylor G4KLX
2016-05-16 20:57:32 +00:00
*
* 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 "YSFDefines.h"
#include "YSFNetwork.h"
#include "Defines.h"
#include "Utils.h"
#include "Log.h"
#include <cstdio>
#include <cassert>
#include <cstring>
const unsigned int BUFFER_LENGTH = 200U;
CYSFNetwork::CYSFNetwork(const std::string& myAddress, unsigned short myPort, const std::string& gatewayAddress, unsigned short gatewayPort, const std::string& callsign, bool debug) :
2016-06-07 20:17:57 +00:00
m_socket(myAddress, myPort),
2020-09-06 11:54:08 +00:00
m_addr(),
m_addrLen(0U),
2016-05-16 20:57:32 +00:00
m_callsign(),
m_debug(debug),
2016-05-16 20:57:32 +00:00
m_enabled(false),
m_buffer(1000U, "YSF Network"),
m_pollTimer(1000U, 5U),
2016-05-16 20:57:32 +00:00
m_tag(NULL)
{
m_callsign = callsign;
m_callsign.resize(YSF_CALLSIGN_LENGTH, ' ');
2020-09-06 11:54:08 +00:00
if (CUDPSocket::lookup(gatewayAddress, gatewayPort, m_addr, m_addrLen) != 0)
m_addrLen = 0U;
2016-05-16 20:57:32 +00:00
m_tag = new unsigned char[YSF_CALLSIGN_LENGTH];
::memset(m_tag, ' ', YSF_CALLSIGN_LENGTH);
}
CYSFNetwork::~CYSFNetwork()
{
delete[] m_tag;
}
bool CYSFNetwork::open()
{
2020-09-06 11:54:08 +00:00
if (m_addrLen == 0U) {
LogError("Unable to resolve the address of the YSF Gateway");
2016-05-16 20:57:32 +00:00
return false;
2020-09-06 11:54:08 +00:00
}
LogMessage("Opening YSF network connection");
2016-05-16 20:57:32 +00:00
m_pollTimer.start();
2020-09-06 11:54:08 +00:00
return m_socket.open(m_addr);
2016-05-16 20:57:32 +00:00
}
2016-06-13 20:29:19 +00:00
bool CYSFNetwork::write(const unsigned char* src, const unsigned char* dest, const unsigned char* data, unsigned int count, bool end)
2016-05-16 20:57:32 +00:00
{
assert(data != NULL);
unsigned char buffer[200U];
buffer[0] = 'Y';
buffer[1] = 'S';
buffer[2] = 'F';
buffer[3] = 'D';
for (unsigned int i = 0U; i < YSF_CALLSIGN_LENGTH; i++)
buffer[i + 4U] = m_callsign.at(i);
if (src != NULL)
::memcpy(buffer + 14U, src, YSF_CALLSIGN_LENGTH);
else
::memset(buffer + 14U, ' ', YSF_CALLSIGN_LENGTH);
if (dest != NULL)
::memcpy(buffer + 24U, dest, YSF_CALLSIGN_LENGTH);
else
::memset(buffer + 24U, ' ', YSF_CALLSIGN_LENGTH);
buffer[34U] = end ? 0x01U : 0x00U;
2016-06-13 20:29:19 +00:00
buffer[34U] |= (count & 0x7FU) << 1;
2016-05-16 20:57:32 +00:00
::memcpy(buffer + 35U, data, YSF_FRAME_LENGTH_BYTES);
if (m_debug)
CUtils::dump(1U, "YSF Network Data Sent", buffer, 155U);
2020-09-06 11:54:08 +00:00
return m_socket.write(buffer, 155U, m_addr, m_addrLen);
2016-05-16 20:57:32 +00:00
}
bool CYSFNetwork::writePoll()
{
unsigned char buffer[20U];
buffer[0] = 'Y';
buffer[1] = 'S';
buffer[2] = 'F';
buffer[3] = 'P';
for (unsigned int i = 0U; i < YSF_CALLSIGN_LENGTH; i++)
buffer[i + 4U] = m_callsign.at(i);
if (m_debug)
CUtils::dump(1U, "YSF Network Poll Sent", buffer, 14U);
2020-09-06 11:54:08 +00:00
return m_socket.write(buffer, 14U, m_addr, m_addrLen);
2016-05-16 20:57:32 +00:00
}
void CYSFNetwork::clock(unsigned int ms)
{
m_pollTimer.clock(ms);
if (m_pollTimer.hasExpired()) {
writePoll();
m_pollTimer.start();
}
unsigned char buffer[BUFFER_LENGTH];
2020-03-27 20:40:35 +00:00
sockaddr_storage address;
2020-09-06 11:54:08 +00:00
unsigned int addrLen;
int length = m_socket.read(buffer, BUFFER_LENGTH, address, addrLen);
if (length <= 0)
return;
if (!CUDPSocket::match(m_addr, address)) {
LogMessage("YSF, packet received from an invalid source");
2016-05-16 20:57:32 +00:00
return;
2020-09-06 11:54:08 +00:00
}
2016-05-16 20:57:32 +00:00
if (m_debug)
CUtils::dump(1U, "YSF Network Data Received", buffer, length);
2016-05-16 20:57:32 +00:00
// Invalid packet type?
if (::memcmp(buffer, "YSFD", 4U) != 0)
return;
if (!m_enabled)
return;
if (::memcmp(m_tag, " ", YSF_CALLSIGN_LENGTH) == 0) {
::memcpy(m_tag, buffer + 4U, YSF_CALLSIGN_LENGTH);
} else {
if (::memcmp(m_tag, buffer + 4U, YSF_CALLSIGN_LENGTH) != 0)
return;
}
2016-06-09 18:51:23 +00:00
bool end = (buffer[34U] & 0x01U) == 0x01U;
2016-05-16 20:57:32 +00:00
if (end)
::memset(m_tag, ' ', YSF_CALLSIGN_LENGTH);
m_buffer.addData(buffer, 155U);
2016-05-16 20:57:32 +00:00
}
unsigned int CYSFNetwork::read(unsigned char* data)
{
assert(data != NULL);
if (m_buffer.isEmpty())
return 0U;
m_buffer.getData(data, 155U);
2016-05-16 20:57:32 +00:00
return 155U;
2016-05-16 20:57:32 +00:00
}
void CYSFNetwork::reset()
{
::memset(m_tag, ' ', YSF_CALLSIGN_LENGTH);
}
bool CYSFNetwork::isConnected() const
{
return (m_addrLen != 0);
}
2016-05-16 20:57:32 +00:00
void CYSFNetwork::close()
{
m_socket.close();
LogMessage("Closing YSF network connection");
}
void CYSFNetwork::enable(bool enabled)
{
if (enabled && !m_enabled)
reset();
else if (!enabled && m_enabled)
m_buffer.clear();
2016-05-16 20:57:32 +00:00
m_enabled = enabled;
}