MMDVMHost-Private/YSFControl.cpp

531 lines
12 KiB
C++
Raw Normal View History

2016-02-16 07:27:13 +00:00
/*
* Copyright (C) 2015,2016 Jonathan Naylor, G4KLX
*
* 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; version 2 of the License.
*
* 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.
*/
#include "YSFControl.h"
2016-04-21 17:00:26 +00:00
#include "YSFFICH.h"
2016-02-16 07:27:13 +00:00
#include "Utils.h"
#include "Sync.h"
#include "Log.h"
2016-03-07 20:21:55 +00:00
#include <cstdio>
2016-02-16 07:27:13 +00:00
#include <cassert>
2016-04-21 17:00:26 +00:00
#include <cstring>
2016-02-16 07:27:13 +00:00
#include <ctime>
// #define DUMP_YSF
2016-05-16 20:57:32 +00:00
CYSFControl::CYSFControl(const std::string& callsign, CYSFNetwork* network, CDisplay* display, unsigned int timeout, bool duplex) :
m_network(network),
2016-02-16 07:27:13 +00:00
m_display(display),
m_duplex(duplex),
2016-05-10 17:54:35 +00:00
m_queue(5000U, "YSF Control"),
2016-05-16 20:57:32 +00:00
m_rfState(RS_RF_LISTENING),
m_netState(RS_NET_IDLE),
m_rfTimeoutTimer(1000U, timeout),
m_netTimeoutTimer(1000U, timeout),
m_networkWatchdog(1000U, 0U, 1500U),
m_holdoffTimer(1000U, 0U, 500U),
m_rfFrames(0U),
m_netFrames(0U),
m_rfErrs(0U),
m_rfBits(0U),
m_rfSource(NULL),
m_rfDest(NULL),
m_netSource(NULL),
m_netDest(NULL),
2016-02-29 19:52:11 +00:00
m_payload(),
2016-02-16 07:27:13 +00:00
m_fp(NULL)
{
assert(display != NULL);
2016-02-22 21:13:48 +00:00
2016-02-29 19:52:11 +00:00
m_payload.setUplink(callsign);
m_payload.setDownlink(callsign);
m_netSource = new unsigned char[YSF_CALLSIGN_LENGTH];
m_netDest = new unsigned char[YSF_CALLSIGN_LENGTH];
2016-02-16 07:27:13 +00:00
}
CYSFControl::~CYSFControl()
{
delete[] m_netSource;
delete[] m_netDest;
2016-02-16 07:27:13 +00:00
}
bool CYSFControl::writeModem(unsigned char *data)
{
2016-03-07 20:21:55 +00:00
assert(data != NULL);
2016-02-16 07:27:13 +00:00
unsigned char type = data[0U];
2016-05-16 20:57:32 +00:00
if (type == TAG_LOST && m_rfState == RS_RF_AUDIO) {
LogMessage("YSF, transmission lost, %.1f seconds, BER: %.1f%%", float(m_rfFrames) / 10.0F, float(m_rfErrs * 100U) / float(m_rfBits));
writeEndRF();
2016-02-16 07:27:13 +00:00
return false;
}
if (type == TAG_LOST)
return false;
2016-04-21 17:00:26 +00:00
CYSFFICH fich;
bool valid = fich.decode(data + 2U);
2016-05-16 20:57:32 +00:00
if (valid && m_rfState == RS_RF_LISTENING) {
2016-04-21 17:00:26 +00:00
unsigned char fi = fich.getFI();
if (fi == YSF_FI_TERMINATOR)
return false;
2016-02-16 07:27:13 +00:00
2016-05-17 05:30:59 +00:00
if (m_netState == RS_NET_IDLE)
m_holdoffTimer.stop();
2016-05-16 20:57:32 +00:00
m_rfFrames = 0U;
m_rfErrs = 0U;
m_rfBits = 1U;
m_rfTimeoutTimer.start();
m_payload.reset();
2016-05-16 20:57:32 +00:00
m_rfState = RS_RF_AUDIO;
#if defined(DUMP_YSF)
openFile();
#endif
2016-02-16 07:27:13 +00:00
}
2016-05-16 20:57:32 +00:00
if (m_rfState != RS_RF_AUDIO)
2016-02-16 07:57:46 +00:00
return false;
2016-02-16 07:27:13 +00:00
2016-04-21 17:00:26 +00:00
unsigned char fi = fich.getFI();
if (valid && fi == YSF_FI_HEADER) {
2016-02-16 07:57:46 +00:00
CSync::addYSFSync(data + 2U);
2016-02-16 07:27:13 +00:00
2016-05-16 20:57:32 +00:00
m_rfFrames++;
2016-04-25 17:06:19 +00:00
valid = m_payload.processHeaderData(data + 2U);
2016-04-25 17:06:19 +00:00
2016-04-21 17:00:26 +00:00
if (valid)
m_rfSource = m_payload.getSource();
2016-04-21 17:00:26 +00:00
2016-05-10 19:17:57 +00:00
unsigned char cm = fich.getCM();
2016-04-21 17:00:26 +00:00
if (cm == YSF_CM_GROUP) {
m_rfDest = (unsigned char*)"ALL ";
2016-04-21 17:00:26 +00:00
} else {
if (valid)
m_rfDest = m_payload.getDest();
2016-04-21 17:00:26 +00:00
}
if (m_rfSource != NULL && m_rfDest != NULL) {
m_display->writeFusion((char*)m_rfSource, (char*)m_rfDest, "R", " ");
LogMessage("YSF, received RF header from %10.10s to %10.10s", m_rfSource, m_rfDest);
} else if (m_rfSource == NULL && m_rfDest != NULL) {
m_display->writeFusion("??????????", (char*)m_rfDest, "R", " ");
LogMessage("YSF, received RF header from ?????????? to %10.10s", m_rfDest);
} else if (m_rfSource != NULL && m_rfDest == NULL) {
m_display->writeFusion((char*)m_rfSource, "??????????", "R", " ");
LogMessage("YSF, received RF header from %10.10s to ??????????", m_rfSource);
} else {
m_display->writeFusion("??????????", "??????????", "R", " ");
LogMessage("YSF, received RF header from ?????????? to ??????????");
}
data[0U] = TAG_DATA;
data[1U] = 0x00U;
writeNetwork(data);
2016-04-21 17:00:26 +00:00
#if defined(DUMP_YSF)
writeFile(data + 2U);
#endif
if (m_duplex) {
fich.setMR(YSF_MR_BUSY);
fich.encode(data + 2U);
writeQueueRF(data);
2016-05-16 20:57:32 +00:00
}
2016-04-21 17:00:26 +00:00
} else if (valid && fi == YSF_FI_TERMINATOR) {
CSync::addYSFSync(data + 2U);
2016-05-16 20:57:32 +00:00
m_rfFrames++;
2016-02-16 07:27:13 +00:00
m_payload.processHeaderData(data + 2U);
2016-04-25 17:06:19 +00:00
2016-05-16 20:57:32 +00:00
data[0U] = TAG_EOT;
data[1U] = 0x00U;
writeNetwork(data);
#if defined(DUMP_YSF)
writeFile(data + 2U);
#endif
2016-02-16 07:57:46 +00:00
if (m_duplex) {
2016-04-21 17:00:26 +00:00
fich.setMR(YSF_MR_BUSY);
fich.encode(data + 2U);
2016-05-16 20:57:32 +00:00
writeQueueRF(data);
2016-02-16 07:27:13 +00:00
}
2016-05-16 20:57:32 +00:00
LogMessage("YSF, received RF end of transmission, %.1f seconds, BER: %.1f%%", float(m_rfFrames) / 10.0F, float(m_rfErrs * 100U) / float(m_rfBits));
writeEndRF();
2016-02-16 07:57:46 +00:00
2016-02-16 07:27:13 +00:00
return false;
2016-04-21 17:00:26 +00:00
} else if (valid) {
2016-02-16 07:27:13 +00:00
CSync::addYSFSync(data + 2U);
2016-04-21 17:00:26 +00:00
unsigned char fn = fich.getFN();
unsigned char ft = fich.getFT();
unsigned char dt = fich.getDT();
2016-05-16 20:57:32 +00:00
m_rfFrames++;
2016-04-25 17:06:19 +00:00
switch (dt) {
case YSF_DT_VD_MODE1:
valid = m_payload.processVDMode1Data(data + 2U, fn);
2016-05-16 20:57:32 +00:00
m_rfErrs += m_payload.processVDMode1Audio(data + 2U);
m_rfBits += 235U;
2016-04-25 17:06:19 +00:00
break;
case YSF_DT_VD_MODE2:
valid = m_payload.processVDMode2Data(data + 2U, fn);
2016-05-16 20:57:32 +00:00
m_rfErrs += m_payload.processVDMode2Audio(data + 2U);
m_rfBits += 135U;
2016-04-25 17:06:19 +00:00
break;
case YSF_DT_DATA_FR_MODE:
valid = m_payload.processDataFRModeData(data + 2U, fn);
break;
2016-05-10 18:24:27 +00:00
case YSF_DT_VOICE_FR_MODE:
2016-04-25 19:10:03 +00:00
if (fn != 0U || ft != 1U) {
2016-04-25 17:06:19 +00:00
// The first packet after the header is odd, don't try and regenerate it
2016-05-16 20:57:32 +00:00
m_rfErrs += m_payload.processVoiceFRModeAudio(data + 2U);
m_rfBits += 720U;
2016-04-25 17:06:19 +00:00
}
2016-04-26 05:24:48 +00:00
valid = false;
2016-04-25 17:06:19 +00:00
break;
2016-05-10 18:24:27 +00:00
default:
break;
2016-04-25 17:06:19 +00:00
}
2016-04-21 17:00:26 +00:00
bool change = false;
if (m_rfDest == NULL) {
2016-05-10 19:17:57 +00:00
unsigned char cm = fich.getCM();
2016-04-21 17:00:26 +00:00
if (cm == YSF_CM_GROUP) {
m_rfDest = (unsigned char*)"ALL ";
2016-04-21 17:00:26 +00:00
change = true;
2016-04-25 19:10:03 +00:00
} else if (valid) {
m_rfDest = m_payload.getDest();
if (m_rfDest != NULL)
2016-04-19 06:45:49 +00:00
change = true;
}
2016-04-21 17:00:26 +00:00
}
if (valid && m_rfSource == NULL) {
m_rfSource = m_payload.getSource();
if (m_rfSource != NULL)
2016-04-21 17:00:26 +00:00
change = true;
}
2016-04-21 17:00:26 +00:00
if (change) {
if (m_rfSource != NULL && m_rfDest != NULL) {
m_display->writeFusion((char*)m_rfSource, (char*)m_rfDest, "R", " ");
LogMessage("YSF, received RF data from %10.10s to %10.10s", m_rfSource, m_rfDest);
2016-04-21 17:00:26 +00:00
}
if (m_rfSource != NULL && m_rfDest == NULL) {
m_display->writeFusion((char*)m_rfSource, "??????????", "R", " ");
LogMessage("YSF, received RF data from %10.10s to ??????????", m_rfSource);
2016-04-21 17:00:26 +00:00
}
if (m_rfSource == NULL && m_rfDest != NULL) {
m_display->writeFusion("??????????", (char*)m_rfDest, "R", " ");
LogMessage("YSF, received RF data from ?????????? to %10.10s", m_rfDest);
}
}
2016-05-16 20:57:32 +00:00
data[0U] = TAG_DATA;
data[1U] = 0x00U;
writeNetwork(data);
2016-02-16 07:27:13 +00:00
if (m_duplex) {
2016-04-21 17:00:26 +00:00
fich.setMR(YSF_MR_BUSY);
fich.encode(data + 2U);
2016-05-16 20:57:32 +00:00
writeQueueRF(data);
2016-02-16 07:27:13 +00:00
}
2016-04-21 17:00:26 +00:00
#if defined(DUMP_YSF)
writeFile(data + 2U);
#endif
} else {
CSync::addYSFSync(data + 2U);
2016-05-16 20:57:32 +00:00
m_rfFrames++;
2016-04-21 17:00:26 +00:00
2016-05-16 20:57:32 +00:00
data[0U] = TAG_DATA;
data[1U] = 0x00U;
writeNetwork(data);
2016-04-21 17:00:26 +00:00
2016-05-16 20:57:32 +00:00
if (m_duplex)
writeQueueRF(data);
2016-04-21 17:00:26 +00:00
#if defined(DUMP_YSF)
writeFile(data + 2U);
#endif
2016-02-16 07:27:13 +00:00
}
return true;
}
unsigned int CYSFControl::readModem(unsigned char* data)
{
2016-03-07 20:21:55 +00:00
assert(data != NULL);
2016-02-16 07:27:13 +00:00
if (m_queue.isEmpty())
return 0U;
2016-05-16 20:57:32 +00:00
// Don't relay data until the timer has stopped.
if (m_holdoffTimer.isRunning())
return 0U;
2016-02-16 07:27:13 +00:00
unsigned char len = 0U;
m_queue.getData(&len, 1U);
m_queue.getData(data, len);
return len;
}
2016-05-16 20:57:32 +00:00
void CYSFControl::writeEndRF()
2016-02-16 07:27:13 +00:00
{
2016-05-16 20:57:32 +00:00
m_rfState = RS_RF_LISTENING;
2016-02-16 07:27:13 +00:00
2016-05-17 05:13:09 +00:00
m_rfTimeoutTimer.stop();
m_payload.reset();
// These variables are free'd by YSFPayload
m_rfSource = NULL;
m_rfDest = NULL;
2016-02-16 07:27:13 +00:00
2016-05-17 05:13:09 +00:00
if (m_netState == RS_NET_IDLE) {
2016-05-16 20:57:32 +00:00
m_display->clearFusion();
2016-05-16 20:57:32 +00:00
if (m_network != NULL)
m_network->reset();
}
2016-02-16 07:27:13 +00:00
#if defined(DUMP_YSF)
closeFile();
#endif
}
2016-05-16 20:57:32 +00:00
void CYSFControl::writeEndNet()
{
m_netState = RS_NET_IDLE;
m_netTimeoutTimer.stop();
m_networkWatchdog.stop();
m_display->clearFusion();
if (m_network != NULL)
m_network->reset();
}
void CYSFControl::writeNetwork()
{
unsigned char data[200U];
unsigned int length = m_network->read(data);
if (length == 0U)
return;
if (m_rfState != RS_RF_LISTENING && m_netState == RS_NET_IDLE)
return;
m_networkWatchdog.start();
if (!m_netTimeoutTimer.isRunning()) {
if (::memcmp(data + 14U, " ", YSF_CALLSIGN_LENGTH) != 0)
::memcpy(m_netSource, data + 14U, YSF_CALLSIGN_LENGTH);
else
::memcpy(m_netSource, "??????????", YSF_CALLSIGN_LENGTH);
if (::memcmp(data + 24U, " ", YSF_CALLSIGN_LENGTH) != 0)
::memcpy(m_netDest, data + 24U, YSF_CALLSIGN_LENGTH);
else
::memcpy(m_netDest, "??????????", YSF_CALLSIGN_LENGTH);
m_display->writeFusion((char*)m_netSource, (char*)m_netDest, "N", (char*)(data + 4U));
LogMessage("YSF, received network data from %10.10s to %10.10s at %10.10s", m_netSource, m_netDest, data + 4U);
2016-05-16 20:57:32 +00:00
m_netTimeoutTimer.start();
2016-05-16 21:07:18 +00:00
m_holdoffTimer.start();
2016-05-16 20:57:32 +00:00
m_netState = RS_NET_AUDIO;
m_netFrames = 0U;
} else {
bool changed = false;
if (::memcmp(data + 14U, " ", YSF_CALLSIGN_LENGTH) != 0 && ::memcmp(m_netSource, "??????????", YSF_CALLSIGN_LENGTH) == 0) {
::memcpy(m_netSource, data + 14U, YSF_CALLSIGN_LENGTH);
changed = true;
}
if (::memcmp(data + 24U, " ", YSF_CALLSIGN_LENGTH) != 0 && ::memcmp(m_netDest, "??????????", YSF_CALLSIGN_LENGTH) == 0) {
::memcpy(m_netDest, data + 24U, YSF_CALLSIGN_LENGTH);
changed = true;
}
if (changed) {
m_display->writeFusion((char*)m_netSource, (char*)m_netDest, "N", (char*)(data + 4U));
LogMessage("YSF, received network data from %10.10s to %10.10s from %10.10s", m_netSource, m_netDest, data + 4U);
}
2016-05-16 20:57:32 +00:00
}
m_netFrames++;
bool end = data[34U] == 0x01U;
data[33U] = end ? TAG_EOT : TAG_DATA;
data[34U] = 0x00U;
2016-05-17 05:30:59 +00:00
CYSFFICH fich;
bool valid = fich.decode(data + 35U);
2016-05-17 05:30:59 +00:00
if (valid) {
// XXX Should set the downlink callsign
fich.setVoIP(true);
fich.setMR(YSF_MR_NOT_BUSY);
fich.encode(data + 2U);
}
writeQueueNet(data + 33U);
2016-05-16 20:57:32 +00:00
if (end) {
2016-05-16 20:57:32 +00:00
LogMessage("YSF, received network end of transmission, %.1f seconds", float(m_netFrames) / 10.0F);
writeEndNet();
}
}
void CYSFControl::clock(unsigned int ms)
2016-02-16 07:27:13 +00:00
{
2016-05-16 20:57:32 +00:00
if (m_network != NULL)
writeNetwork();
m_holdoffTimer.clock(ms);
if (m_holdoffTimer.isRunning() && m_holdoffTimer.hasExpired())
m_holdoffTimer.stop();
m_rfTimeoutTimer.clock(ms);
m_netTimeoutTimer.clock(ms);
if (m_netState == RS_NET_AUDIO) {
m_networkWatchdog.clock(ms);
if (m_networkWatchdog.hasExpired()) {
LogMessage("YSF, network watchdog has expired, %.1f seconds", float(m_netFrames) / 10.0F);
writeEndNet();
}
}
2016-02-16 07:27:13 +00:00
}
2016-05-16 20:57:32 +00:00
void CYSFControl::writeQueueRF(const unsigned char *data)
2016-02-16 07:27:13 +00:00
{
assert(data != NULL);
2016-05-16 20:57:32 +00:00
if (m_netState != RS_NET_IDLE)
return;
if (m_rfTimeoutTimer.isRunning() && m_rfTimeoutTimer.hasExpired())
2016-02-16 07:27:13 +00:00
return;
unsigned char len = YSF_FRAME_LENGTH_BYTES + 2U;
unsigned int space = m_queue.freeSpace();
if (space < (len + 1U)) {
LogError("YSF, overflow in the System Fusion RF queue");
return;
}
2016-02-16 07:27:13 +00:00
m_queue.addData(&len, 1U);
m_queue.addData(data, len);
}
2016-05-16 20:57:32 +00:00
void CYSFControl::writeQueueNet(const unsigned char *data)
{
assert(data != NULL);
if (m_netTimeoutTimer.isRunning() && m_netTimeoutTimer.hasExpired())
return;
unsigned char len = YSF_FRAME_LENGTH_BYTES + 2U;
unsigned int space = m_queue.freeSpace();
if (space < (len + 1U)) {
LogError("YSF, overflow in the System Fusion RF queue");
return;
}
m_queue.addData(&len, 1U);
m_queue.addData(data, len);
}
void CYSFControl::writeNetwork(const unsigned char *data)
{
assert(data != NULL);
if (m_network == NULL)
return;
if (m_rfTimeoutTimer.isRunning() && m_rfTimeoutTimer.hasExpired())
return;
m_network->write(m_rfSource, m_rfDest, data + 2U, data[0U] == TAG_EOT);
2016-05-16 20:57:32 +00:00
}
2016-02-22 21:13:48 +00:00
2016-02-16 07:27:13 +00:00
bool CYSFControl::openFile()
{
if (m_fp != NULL)
return true;
time_t t;
::time(&t);
struct tm* tm = ::localtime(&t);
char name[100U];
::sprintf(name, "YSF_%04d%02d%02d_%02d%02d%02d.ambe", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
m_fp = ::fopen(name, "wb");
if (m_fp == NULL)
return false;
::fwrite("YSF", 1U, 3U, m_fp);
return true;
}
bool CYSFControl::writeFile(const unsigned char* data)
{
if (m_fp == NULL)
return false;
::fwrite(data, 1U, YSF_FRAME_LENGTH_BYTES, m_fp);
return true;
}
void CYSFControl::closeFile()
{
if (m_fp != NULL) {
::fclose(m_fp);
m_fp = NULL;
}
}