Skip to content

Commit

Permalink
Refactor the Golay 24,12,8 code and its uses.
Browse files Browse the repository at this point in the history
  • Loading branch information
g4klx committed Apr 7, 2021
1 parent 8dc54ed commit ffb4bdb
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 32 deletions.
26 changes: 17 additions & 9 deletions AMBEFEC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,22 +793,25 @@ unsigned int CAMBEFEC::regenerateIMBE(unsigned char* bytes) const

unsigned int CAMBEFEC::regenerateDStar(unsigned int& a, unsigned int& b) const
{
bool valid;

unsigned int orig_a = a;
unsigned int orig_b = b;

unsigned int data = CGolay24128::decode24128(a, valid);

a = CGolay24128::encode24128(data);
unsigned int data;
bool valid1 = CGolay24128::decode24128(a, data);
if (!valid1)
return 10U;

// The PRNG
unsigned int p = PRNG_TABLE[data];

b ^= p;

unsigned int datb = CGolay24128::decode24128(b, valid);
unsigned int datb;
bool valid2 = CGolay24128::decode24128(b, datb);
if (!valid2)
return 10U;

a = CGolay24128::encode24128(data);
b = CGolay24128::encode24128(datb);

b ^= p;
Expand All @@ -824,12 +827,17 @@ unsigned int CAMBEFEC::regenerateDStar(unsigned int& a, unsigned int& b) const

unsigned int CAMBEFEC::regenerateDMR(unsigned int& a, unsigned int& b, unsigned int& c) const
{
bool valid;

unsigned int orig_a = a;
unsigned int orig_b = b;

unsigned int data = CGolay24128::decode24128(a, valid);
unsigned int data;
bool valid = CGolay24128::decode24128(a, data);
if (!valid) {
a = 0xF00292U;
b = 0x0E0B20U;
c = 0x000000U;
return 10U; // An invalid A block gives an error count of 10
}

a = CGolay24128::encode24128(data);

Expand Down
20 changes: 11 additions & 9 deletions Golay24128.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1090,23 +1090,25 @@ unsigned int CGolay24128::decode23127(unsigned int code)
return code >> 11;
}

unsigned int CGolay24128::decode24128(unsigned int input, bool& valid)
bool CGolay24128::decode24128(unsigned int in, unsigned int& out)
{
unsigned int syndrome = ::get_syndrome_23127(input >> 1);
unsigned int syndrome = ::get_syndrome_23127(in >> 1);
unsigned int error_pattern = DECODING_TABLE_23127[syndrome] << 1;

unsigned int output = input ^ error_pattern;
out = in ^ error_pattern;

valid = (CUtils::countBits(syndrome) < 3U) || !(CUtils::countBits(output) & 1);
bool valid = (CUtils::countBits(syndrome) < 3U) || !(CUtils::countBits(out) & 1);

return output >> 12;
out >>= 12;

return valid;
}

unsigned int CGolay24128::decode24128(unsigned char* bytes, bool& valid)
bool CGolay24128::decode24128(unsigned char* in, unsigned int& out)
{
assert(bytes != NULL);
assert(in != NULL);

unsigned int code = (bytes[0U] << 16) | (bytes[1U] << 8) | (bytes[2U] << 0);
unsigned int code = (in[0U] << 16) | (in[1U] << 8) | (in[2U] << 0);

return decode24128(code, valid);
return decode24128(code, out);
}
5 changes: 3 additions & 2 deletions Golay24128.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ class CGolay24128 {
static unsigned int encode24128(unsigned int data);

static unsigned int decode23127(unsigned int code);
static unsigned int decode24128(unsigned int code, bool& valid);
static unsigned int decode24128(unsigned char* bytes, bool& valid);

static bool decode24128(unsigned int in, unsigned int& out);
static bool decode24128(unsigned char* in, unsigned int& out);
};

#endif
10 changes: 5 additions & 5 deletions M17Control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ bool CM17Control::writeModem(unsigned char* data, unsigned int len)
}

if (m_rfState == RS_RF_LATE_ENTRY && data[0U] == TAG_DATA1) {
bool valid1, valid2, valid3, valid4;
unsigned int lich1 = CGolay24128::decode24128(data + 2U + M17_SYNC_LENGTH_BYTES + 0U, valid1);
unsigned int lich2 = CGolay24128::decode24128(data + 2U + M17_SYNC_LENGTH_BYTES + 3U, valid2);
unsigned int lich3 = CGolay24128::decode24128(data + 2U + M17_SYNC_LENGTH_BYTES + 6U, valid3);
unsigned int lich4 = CGolay24128::decode24128(data + 2U + M17_SYNC_LENGTH_BYTES + 9U, valid4);
unsigned int lich1, lich2, lich3, lich4;
bool valid1 = CGolay24128::decode24128(data + 2U + M17_SYNC_LENGTH_BYTES + 0U, lich1);
bool valid2 = CGolay24128::decode24128(data + 2U + M17_SYNC_LENGTH_BYTES + 3U, lich2);
bool valid3 = CGolay24128::decode24128(data + 2U + M17_SYNC_LENGTH_BYTES + 6U, lich3);
bool valid4 = CGolay24128::decode24128(data + 2U + M17_SYNC_LENGTH_BYTES + 9U, lich4);

if (!valid1 || !valid2 || !valid3 || !valid4)
return false;
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 = "20210329";
const char* VERSION = "20210407";

#endif
12 changes: 6 additions & 6 deletions YSFFICH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ bool CYSFFICH::decode(const unsigned char* bytes)
unsigned char output[13U];
viterbi.chainback(output, 96U);

bool valid1, valid2, valid3, valid4;
unsigned int b0 = CGolay24128::decode24128(output + 0U, valid1);
unsigned int b1 = CGolay24128::decode24128(output + 3U, valid2);
unsigned int b2 = CGolay24128::decode24128(output + 6U, valid3);
unsigned int b3 = CGolay24128::decode24128(output + 9U, valid4);
unsigned int b0, b1, b2, b3;
bool valid0 = CGolay24128::decode24128(output + 0U, b0);
bool valid1 = CGolay24128::decode24128(output + 3U, b1);
bool valid2 = CGolay24128::decode24128(output + 6U, b2);
bool valid3 = CGolay24128::decode24128(output + 9U, b3);

if (!valid1 || !valid2 || !valid3 || !valid4)
if (!valid0 || !valid1 || !valid2 || !valid3)
return false;

m_fich[0U] = (b0 >> 4) & 0xFFU;
Expand Down

0 comments on commit ffb4bdb

Please sign in to comment.