Skip to content

Commit

Permalink
Add the USDP metadata header to the FM network output.
Browse files Browse the repository at this point in the history
  • Loading branch information
g4klx committed Apr 24, 2021
1 parent 82b5b01 commit 609f1e3
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 16 deletions.
2 changes: 1 addition & 1 deletion FMControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ bool CFMControl::writeModem(const unsigned char* data, unsigned int length)
return true;

if (data[0U] == TAG_EOT)
return m_network->writeEOT();
return m_network->writeEnd();

if (data[0U] != TAG_DATA1)
return false;
Expand Down
138 changes: 127 additions & 11 deletions FMNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

const unsigned int BUFFER_LENGTH = 1500U;

CFMNetwork::CFMNetwork(const std::string& protocol, const std::string& localAddress, unsigned int localPort, const std::string& gatewayAddress, unsigned int gatewayPort, bool debug) :
CFMNetwork::CFMNetwork(const std::string& callsign, const std::string& protocol, const std::string& localAddress, unsigned int localPort, const std::string& gatewayAddress, unsigned int gatewayPort, bool debug) :
m_callsign(callsign),
m_protocol(FMNP_USRP),
m_socket(localAddress, localPort),
m_addr(),
Expand All @@ -37,12 +38,18 @@ m_enabled(false),
m_buffer(2000U, "FM Network"),
m_seqNo(0U)
{
assert(!callsign.empty());
assert(gatewayPort > 0U);
assert(!gatewayAddress.empty());

if (CUDPSocket::lookup(gatewayAddress, gatewayPort, m_addr, m_addrLen) != 0)
m_addrLen = 0U;

// Remove any trailing letters in the callsign
size_t pos = callsign.find_first_of(' ');
if (pos != std::string::npos)
m_callsign = callsign.substr(0U, pos);

// if (protocol == "USRP")
// m_protocol = FMNP_USRP;
}
Expand All @@ -68,8 +75,14 @@ bool CFMNetwork::writeData(float* data, unsigned int nSamples)
assert(data != NULL);
assert(nSamples > 0U);

unsigned char buffer[1500U];
::memset(buffer, 0x00U, 1500U);
if (m_seqNo == 0U) {
bool ret = writeStart();
if (!ret)
return false;
}

unsigned char buffer[500U];
::memset(buffer, 0x00U, 500U);

unsigned int length = 0U;

Expand Down Expand Up @@ -133,10 +146,10 @@ bool CFMNetwork::writeData(float* data, unsigned int nSamples)
return m_socket.write(buffer, length, m_addr, m_addrLen);
}

bool CFMNetwork::writeEOT()
bool CFMNetwork::writeEnd()
{
unsigned char buffer[1500U];
::memset(buffer, 0x00U, 1500U);
unsigned char buffer[500U];
::memset(buffer, 0x00U, 500U);

unsigned int length = 0U;

Expand Down Expand Up @@ -184,15 +197,19 @@ bool CFMNetwork::writeEOT()
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;

length += 160U * sizeof(int16_t);
length += 320U;
}

if (m_debug)
CUtils::dump(1U, "FM Network Data Sent", buffer, length);
m_seqNo = 0U;

m_seqNo++;
if (length > 0U) {
if (m_debug)
CUtils::dump(1U, "FM Network Data Sent", buffer, length);

return m_socket.write(buffer, length, m_addr, m_addrLen);
return m_socket.write(buffer, length, m_addr, m_addrLen);
} else {
return true;
}
}

void CFMNetwork::clock(unsigned int ms)
Expand Down Expand Up @@ -280,3 +297,102 @@ void CFMNetwork::enable(bool enabled)

m_enabled = enabled;
}

bool CFMNetwork::writeStart()
{
unsigned char buffer[500U];
::memset(buffer, 0x00U, 500U);

unsigned int length = 0U;

if (m_protocol == FMNP_USRP) {
buffer[length++] = 'U';
buffer[length++] = 'S';
buffer[length++] = 'R';
buffer[length++] = 'P';

// Sequence number
buffer[length++] = (m_seqNo >> 24) & 0xFFU;
buffer[length++] = (m_seqNo >> 16) & 0xFFU;
buffer[length++] = (m_seqNo >> 8) & 0xFFU;
buffer[length++] = (m_seqNo >> 0) & 0xFFU;

buffer[length++] = 0x00U;
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;

// PTT off
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;

buffer[length++] = 0x00U;
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;

// Type, 2 for metadata
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;
buffer[length++] = 0x02U;

buffer[length++] = 0x00U;
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;

buffer[length++] = 0x00U;
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;

// TLV TAG for Metadata
buffer[length++] = 0x08U;

// TLV Length
buffer[length++] = 3U + 4U + 3U + 1U + 1U + m_callsign.size() + 1U;

// DMR Id
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;

// Rpt Id
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;

// Talk Group
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;

// Time Slot
buffer[length++] = 0x00U;

// Color Code
buffer[length++] = 0x00U;

// Callsign
for (std::string::const_iterator it = m_callsign.cbegin(); it != m_callsign.cend(); ++it)
buffer[length++] = *it;

// End of Metadata
buffer[length++] = 0x00U;

length = 70U;
}

if (length > 0U) {
if (m_debug)
CUtils::dump(1U, "FM Network Data Sent", buffer, length);

return m_socket.write(buffer, length, m_addr, m_addrLen);
} else {
return true;
}
}
7 changes: 5 additions & 2 deletions FMNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ enum FM_NETWORK_PROTOCOL {

class CFMNetwork {
public:
CFMNetwork(const std::string& protocol, const std::string& myAddress, unsigned int myPort, const std::string& gatewayAddress, unsigned int gatewayPort, bool debug);
CFMNetwork(const std::string& callsign, const std::string& protocol, const std::string& myAddress, unsigned int myPort, const std::string& gatewayAddress, unsigned int gatewayPort, bool debug);
~CFMNetwork();

bool open();
Expand All @@ -40,7 +40,7 @@ class CFMNetwork {

bool writeData(float* data, unsigned int nSamples);

bool writeEOT();
bool writeEnd();

unsigned int read(float* data, unsigned int nSamples);

Expand All @@ -51,6 +51,7 @@ class CFMNetwork {
void clock(unsigned int ms);

private:
std::string m_callsign;
FM_NETWORK_PROTOCOL m_protocol;
CUDPSocket m_socket;
sockaddr_storage m_addr;
Expand All @@ -59,6 +60,8 @@ class CFMNetwork {
bool m_enabled;
CRingBuffer<unsigned char> m_buffer;
unsigned int m_seqNo;

bool writeStart();
};

#endif
3 changes: 2 additions & 1 deletion MMDVMHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,7 @@ bool CMMDVMHost::createPOCSAGNetwork()

bool CMMDVMHost::createFMNetwork()
{
std::string callsign = m_conf.getFMCallsign();
std::string protocol = m_conf.getFMNetworkProtocol();
std::string gatewayAddress = m_conf.getFMGatewayAddress();
unsigned int gatewayPort = m_conf.getFMGatewayPort();
Expand All @@ -1824,7 +1825,7 @@ bool CMMDVMHost::createFMNetwork()
LogInfo(" RX Audio Gain: %.2f", rxAudioGain);
LogInfo(" Mode Hang: %us", m_fmNetModeHang);

m_fmNetwork = new CFMNetwork(protocol, localAddress, localPort, gatewayAddress, gatewayPort, debug);
m_fmNetwork = new CFMNetwork(callsign, protocol, localAddress, localPort, gatewayAddress, gatewayPort, debug);

bool ret = m_fmNetwork->open();
if (!ret) {
Expand Down
2 changes: 1 addition & 1 deletion Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
#if !defined(VERSION_H)
#define VERSION_H

const char* VERSION = "20210420";
const char* VERSION = "20210424";

#endif

0 comments on commit 609f1e3

Please sign in to comment.