DMR networking code cleanup.

This commit is contained in:
Jonathan Naylor 2021-03-09 20:59:04 +00:00
parent 5d8d1a3fb9
commit 79fc7623dc
3 changed files with 75 additions and 92 deletions

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
@ -18,7 +18,6 @@
#include "DMRDirectNetwork.h" #include "DMRDirectNetwork.h"
#include "StopWatch.h"
#include "SHA256.h" #include "SHA256.h"
#include "Utils.h" #include "Utils.h"
#include "Log.h" #include "Log.h"
@ -91,9 +90,6 @@ m_beacon(false)
std::uniform_int_distribution<uint32_t> dist(0x00000001, 0xfffffffe); std::uniform_int_distribution<uint32_t> dist(0x00000001, 0xfffffffe);
m_streamId[0U] = dist(m_random); m_streamId[0U] = dist(m_random);
m_streamId[1U] = dist(m_random); m_streamId[1U] = dist(m_random);
CStopWatch stopWatch;
::srand(stopWatch.start());
} }
CDMRDirectNetwork::~CDMRDirectNetwork() CDMRDirectNetwork::~CDMRDirectNetwork()
@ -340,23 +336,36 @@ void CDMRDirectNetwork::close()
void CDMRDirectNetwork::clock(unsigned int ms) void CDMRDirectNetwork::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()) { switch (m_status) {
bool ret = m_socket.open(m_addr); case WAITING_CONNECT:
if (ret) { if (m_socket.open(m_addr.ss_family)) {
ret = writeLogin(); if (writeLogin()) {
if (!ret) m_status = WAITING_LOGIN;
return; }
m_status = WAITING_LOGIN;
m_timeoutTimer.start();
} }
break;
m_retryTimer.start(); 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;
} }
return; m_retryTimer.start();
} }
sockaddr_storage address; sockaddr_storage address;
@ -369,20 +378,22 @@ void CDMRDirectNetwork::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("DMR, packet received from an invalid source");
return;
}
if (m_debug)
CUtils::dump(1U, "DMR, Network Received", 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, "DMR Network Received", m_buffer, length);
if (m_enabled) { if (m_enabled) {
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);
} }
} else if (::memcmp(m_buffer, "MSTNAK", 6U) == 0) { } else if (::memcmp(m_buffer, "MSTNAK", 6U) == 0) {
if (m_status == RUNNING) { if (m_status == RUNNING) {
LogWarning("DMR, Login to the master has failed, retrying login ..."); LogWarning("DMR, Login to the master has failed, retrying login ...");
m_status = WAITING_LOGIN; m_status = WAITING_LOGIN;
@ -397,45 +408,45 @@ void CDMRDirectNetwork::clock(unsigned int ms)
open(); open();
return; return;
} }
} else if (::memcmp(m_buffer, "RPTACK", 6U) == 0) { } else if (::memcmp(m_buffer, "RPTACK", 6U) == 0) {
switch (m_status) { switch (m_status) {
case WAITING_LOGIN: case WAITING_LOGIN:
LogDebug("DMR, Sending authorisation"); LogDebug("DMR, Sending authorisation");
::memcpy(m_salt, m_buffer + 6U, sizeof(uint32_t)); ::memcpy(m_salt, m_buffer + 6U, sizeof(uint32_t));
writeAuthorisation(); writeAuthorisation();
m_status = WAITING_AUTHORISATION; m_status = WAITING_AUTHORISATION;
m_timeoutTimer.start(); m_timeoutTimer.start();
m_retryTimer.start(); m_retryTimer.start();
break; break;
case WAITING_AUTHORISATION: case WAITING_AUTHORISATION:
LogDebug("DMR, Sending configuration"); LogDebug("DMR, Sending configuration");
writeConfig(); writeConfig();
m_status = WAITING_CONFIG; m_status = WAITING_CONFIG;
m_timeoutTimer.start(); m_timeoutTimer.start();
m_retryTimer.start(); m_retryTimer.start();
break; break;
case WAITING_CONFIG: case WAITING_CONFIG:
if (m_options.empty()) { if (m_options.empty()) {
LogMessage("DMR, Logged into the master successfully");
m_status = RUNNING;
} else {
LogDebug("DMR, Sending options");
writeOptions();
m_status = WAITING_OPTIONS;
}
m_timeoutTimer.start();
m_retryTimer.start();
break;
case WAITING_OPTIONS:
LogMessage("DMR, Logged into the master successfully"); LogMessage("DMR, Logged into the master successfully");
m_status = RUNNING; m_status = RUNNING;
m_timeoutTimer.start(); } else {
m_retryTimer.start(); LogDebug("DMR, Sending options");
break; writeOptions();
default: m_status = WAITING_OPTIONS;
break; }
m_timeoutTimer.start();
m_retryTimer.start();
break;
case WAITING_OPTIONS:
LogMessage("DMR, Logged into the master successfully");
m_status = RUNNING;
m_timeoutTimer.start();
m_retryTimer.start();
break;
default:
break;
} }
} else if (::memcmp(m_buffer, "MSTCL", 5U) == 0) { } else if (::memcmp(m_buffer, "MSTCL", 5U) == 0) {
LogError("DMR, Master is closing down"); LogError("DMR, Master is closing down");
close(); close();
open(); open();
@ -444,37 +455,10 @@ void CDMRDirectNetwork::clock(unsigned int ms)
} else if (::memcmp(m_buffer, "RPTSBKN", 7U) == 0) { } else if (::memcmp(m_buffer, "RPTSBKN", 7U) == 0) {
m_beacon = true; m_beacon = true;
} else { } else {
char buffer[100U]; CUtils::dump("DMR, Unknown packet from the master", m_buffer, length);
::sprintf(buffer, "DMR, Unknown packet from the master");
CUtils::dump(buffer, m_buffer, length);
} }
} }
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("DMR, Connection to the master has timed out, retrying connection"); LogError("DMR, Connection to the master has timed out, retrying connection");

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2015-2020 by Jonathan Naylor G4KLX * Copyright (C) 2015-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
@ -18,7 +18,6 @@
#include "DMRGatewayNetwork.h" #include "DMRGatewayNetwork.h"
#include "StopWatch.h"
#include "Utils.h" #include "Utils.h"
#include "Log.h" #include "Log.h"

View file

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