NXDNClients/NXDNGateway/IcomNetwork.cpp

142 lines
3.2 KiB
C++
Raw Normal View History

2018-03-13 18:16:48 -04:00
/*
* Copyright (C) 2009-2014,2016,2018,2020 by Jonathan Naylor G4KLX
2018-03-13 18:16:48 -04: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 "IcomNetwork.h"
#include "Utils.h"
#include "Log.h"
#include <cstdio>
#include <cassert>
#include <cstring>
const unsigned int BUFFER_LENGTH = 200U;
2020-04-06 16:32:50 -04:00
CIcomNetwork::CIcomNetwork(unsigned int localPort, const std::string& rptAddress, unsigned int rptPort, bool debug) :
2018-03-16 03:07:33 -04:00
m_socket(localPort),
2020-04-06 16:32:50 -04:00
m_address(),
m_port(rptPort),
2018-03-16 03:07:33 -04:00
m_debug(debug)
2018-03-13 18:16:48 -04:00
{
2020-04-06 16:32:50 -04:00
assert(localPort > 0U);
assert(!rptAddress.empty());
assert(rptPort > 0U);
m_address = CUDPSocket::lookup(rptAddress);
2018-03-13 18:16:48 -04:00
}
CIcomNetwork::~CIcomNetwork()
{
}
bool CIcomNetwork::open()
{
LogMessage("Opening Icom connection");
if (m_address.s_addr == INADDR_NONE)
return false;
return m_socket.open();
}
2020-04-06 16:32:50 -04:00
bool CIcomNetwork::write(const unsigned char* data, unsigned int length)
2018-03-13 18:16:48 -04:00
{
assert(data != NULL);
unsigned char buffer[110U];
::memset(buffer, 0x00U, 110U);
buffer[0U] = 'I';
buffer[1U] = 'C';
buffer[2U] = 'O';
buffer[3U] = 'M';
buffer[4U] = 0x01U;
buffer[5U] = 0x01U;
buffer[6U] = 0x08U;
buffer[7U] = 0xE0U;
if ((data[0U] & 0xF0U) == 0x90U) {
buffer[37U] = 0x23U;
buffer[38U] = 0x02U;
buffer[39U] = 0x18U;
} else {
buffer[37U] = 0x23U;
buffer[38U] = (data[0U] == 0x81U || data[0U] == 0x83U) ? 0x1CU : 0x10U;
buffer[39U] = 0x21U;
}
2018-03-13 18:16:48 -04:00
::memcpy(buffer + 40U, data, length);
if (m_debug)
CUtils::dump(1U, "Icom Data Sent", buffer, 102U);
2020-04-06 16:32:50 -04:00
return m_socket.write(buffer, 102U, m_address, m_port);
2018-03-13 18:16:48 -04:00
}
2020-05-19 11:45:20 -04:00
unsigned int CIcomNetwork::read(unsigned char* data)
2018-03-13 18:16:48 -04:00
{
2018-03-16 03:07:33 -04:00
assert(data != NULL);
2018-03-13 18:16:48 -04:00
unsigned char buffer[BUFFER_LENGTH];
2020-04-06 16:32:50 -04:00
in_addr address;
unsigned int port;
2018-03-13 18:16:48 -04:00
int length = m_socket.read(buffer, BUFFER_LENGTH, address, port);
if (length <= 0)
2020-05-19 11:45:20 -04:00
return 0U;
2018-03-13 18:16:48 -04:00
2020-04-06 16:32:50 -04:00
if (m_address.s_addr != address.s_addr || m_port != port) {
LogWarning("Icom Data received from an unknown address or port - %08X:%u", ntohl(address.s_addr), port);
2020-05-19 11:45:20 -04:00
return 0U;
2020-04-06 16:32:50 -04:00
}
// Invalid packet type?
if (::memcmp(buffer, "ICOM", 4U) != 0)
2020-05-19 11:45:20 -04:00
return 0U;
2018-03-13 18:16:48 -04:00
// An Icom repeater connect request
if (buffer[4U] == 0x01U && buffer[5U] == 0x61U) {
buffer[5U] = 0x62U;
buffer[37U] = 0x02U;
buffer[38U] = 0x4FU;
buffer[39U] = 0x4BU;
2018-03-16 03:07:33 -04:00
m_socket.write(buffer, length, address, port);
2020-05-19 11:45:20 -04:00
return 0U;
2018-03-13 18:16:48 -04:00
}
if (length != 102)
2020-05-19 11:45:20 -04:00
return 0U;
2018-03-13 18:16:48 -04:00
if (m_debug)
CUtils::dump(1U, "Icom Data Received", buffer, length);
2018-03-16 03:07:33 -04:00
::memcpy(data, buffer + 40U, 33U);
2018-03-13 18:16:48 -04:00
2020-05-19 11:45:20 -04:00
return 33U;
2018-03-13 18:16:48 -04:00
}
void CIcomNetwork::close()
{
m_socket.close();
LogMessage("Closing Icom connection");
}
void CIcomNetwork::clock(unsigned int ms)
{
}