diff --git a/M17CRC.cpp b/M17CRC.cpp new file mode 100644 index 0000000..933f86f --- /dev/null +++ b/M17CRC.cpp @@ -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 +#include + +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; +} diff --git a/M17CRC.h b/M17CRC.h index af0f3d0..22c8f31 100644 --- a/M17CRC.h +++ b/M17CRC.h @@ -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 diff --git a/M17Control.cpp b/M17Control.cpp index 39b83d3..3086c1b 100644 --- a/M17Control.cpp +++ b/M17Control.cpp @@ -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() diff --git a/M17Network.cpp b/M17Network.cpp index 8c8d251..7a3c6ff 100644 --- a/M17Network.cpp +++ b/M17Network.cpp @@ -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) diff --git a/M17Utils.cpp b/M17Utils.cpp new file mode 100644 index 0000000..406ebdb --- /dev/null +++ b/M17Utils.cpp @@ -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 + +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; + } +} diff --git a/M17Utils.h b/M17Utils.h index 025e65a..c9a18df 100644 --- a/M17Utils.h +++ b/M17Utils.h @@ -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: }; diff --git a/MMDVMHost.vcxproj b/MMDVMHost.vcxproj index a399c3a..e1a78c7 100644 --- a/MMDVMHost.vcxproj +++ b/MMDVMHost.vcxproj @@ -291,6 +291,7 @@ + @@ -330,6 +331,7 @@ + diff --git a/MMDVMHost.vcxproj.filters b/MMDVMHost.vcxproj.filters index 8f9a797..c1bd903 100644 --- a/MMDVMHost.vcxproj.filters +++ b/MMDVMHost.vcxproj.filters @@ -589,5 +589,11 @@ Source Files + + Source Files + + + Source Files + \ No newline at end of file