Improve the ability of the logging in to the DMR Master when the

connection is lost.
This commit is contained in:
Jonathan Naylor 2021-03-13 12:24:14 +00:00
parent d4f7d11bdd
commit 9725179f85
3 changed files with 82 additions and 87 deletions

View File

@ -31,6 +31,8 @@ const unsigned int HOMEBREW_DATA_PACKET_LENGTH = 55U;
CDMRNetwork::CDMRNetwork(const std::string& address, unsigned int port, unsigned int local, unsigned int id, const std::string& password, const std::string& name, bool location, bool debug) : CDMRNetwork::CDMRNetwork(const std::string& address, unsigned int port, unsigned int local, unsigned int id, const std::string& password, const std::string& name, bool location, bool debug) :
m_address(address),
m_port(port),
m_addr(), m_addr(),
m_addrLen(0U), m_addrLen(0U),
m_id(NULL), m_id(NULL),
@ -55,9 +57,6 @@ m_beacon(false)
assert(id > 1000U); assert(id > 1000U);
assert(!password.empty()); assert(!password.empty());
if (CUDPSocket::lookup(address, port, m_addr, m_addrLen) != 0)
m_addrLen = 0U;
m_buffer = new unsigned char[BUFFER_LENGTH]; m_buffer = new unsigned char[BUFFER_LENGTH];
m_salt = new unsigned char[sizeof(uint32_t)]; m_salt = new unsigned char[sizeof(uint32_t)];
m_id = new uint8_t[4U]; m_id = new uint8_t[4U];
@ -90,7 +89,7 @@ void CDMRNetwork::setConfig(const unsigned char* data, unsigned int len)
bool CDMRNetwork::open() bool CDMRNetwork::open()
{ {
if (m_addrLen == 0U) { if (CUDPSocket::lookup(m_address, m_port, m_addr, m_addrLen) != 0) {
LogError("%s, Could not lookup the address of the master", m_name.c_str()); LogError("%s, Could not lookup the address of the master", m_name.c_str());
return false; return false;
} }
@ -304,25 +303,38 @@ void CDMRNetwork::close()
void CDMRNetwork::clock(unsigned int ms) void CDMRNetwork::clock(unsigned int ms)
{ {
if (m_status == WAITING_CONNECT) {
m_retryTimer.clock(ms); m_retryTimer.clock(ms);
if (m_retryTimer.isRunning() && m_retryTimer.hasExpired()) { if (m_retryTimer.isRunning() && m_retryTimer.hasExpired()) {
bool ret = m_socket.open(m_addr); switch (m_status) {
if (ret) { case WAITING_CONNECT:
ret = writeLogin(); if (m_socket.open(m_addr.ss_family)) {
if (!ret) if (writeLogin()) {
return;
m_status = WAITING_LOGIN; m_status = WAITING_LOGIN;
m_timeoutTimer.start(); }
}
break;
case WAITING_LOGIN:
writeLogin();
break;
case WAITING_AUTHORISATION:
writeAuthorisation();
break;
case WAITING_OPTIONS:
writeOptions();
break;
case WAITING_CONFIG:
writeConfig();
break;
case RUNNING:
writePing();
break;
default:
break;
} }
m_retryTimer.start(); m_retryTimer.start();
} }
return;
}
sockaddr_storage address; sockaddr_storage address;
unsigned int addrlen; unsigned int addrlen;
int length = m_socket.read(m_buffer, BUFFER_LENGTH, address, addrlen); int length = m_socket.read(m_buffer, BUFFER_LENGTH, address, addrlen);
@ -333,14 +345,19 @@ void CDMRNetwork::clock(unsigned int ms)
return; return;
} }
if (m_debug && length > 0) if (length > 0) {
CUtils::dump(1U, "Network Received", m_buffer, length); if (!CUDPSocket::match(m_addr, address)) {
LogMessage("%s, packet received from an invalid source", m_name.c_str());
return;
}
if (m_debug) {
char buffer[100U];
::sprintf(buffer, "%s, Network Received", m_name.c_str());
CUtils::dump(1U, buffer, m_buffer, length);
}
if (length > 0 && CUDPSocket::match(m_addr, address)) {
if (::memcmp(m_buffer, "DMRD", 4U) == 0) { if (::memcmp(m_buffer, "DMRD", 4U) == 0) {
if (m_debug)
CUtils::dump(1U, "Network Received", m_buffer, length);
unsigned char len = length; unsigned char len = length;
m_rxData.addData(&len, 1U); m_rxData.addData(&len, 1U);
m_rxData.addData(m_buffer, len); m_rxData.addData(m_buffer, len);
@ -380,7 +397,8 @@ void CDMRNetwork::clock(unsigned int ms)
if (m_options.empty()) { if (m_options.empty()) {
LogMessage("%s, Logged into the master successfully", m_name.c_str()); LogMessage("%s, Logged into the master successfully", m_name.c_str());
m_status = RUNNING; m_status = RUNNING;
} else { }
else {
LogDebug("%s, Sending options", m_name.c_str()); LogDebug("%s, Sending options", m_name.c_str());
writeOptions(); writeOptions();
m_status = WAITING_OPTIONS; m_status = WAITING_OPTIONS;
@ -412,31 +430,6 @@ void CDMRNetwork::clock(unsigned int ms)
} }
} }
m_retryTimer.clock(ms);
if (m_retryTimer.isRunning() && m_retryTimer.hasExpired()) {
switch (m_status) {
case WAITING_LOGIN:
writeLogin();
break;
case WAITING_AUTHORISATION:
writeAuthorisation();
break;
case WAITING_OPTIONS:
writeOptions();
break;
case WAITING_CONFIG:
writeConfig();
break;
case RUNNING:
writePing();
break;
default:
break;
}
m_retryTimer.start();
}
m_timeoutTimer.clock(ms); m_timeoutTimer.clock(ms);
if (m_timeoutTimer.isRunning() && m_timeoutTimer.hasExpired()) { if (m_timeoutTimer.isRunning() && m_timeoutTimer.hasExpired()) {
LogError("%s, Connection to the master has timed out, retrying connection", m_name.c_str()); LogError("%s, Connection to the master has timed out, retrying connection", m_name.c_str());

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2015,2016,2017,2018,2020 by Jonathan Naylor G4KLX * Copyright (C) 2015,2016,2017,2018,2020,2021 by Jonathan Naylor G4KLX
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -58,6 +58,8 @@ public:
void close(); void close();
private: private:
std::string m_address;
unsigned int m_port;
sockaddr_storage m_addr; sockaddr_storage m_addr;
unsigned int m_addrLen; unsigned int m_addrLen;
uint8_t* m_id; uint8_t* m_id;

View File

@ -19,6 +19,6 @@
#if !defined(VERSION_H) #if !defined(VERSION_H)
#define VERSION_H #define VERSION_H
const char* VERSION = "20210309"; const char* VERSION = "20210313";
#endif #endif