2018-01-15 21:03:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009-2014,2016,2018 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 "NXDNDefines.h"
|
|
|
|
#include "NXDNNetwork.h"
|
|
|
|
#include "Defines.h"
|
|
|
|
#include "Utils.h"
|
|
|
|
#include "Log.h"
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
const unsigned int BUFFER_LENGTH = 200U;
|
|
|
|
|
|
|
|
CNXDNNetwork::CNXDNNetwork(const std::string& myAddress, unsigned int myPort, const std::string& gatewayAddress, unsigned int gatewayPort, const std::string& callsign, bool debug) :
|
|
|
|
m_socket(myAddress, myPort),
|
|
|
|
m_address(),
|
|
|
|
m_port(gatewayPort),
|
2018-02-06 19:00:01 +00:00
|
|
|
m_callsign(callsign),
|
2018-01-15 21:03:34 +00:00
|
|
|
m_debug(debug),
|
|
|
|
m_enabled(false),
|
|
|
|
m_buffer(1000U, "NXDN Network"),
|
|
|
|
m_pollTimer(1000U, 5U)
|
|
|
|
{
|
2018-02-06 19:00:01 +00:00
|
|
|
m_callsign.resize(10U, ' ');
|
|
|
|
|
2018-01-15 21:03:34 +00:00
|
|
|
m_address = CUDPSocket::lookup(gatewayAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
CNXDNNetwork::~CNXDNNetwork()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CNXDNNetwork::open()
|
|
|
|
{
|
|
|
|
LogMessage("Opening NXDN network connection");
|
|
|
|
|
|
|
|
if (m_address.s_addr == INADDR_NONE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_pollTimer.start();
|
|
|
|
|
|
|
|
return m_socket.open();
|
|
|
|
}
|
|
|
|
|
2018-02-07 18:26:26 +00:00
|
|
|
bool CNXDNNetwork::write(const unsigned char* data, unsigned short src, bool grp, unsigned short dst, bool dat, unsigned char cnt, bool end)
|
2018-01-15 21:03:34 +00:00
|
|
|
{
|
|
|
|
assert(data != NULL);
|
|
|
|
|
2018-01-29 23:32:44 +00:00
|
|
|
unsigned char buffer[100U];
|
2018-01-15 21:03:34 +00:00
|
|
|
|
|
|
|
buffer[0U] = 'N';
|
|
|
|
buffer[1U] = 'X';
|
|
|
|
buffer[2U] = 'D';
|
|
|
|
buffer[3U] = 'N';
|
|
|
|
buffer[4U] = 'D';
|
|
|
|
|
2018-01-29 23:32:44 +00:00
|
|
|
buffer[5U] = (src >> 8) & 0xFFU;
|
2018-02-13 19:07:30 +00:00
|
|
|
buffer[6U] = (src >> 0) & 0xFFU;
|
2018-01-15 21:03:34 +00:00
|
|
|
|
2018-01-31 07:55:59 +00:00
|
|
|
buffer[7U] = grp ? 0x01U : 0x00U;
|
2018-02-06 19:00:01 +00:00
|
|
|
buffer[7U] |= dat ? 0x02U : 0x00U;
|
|
|
|
buffer[7U] |= end ? 0x04U : 0x00U;
|
2018-01-29 23:32:44 +00:00
|
|
|
|
|
|
|
buffer[8U] = (dst >> 8) & 0xFFU;
|
2018-02-13 19:07:30 +00:00
|
|
|
buffer[9U] = (dst >> 0) & 0xFFU;
|
2018-01-29 23:32:44 +00:00
|
|
|
|
2018-01-31 07:55:59 +00:00
|
|
|
buffer[10U] = cnt;
|
2018-01-29 23:32:44 +00:00
|
|
|
|
|
|
|
::memcpy(buffer + 11U, data, NXDN_FRAME_LENGTH_BYTES);
|
2018-01-15 21:03:34 +00:00
|
|
|
|
|
|
|
if (m_debug)
|
2018-01-29 23:32:44 +00:00
|
|
|
CUtils::dump(1U, "NXDN Network Data Sent", buffer, 59U);
|
2018-01-15 21:03:34 +00:00
|
|
|
|
2018-01-29 23:32:44 +00:00
|
|
|
return m_socket.write(buffer, 59U, m_address, m_port);
|
2018-01-15 21:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CNXDNNetwork::writePoll()
|
|
|
|
{
|
|
|
|
unsigned char buffer[20U];
|
|
|
|
|
|
|
|
buffer[0U] = 'N';
|
|
|
|
buffer[1U] = 'X';
|
|
|
|
buffer[2U] = 'D';
|
|
|
|
buffer[3U] = 'N';
|
|
|
|
buffer[4U] = 'P';
|
|
|
|
|
2018-02-06 19:00:01 +00:00
|
|
|
for (unsigned int i = 0U; i < 10U; i++)
|
|
|
|
buffer[5U + i] = m_callsign.at(i);
|
|
|
|
|
2018-01-15 21:03:34 +00:00
|
|
|
if (m_debug)
|
2018-02-06 19:00:01 +00:00
|
|
|
CUtils::dump(1U, "NXDN Network Poll Sent", buffer, 15U);
|
2018-01-15 21:03:34 +00:00
|
|
|
|
2018-02-06 19:00:01 +00:00
|
|
|
return m_socket.write(buffer, 15U, m_address, m_port);
|
2018-01-15 21:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CNXDNNetwork::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("NXDN 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, "NXDNP", 5U) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Invalid packet type?
|
|
|
|
if (::memcmp(buffer, "NXDND", 5U) != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!m_enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_debug)
|
|
|
|
CUtils::dump(1U, "NXDN Network Data Received", buffer, length);
|
|
|
|
|
2018-01-29 23:32:44 +00:00
|
|
|
m_buffer.addData(buffer, 59U);
|
2018-01-15 21:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 18:26:26 +00:00
|
|
|
unsigned int CNXDNNetwork::read(unsigned char* data, unsigned short& src, bool& grp, unsigned short& dst, bool& dat, unsigned char& cnt, bool& end)
|
2018-01-15 21:03:34 +00:00
|
|
|
{
|
|
|
|
assert(data != NULL);
|
|
|
|
|
|
|
|
if (m_buffer.isEmpty())
|
|
|
|
return 0U;
|
|
|
|
|
2018-01-29 23:32:44 +00:00
|
|
|
unsigned char buffer[100U];
|
|
|
|
m_buffer.getData(buffer, 59U);
|
|
|
|
|
|
|
|
src = (buffer[5U] << 8) + buffer[6U];
|
|
|
|
grp = (buffer[7U] & 0x01U) == 0x01U;
|
2018-02-06 19:00:01 +00:00
|
|
|
dat = (buffer[7U] & 0x02U) == 0x02U;
|
|
|
|
end = (buffer[7U] & 0x04U) == 0x04U;
|
2018-01-29 23:32:44 +00:00
|
|
|
dst = (buffer[8U] << 8) + buffer[9U];
|
|
|
|
|
2018-01-31 07:55:59 +00:00
|
|
|
cnt = buffer[10U];
|
2018-01-29 23:32:44 +00:00
|
|
|
|
|
|
|
::memcpy(data, buffer + 11U, NXDN_FRAME_LENGTH_BYTES);
|
2018-01-15 21:03:34 +00:00
|
|
|
|
2018-01-29 23:32:44 +00:00
|
|
|
return NXDN_FRAME_LENGTH_BYTES;
|
2018-01-15 21:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CNXDNNetwork::reset()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CNXDNNetwork::close()
|
|
|
|
{
|
|
|
|
m_socket.close();
|
|
|
|
|
|
|
|
LogMessage("Closing NXDN network connection");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CNXDNNetwork::enable(bool enabled)
|
|
|
|
{
|
|
|
|
if (enabled && !m_enabled)
|
|
|
|
reset();
|
|
|
|
|
|
|
|
m_enabled = enabled;
|
|
|
|
}
|