Skip to content

Commit

Permalink
Change the internal network protocol.
Browse files Browse the repository at this point in the history
  • Loading branch information
g4klx committed Aug 22, 2021
1 parent 8e34cab commit e966d35
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 40 deletions.
50 changes: 25 additions & 25 deletions M17Control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ bool CM17Control::writeModem(unsigned char* data, unsigned int len)
m_rssiCount = 1U;
m_rfLSFn = 0U;

if (m_network != NULL)
m_network->writeHeader(m_rfLSF.getSource(), m_rfLSF.getDest(), frame);

#if defined(DUMP_M17)
openFile();
#endif
Expand Down Expand Up @@ -237,6 +240,12 @@ bool CM17Control::writeModem(unsigned char* data, unsigned int len)
m_aveRSSI = m_rssi;
m_rssiCount = 1U;

if (m_network != NULL) {
unsigned char lsf[M17_LSF_LENGTH_BYTES];
m_rfLSF.getNetwork(lsf);
m_network->writeHeader(m_rfLSF.getSource(), m_rfLSF.getDest(), lsf);
}

#if defined(DUMP_M17)
openFile();
#endif
Expand Down Expand Up @@ -300,18 +309,8 @@ bool CM17Control::writeModem(unsigned char* data, unsigned int len)
writeQueueRF(rfData);
}

if (m_network != NULL && m_rfTimeoutTimer.isRunning() && !m_rfTimeoutTimer.hasExpired()) {
unsigned char netData[M17_LSF_LENGTH_BYTES + M17_FN_LENGTH_BYTES + M17_PAYLOAD_LENGTH_BYTES + M17_CRC_LENGTH_BYTES];

m_rfLSF.getNetwork(netData + 0U);

// Copy the FN and payload from the frame
::memcpy(netData + M17_LSF_LENGTH_BYTES - M17_CRC_LENGTH_BYTES, frame, M17_FN_LENGTH_BYTES + M17_PAYLOAD_LENGTH_BYTES);

// The CRC is added in the networking code

m_network->write(netData);
}
if (m_network != NULL && m_rfTimeoutTimer.isRunning() && !m_rfTimeoutTimer.hasExpired())
m_network->writeData(m_rfLSF.getSource(), m_rfLSF.getDest(), frame);

m_rfFrames++;

Expand Down Expand Up @@ -404,16 +403,18 @@ void CM17Control::writeNetwork()

m_networkWatchdog.start();

m_netLSF.setNetwork(netData);
m_netLSF.setCAN(m_can);
if (netData[0U] == TAG_HEADER) {
m_netLSF.setNetwork(netData + 25U);
m_netLSF.setCAN(m_can);

if (!m_allowEncryption) {
unsigned char type = m_netLSF.getEncryptionType();
if (type != M17_ENCRYPTION_TYPE_NONE)
return;
}
if (!m_allowEncryption) {
unsigned char type = m_netLSF.getEncryptionType();
if (type != M17_ENCRYPTION_TYPE_NONE) {
m_network->reset();
return;
}
}

if (m_netState == RS_NET_IDLE) {
std::string source = m_netLSF.getSource();
std::string dest = m_netLSF.getDest();

Expand All @@ -432,8 +433,7 @@ void CM17Control::writeNetwork()
m_netState = RS_NET_DATA_AUDIO;
break;
default:
LogMessage("M17, received network unknown transmission from %s to %s", source.c_str(), dest.c_str());
m_netState = RS_NET_DATA;
m_network->reset();
break;
}

Expand Down Expand Up @@ -467,7 +467,7 @@ void CM17Control::writeNetwork()
writeQueueNet(start);
}

if (m_netState == RS_NET_AUDIO || m_netState == RS_NET_DATA_AUDIO) {
if (netData[0U] == TAG_DATA) {
unsigned char data[M17_FRAME_LENGTH_BYTES + 2U];

data[0U] = TAG_DATA;
Expand Down Expand Up @@ -498,7 +498,7 @@ void CM17Control::writeNetwork()

// Add the FN and the data/audio
unsigned char payload[M17_FN_LENGTH_BYTES + M17_PAYLOAD_LENGTH_BYTES];
::memcpy(payload, netData + 28U, M17_FN_LENGTH_BYTES + M17_PAYLOAD_LENGTH_BYTES);
::memcpy(payload, netData + 24U, M17_FN_LENGTH_BYTES + M17_PAYLOAD_LENGTH_BYTES);

// Add the Convolution FEC
CM17Convolution conv;
Expand All @@ -515,7 +515,7 @@ void CM17Control::writeNetwork()
m_netLSFn = 0U;

// EOT handling
uint16_t fn = (netData[28U] << 8) + (netData[29U] << 0);
uint16_t fn = (netData[24U] << 8) + (netData[25U] << 0);
if ((fn & 0x8000U) == 0x8000U) {
std::string source = m_netLSF.getSource();
std::string dest = m_netLSF.getDest();
Expand Down
68 changes: 55 additions & 13 deletions M17Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ bool CM17Network::open()
}
}

bool CM17Network::write(const unsigned char* data)
bool CM17Network::writeHeader(const std::string& source, const std::string& dest, const unsigned char* data)
{
if (m_addrLen == 0U)
return false;

assert(data != NULL);

unsigned char buffer[100U];
unsigned char buffer[60U];

buffer[0U] = 'M';
buffer[1U] = '1';
buffer[2U] = '7';
buffer[3U] = ' ';
buffer[3U] = 'H';

// Create a random id for this transmission if needed
if (m_outId == 0U) {
Expand All @@ -97,16 +97,49 @@ bool CM17Network::write(const unsigned char* data)
buffer[4U] = m_outId / 256U; // Unique session id
buffer[5U] = m_outId % 256U;

::memcpy(buffer + 6U, data, 46U);
::memset(buffer + 6U, ' ', 9U);
::memcpy(buffer + 6U, source.c_str(), source.size());

// Dummy CRC
buffer[52U] = 0x00U;
buffer[53U] = 0x00U;
::memset(buffer + 15U, ' ', 9U);
::memcpy(buffer + 15U, dest.c_str(), dest.size());

::memcpy(buffer + 24U, data, 28U);

if (m_debug)
CUtils::dump(1U, "M17 data transmitted", buffer, 54U);
CUtils::dump(1U, "M17 header transmitted", buffer, 52U);

return m_socket.write(buffer, 54U, m_addr, m_addrLen);
return m_socket.write(buffer, 52U, m_addr, m_addrLen);
}

bool CM17Network::writeData(const std::string& source, const std::string& dest, const unsigned char* data)
{
if (m_addrLen == 0U)
return false;

assert(data != NULL);

unsigned char buffer[50U];

buffer[0U] = 'M';
buffer[1U] = '1';
buffer[2U] = '7';
buffer[3U] = 'D';

buffer[4U] = m_outId / 256U; // Unique session id
buffer[5U] = m_outId % 256U;

::memset(buffer + 6U, ' ', 9U);
::memcpy(buffer + 6U, source.c_str(), source.size());

::memset(buffer + 15U, ' ', 9U);
::memcpy(buffer + 15U, dest.c_str(), dest.size());

::memcpy(buffer + 24U, data, 18U);

if (m_debug)
CUtils::dump(1U, "M17 data transmitted", buffer, 42U);

return m_socket.write(buffer, 42U, m_addr, m_addrLen);
}

void CM17Network::clock(unsigned int ms)
Expand Down Expand Up @@ -139,7 +172,7 @@ void CM17Network::clock(unsigned int ms)
if (::memcmp(buffer + 0U, "PING", 4U) == 0)
return;

if (::memcmp(buffer + 0U, "M17 ", 4U) != 0) {
if (::memcmp(buffer + 0U, "M17H", 4U) != 0 && ::memcmp(buffer + 0U, "M17D", 4U) != 0) {
CUtils::dump(2U, "M17, received unknown packet", buffer, length);
return;
}
Expand All @@ -152,10 +185,19 @@ void CM17Network::clock(unsigned int ms)
return;
}

unsigned char c = length - 6U;
m_buffer.addData(&c, 1U);
if (::memcmp(buffer + 0U, "M17H", 4U) == 0) {
unsigned char c = length - 5U;
m_buffer.addData(&c, 1U);

m_buffer.addData(buffer + 6U, length - 6U);
m_buffer.addData(&TAG_HEADER, 1U);
m_buffer.addData(buffer + 6U, length - 6U);
} else {
unsigned char c = length - 5U;
m_buffer.addData(&c, 1U);

m_buffer.addData(&TAG_DATA, 1U);
m_buffer.addData(buffer + 6U, length - 6U);
}
}

bool CM17Network::read(unsigned char* data)
Expand Down
4 changes: 3 additions & 1 deletion M17Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ class CM17Network {

void enable(bool enabled);

bool write(const unsigned char* data);
bool writeHeader(const std::string& source, const std::string& dest, const unsigned char* data);

bool writeData(const std::string& source, const std::string& dest, const unsigned char* data);

bool read(unsigned char* data);

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 = "20210818";
const char* VERSION = "20210822";

#endif

0 comments on commit e966d35

Please sign in to comment.