2016-01-14 18:45:04 +00:00
|
|
|
/*
|
2019-01-15 21:45:29 +00:00
|
|
|
* Copyright (C) 2015-2019 by Jonathan Naylor G4KLX
|
2016-01-14 18:45:04 +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.
|
|
|
|
*/
|
|
|
|
|
2016-09-14 06:41:37 +00:00
|
|
|
#include "DMRNetwork.h"
|
2016-02-15 18:45:57 +00:00
|
|
|
|
2016-01-14 18:45:04 +00:00
|
|
|
#include "StopWatch.h"
|
|
|
|
#include "SHA256.h"
|
|
|
|
#include "Utils.h"
|
|
|
|
#include "Log.h"
|
|
|
|
|
2016-03-07 20:21:55 +00:00
|
|
|
#include <cstdio>
|
2016-01-14 18:45:04 +00:00
|
|
|
#include <cassert>
|
2018-01-10 19:05:59 +00:00
|
|
|
#include <cstring>
|
2018-07-04 18:00:51 +00:00
|
|
|
#include <cstdlib>
|
2016-01-14 18:45:04 +00:00
|
|
|
|
|
|
|
const unsigned int BUFFER_LENGTH = 500U;
|
|
|
|
|
2016-03-23 17:30:06 +00:00
|
|
|
const unsigned int HOMEBREW_DATA_PACKET_LENGTH = 55U;
|
2016-01-14 18:45:04 +00:00
|
|
|
|
|
|
|
|
2018-02-08 20:25:03 +00:00
|
|
|
CDMRNetwork::CDMRNetwork(const std::string& address, unsigned int port, unsigned int local, unsigned int id, const std::string& password, bool duplex, const char* version, bool debug, bool slot1, bool slot2, HW_TYPE hwType) :
|
2018-07-03 03:35:55 +00:00
|
|
|
m_addressStr(address),
|
2016-01-14 18:45:04 +00:00
|
|
|
m_address(),
|
|
|
|
m_port(port),
|
|
|
|
m_id(NULL),
|
|
|
|
m_password(password),
|
2016-03-08 17:26:51 +00:00
|
|
|
m_duplex(duplex),
|
2016-01-14 18:45:04 +00:00
|
|
|
m_version(version),
|
2016-03-08 17:26:51 +00:00
|
|
|
m_debug(debug),
|
2016-04-03 18:11:45 +00:00
|
|
|
m_socket(local),
|
2016-01-25 18:11:44 +00:00
|
|
|
m_enabled(false),
|
2016-02-15 20:36:05 +00:00
|
|
|
m_slot1(slot1),
|
|
|
|
m_slot2(slot2),
|
2016-09-05 16:40:43 +00:00
|
|
|
m_hwType(hwType),
|
2016-05-16 17:08:10 +00:00
|
|
|
m_status(WAITING_CONNECT),
|
|
|
|
m_retryTimer(1000U, 10U),
|
2016-05-10 06:44:45 +00:00
|
|
|
m_timeoutTimer(1000U, 60U),
|
2016-01-14 18:45:04 +00:00
|
|
|
m_buffer(NULL),
|
|
|
|
m_salt(NULL),
|
|
|
|
m_streamId(NULL),
|
2018-02-08 20:25:03 +00:00
|
|
|
m_rxData(1000U, "DMR Network"),
|
2016-11-03 19:27:52 +00:00
|
|
|
m_options(),
|
2016-01-14 18:45:04 +00:00
|
|
|
m_callsign(),
|
|
|
|
m_rxFrequency(0U),
|
|
|
|
m_txFrequency(0U),
|
|
|
|
m_power(0U),
|
|
|
|
m_colorCode(0U),
|
|
|
|
m_latitude(0.0F),
|
|
|
|
m_longitude(0.0F),
|
|
|
|
m_height(0),
|
|
|
|
m_location(),
|
|
|
|
m_description(),
|
|
|
|
m_url(),
|
|
|
|
m_beacon(false)
|
|
|
|
{
|
|
|
|
assert(!address.empty());
|
|
|
|
assert(port > 0U);
|
|
|
|
assert(id > 1000U);
|
|
|
|
assert(!password.empty());
|
|
|
|
|
|
|
|
m_address = CUDPSocket::lookup(address);
|
|
|
|
|
2018-02-08 20:25:03 +00:00
|
|
|
m_buffer = new unsigned char[BUFFER_LENGTH];
|
|
|
|
m_salt = new unsigned char[sizeof(uint32_t)];
|
|
|
|
m_id = new uint8_t[4U];
|
|
|
|
m_streamId = new uint32_t[2U];
|
2018-02-05 19:46:27 +00:00
|
|
|
|
2016-01-14 18:45:04 +00:00
|
|
|
m_id[0U] = id >> 24;
|
|
|
|
m_id[1U] = id >> 16;
|
|
|
|
m_id[2U] = id >> 8;
|
|
|
|
m_id[3U] = id >> 0;
|
|
|
|
|
|
|
|
CStopWatch stopWatch;
|
|
|
|
::srand(stopWatch.start());
|
2018-01-12 07:06:38 +00:00
|
|
|
|
|
|
|
m_streamId[0U] = ::rand() + 1U;
|
|
|
|
m_streamId[1U] = ::rand() + 1U;
|
2016-01-14 18:45:04 +00:00
|
|
|
}
|
|
|
|
|
2016-09-14 06:41:37 +00:00
|
|
|
CDMRNetwork::~CDMRNetwork()
|
2016-01-14 18:45:04 +00:00
|
|
|
{
|
2017-11-28 10:19:13 +00:00
|
|
|
delete[] m_buffer;
|
2016-01-14 18:45:04 +00:00
|
|
|
delete[] m_salt;
|
|
|
|
delete[] m_streamId;
|
|
|
|
delete[] m_id;
|
|
|
|
}
|
|
|
|
|
2016-11-03 19:27:52 +00:00
|
|
|
void CDMRNetwork::setOptions(const std::string& options)
|
|
|
|
{
|
|
|
|
m_options = options;
|
|
|
|
}
|
|
|
|
|
2016-09-14 06:41:37 +00:00
|
|
|
void CDMRNetwork::setConfig(const std::string& callsign, unsigned int rxFrequency, unsigned int txFrequency, unsigned int power, unsigned int colorCode, float latitude, float longitude, int height, const std::string& location, const std::string& description, const std::string& url)
|
2016-01-14 18:45:04 +00:00
|
|
|
{
|
|
|
|
m_callsign = callsign;
|
|
|
|
m_rxFrequency = rxFrequency;
|
|
|
|
m_txFrequency = txFrequency;
|
|
|
|
m_power = power;
|
|
|
|
m_colorCode = colorCode;
|
|
|
|
m_latitude = latitude;
|
|
|
|
m_longitude = longitude;
|
|
|
|
m_height = height;
|
|
|
|
m_location = location;
|
|
|
|
m_description = description;
|
|
|
|
m_url = url;
|
|
|
|
}
|
|
|
|
|
2016-09-14 06:41:37 +00:00
|
|
|
bool CDMRNetwork::open()
|
2016-01-14 18:45:04 +00:00
|
|
|
{
|
2016-09-29 20:31:58 +00:00
|
|
|
LogMessage("DMR, Opening DMR Network");
|
2018-07-04 18:00:51 +00:00
|
|
|
|
2018-07-03 03:35:55 +00:00
|
|
|
if (m_address.s_addr == INADDR_NONE)
|
|
|
|
m_address = CUDPSocket::lookup(m_addressStr);
|
2018-07-04 18:00:51 +00:00
|
|
|
|
2016-10-31 18:11:32 +00:00
|
|
|
m_status = WAITING_CONNECT;
|
|
|
|
m_timeoutTimer.stop();
|
2016-01-14 18:45:04 +00:00
|
|
|
m_retryTimer.start();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-14 06:41:37 +00:00
|
|
|
void CDMRNetwork::enable(bool enabled)
|
2016-01-25 18:11:44 +00:00
|
|
|
{
|
2019-01-15 21:45:29 +00:00
|
|
|
if (!enabled && m_enabled)
|
|
|
|
m_rxData.clear();
|
|
|
|
|
2016-01-25 18:11:44 +00:00
|
|
|
m_enabled = enabled;
|
|
|
|
}
|
|
|
|
|
2016-09-14 06:41:37 +00:00
|
|
|
bool CDMRNetwork::read(CDMRData& data)
|
2016-01-14 18:45:04 +00:00
|
|
|
{
|
|
|
|
if (m_status != RUNNING)
|
|
|
|
return false;
|
|
|
|
|
2018-02-08 20:25:03 +00:00
|
|
|
if (m_rxData.isEmpty())
|
|
|
|
return false;
|
2018-02-05 19:46:27 +00:00
|
|
|
|
2018-02-08 20:25:03 +00:00
|
|
|
unsigned char length = 0U;
|
|
|
|
m_rxData.getData(&length, 1U);
|
|
|
|
m_rxData.getData(m_buffer, length);
|
2018-02-05 19:46:27 +00:00
|
|
|
|
2018-02-08 20:25:03 +00:00
|
|
|
// Is this a data packet?
|
|
|
|
if (::memcmp(m_buffer, "DMRD", 4U) != 0)
|
|
|
|
return false;
|
2018-01-10 19:05:59 +00:00
|
|
|
|
2018-02-08 20:25:03 +00:00
|
|
|
unsigned char seqNo = m_buffer[4U];
|
2018-01-10 19:05:59 +00:00
|
|
|
|
2018-02-08 20:25:03 +00:00
|
|
|
unsigned int srcId = (m_buffer[5U] << 16) | (m_buffer[6U] << 8) | (m_buffer[7U] << 0);
|
2018-01-10 19:05:59 +00:00
|
|
|
|
2018-02-08 20:25:03 +00:00
|
|
|
unsigned int dstId = (m_buffer[8U] << 16) | (m_buffer[9U] << 8) | (m_buffer[10U] << 0);
|
2018-01-10 19:05:59 +00:00
|
|
|
|
2018-02-08 20:25:03 +00:00
|
|
|
unsigned int slotNo = (m_buffer[15U] & 0x80U) == 0x80U ? 2U : 1U;
|
2018-01-10 19:05:59 +00:00
|
|
|
|
2018-02-08 20:25:03 +00:00
|
|
|
// DMO mode slot disabling
|
|
|
|
if (slotNo == 1U && !m_duplex)
|
|
|
|
return false;
|
2018-01-10 19:05:59 +00:00
|
|
|
|
2018-02-08 20:25:03 +00:00
|
|
|
// Individual slot disabling
|
|
|
|
if (slotNo == 1U && !m_slot1)
|
|
|
|
return false;
|
|
|
|
if (slotNo == 2U && !m_slot2)
|
|
|
|
return false;
|
2017-11-23 11:54:45 +00:00
|
|
|
|
2018-02-08 20:25:03 +00:00
|
|
|
FLCO flco = (m_buffer[15U] & 0x40U) == 0x40U ? FLCO_USER_USER : FLCO_GROUP;
|
|
|
|
|
|
|
|
data.setSeqNo(seqNo);
|
|
|
|
data.setSlotNo(slotNo);
|
|
|
|
data.setSrcId(srcId);
|
|
|
|
data.setDstId(dstId);
|
|
|
|
data.setFLCO(flco);
|
|
|
|
|
|
|
|
bool dataSync = (m_buffer[15U] & 0x20U) == 0x20U;
|
|
|
|
bool voiceSync = (m_buffer[15U] & 0x10U) == 0x10U;
|
|
|
|
|
|
|
|
if (dataSync) {
|
|
|
|
unsigned char dataType = m_buffer[15U] & 0x0FU;
|
|
|
|
data.setData(m_buffer + 20U);
|
|
|
|
data.setDataType(dataType);
|
|
|
|
data.setN(0U);
|
|
|
|
} else if (voiceSync) {
|
|
|
|
data.setData(m_buffer + 20U);
|
|
|
|
data.setDataType(DT_VOICE_SYNC);
|
|
|
|
data.setN(0U);
|
|
|
|
} else {
|
|
|
|
unsigned char n = m_buffer[15U] & 0x0FU;
|
|
|
|
data.setData(m_buffer + 20U);
|
|
|
|
data.setDataType(DT_VOICE);
|
|
|
|
data.setN(n);
|
2016-01-14 18:45:04 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 20:25:03 +00:00
|
|
|
return true;
|
2016-01-14 18:45:04 +00:00
|
|
|
}
|
|
|
|
|
2016-09-14 06:41:37 +00:00
|
|
|
bool CDMRNetwork::write(const CDMRData& data)
|
2016-01-14 18:45:04 +00:00
|
|
|
{
|
|
|
|
if (m_status != RUNNING)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned char buffer[HOMEBREW_DATA_PACKET_LENGTH];
|
|
|
|
::memset(buffer, 0x00U, HOMEBREW_DATA_PACKET_LENGTH);
|
|
|
|
|
|
|
|
buffer[0U] = 'D';
|
|
|
|
buffer[1U] = 'M';
|
|
|
|
buffer[2U] = 'R';
|
|
|
|
buffer[3U] = 'D';
|
|
|
|
|
|
|
|
unsigned int srcId = data.getSrcId();
|
|
|
|
buffer[5U] = srcId >> 16;
|
|
|
|
buffer[6U] = srcId >> 8;
|
|
|
|
buffer[7U] = srcId >> 0;
|
|
|
|
|
|
|
|
unsigned int dstId = data.getDstId();
|
|
|
|
buffer[8U] = dstId >> 16;
|
|
|
|
buffer[9U] = dstId >> 8;
|
|
|
|
buffer[10U] = dstId >> 0;
|
|
|
|
|
|
|
|
::memcpy(buffer + 11U, m_id, 4U);
|
|
|
|
|
|
|
|
unsigned int slotNo = data.getSlotNo();
|
2016-02-15 20:36:05 +00:00
|
|
|
|
|
|
|
// Individual slot disabling
|
|
|
|
if (slotNo == 1U && !m_slot1)
|
|
|
|
return false;
|
|
|
|
if (slotNo == 2U && !m_slot2)
|
|
|
|
return false;
|
|
|
|
|
2016-01-14 18:45:04 +00:00
|
|
|
buffer[15U] = slotNo == 1U ? 0x00U : 0x80U;
|
|
|
|
|
|
|
|
FLCO flco = data.getFLCO();
|
|
|
|
buffer[15U] |= flco == FLCO_GROUP ? 0x00U : 0x40U;
|
|
|
|
|
|
|
|
unsigned int slotIndex = slotNo - 1U;
|
|
|
|
|
|
|
|
unsigned char dataType = data.getDataType();
|
|
|
|
if (dataType == DT_VOICE_SYNC) {
|
|
|
|
buffer[15U] |= 0x10U;
|
|
|
|
} else if (dataType == DT_VOICE) {
|
|
|
|
buffer[15U] |= data.getN();
|
|
|
|
} else {
|
2019-12-15 22:22:47 +00:00
|
|
|
if (dataType == DT_VOICE_LC_HEADER)
|
2018-02-08 20:34:18 +00:00
|
|
|
m_streamId[slotIndex] = ::rand() + 1U;
|
|
|
|
|
2019-12-15 22:22:47 +00:00
|
|
|
if (dataType == DT_CSBK || dataType == DT_DATA_HEADER)
|
2018-02-08 20:34:18 +00:00
|
|
|
m_streamId[slotIndex] = ::rand() + 1U;
|
2016-02-29 06:45:40 +00:00
|
|
|
|
2016-01-14 18:45:04 +00:00
|
|
|
buffer[15U] |= (0x20U | dataType);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[4U] = data.getSeqNo();
|
|
|
|
|
|
|
|
::memcpy(buffer + 16U, m_streamId + slotIndex, 4U);
|
|
|
|
|
|
|
|
data.getData(buffer + 20U);
|
|
|
|
|
2016-03-23 17:30:06 +00:00
|
|
|
buffer[53U] = data.getBER();
|
|
|
|
|
2017-01-05 19:15:10 +00:00
|
|
|
buffer[54U] = data.getRSSI();
|
2016-03-23 17:30:06 +00:00
|
|
|
|
2016-02-18 19:09:10 +00:00
|
|
|
if (m_debug)
|
2016-09-14 06:41:37 +00:00
|
|
|
CUtils::dump(1U, "Network Transmitted", buffer, HOMEBREW_DATA_PACKET_LENGTH);
|
2016-02-18 19:09:10 +00:00
|
|
|
|
2019-12-15 22:22:47 +00:00
|
|
|
write(buffer, HOMEBREW_DATA_PACKET_LENGTH);
|
2016-02-28 17:40:15 +00:00
|
|
|
|
|
|
|
return true;
|
2016-01-14 18:45:04 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 07:53:11 +00:00
|
|
|
bool CDMRNetwork::writeRadioPosition(unsigned int id, const unsigned char* data)
|
2017-05-17 17:16:08 +00:00
|
|
|
{
|
|
|
|
if (m_status != RUNNING)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned char buffer[20U];
|
|
|
|
|
|
|
|
::memcpy(buffer + 0U, "DMRG", 4U);
|
|
|
|
|
|
|
|
::memcpy(buffer + 4U, m_id, 4U);
|
|
|
|
|
|
|
|
buffer[8U] = id >> 16;
|
|
|
|
buffer[9U] = id >> 8;
|
|
|
|
buffer[10U] = id >> 0;
|
|
|
|
|
|
|
|
::memcpy(buffer + 11U, data + 2U, 7U);
|
|
|
|
|
|
|
|
return write(buffer, 18U);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CDMRNetwork::writeTalkerAlias(unsigned int id, unsigned char type, const unsigned char* data)
|
|
|
|
{
|
|
|
|
if (m_status != RUNNING)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned char buffer[20U];
|
|
|
|
|
|
|
|
::memcpy(buffer + 0U, "DMRA", 4U);
|
|
|
|
|
|
|
|
::memcpy(buffer + 4U, m_id, 4U);
|
|
|
|
|
|
|
|
buffer[8U] = id >> 16;
|
|
|
|
buffer[9U] = id >> 8;
|
|
|
|
buffer[10U] = id >> 0;
|
|
|
|
|
|
|
|
buffer[11U] = type;
|
|
|
|
|
|
|
|
::memcpy(buffer + 12U, data + 2U, 7U);
|
|
|
|
|
|
|
|
return write(buffer, 19U);
|
|
|
|
}
|
|
|
|
|
2018-10-30 07:53:11 +00:00
|
|
|
bool CDMRNetwork::writeHomePosition(float latitude, float longitude)
|
|
|
|
{
|
|
|
|
m_latitude = latitude;
|
|
|
|
m_longitude = longitude;
|
|
|
|
|
|
|
|
if (m_status != RUNNING)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char buffer[50U];
|
|
|
|
|
|
|
|
::memcpy(buffer + 0U, "RPTG", 4U);
|
|
|
|
::memcpy(buffer + 4U, m_id, 4U);
|
|
|
|
|
|
|
|
::sprintf(buffer + 8U, "%08f%09f", latitude, longitude);
|
|
|
|
|
|
|
|
return write((unsigned char*)buffer, 25U);
|
|
|
|
}
|
|
|
|
|
2016-09-14 06:41:37 +00:00
|
|
|
void CDMRNetwork::close()
|
2016-01-14 18:45:04 +00:00
|
|
|
{
|
2016-09-29 20:31:58 +00:00
|
|
|
LogMessage("DMR, Closing DMR Network");
|
2016-01-14 18:45:04 +00:00
|
|
|
|
2016-05-09 08:52:14 +00:00
|
|
|
if (m_status == RUNNING) {
|
|
|
|
unsigned char buffer[9U];
|
|
|
|
::memcpy(buffer + 0U, "RPTCL", 5U);
|
|
|
|
::memcpy(buffer + 5U, m_id, 4U);
|
|
|
|
write(buffer, 9U);
|
|
|
|
}
|
2016-01-14 18:45:04 +00:00
|
|
|
|
|
|
|
m_socket.close();
|
2016-05-16 17:08:10 +00:00
|
|
|
|
|
|
|
m_retryTimer.stop();
|
|
|
|
m_timeoutTimer.stop();
|
2016-01-14 18:45:04 +00:00
|
|
|
}
|
|
|
|
|
2016-09-14 06:41:37 +00:00
|
|
|
void CDMRNetwork::clock(unsigned int ms)
|
2016-01-14 18:45:04 +00:00
|
|
|
{
|
2016-05-16 17:08:10 +00:00
|
|
|
if (m_status == WAITING_CONNECT) {
|
|
|
|
m_retryTimer.clock(ms);
|
|
|
|
if (m_retryTimer.isRunning() && m_retryTimer.hasExpired()) {
|
2016-10-31 18:11:32 +00:00
|
|
|
bool ret = m_socket.open();
|
|
|
|
if (ret) {
|
|
|
|
ret = writeLogin();
|
|
|
|
if (!ret)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_status = WAITING_LOGIN;
|
|
|
|
m_timeoutTimer.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_retryTimer.start();
|
2016-05-16 17:08:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-14 18:45:04 +00:00
|
|
|
in_addr address;
|
|
|
|
unsigned int port;
|
|
|
|
int length = m_socket.read(m_buffer, BUFFER_LENGTH, address, port);
|
2016-05-04 07:06:12 +00:00
|
|
|
if (length < 0) {
|
2016-09-29 20:31:58 +00:00
|
|
|
LogError("DMR, Socket has failed, retrying connection to the master");
|
2016-05-04 07:06:12 +00:00
|
|
|
close();
|
2016-10-31 18:11:32 +00:00
|
|
|
open();
|
2016-05-04 07:06:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-01-14 18:45:04 +00:00
|
|
|
|
2016-02-18 19:09:10 +00:00
|
|
|
// if (m_debug && length > 0)
|
2016-09-14 06:41:37 +00:00
|
|
|
// CUtils::dump(1U, "Network Received", m_buffer, length);
|
2016-01-14 18:45:04 +00:00
|
|
|
|
|
|
|
if (length > 0 && m_address.s_addr == address.s_addr && m_port == port) {
|
|
|
|
if (::memcmp(m_buffer, "DMRD", 4U) == 0) {
|
2016-01-25 18:11:44 +00:00
|
|
|
if (m_enabled) {
|
2016-02-18 19:09:10 +00:00
|
|
|
if (m_debug)
|
2016-09-14 06:41:37 +00:00
|
|
|
CUtils::dump(1U, "Network Received", m_buffer, length);
|
2018-02-08 20:25:03 +00:00
|
|
|
|
|
|
|
unsigned char len = length;
|
|
|
|
m_rxData.addData(&len, 1U);
|
|
|
|
m_rxData.addData(m_buffer, len);
|
2016-01-25 18:11:44 +00:00
|
|
|
}
|
2016-01-14 18:45:04 +00:00
|
|
|
} else if (::memcmp(m_buffer, "MSTNAK", 6U) == 0) {
|
2016-01-14 19:26:06 +00:00
|
|
|
if (m_status == RUNNING) {
|
2017-06-08 09:39:46 +00:00
|
|
|
LogWarning("DMR, Login to the master has failed, retrying login ...");
|
2016-01-14 19:26:06 +00:00
|
|
|
m_status = WAITING_LOGIN;
|
|
|
|
m_timeoutTimer.start();
|
|
|
|
m_retryTimer.start();
|
|
|
|
} else {
|
2016-05-14 10:39:56 +00:00
|
|
|
/* Once the modem death spiral has been prevented in Modem.cpp
|
2016-09-14 06:41:37 +00:00
|
|
|
the Network sometimes times out and reaches here.
|
2016-05-14 10:39:56 +00:00
|
|
|
We want it to reconnect so... */
|
2017-06-08 09:39:46 +00:00
|
|
|
LogError("DMR, Login to the master has failed, retrying network ...");
|
2016-05-14 10:39:56 +00:00
|
|
|
close();
|
2016-10-31 18:11:32 +00:00
|
|
|
open();
|
2016-05-14 10:39:56 +00:00
|
|
|
return;
|
2016-01-14 19:26:06 +00:00
|
|
|
}
|
2016-01-14 18:45:04 +00:00
|
|
|
} else if (::memcmp(m_buffer, "RPTACK", 6U) == 0) {
|
|
|
|
switch (m_status) {
|
|
|
|
case WAITING_LOGIN:
|
2016-11-10 17:54:15 +00:00
|
|
|
LogDebug("DMR, Sending authorisation");
|
|
|
|
::memcpy(m_salt, m_buffer + 6U, sizeof(uint32_t));
|
2016-01-14 18:45:04 +00:00
|
|
|
writeAuthorisation();
|
|
|
|
m_status = WAITING_AUTHORISATION;
|
|
|
|
m_timeoutTimer.start();
|
|
|
|
m_retryTimer.start();
|
|
|
|
break;
|
|
|
|
case WAITING_AUTHORISATION:
|
2016-11-10 17:54:15 +00:00
|
|
|
LogDebug("DMR, Sending configuration");
|
|
|
|
writeConfig();
|
|
|
|
m_status = WAITING_CONFIG;
|
|
|
|
m_timeoutTimer.start();
|
|
|
|
m_retryTimer.start();
|
|
|
|
break;
|
|
|
|
case WAITING_CONFIG:
|
2016-11-03 19:27:52 +00:00
|
|
|
if (m_options.empty()) {
|
2016-11-10 17:54:15 +00:00
|
|
|
LogMessage("DMR, Logged into the master successfully");
|
|
|
|
m_status = RUNNING;
|
2016-11-03 19:27:52 +00:00
|
|
|
} else {
|
2016-11-10 17:54:15 +00:00
|
|
|
LogDebug("DMR, Sending options");
|
2016-11-03 19:27:52 +00:00
|
|
|
writeOptions();
|
|
|
|
m_status = WAITING_OPTIONS;
|
|
|
|
}
|
|
|
|
m_timeoutTimer.start();
|
|
|
|
m_retryTimer.start();
|
|
|
|
break;
|
|
|
|
case WAITING_OPTIONS:
|
2016-09-29 20:31:58 +00:00
|
|
|
LogMessage("DMR, Logged into the master successfully");
|
2016-01-14 18:45:04 +00:00
|
|
|
m_status = RUNNING;
|
|
|
|
m_timeoutTimer.start();
|
2016-05-10 07:00:41 +00:00
|
|
|
m_retryTimer.start();
|
2016-01-14 18:45:04 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (::memcmp(m_buffer, "MSTCL", 5U) == 0) {
|
2016-09-29 20:31:58 +00:00
|
|
|
LogError("DMR, Master is closing down");
|
2016-05-03 20:42:51 +00:00
|
|
|
close();
|
2016-10-31 18:11:32 +00:00
|
|
|
open();
|
2016-01-14 18:45:04 +00:00
|
|
|
} else if (::memcmp(m_buffer, "MSTPONG", 7U) == 0) {
|
|
|
|
m_timeoutTimer.start();
|
|
|
|
} else if (::memcmp(m_buffer, "RPTSBKN", 7U) == 0) {
|
|
|
|
m_beacon = true;
|
|
|
|
} else {
|
|
|
|
CUtils::dump("Unknown packet from the master", m_buffer, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 07:00:41 +00:00
|
|
|
m_retryTimer.clock(ms);
|
|
|
|
if (m_retryTimer.isRunning() && m_retryTimer.hasExpired()) {
|
|
|
|
switch (m_status) {
|
|
|
|
case WAITING_LOGIN:
|
|
|
|
writeLogin();
|
|
|
|
break;
|
|
|
|
case WAITING_AUTHORISATION:
|
|
|
|
writeAuthorisation();
|
|
|
|
break;
|
2016-11-03 19:27:52 +00:00
|
|
|
case WAITING_OPTIONS:
|
|
|
|
writeOptions();
|
|
|
|
break;
|
2016-05-10 07:00:41 +00:00
|
|
|
case WAITING_CONFIG:
|
|
|
|
writeConfig();
|
|
|
|
break;
|
|
|
|
case RUNNING:
|
|
|
|
writePing();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2016-01-14 18:45:04 +00:00
|
|
|
}
|
2016-05-10 07:00:41 +00:00
|
|
|
|
|
|
|
m_retryTimer.start();
|
2016-01-14 18:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_timeoutTimer.clock(ms);
|
|
|
|
if (m_timeoutTimer.isRunning() && m_timeoutTimer.hasExpired()) {
|
2016-09-29 20:31:58 +00:00
|
|
|
LogError("DMR, Connection to the master has timed out, retrying connection");
|
2016-05-03 20:42:51 +00:00
|
|
|
close();
|
2016-10-31 18:11:32 +00:00
|
|
|
open();
|
2016-01-14 18:45:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-14 06:41:37 +00:00
|
|
|
bool CDMRNetwork::writeLogin()
|
2016-01-14 18:45:04 +00:00
|
|
|
{
|
|
|
|
unsigned char buffer[8U];
|
|
|
|
|
|
|
|
::memcpy(buffer + 0U, "RPTL", 4U);
|
|
|
|
::memcpy(buffer + 4U, m_id, 4U);
|
|
|
|
|
|
|
|
return write(buffer, 8U);
|
|
|
|
}
|
|
|
|
|
2016-09-14 06:41:37 +00:00
|
|
|
bool CDMRNetwork::writeAuthorisation()
|
2016-01-14 18:45:04 +00:00
|
|
|
{
|
2016-10-31 18:11:32 +00:00
|
|
|
size_t size = m_password.size();
|
2016-01-14 18:45:04 +00:00
|
|
|
|
|
|
|
unsigned char* in = new unsigned char[size + sizeof(uint32_t)];
|
|
|
|
::memcpy(in, m_salt, sizeof(uint32_t));
|
2016-10-31 18:11:32 +00:00
|
|
|
for (size_t i = 0U; i < size; i++)
|
2016-01-14 18:45:04 +00:00
|
|
|
in[i + sizeof(uint32_t)] = m_password.at(i);
|
|
|
|
|
|
|
|
unsigned char out[40U];
|
|
|
|
::memcpy(out + 0U, "RPTK", 4U);
|
|
|
|
::memcpy(out + 4U, m_id, 4U);
|
|
|
|
|
|
|
|
CSHA256 sha256;
|
2016-10-31 18:11:32 +00:00
|
|
|
sha256.buffer(in, (unsigned int)(size + sizeof(uint32_t)), out + 8U);
|
2016-01-14 18:45:04 +00:00
|
|
|
|
|
|
|
delete[] in;
|
|
|
|
|
|
|
|
return write(out, 40U);
|
|
|
|
}
|
|
|
|
|
2016-11-03 19:27:52 +00:00
|
|
|
bool CDMRNetwork::writeOptions()
|
|
|
|
{
|
|
|
|
char buffer[300U];
|
2016-11-10 17:54:15 +00:00
|
|
|
|
|
|
|
::memcpy(buffer + 0U, "RPTO", 4U);
|
|
|
|
::memcpy(buffer + 4U, m_id, 4U);
|
|
|
|
::strcpy(buffer + 8U, m_options.c_str());
|
2016-11-03 19:27:52 +00:00
|
|
|
|
2016-11-11 13:22:01 +00:00
|
|
|
return write((unsigned char*)buffer, (unsigned int)m_options.length() + 8U);
|
2016-11-03 19:27:52 +00:00
|
|
|
}
|
|
|
|
|
2016-09-14 06:41:37 +00:00
|
|
|
bool CDMRNetwork::writeConfig()
|
2016-01-14 18:45:04 +00:00
|
|
|
{
|
2017-12-26 21:10:47 +00:00
|
|
|
const char* software;
|
2016-02-17 07:05:01 +00:00
|
|
|
char slots = '0';
|
2016-03-08 17:26:51 +00:00
|
|
|
if (m_duplex) {
|
|
|
|
if (m_slot1 && m_slot2)
|
|
|
|
slots = '3';
|
|
|
|
else if (m_slot1 && !m_slot2)
|
|
|
|
slots = '1';
|
|
|
|
else if (!m_slot1 && m_slot2)
|
|
|
|
slots = '2';
|
2018-01-10 19:05:59 +00:00
|
|
|
|
2017-12-26 21:10:47 +00:00
|
|
|
switch (m_hwType) {
|
|
|
|
case HWT_MMDVM:
|
|
|
|
software = "MMDVM";
|
|
|
|
break;
|
|
|
|
case HWT_MMDVM_HS:
|
|
|
|
software = "MMDVM_MMDVM_HS";
|
|
|
|
break;
|
2018-03-05 22:46:01 +00:00
|
|
|
case HWT_MMDVM_HS_DUAL_HAT:
|
|
|
|
software = "MMDVM_MMDVM_HS_Dual_Hat";
|
|
|
|
break;
|
2018-05-06 17:51:30 +00:00
|
|
|
case HWT_NANO_HOTSPOT:
|
|
|
|
software = "MMDVM_Nano_hotSPOT";
|
|
|
|
break;
|
2017-12-26 21:10:47 +00:00
|
|
|
default:
|
|
|
|
software = "MMDVM_Unknown";
|
|
|
|
break;
|
|
|
|
}
|
2016-03-08 17:26:51 +00:00
|
|
|
} else {
|
|
|
|
slots = '4';
|
2016-09-05 16:40:43 +00:00
|
|
|
|
|
|
|
switch (m_hwType) {
|
|
|
|
case HWT_MMDVM:
|
|
|
|
software = "MMDVM_DMO";
|
|
|
|
break;
|
|
|
|
case HWT_DVMEGA:
|
|
|
|
software = "MMDVM_DVMega";
|
|
|
|
break;
|
2017-12-26 21:10:47 +00:00
|
|
|
case HWT_MMDVM_ZUMSPOT:
|
|
|
|
software = "MMDVM_ZUMspot";
|
|
|
|
break;
|
|
|
|
case HWT_MMDVM_HS_HAT:
|
|
|
|
software = "MMDVM_MMDVM_HS_Hat";
|
|
|
|
break;
|
2018-03-05 21:04:01 +00:00
|
|
|
case HWT_MMDVM_HS_DUAL_HAT:
|
|
|
|
software = "MMDVM_MMDVM_HS_Dual_Hat";
|
|
|
|
break;
|
2018-02-14 15:18:34 +00:00
|
|
|
case HWT_NANO_HOTSPOT:
|
|
|
|
software = "MMDVM_Nano_hotSPOT";
|
|
|
|
break;
|
2018-05-05 13:42:01 +00:00
|
|
|
case HWT_NANO_DV:
|
|
|
|
software = "MMDVM_Nano_DV";
|
|
|
|
break;
|
2019-05-30 01:22:53 +00:00
|
|
|
case HWT_D2RG_MMDVM_HS:
|
|
|
|
software = "MMDVM_D2RG_MMDVM_HS";
|
|
|
|
break;
|
2017-11-16 19:57:11 +00:00
|
|
|
case HWT_MMDVM_HS:
|
2017-12-26 21:10:47 +00:00
|
|
|
software = "MMDVM_MMDVM_HS";
|
2017-11-16 19:39:20 +00:00
|
|
|
break;
|
2020-01-08 09:58:41 +00:00
|
|
|
case HWT_OPENGD77_HS:
|
|
|
|
software = "MMDVM_OpenGD77_HS";
|
|
|
|
break;
|
2016-09-05 16:40:43 +00:00
|
|
|
default:
|
|
|
|
software = "MMDVM_Unknown";
|
|
|
|
break;
|
|
|
|
}
|
2016-03-08 17:26:51 +00:00
|
|
|
}
|
2016-02-17 07:05:01 +00:00
|
|
|
|
2016-01-14 18:45:04 +00:00
|
|
|
char buffer[400U];
|
|
|
|
|
|
|
|
::memcpy(buffer + 0U, "RPTC", 4U);
|
|
|
|
::memcpy(buffer + 4U, m_id, 4U);
|
|
|
|
|
2016-09-20 17:57:47 +00:00
|
|
|
char latitude[20U];
|
|
|
|
::sprintf(latitude, "%08f", m_latitude);
|
|
|
|
|
|
|
|
char longitude[20U];
|
|
|
|
::sprintf(longitude, "%09f", m_longitude);
|
|
|
|
|
2016-11-24 10:18:28 +00:00
|
|
|
unsigned int power = m_power;
|
|
|
|
if (power > 99U)
|
|
|
|
power = 99U;
|
|
|
|
|
|
|
|
int height = m_height;
|
2016-11-24 10:20:04 +00:00
|
|
|
if (height > 999)
|
|
|
|
height = 999;
|
2016-11-24 10:18:28 +00:00
|
|
|
|
2016-11-10 17:55:33 +00:00
|
|
|
::sprintf(buffer + 8U, "%-8.8s%09u%09u%02u%02u%8.8s%9.9s%03d%-20.20s%-19.19s%c%-124.124s%-40.40s%-40.40s", m_callsign.c_str(),
|
2016-11-24 10:18:28 +00:00
|
|
|
m_rxFrequency, m_txFrequency, power, m_colorCode, latitude, longitude, height, m_location.c_str(),
|
2016-03-08 17:26:51 +00:00
|
|
|
m_description.c_str(), slots, m_url.c_str(), m_version, software);
|
2016-01-14 18:45:04 +00:00
|
|
|
|
|
|
|
return write((unsigned char*)buffer, 302U);
|
|
|
|
}
|
|
|
|
|
2016-09-14 06:41:37 +00:00
|
|
|
bool CDMRNetwork::writePing()
|
2016-01-14 18:45:04 +00:00
|
|
|
{
|
|
|
|
unsigned char buffer[11U];
|
|
|
|
|
|
|
|
::memcpy(buffer + 0U, "RPTPING", 7U);
|
|
|
|
::memcpy(buffer + 7U, m_id, 4U);
|
|
|
|
|
|
|
|
return write(buffer, 11U);
|
|
|
|
}
|
|
|
|
|
2016-09-14 06:41:37 +00:00
|
|
|
bool CDMRNetwork::wantsBeacon()
|
2016-01-14 18:45:04 +00:00
|
|
|
{
|
|
|
|
bool beacon = m_beacon;
|
|
|
|
|
|
|
|
m_beacon = false;
|
|
|
|
|
|
|
|
return beacon;
|
|
|
|
}
|
|
|
|
|
2016-09-14 06:41:37 +00:00
|
|
|
bool CDMRNetwork::write(const unsigned char* data, unsigned int length)
|
2016-01-14 18:45:04 +00:00
|
|
|
{
|
|
|
|
assert(data != NULL);
|
|
|
|
assert(length > 0U);
|
|
|
|
|
2016-02-18 19:09:10 +00:00
|
|
|
// if (m_debug)
|
2016-09-14 06:41:37 +00:00
|
|
|
// CUtils::dump(1U, "Network Transmitted", data, length);
|
2016-01-14 18:45:04 +00:00
|
|
|
|
2016-05-10 06:20:38 +00:00
|
|
|
bool ret = m_socket.write(data, length, m_address, m_port);
|
|
|
|
if (!ret) {
|
2016-09-29 20:31:58 +00:00
|
|
|
LogError("DMR, Socket has failed when writing data to the master, retrying connection");
|
2016-11-18 03:45:10 +00:00
|
|
|
m_socket.close();
|
2016-10-31 18:11:32 +00:00
|
|
|
open();
|
2016-05-10 06:20:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-01-14 18:45:04 +00:00
|
|
|
}
|