Skip to content

Commit

Permalink
Merge branch 'master' into P25Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
g4klx committed Jan 25, 2024
2 parents 12c4fd5 + 9471e32 commit 3bde944
Show file tree
Hide file tree
Showing 222 changed files with 63,508 additions and 13,789 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Debug
Release
x64
MMDVMHost
RemoteCommand
*.o
*.opendb
*.bak
Expand Down
49 changes: 23 additions & 26 deletions AMBEFEC.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2010,2014,2016,2018 by Jonathan Naylor G4KLX
* Copyright (C) 2010,2014,2016,2018,2021 by Jonathan Naylor G4KLX
* Copyright (C) 2016 Mathias Weyland, HB9FRV
*
* This program is free software; you can redistribute it and/or modify
Expand All @@ -20,6 +20,7 @@
#include "Golay24128.h"
#include "Hamming.h"
#include "AMBEFEC.h"
#include "Utils.h"

#include <cstdio>
#include <cassert>
Expand Down Expand Up @@ -795,34 +796,31 @@ unsigned int CAMBEFEC::regenerateDStar(unsigned int& a, unsigned int& b) const
unsigned int orig_a = a;
unsigned int orig_b = b;

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

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);
unsigned int datb;
bool valid2 = CGolay24128::decode24128(b, datb);
if (!valid2)
return 10U;

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

b ^= p;

unsigned int errsA = 0U, errsB = 0U;

unsigned int v = a ^ orig_a;
while (v != 0U) {
v &= v - 1U;
errsA++;
}
unsigned int errsA = CUtils::countBits(v);

v = b ^ orig_b;
while (v != 0U) {
v &= v - 1U;
errsB++;
}
unsigned int errsB = CUtils::countBits(v);

return errsA + errsB;
}
Expand All @@ -832,7 +830,14 @@ unsigned int CAMBEFEC::regenerateDMR(unsigned int& a, unsigned int& b, unsigned
unsigned int orig_a = a;
unsigned int orig_b = b;

unsigned int data = CGolay24128::decode24128(a);
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 All @@ -847,19 +852,11 @@ unsigned int CAMBEFEC::regenerateDMR(unsigned int& a, unsigned int& b, unsigned

b ^= p;

unsigned int errsA = 0U, errsB = 0U;

unsigned int v = a ^ orig_a;
while (v != 0U) {
v &= v - 1U;
errsA++;
}
unsigned int errsA = CUtils::countBits(v);

v = b ^ orig_b;
while (v != 0U) {
v &= v - 1U;
errsB++;
}
unsigned int errsB = CUtils::countBits(v);

if (errsA >= 4U || ((errsA + errsB) >= 6U && errsA >= 2U)) {
a = 0xF00292U;
Expand Down
252 changes: 252 additions & 0 deletions AX25Control.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
/*
* Copyright (C) 2020 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; version 2 of the License.
*
* 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.
*/

#include "AX25Control.h"
#include "AX25Defines.h"
#include "Utils.h"
#include "Log.h"

#include <cstdio>
#include <cassert>
#include <cstring>
#include <ctime>

// #define DUMP_AX25

const unsigned char BIT_MASK_TABLE[] = { 0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U, 0x02U, 0x01U };

#define WRITE_BIT1(p,i,b) p[(i)>>3] = (b) ? (p[(i)>>3] | BIT_MASK_TABLE[(i)&7]) : (p[(i)>>3] & ~BIT_MASK_TABLE[(i)&7])
#define READ_BIT1(p,i) (p[(i)>>3] & BIT_MASK_TABLE[(i)&7])

CAX25Control::CAX25Control(CAX25Network* network, bool trace) :
m_network(network),
m_trace(trace),
m_enabled(true),
m_fp(NULL)
{
}

CAX25Control::~CAX25Control()
{
}

bool CAX25Control::writeModem(unsigned char *data, unsigned int len)
{
assert(data != NULL);

if (!m_enabled)
return false;

if (m_trace)
decode(data, len);

CUtils::dump(1U, "AX.25 received packet", data, len);

if (m_network == NULL)
return true;

return m_network->write(data, len);
}

unsigned int CAX25Control::readModem(unsigned char* data)
{
assert(data != NULL);

if (m_network == NULL)
return 0U;

if (!m_enabled)
return 0U;

unsigned int length = m_network->read(data, 500U);

if (length > 0U)
CUtils::dump(1U, "AX.25 transmitted packet", data, length);

return length;
}

bool CAX25Control::openFile()
{
if (m_fp != NULL)
return true;

time_t t;
::time(&t);

struct tm* tm = ::localtime(&t);

char name[100U];
::sprintf(name, "AX25_%04d%02d%02d_%02d%02d%02d.ambe", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);

m_fp = ::fopen(name, "wb");
if (m_fp == NULL)
return false;

::fwrite("AX25", 1U, 4U, m_fp);

return true;
}

bool CAX25Control::writeFile(const unsigned char* data, unsigned int length)
{
if (m_fp == NULL)
return false;

::fwrite(&length, 1U, sizeof(unsigned int), m_fp);
::fwrite(data, 1U, length, m_fp);

return true;
}

void CAX25Control::closeFile()
{
if (m_fp != NULL) {
::fclose(m_fp);
m_fp = NULL;
}
}

void CAX25Control::enable(bool enabled)
{
m_enabled = enabled;
}

void CAX25Control::decode(const unsigned char* data, unsigned int length)
{
assert(data != NULL);
assert(length >= 15U);

std::string text;

bool more = decodeAddress(data + 7U, text);

text += '>';

decodeAddress(data + 0U, text);

unsigned int n = 14U;
while (more && n < length) {
text += ',';
more = decodeAddress(data + n, text, true);
n += 7U;
}

text += ' ';

if ((data[n] & 0x01U) == 0x00U) {
// I frame
char t[20U];
::sprintf(t, "<I S%u R%u>", (data[n] >> 1) & 0x07U, (data[n] >> 5) & 0x07U);
text += t;
} else {
if ((data[n] & 0x02U) == 0x00U) {
// S frame
char t[20U];
switch (data[n] & 0x0FU) {
case 0x01U:
sprintf(t, "<RR R%u>", (data[n] >> 5) & 0x07U);
break;
case 0x05U:
sprintf(t, "<RNR R%u>", (data[n] >> 5) & 0x07U);
break;
case 0x09U:
sprintf(t, "<REJ R%u>", (data[n] >> 5) & 0x07U);
break;
case 0x0DU:
sprintf(t, "<SREJ R%u>", (data[n] >> 5) & 0x07U);
break;
default:
sprintf(t, "<Unknown R%u>", (data[n] >> 5) & 0x07U);
break;
}

text += t;
LogMessage("AX.25, %s", text.c_str());
return;
} else {
// U frame
switch (data[n] & 0xEFU) {
case 0x6FU:
text += "<SABME>";
break;
case 0x2FU:
text += "<SABM>";
break;
case 0x43U:
text += "<DISC>";
break;
case 0x0FU:
text += "<DM>";
break;
case 0x63U:
text += "<UA>";
break;
case 0x87U:
text += "<FRMR>";
break;
case 0x03U:
text += "<UI>";
break;
case 0xAFU:
text += "<XID>";
break;
case 0xE3U:
text += "<TEST>";
break;
default:
text += "<Unknown>";
break;
}

if ((data[n] & 0xEFU) != 0x03U) {
LogMessage("AX.25, %s", text.c_str());
return;
}
}
}

n += 2U;

LogMessage("AX.25, %s %.*s", text.c_str(), length - n, data + n);
}

bool CAX25Control::decodeAddress(const unsigned char* data, std::string& text, bool isDigi) const
{
assert(data != NULL);

for (unsigned int i = 0U; i < 6U; i++) {
char c = data[i] >> 1;
if (c != ' ')
text += c;
}

unsigned char ssid = (data[6U] >> 1) & 0x0FU;
if (ssid > 0U) {
text += '-';
if (ssid >= 10U) {
text += '1';
text += '0' + ssid - 10U;
}
else {
text += '0' + ssid;
}
}

if (isDigi) {
if ((data[6U] & 0x80U) == 0x80U)
text += '*';
}

return (data[6U] & 0x01U) == 0x00U;
}
Loading

0 comments on commit 3bde944

Please sign in to comment.