2018-03-13 18:16:48 -04:00
|
|
|
/*
|
2021-03-09 16:29:48 -05:00
|
|
|
* Copyright (C) 2009-2014,2016,2018,2020,2021 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.
|
|
|
|
*/
|
|
|
|
|
2020-04-20 11:02:10 -04:00
|
|
|
#include "IcomNetwork.h"
|
2018-03-13 18:16:48 -04:00
|
|
|
#include "Utils.h"
|
|
|
|
#include "Log.h"
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
const unsigned int BUFFER_LENGTH = 200U;
|
|
|
|
|
2020-04-20 11:02:10 -04:00
|
|
|
const unsigned int ICOM_PORT = 41300U;
|
2018-03-13 18:16:48 -04:00
|
|
|
|
2020-04-20 11:02:10 -04:00
|
|
|
CIcomNetwork::CIcomNetwork(const std::string& address, bool debug) :
|
|
|
|
m_socket(ICOM_PORT),
|
2020-09-03 10:12:03 -04:00
|
|
|
m_addr(),
|
2020-09-06 08:45:06 -04:00
|
|
|
m_addrLen(0U),
|
2018-03-22 15:12:02 -04:00
|
|
|
m_debug(debug)
|
2018-03-13 18:16:48 -04:00
|
|
|
{
|
|
|
|
assert(!address.empty());
|
|
|
|
|
2020-09-06 08:45:06 -04:00
|
|
|
if (CUDPSocket::lookup(address, ICOM_PORT, m_addr, m_addrLen) != 0)
|
|
|
|
m_addrLen = 0U;
|
2018-03-13 18:16:48 -04:00
|
|
|
}
|
|
|
|
|
2020-04-20 11:02:10 -04:00
|
|
|
CIcomNetwork::~CIcomNetwork()
|
2018-03-13 18:16:48 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-04-20 11:02:10 -04:00
|
|
|
bool CIcomNetwork::open()
|
2018-03-13 18:16:48 -04:00
|
|
|
{
|
2020-09-06 08:45:06 -04:00
|
|
|
if (m_addrLen == 0U) {
|
|
|
|
LogError("Unable to resolve the address of the Icom network");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-09 16:29:48 -05:00
|
|
|
bool ret = m_socket.open(m_addr);
|
|
|
|
if (!ret) {
|
|
|
|
LogError("Unable to open the Icom network connection");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LogMessage("Opened the Icom network connection");
|
2018-03-13 18:16:48 -04:00
|
|
|
|
2021-03-09 16:29:48 -05:00
|
|
|
return true;
|
2018-03-13 18:16:48 -04:00
|
|
|
}
|
|
|
|
|
2020-04-20 11:02:10 -04:00
|
|
|
bool CIcomNetwork::write(const unsigned char* data, unsigned int len)
|
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;
|
|
|
|
|
2018-05-17 15:32:00 -04:00
|
|
|
if ((data[9U] & 0x02U) == 0x02U) {
|
|
|
|
buffer[37U] = 0x23U;
|
|
|
|
buffer[38U] = 0x02U;
|
|
|
|
buffer[39U] = 0x18U;
|
|
|
|
} else {
|
|
|
|
buffer[37U] = 0x23U;
|
|
|
|
buffer[38U] = (data[9U] & 0x0CU) != 0x00U ? 0x1CU : 0x10U;
|
|
|
|
buffer[39U] = 0x21U;
|
|
|
|
}
|
2018-03-13 18:16:48 -04:00
|
|
|
|
|
|
|
::memcpy(buffer + 40U, data + 10U, 33U);
|
|
|
|
|
|
|
|
if (m_debug)
|
2020-04-20 11:02:10 -04:00
|
|
|
CUtils::dump(1U, "Icom Network Data Sent", buffer, 102U);
|
2018-03-13 18:16:48 -04:00
|
|
|
|
2020-09-03 10:12:03 -04:00
|
|
|
return m_socket.write(buffer, 102U, m_addr, m_addrLen);
|
2018-03-13 18:16:48 -04:00
|
|
|
}
|
|
|
|
|
2020-04-20 11:02:10 -04:00
|
|
|
unsigned int CIcomNetwork::read(unsigned char* data)
|
2018-03-13 18:16:48 -04:00
|
|
|
{
|
|
|
|
unsigned char buffer[BUFFER_LENGTH];
|
|
|
|
|
2020-09-03 10:12:03 -04:00
|
|
|
sockaddr_storage addr;
|
|
|
|
unsigned int addrLen;
|
|
|
|
int length = m_socket.read(buffer, BUFFER_LENGTH, addr, addrLen);
|
2018-03-13 18:16:48 -04:00
|
|
|
if (length <= 0)
|
2018-03-22 15:12:02 -04:00
|
|
|
return 0U;
|
2018-03-13 18:16:48 -04:00
|
|
|
|
|
|
|
// Check if the data is for us
|
2020-09-03 10:12:03 -04:00
|
|
|
if (!CUDPSocket::match(m_addr, addr)) {
|
|
|
|
LogMessage("Icom packet received from an invalid source");
|
2018-03-22 15:12:02 -04:00
|
|
|
return 0U;
|
2018-03-13 18:16:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invalid packet type?
|
|
|
|
if (::memcmp(buffer, "ICOM", 4U) != 0)
|
2018-03-22 15:12:02 -04:00
|
|
|
return 0U;
|
2018-03-13 18:16:48 -04:00
|
|
|
|
|
|
|
if (length != 102)
|
2018-03-22 15:12:02 -04:00
|
|
|
return 0U;
|
2018-03-13 18:16:48 -04:00
|
|
|
|
|
|
|
if (m_debug)
|
2020-04-20 11:02:10 -04:00
|
|
|
CUtils::dump(1U, "Icom Network Data Received", buffer, length);
|
2018-03-13 18:16:48 -04:00
|
|
|
|
2018-03-22 15:12:02 -04:00
|
|
|
::memcpy(data, buffer + 40U, 33U);
|
2018-03-13 18:16:48 -04:00
|
|
|
|
2018-03-22 15:12:02 -04:00
|
|
|
return 33U;
|
2018-03-13 18:16:48 -04:00
|
|
|
}
|
|
|
|
|
2020-04-20 11:02:10 -04:00
|
|
|
void CIcomNetwork::clock(unsigned int ms)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CIcomNetwork::close()
|
2018-03-13 18:16:48 -04:00
|
|
|
{
|
|
|
|
m_socket.close();
|
|
|
|
|
2020-04-20 11:02:10 -04:00
|
|
|
LogMessage("Closing Icom network connection");
|
2018-03-13 18:16:48 -04:00
|
|
|
}
|