Add the D-Star networking.

This commit is contained in:
Jonathan Naylor 2016-01-25 21:00:19 +00:00
parent 2f8c8fbba3
commit bdb41fb64e
7 changed files with 466 additions and 7 deletions

340
DStarNetwork.cpp Normal file
View file

@ -0,0 +1,340 @@
/*
* Copyright (C) 2009-2014,2016 by 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; 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.
*/
#include "DStarDefines.h"
#include "DStarNetwork.h"
#include "StopWatch.h"
#include "Defines.h"
#include "Utils.h"
#include "Log.h"
#include <cstdio>
#include <cassert>
const unsigned int BUFFER_LENGTH = 100U;
CDStarNetwork::CDStarNetwork(const std::string& gatewayAddress, unsigned int gatewayPort, unsigned int localPort, bool debug) :
m_socket("", localPort),
m_address(),
m_port(gatewayPort),
m_debug(debug),
m_enabled(false),
m_outId(0U),
m_outSeq(0U),
m_inId(0U),
m_buffer(1000U)
{
m_address = CUDPSocket::lookup(gatewayAddress);
CStopWatch stopWatch;
::srand(stopWatch.start());
}
CDStarNetwork::~CDStarNetwork()
{
}
bool CDStarNetwork::open()
{
if (m_address.s_addr == INADDR_NONE)
return false;
return m_socket.open();
}
bool CDStarNetwork::writeHeader(const unsigned char* header, unsigned int length)
{
assert(header != NULL);
unsigned char buffer[50U];
buffer[0] = 'D';
buffer[1] = 'S';
buffer[2] = 'R';
buffer[3] = 'P';
buffer[4] = 0x20U;
// Create a random id for this transmission
m_outId = (::rand() % 65535U) + 1U;
buffer[5] = m_outId / 256U; // Unique session id
buffer[6] = m_outId % 256U;
buffer[7] = 0U;
::memcpy(buffer + 8U, header, length);
m_outSeq = 0U;
if (m_debug)
CUtils::dump(1U, "Sending D-Star Header", buffer, 49U);
for (unsigned int i = 0U; i < 2U; i++) {
bool ret = m_socket.write(buffer, 49U, m_address, m_port);
if (!ret)
return false;
}
return true;
}
bool CDStarNetwork::writeData(const unsigned char* data, unsigned int length, unsigned int errors, bool end)
{
assert(data != NULL);
unsigned char buffer[30U];
buffer[0] = 'D';
buffer[1] = 'S';
buffer[2] = 'R';
buffer[3] = 'P';
buffer[4] = 0x21U;
buffer[5] = m_outId / 256U; // Unique session id
buffer[6] = m_outId % 256U;
// If this is a data sync, reset the sequence to zero
if (data[9] == 0x55 && data[10] == 0x2D && data[11] == 0x16)
m_outSeq = 0U;
buffer[7] = m_outSeq;
if (end)
buffer[7] |= 0x40U; // End of data marker
buffer[8] = errors;
m_outSeq++;
if (m_outSeq > 0x14U)
m_outSeq = 0U;
::memcpy(buffer + 9U, data, length);
if (m_debug)
CUtils::dump(1U, "Sending D-Star Data", buffer, length + 9U);
return m_socket.write(buffer, length + 9U, m_address, m_port);
}
bool CDStarNetwork::writeBusyHeader(const unsigned char* header, unsigned int length)
{
assert(header != NULL);
unsigned char buffer[50U];
buffer[0] = 'D';
buffer[1] = 'S';
buffer[2] = 'R';
buffer[3] = 'P';
buffer[4] = 0x22U;
// Create a random id for this header
m_outId = (::rand() % 65535U) + 1U;
buffer[5] = m_outId / 256U; // Unique session id
buffer[6] = m_outId % 256U;
buffer[7] = 0U;
::memcpy(buffer + 8U, header, length);
m_outSeq = 0U;
if (m_debug)
CUtils::dump(1U, "Sending D-Star Busy Header", buffer, 49U);
return m_socket.write(buffer, 49U, m_address, m_port);
}
bool CDStarNetwork::writeBusyData(const unsigned char* data, unsigned int length, unsigned int errors, bool end)
{
assert(data != NULL);
unsigned char buffer[30U];
buffer[0] = 'D';
buffer[1] = 'S';
buffer[2] = 'R';
buffer[3] = 'P';
buffer[4] = 0x23U;
buffer[5] = m_outId / 256U; // Unique session id
buffer[6] = m_outId % 256U;
// If this is a data sync, reset the sequence to zero
if (data[9] == 0x55 && data[10] == 0x2D && data[11] == 0x16)
m_outSeq = 0U;
buffer[7] = m_outSeq;
if (end)
buffer[7] |= 0x40U; // End of data marker
buffer[8] = errors;
m_outSeq++;
if (m_outSeq > 0x14U)
m_outSeq = 0U;
::memcpy(buffer + 9U, data, length);
if (m_debug)
CUtils::dump(1U, "Sending D-Star Busy Data", buffer, length + 9U);
return m_socket.write(buffer, length + 9U, m_address, m_port);
}
bool CDStarNetwork::writePoll(const std::string& text)
{
unsigned char buffer[40U];
buffer[0] = 'D';
buffer[1] = 'S';
buffer[2] = 'R';
buffer[3] = 'P';
buffer[4] = 0x0A; // Poll with text
unsigned int length = text.length();
for (unsigned int i = 0U; i < length; i++)
buffer[5U + i] = text.at(i);
buffer[5U + length] = 0x00;
if (m_debug)
CUtils::dump(1U, "Sending D-Star Poll", buffer, 6U + length);
return m_socket.write(buffer, 6U + length, m_address, m_port);
}
void CDStarNetwork::clock(unsigned int ms)
{
unsigned char buffer[BUFFER_LENGTH];
in_addr address;
unsigned int port;
int length = m_socket.read(buffer, BUFFER_LENGTH, address, port);
if (length <= 0)
return;
// Check if the data is for us
if (m_address.s_addr != address.s_addr || m_port != port) {
LogMessage("D-Star packet received from an invalid source, %08X != %08X and/or %u != %u", m_address.s_addr, address.s_addr, m_port, port);
CUtils::dump("D-Star Data", buffer, length);
return;
}
// Invalid packet type?
if (::memcmp(buffer, "DSRP", 4U) != 0)
return;
switch (buffer[4]) {
case 0x00U: // NETWORK_TEXT;
case 0x01U: // NETWORK_TEMPTEXT;
case 0x04U: // NETWORK_STATUS1..5
case 0x24U: // NETWORK_DD_DATA
return;
case 0x20U: // NETWORK_HEADER
if (m_inId == 0U && m_enabled) {
m_inId = buffer[5] * 256U + buffer[6];
unsigned char c = length - 6U;
m_buffer.addData(&c, 1U);
c = TAG_HEADER;
m_buffer.addData(&c, 1U);
m_buffer.addData(buffer + 7U, length - 7U);
}
break;
case 0x21U: // NETWORK_DATA
if (m_enabled) {
uint16_t id = buffer[5] * 256U + buffer[6];
// Check that the stream id matches the valid header, reject otherwise
if (id == m_inId && m_enabled) {
unsigned char c = length - 8U;
m_buffer.addData(&c, 1U);
// Is this the last packet in the stream?
if ((buffer[7] & 0x40) == 0x40) {
m_inId = 0U;
c = TAG_EOT;
} else {
c = TAG_DATA;
}
m_buffer.addData(&c, 1U);
m_buffer.addData(buffer + 9U, length - 9U);
}
}
break;
default:
CUtils::dump("Unknown D-Star packet from the Gateway", buffer, length);
break;
}
}
unsigned int CDStarNetwork::read(unsigned char* data, unsigned int length)
{
assert(data != NULL);
if (m_buffer.isEmpty())
return 0U;
unsigned char c = 0U;
m_buffer.getData(&c, 1U);
assert(c <= 100U);
assert(c <= length);
unsigned char buffer[100U];
m_buffer.getData(buffer, c);
switch (buffer[0U]) {
case TAG_HEADER:
case TAG_DATA:
case TAG_EOT:
::memcpy(data, buffer, c);
return c;
default:
return 0U;
}
}
void CDStarNetwork::reset()
{
m_inId = 0U;
}
void CDStarNetwork::close()
{
m_socket.close();
}
void CDStarNetwork::enable(bool enabled)
{
m_enabled = enabled;
}

64
DStarNetwork.h Normal file
View file

@ -0,0 +1,64 @@
/*
* Copyright (C) 2009-2014,2016 by 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; 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.
*/
#ifndef DStarNetwork_H
#define DStarNetwork_H
#include "DStarDefines.h"
#include "RingBuffer.h"
#include "UDPSocket.h"
#include <cstdint>
#include <string>
class CDStarNetwork {
public:
CDStarNetwork(const std::string& gatewayAddress, unsigned int gatewayPort, unsigned int localPort, bool debug);
~CDStarNetwork();
bool open();
void enable(bool enabled);
bool writeHeader(const unsigned char* header, unsigned int length);
bool writeBusyHeader(const unsigned char* header, unsigned int length);
bool writeData(const unsigned char* data, unsigned int length, unsigned int errors, bool end);
bool writeBusyData(const unsigned char* data, unsigned int length, unsigned int errors, bool end);
bool writePoll(const std::string& text);
unsigned int read(unsigned char* data, unsigned int length);
void reset();
void close();
void clock(unsigned int ms);
private:
CUDPSocket m_socket;
in_addr m_address;
unsigned int m_port;
bool m_debug;
bool m_enabled;
uint16_t m_outId;
uint8_t m_outSeq;
uint16_t m_inId;
CRingBuffer<unsigned char> m_buffer;
};
#endif

View file

@ -73,6 +73,7 @@ int main(int argc, char** argv)
CMMDVMHost::CMMDVMHost(const std::string& confFile) :
m_conf(confFile),
m_modem(NULL),
m_dstarNetwork(NULL),
m_dmrNetwork(NULL),
m_display(NULL),
m_dstarEnabled(false),
@ -116,6 +117,12 @@ int CMMDVMHost::run()
createDisplay();
if (m_dstarEnabled && m_conf.getDStarNetworkEnabled()) {
ret = createDStarNetwork();
if (!ret)
return 1;
}
if (m_dmrEnabled && m_conf.getDMRNetworkEnabled()) {
ret = createDMRNetwork();
if (!ret)
@ -375,6 +382,11 @@ int CMMDVMHost::run()
m_display->close();
delete m_display;
if (m_dstarNetwork != NULL) {
m_dstarNetwork->close();
delete m_dstarNetwork;
}
if (m_dmrNetwork != NULL) {
m_dmrNetwork->close();
delete m_dmrNetwork;
@ -422,6 +434,35 @@ bool CMMDVMHost::createModem()
return true;
}
bool CMMDVMHost::createDStarNetwork()
{
if (!m_conf.getDStarNetworkEnabled())
return false;
std::string gatewayAddress = m_conf.getDStarGatewayAddress();
unsigned int gatewayPort = m_conf.getDStarGatewayPort();
unsigned int localPort = m_conf.getDStarLocalPort();
bool debug = m_conf.getDStarNetworkDebug();
LogInfo("D-Star Network Parameters");
LogInfo(" Gateway Address: %s", gatewayAddress.c_str());
LogInfo(" Gateway Port: %u", gatewayPort);
LogInfo(" Local Port: %u", localPort);
m_dstarNetwork = new CDStarNetwork(gatewayAddress, gatewayPort, localPort, debug);
bool ret = m_dstarNetwork->open();
if (!ret) {
delete m_dstarNetwork;
m_dstarNetwork = NULL;
return false;
}
m_dstarNetwork->enable(true);
return true;
}
bool CMMDVMHost::createDMRNetwork()
{
if (!m_conf.getDMRNetworkEnabled())

View file

@ -20,6 +20,7 @@
#define MMDVMHOST_H
#include "HomebrewDMRIPSC.h"
#include "DStarNetwork.h"
#include "Display.h"
#include "Modem.h"
#include "Conf.h"
@ -37,6 +38,7 @@ public:
private:
CConf m_conf;
CModem* m_modem;
CDStarNetwork* m_dstarNetwork;
CHomebrewDMRIPSC* m_dmrNetwork;
IDisplay* m_display;
bool m_dstarEnabled;
@ -45,6 +47,7 @@ private:
void readParams();
bool createModem();
bool createDStarNetwork();
bool createDMRNetwork();
void createDisplay();
};

View file

@ -160,6 +160,7 @@
<ClInclude Include="DMRSync.h" />
<ClInclude Include="DStarDefines.h" />
<ClInclude Include="DStarEcho.h" />
<ClInclude Include="DStarNetwork.h" />
<ClInclude Include="EMB.h" />
<ClInclude Include="EmbeddedLC.h" />
<ClInclude Include="FullLC.h" />
@ -200,6 +201,7 @@
<ClCompile Include="DMRSlot.cpp" />
<ClCompile Include="DMRSync.cpp" />
<ClCompile Include="DStarEcho.cpp" />
<ClCompile Include="DStarNetwork.cpp" />
<ClCompile Include="EMB.cpp" />
<ClCompile Include="EmbeddedLC.cpp" />
<ClCompile Include="FullLC.cpp" />

View file

@ -134,6 +134,9 @@
<ClInclude Include="AMBEFEC.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="DStarNetwork.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="BPTC19696.cpp">
@ -241,5 +244,8 @@
<ClCompile Include="AMBEFEC.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="DStarNetwork.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View file

@ -5,12 +5,12 @@ LDFLAGS = -g
all: MMDVMHost
MMDVMHost: AMBEFEC.o BPTC19696.o Conf.o CRC.o CSBK.o Display.o DMRControl.o DMRData.o DMRSlot.o DMRSync.o DStarEcho.o EMB.o EmbeddedLC.o FullLC.o Golay2087.o \
Golay24128.o Hamming.o HomebrewDMRIPSC.o LC.o Log.o MMDVMHost.o Modem.o NullDisplay.o QR1676.o RS129.o SerialController.o SHA256.o ShortLC.o SlotType.o \
StopWatch.o TFTSerial.o Timer.o UDPSocket.o Utils.o YSFEcho.o
$(CC) $(LDFLAGS) -o MMDVMHost AMBEFEC.o BPTC19696.o Conf.o CRC.o CSBK.o Display.o DMRControl.o DMRData.o DMRSlot.o DMRSync.o DStarEcho.o EMB.o EmbeddedLC.o \
FullLC.o Golay2087.o Golay24128.o Hamming.o HomebrewDMRIPSC.o LC.o Log.o MMDVMHost.o Modem.o NullDisplay.o QR1676.o RS129.o SerialController.o SHA256.o \
ShortLC.o SlotType.o StopWatch.o TFTSerial.o Timer.o UDPSocket.o Utils.o YSFEcho.o $(LIBS)
MMDVMHost: AMBEFEC.o BPTC19696.o Conf.o CRC.o CSBK.o Display.o DMRControl.o DMRData.o DMRSlot.o DMRSync.o DStarEcho.o DStarNetwork.o EMB.o EmbeddedLC.o FullLC.o \
Golay2087.o Golay24128.o Hamming.o HomebrewDMRIPSC.o LC.o Log.o MMDVMHost.o Modem.o NullDisplay.o QR1676.o RS129.o SerialController.o SHA256.o ShortLC.o \
SlotType.o StopWatch.o TFTSerial.o Timer.o UDPSocket.o Utils.o YSFEcho.o
$(CC) $(LDFLAGS) -o MMDVMHost AMBEFEC.o BPTC19696.o Conf.o CRC.o CSBK.o Display.o DMRControl.o DMRData.o DMRSlot.o DMRSync.o DStarEcho.o DStarNetwork.o EMB.o \
EmbeddedLC.o FullLC.o Golay2087.o Golay24128.o Hamming.o HomebrewDMRIPSC.o LC.o Log.o MMDVMHost.o Modem.o NullDisplay.o QR1676.o RS129.o \
SerialController.o SHA256.o ShortLC.o SlotType.o StopWatch.o TFTSerial.o Timer.o UDPSocket.o Utils.o YSFEcho.o $(LIBS)
AMBEFEC.o: AMBEFEC.cpp AMBEFEC.h Golay24128.h
$(CC) $(CFLAGS) -c AMBEFEC.cpp
@ -46,6 +46,9 @@ DMRSync.o: DMRSync.cpp DMRSync.h DMRDefines.h
DStarEcho.o: DStarEcho.cpp DStarEcho.h RingBuffer.h Timer.h
$(CC) $(CFLAGS) -c DStarEcho.cpp
DStarNetwork.o: DStarNetwork.cpp DStarNetwork.h Log.h UDPSocket.h RingBuffer.h Utils.h StopWatch.h DStarDefines.h Defines.h
$(CC) $(CFLAGS) -c DStarNetwork.cpp
EMB.o: EMB.cpp EMB.h
$(CC) $(CFLAGS) -c EMB.cpp
@ -74,7 +77,7 @@ Log.o: Log.cpp Log.h
$(CC) $(CFLAGS) -c Log.cpp
MMDVMHost.o: MMDVMHost.cpp MMDVMHost.h Conf.h Log.h Version.h Modem.h StopWatch.h Defines.h DMRSync.h DStarEcho.h YSFEcho.h DMRControl.h HomebrewDMRIPSC.h \
Display.h TFTSerial.h NullDisplay.h
Display.h TFTSerial.h NullDisplay.h DStarNetwork.h
$(CC) $(CFLAGS) -c MMDVMHost.cpp
Modem.o: Modem.cpp Modem.h Log.h SerialController.h Timer.h RingBuffer.h Utils.o DMRDefines.h DStarDefines.h YSFDefines.h Defines.h