Add the CRC and callsign encoding.

This commit is contained in:
Jonathan Naylor 2020-10-18 14:51:52 +01:00
parent 48f95be982
commit 521da9b54d
8 changed files with 187 additions and 16 deletions

69
M17CRC.cpp Normal file
View file

@ -0,0 +1,69 @@
/*
* Copyright (C) 2018,2020 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 "M17CRC.h"
#include <cstdio>
#include <cassert>
const uint8_t BIT_MASK_TABLE1[] = { 0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U, 0x02U, 0x01U };
#define READ_BIT(p,i) (p[(i)>>3] & BIT_MASK_TABLE1[(i)&7])
bool CM17CRC::checkCRC(const unsigned char* in, unsigned int nBytes)
{
assert(in != NULL);
assert(nBytes > 2U);
uint16_t crc = createCRC(in, nBytes);
uint8_t temp[2U];
temp[0U] = (crc >> 8) & 0xFFU;
temp[1U] = (crc >> 0) & 0xFFU;
return temp[0U] == in[nBytes - 2U] && temp[1U] == in[nBytes - 1U];
}
void CM17CRC::encodeCRC(unsigned char* in, unsigned int nBytes)
{
assert(in != NULL);
assert(nBytes > 2U);
uint16_t crc = createCRC(in, nBytes);
in[nBytes - 2U] = (crc >> 8) & 0xFFU;
in[nBytes - 1U] = (crc >> 0) & 0xFFU;
}
uint16_t CM17CRC::createCRC(const unsigned char* in, unsigned int nBytes)
{
assert(in != NULL);
uint16_t crc = 0xFFFFU;
for (unsigned int i = 0U; i < nBytes; i++) {
bool bit1 = READ_BIT(in, i) != 0x00U;
bool bit2 = (crc & 0x8000U) == 0x8000U;
crc <<= 1;
if (bit1 ^ bit2)
crc ^= 0x5935U;
}
return crc;
}

View file

@ -24,11 +24,11 @@
class CM17CRC
{
public:
static bool checkCRC(const unsigned char* in, unsigned int length);
static void encodeCRC(unsigned char* in, unsigned int length);
static bool checkCRC(const unsigned char* in, unsigned int nBytes);
static void encodeCRC(unsigned char* in, unsigned int nBytes);
private:
static uint16_t createCRC(const unsigned char* in, unsigned int length);
static uint16_t createCRC(const unsigned char* in, unsigned int nBytes);
};
#endif

View file

@ -169,7 +169,7 @@ bool CM17Control::writeModem(unsigned char* data, unsigned int len)
unsigned char frame[M17_LICH_LENGTH_BYTES];
conv.chainback(frame, M17_LICH_LENGTH_BITS);
bool valid = CM17CRC::checkCRC(frame, M17_LICH_LENGTH_BITS);
bool valid = CM17CRC::checkCRC(frame, M17_LICH_LENGTH_BYTES);
if (valid) {
m_rfFrames = 0U;
m_rfErrs = 0U;
@ -225,7 +225,7 @@ bool CM17Control::writeModem(unsigned char* data, unsigned int len)
m_rfLICH.getLinkSetup(data + 2U);
// Add the CRC
CM17CRC::encodeCRC(data + 2U + M17_SYNC_LENGTH_BYTES, M17_LICH_LENGTH_BITS + M17_CRC_LENGTH_BITS);
CM17CRC::encodeCRC(data + 2U + M17_SYNC_LENGTH_BYTES, M17_LICH_LENGTH_BYTES + M17_CRC_LENGTH_BYTES);
// Add the convolution FEC
CM17Convolution conv;
@ -257,7 +257,7 @@ bool CM17Control::writeModem(unsigned char* data, unsigned int len)
unsigned char frame[M17_FN_LENGTH_BYTES + M17_PAYLOAD_LENGTH_BYTES + M17_CRC_LENGTH_BYTES];
conv.chainback(frame, M17_FN_LENGTH_BITS + M17_PAYLOAD_LENGTH_BITS + M17_CRC_LENGTH_BITS);
bool valid = CM17CRC::checkCRC(frame, M17_FN_LENGTH_BITS + M17_PAYLOAD_LENGTH_BITS + M17_CRC_LENGTH_BITS);
bool valid = CM17CRC::checkCRC(frame, M17_FN_LENGTH_BYTES + M17_PAYLOAD_LENGTH_BYTES + M17_CRC_LENGTH_BYTES);
if (valid) {
m_rfFN = (frame[0U] << 8) + (frame[1U] << 0);
@ -323,7 +323,7 @@ bool CM17Control::writeModem(unsigned char* data, unsigned int len)
m_rfLICH.getLinkSetup(data + 2U);
// Add the CRC
CM17CRC::encodeCRC(data + 2U + M17_SYNC_LENGTH_BYTES, M17_LICH_LENGTH_BITS + M17_CRC_LENGTH_BITS);
CM17CRC::encodeCRC(data + 2U + M17_SYNC_LENGTH_BYTES, M17_LICH_LENGTH_BYTES + M17_CRC_LENGTH_BYTES);
// Add the convolution FEC
CM17Convolution conv;
@ -357,7 +357,7 @@ bool CM17Control::writeModem(unsigned char* data, unsigned int len)
unsigned char frame[M17_FN_LENGTH_BYTES + M17_PAYLOAD_LENGTH_BYTES + M17_CRC_LENGTH_BYTES];
conv.chainback(frame, M17_FN_LENGTH_BITS + M17_PAYLOAD_LENGTH_BITS + M17_CRC_LENGTH_BITS);
bool valid = CM17CRC::checkCRC(frame, M17_FN_LENGTH_BITS + M17_PAYLOAD_LENGTH_BITS + M17_CRC_LENGTH_BITS);
bool valid = CM17CRC::checkCRC(frame, M17_FN_LENGTH_BYTES + M17_PAYLOAD_LENGTH_BYTES + M17_CRC_LENGTH_BYTES);
if (valid) {
m_rfFN = (frame[0U] << 8) + (frame[1U] << 0);
} else {
@ -383,7 +383,7 @@ bool CM17Control::writeModem(unsigned char* data, unsigned int len)
}
// Add the CRC
CM17CRC::encodeCRC(frame, M17_FN_LENGTH_BITS + M17_PAYLOAD_LENGTH_BITS + M17_CRC_LENGTH_BITS);
CM17CRC::encodeCRC(frame, M17_FN_LENGTH_BYTES + M17_PAYLOAD_LENGTH_BYTES + M17_CRC_LENGTH_BYTES);
}
unsigned char rfData[2U + M17_FRAME_LENGTH_BYTES];
@ -422,7 +422,7 @@ bool CM17Control::writeModem(unsigned char* data, unsigned int len)
// Copy the FN and payload from the frame
::memcpy(netData + M17_LICH_LENGTH_BYTES, frame, M17_FN_LENGTH_BYTES + M17_PAYLOAD_LENGTH_BYTES);
CM17CRC::encodeCRC(netData, M17_LICH_LENGTH_BITS + M17_FN_LENGTH_BITS + M17_PAYLOAD_LENGTH_BITS + M17_CRC_LENGTH_BITS);
// The CRC is added in the networking code
writeNetwork(netData);
@ -552,7 +552,7 @@ void CM17Control::writeNetwork()
m_netLICH.getLinkSetup(start + 2U);
// Add the CRC
CM17CRC::encodeCRC(start + 2U + M17_SYNC_LENGTH_BYTES, M17_LICH_LENGTH_BITS + M17_CRC_LENGTH_BITS);
CM17CRC::encodeCRC(start + 2U + M17_SYNC_LENGTH_BYTES, M17_LICH_LENGTH_BYTES + M17_CRC_LENGTH_BYTES);
// Add the convolution FEC
CM17Convolution conv;
@ -612,7 +612,7 @@ void CM17Control::writeNetwork()
::memcpy(payload, netData + 38U, M17_FN_LENGTH_BYTES + M17_PAYLOAD_LENGTH_BYTES);
// Add the CRC
CM17CRC::encodeCRC(payload, M17_FN_LENGTH_BITS + M17_PAYLOAD_LENGTH_BITS + M17_CRC_LENGTH_BITS);
CM17CRC::encodeCRC(payload, M17_FN_LENGTH_BYTES + M17_PAYLOAD_LENGTH_BYTES + M17_CRC_LENGTH_BYTES);
// Add the Convolution FEC
CM17Convolution conv;
@ -726,7 +726,7 @@ void CM17Control::decorrelator(const unsigned char* in, unsigned char* out) cons
assert(out != NULL);
for (unsigned int i = M17_SYNC_LENGTH_BYTES; i < M17_FRAME_LENGTH_BYTES; i++)
out[i] = in[i] ^ SCRAMBLER[i];
out[i] = in[i] ^ SCRAMBLER[i - M17_SYNC_LENGTH_BYTES];
}
bool CM17Control::openFile()

View file

@ -20,6 +20,7 @@
#include "M17Defines.h"
#include "M17Utils.h"
#include "Defines.h"
#include "M17CRC.h"
#include "Utils.h"
#include "Log.h"
@ -119,7 +120,10 @@ bool CM17Network::write(const unsigned char* data)
buffer[4U] = m_outId / 256U; // Unique session id
buffer[5U] = m_outId % 256U;
::memcpy(buffer + 6U, data, 48U);
::memcpy(buffer + 6U, data, 46U);
// Add the CRC
CM17CRC::encodeCRC(buffer + 6U, M17_LICH_LENGTH_BYTES + M17_FN_LENGTH_BYTES + M17_PAYLOAD_LENGTH_BYTES + M17_CRC_LENGTH_BYTES);
if (m_debug)
CUtils::dump(1U, "M17 data transmitted", buffer, 54U);
@ -205,6 +209,13 @@ void CM17Network::clock(unsigned int ms)
return;
}
// Check the CRC
bool valid = CM17CRC::checkCRC(buffer + 6U, M17_LICH_LENGTH_BYTES + M17_FN_LENGTH_BYTES + M17_PAYLOAD_LENGTH_BYTES + M17_CRC_LENGTH_BYTES);
if (!valid) {
LogMessage("M17, network packet received with an invalid CRC");
return;
}
// EOT?
uint16_t fn = (buffer[38U] << 8) + (buffer[39U] << 0);
if ((fn & 0x8000U) == 0x8000U)

83
M17Utils.cpp Normal file
View file

@ -0,0 +1,83 @@
/*
* Copyright (C) 2020 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 "M17Utils.h"
#include <cassert>
void CM17Utils::encodeCallsign(const std::string& callsign, unsigned char* encoded)
{
assert(encoded != NULL);
std::string call = callsign;
call.resize(8U, ' ');
uint64_t enc = 0ULL;
for (int i = 7U; i >= 0; i--) {
char c = call.at(i);
enc *= 40ULL;
// If speed is more important than code space, you can replace this with a lookup into a 256 byte array.
if (c >= 'A' && c <= 'Z') // 1-26
enc += c - 'A' + 1ULL;
else if (c >= '0' && c <= '9') // 27-36
enc += c - '0' + 27ULL;
else if (c == '-') // 37
enc += 37ULL;
// These are just place holders. If other characters make more sense, change these.
// Be sure to change them in the decode array below too.
else if (c == '/') // 38
enc += 38ULL;
else if (c == '.') // 39
enc += 39ULL;
else
// Invalid character or space, represented by 0, decoded as a space.
enc += 0ULL;
}
encoded[0U] = enc >> 40;
encoded[1U] = enc >> 32;
encoded[2U] = enc >> 24;
encoded[3U] = enc >> 16;
encoded[4U] = enc >> 8;
encoded[5U] = enc >> 0;
}
void CM17Utils::decodeCallsign(const unsigned char* encoded, std::string& callsign)
{
assert(encoded != NULL);
callsign.empty();
uint64_t enc = (uint64_t(encoded[5U]) << 40) +
(uint64_t(encoded[4U]) << 32) +
(uint64_t(encoded[3U]) << 24) +
(uint64_t(encoded[2U]) << 16) +
(uint64_t(encoded[1U]) << 8) +
(uint64_t(encoded[0U]) << 0);
if (enc >= 262144000000000ULL) // 40^9
return;
while (enc > 0ULL) {
callsign += " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-/."[enc % 40ULL];
enc /= 40ULL;
}
}

View file

@ -26,9 +26,9 @@ public:
CM17Utils();
~CM17Utils();
static void encodeCallsign(const std::string& callsign, unsigned char* data);
static void encodeCallsign(const std::string& callsign, unsigned char* encoded);
static void decodeCallsign(const unsigned char* data, std::string& callsign);
static void decodeCallsign(const unsigned char* encoded, std::string& callsign);
private:
};

View file

@ -291,6 +291,7 @@
<ClCompile Include="LCDproc.cpp" />
<ClCompile Include="Log.cpp" />
<ClCompile Include="M17Control.cpp" />
<ClCompile Include="M17CRC.cpp" />
<ClCompile Include="M17Network.cpp" />
<ClCompile Include="MMDVMHost.cpp" />
<ClCompile Include="Modem.cpp" />
@ -330,6 +331,7 @@
<ClCompile Include="RSSIInterpolator.cpp" />
<ClCompile Include="SerialController.cpp" />
<ClCompile Include="SerialPort.cpp" />
<ClCompile Include="M17Utils.cpp" />
<ClCompile Include="StopWatch.cpp" />
<ClCompile Include="Sync.cpp" />
<ClCompile Include="TFTSerial.cpp" />

View file

@ -589,5 +589,11 @@
<ClCompile Include="M17Network.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="M17CRC.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="M17Utils.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>