2016-05-16 20:57:32 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009-2014,2016 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 "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;
|
|
|
|
|
2016-06-07 20:17:57 +00:00
|
|
|
CYSFNetwork::CYSFNetwork(const std::string& myAddress, unsigned int myPort, const std::string& gwyAddress, unsigned int gwyPort, const std::string& callsign, bool debug) :
|
|
|
|
m_socket(myAddress, myPort),
|
2016-05-16 20:57:32 +00:00
|
|
|
m_address(),
|
2016-06-07 20:17:57 +00:00
|
|
|
m_port(gwyPort),
|
2016-05-16 20:57:32 +00:00
|
|
|
m_callsign(),
|
2016-05-17 09:03:59 +00:00
|
|
|
m_debug(debug),
|
2016-05-16 20:57:32 +00:00
|
|
|
m_enabled(false),
|
|
|
|
m_buffer(1000U, "YSF Network"),
|
2016-05-21 11:19:25 +00:00
|
|
|
m_pollTimer(1000U, 5U),
|
2016-05-16 20:57:32 +00:00
|
|
|
m_tag(NULL)
|
|
|
|
{
|
|
|
|
m_callsign = callsign;
|
|
|
|
m_callsign.resize(YSF_CALLSIGN_LENGTH, ' ');
|
|
|
|
|
2016-06-07 20:17:57 +00:00
|
|
|
m_address = CUDPSocket::lookup(gwyAddress);
|
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()
|
|
|
|
{
|
|
|
|
LogMessage("Opening YSF network connection");
|
|
|
|
|
|
|
|
if (m_address.s_addr == INADDR_NONE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_pollTimer.start();
|
|
|
|
|
|
|
|
return m_socket.open();
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
return m_socket.write(buffer, 155U, m_address, m_port);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
return m_socket.write(buffer, 14U, m_address, m_port);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CYSFNetwork::clock(unsigned int ms)
|
|
|
|
{
|
|
|
|
m_pollTimer.clock(ms);
|
|
|
|
if (m_pollTimer.hasExpired()) {
|
|
|
|
writePoll();
|
|
|
|
m_pollTimer.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char buffer[BUFFER_LENGTH];
|
|
|
|
|
|
|
|
in_addr address;
|
|
|
|
unsigned int port;
|
|
|
|
int length = m_socket.read(buffer, BUFFER_LENGTH, address, port);
|
|
|
|
if (length <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Check if the data is for us
|
|
|
|
if (m_address.s_addr != address.s_addr || m_port != port) {
|
|
|
|
LogMessage("YSF packet received from an invalid source, %08X != %08X and/or %u != %u", m_address.s_addr, address.s_addr, m_port, port);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore incoming polls
|
|
|
|
if (::memcmp(buffer, "YSFP", 4U) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Invalid packet type?
|
|
|
|
if (::memcmp(buffer, "YSFD", 4U) != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!m_enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_debug)
|
|
|
|
CUtils::dump(1U, "YSF Network Data Received", buffer, length);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2016-05-19 18:08:48 +00:00
|
|
|
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;
|
|
|
|
|
2016-05-19 18:08:48 +00:00
|
|
|
m_buffer.getData(data, 155U);
|
2016-05-16 20:57:32 +00:00
|
|
|
|
2016-05-19 18:08:48 +00:00
|
|
|
return 155U;
|
2016-05-16 20:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CYSFNetwork::reset()
|
|
|
|
{
|
|
|
|
::memset(m_tag, ' ', YSF_CALLSIGN_LENGTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CYSFNetwork::close()
|
|
|
|
{
|
|
|
|
m_socket.close();
|
|
|
|
|
|
|
|
LogMessage("Closing YSF network connection");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CYSFNetwork::enable(bool enabled)
|
|
|
|
{
|
|
|
|
if (enabled && !m_enabled)
|
|
|
|
reset();
|
|
|
|
|
|
|
|
m_enabled = enabled;
|
|
|
|
}
|