Skip to content

Commit

Permalink
Clean up the DMR Talker Alias code and logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
g4klx committed Jun 6, 2023
1 parent e10454c commit d709c2b
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 124 deletions.
65 changes: 33 additions & 32 deletions DMRSlot.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2015-2021 Jonathan Naylor, G4KLX
* Copyright (C) 2015-2021,2023 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
Expand Down Expand Up @@ -81,12 +81,13 @@ m_rfEmbeddedData(NULL),
m_rfEmbeddedReadN(0U),
m_rfEmbeddedWriteN(1U),
m_rfTalkerId(TALKER_ID_NONE),
m_rfTalkerAlias(),
m_rfTalkerAlias(slotNo),
m_netEmbeddedLC(),
m_netEmbeddedData(NULL),
m_netEmbeddedReadN(0U),
m_netEmbeddedWriteN(1U),
m_netTalkerId(TALKER_ID_NONE),
m_netTalkerAlias(slotNo),
m_rfLC(NULL),
m_netLC(NULL),
m_rfSeqNo(0U),
Expand Down Expand Up @@ -661,7 +662,7 @@ bool CDMRSlot::writeModem(unsigned char *data, unsigned int len)
case FLCO_GPS_INFO:
if (m_dumpTAData) {
::sprintf(text, "DMR Slot %u, Embedded GPS Info", m_slotNo);
CUtils::dump(2U, text, data, 9U);
CUtils::dump(1U, text, data, 9U);
logGPSPosition(data);
}
if (m_network != NULL)
Expand All @@ -675,12 +676,12 @@ bool CDMRSlot::writeModem(unsigned char *data, unsigned int len)
if (!(m_rfTalkerId & TALKER_ID_HEADER)) {
if (m_rfTalkerId == TALKER_ID_NONE)
m_rfTalkerAlias.reset();
m_rfTalkerAlias.add(0, data + 2U, 7U);
m_display->writeDMRTA(m_slotNo, (unsigned char*)m_rfTalkerAlias.get(), "R");
m_rfTalkerAlias.add(0U, data + 2U, 7U);
m_display->writeDMRTA(m_slotNo, m_rfTalkerAlias.get(), "R");

if (m_dumpTAData) {
::sprintf(text, "DMR Slot %u, Embedded Talker Alias Header", m_slotNo);
CUtils::dump(2U, text, data, 9U);
CUtils::dump(1U, text, data, 9U);
}

m_rfTalkerId |= TALKER_ID_HEADER;
Expand All @@ -694,12 +695,12 @@ bool CDMRSlot::writeModem(unsigned char *data, unsigned int len)
if (!(m_rfTalkerId & TALKER_ID_BLOCK1)) {
if (m_rfTalkerId == TALKER_ID_NONE)
m_rfTalkerAlias.reset();
m_rfTalkerAlias.add(1, data + 2U, 7U);
m_display->writeDMRTA(m_slotNo, (unsigned char*)m_rfTalkerAlias.get(), "R");
m_rfTalkerAlias.add(1U, data + 2U, 7U);
m_display->writeDMRTA(m_slotNo, m_rfTalkerAlias.get(), "R");

if (m_dumpTAData) {
::sprintf(text, "DMR Slot %u, Embedded Talker Alias Block 1", m_slotNo);
CUtils::dump(2U, text, data, 9U);
CUtils::dump(1U, text, data, 9U);
}

m_rfTalkerId |= TALKER_ID_BLOCK1;
Expand All @@ -713,12 +714,12 @@ bool CDMRSlot::writeModem(unsigned char *data, unsigned int len)
if (!(m_rfTalkerId & TALKER_ID_BLOCK2)) {
if (m_rfTalkerId == TALKER_ID_NONE)
m_rfTalkerAlias.reset();
m_rfTalkerAlias.add(2, data + 2U, 7U);
m_display->writeDMRTA(m_slotNo, (unsigned char*)m_rfTalkerAlias.get(), "R");
m_rfTalkerAlias.add(2U, data + 2U, 7U);
m_display->writeDMRTA(m_slotNo, m_rfTalkerAlias.get(), "R");

if (m_dumpTAData) {
::sprintf(text, "DMR Slot %u, Embedded Talker Alias Block 2", m_slotNo);
CUtils::dump(2U, text, data, 9U);
CUtils::dump(1U, text, data, 9U);
}

m_rfTalkerId |= TALKER_ID_BLOCK2;
Expand All @@ -732,12 +733,12 @@ bool CDMRSlot::writeModem(unsigned char *data, unsigned int len)
if (!(m_rfTalkerId & TALKER_ID_BLOCK3)) {
if (m_rfTalkerId == TALKER_ID_NONE)
m_rfTalkerAlias.reset();
m_rfTalkerAlias.add(3, data + 2U, 7U);
m_display->writeDMRTA(m_slotNo, (unsigned char*)m_rfTalkerAlias.get(), "R");
m_rfTalkerAlias.add(3U, data + 2U, 7U);
m_display->writeDMRTA(m_slotNo, m_rfTalkerAlias.get(), "R");

if (m_dumpTAData) {
::sprintf(text, "DMR Slot %u, Embedded Talker Alias Block 3", m_slotNo);
CUtils::dump(2U, text, data, 9U);
CUtils::dump(1U, text, data, 9U);
}

m_rfTalkerId |= TALKER_ID_BLOCK3;
Expand Down Expand Up @@ -1494,20 +1495,20 @@ void CDMRSlot::writeNetwork(const CDMRData& dmrData)
case FLCO_GPS_INFO:
if (m_dumpTAData) {
::sprintf(text, "DMR Slot %u, Embedded GPS Info", m_slotNo);
CUtils::dump(2U, text, data, 9U);
CUtils::dump(1U, text, data, 9U);
logGPSPosition(data);
}
break;
case FLCO_TALKER_ALIAS_HEADER:
if (!(m_netTalkerId & TALKER_ID_HEADER)) {
if (!m_netTalkerId)
m_rfTalkerAlias.reset();
m_rfTalkerAlias.add(0, data + 2U, 7U);
m_display->writeDMRTA(m_slotNo, (unsigned char*)m_rfTalkerAlias.get(), "N");
m_netTalkerAlias.reset();
m_netTalkerAlias.add(0U, data + 2U, 7U);
m_display->writeDMRTA(m_slotNo, m_netTalkerAlias.get(), "N");

if (m_dumpTAData) {
::sprintf(text, "DMR Slot %u, Embedded Talker Alias Header", m_slotNo);
CUtils::dump(2U, text, data, 9U);
CUtils::dump(1U, text, data, 9U);
}

m_netTalkerId |= TALKER_ID_HEADER;
Expand All @@ -1516,13 +1517,13 @@ void CDMRSlot::writeNetwork(const CDMRData& dmrData)
case FLCO_TALKER_ALIAS_BLOCK1:
if (!(m_netTalkerId & TALKER_ID_BLOCK1)) {
if (!m_netTalkerId)
m_rfTalkerAlias.reset();
m_rfTalkerAlias.add(1, data + 2U, 7U);
m_display->writeDMRTA(m_slotNo, (unsigned char*)m_rfTalkerAlias.get(), "N");
m_netTalkerAlias.reset();
m_netTalkerAlias.add(1U, data + 2U, 7U);
m_display->writeDMRTA(m_slotNo, m_netTalkerAlias.get(), "N");

if (m_dumpTAData) {
::sprintf(text, "DMR Slot %u, Embedded Talker Alias Block 1", m_slotNo);
CUtils::dump(2U, text, data, 9U);
CUtils::dump(1U, text, data, 9U);
}

m_netTalkerId |= TALKER_ID_BLOCK1;
Expand All @@ -1531,13 +1532,13 @@ void CDMRSlot::writeNetwork(const CDMRData& dmrData)
case FLCO_TALKER_ALIAS_BLOCK2:
if (!(m_netTalkerId & TALKER_ID_BLOCK2)) {
if (!m_netTalkerId)
m_rfTalkerAlias.reset();
m_rfTalkerAlias.add(2, data + 2U, 7U);
m_display->writeDMRTA(m_slotNo, (unsigned char*)m_rfTalkerAlias.get(), "N");
m_netTalkerAlias.reset();
m_netTalkerAlias.add(2U, data + 2U, 7U);
m_display->writeDMRTA(m_slotNo, m_netTalkerAlias.get(), "N");

if (m_dumpTAData) {
::sprintf(text, "DMR Slot %u, Embedded Talker Alias Block 2", m_slotNo);
CUtils::dump(2U, text, data, 9U);
CUtils::dump(1U, text, data, 9U);
}

m_netTalkerId |= TALKER_ID_BLOCK2;
Expand All @@ -1546,13 +1547,13 @@ void CDMRSlot::writeNetwork(const CDMRData& dmrData)
case FLCO_TALKER_ALIAS_BLOCK3:
if (!(m_netTalkerId & TALKER_ID_BLOCK3)) {
if (!m_netTalkerId)
m_rfTalkerAlias.reset();
m_rfTalkerAlias.add(3, data+2U, 7U);
m_display->writeDMRTA(m_slotNo, (unsigned char*)m_rfTalkerAlias.get(), "N");
m_netTalkerAlias.reset();
m_netTalkerAlias.add(3U, data + 2U, 7U);
m_display->writeDMRTA(m_slotNo, m_netTalkerAlias.get(), "N");

if (m_dumpTAData) {
::sprintf(text, "DMR Slot %u, Embedded Talker Alias Block 3", m_slotNo);
CUtils::dump(2U, text, data, 9U);
CUtils::dump(1U, text, data, 9U);
}

m_netTalkerId |= TALKER_ID_BLOCK3;
Expand Down
5 changes: 3 additions & 2 deletions DMRSlot.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2015-2021 by Jonathan Naylor G4KLX
* Copyright (C) 2015-2021,2023 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
Expand Down Expand Up @@ -80,6 +80,7 @@ class CDMRSlot {
unsigned int m_netEmbeddedReadN;
unsigned int m_netEmbeddedWriteN;
unsigned char m_netTalkerId;
CDMRTA m_netTalkerAlias;
CDMRLC* m_rfLC;
CDMRLC* m_netLC;
unsigned char m_rfSeqNo;
Expand Down Expand Up @@ -132,7 +133,7 @@ class CDMRSlot {

static unsigned char* m_idle;

static FLCO m_flco1;
static FLCO m_flco1;
static unsigned char m_id1;
static ACTIVITY_TYPE m_activity1;
static FLCO m_flco2;
Expand Down
146 changes: 78 additions & 68 deletions DMRTA.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2015,2016,2017,2018 Jonathan Naylor, G4KLX
* Copyright (C) 2015,2016,2017,2018,2023 Jonathan Naylor, G4KLX
* Copyright (C) 2018 by Shawn Chain, BG5HHP
*
* This program is free software; you can redistribute it and/or modify
Expand All @@ -18,8 +18,9 @@
#include <cstring>
#include <cassert>

CDMRTA::CDMRTA() :
m_TA(),
CDMRTA::CDMRTA(unsigned int slotNo) :
m_slotNo(slotNo),
m_ta(),
m_buf()
{
}
Expand All @@ -31,13 +32,14 @@ CDMRTA::~CDMRTA()
bool CDMRTA::add(unsigned int blockId, const unsigned char* data, unsigned int len)
{
assert(data != NULL);
if (blockId > 3) {

if (blockId > 3U) {
// invalid block id
reset();
return false;
}

unsigned int offset = blockId * 7;
unsigned int offset = blockId * 7U;

if (offset + len >= sizeof(m_buf)) {
// buffer overflow
Expand All @@ -52,75 +54,83 @@ bool CDMRTA::add(unsigned int blockId, const unsigned char* data, unsigned int l

const unsigned char* CDMRTA::get()
{
return (unsigned char*)m_TA;
return (unsigned char*)m_ta;
}

void CDMRTA::reset()
{
::memset(m_TA, 0, sizeof(m_TA));
::memset(m_buf, 0, sizeof(m_buf));
::memset(m_ta, 0x00U, sizeof(m_ta));
::memset(m_buf, 0x00U, sizeof(m_buf));
}

bool CDMRTA::decodeTA()
{
unsigned char *b;
unsigned char c;
int j;
unsigned int i, t1, t2;

unsigned char* talkerAlias = m_buf;

unsigned int TAformat = (talkerAlias[0] >> 6U) & 0x03U;
unsigned int TAsize = (talkerAlias[0] >> 1U) & 0x1FU;
::strcpy(m_TA, "(could not decode)");

switch (TAformat) {
case 0U: // 7 bit
::memset(m_TA, 0, sizeof(m_TA));
b = &talkerAlias[0];
t1 = 0U; t2 = 0U; c = 0U;
for (i = 0U; (i < 32U) && (t2 < TAsize); i++) {
for (j = 7; j >= 0; j--) {
c = (c << 1U) | (b[i] >> j);
if (++t1 == 7U) {
if (i > 0U)
m_TA[t2++] = c & 0x7FU;

t1 = 0U;
c = 0U;
}
}
}
m_TA[TAsize] = 0;
break;

case 1U: // ISO 8 bit
case 2U: // UTF8
::memcpy(m_TA, talkerAlias + 1U, sizeof(m_TA));
break;

case 3U: // UTF16 poor man's conversion
t2=0;
::memset(&m_TA, 0, sizeof(m_TA));
for (i = 0U; (i < 15U) && (t2 < TAsize); i++) {
if (talkerAlias[2U * i + 1U] == 0)
m_TA[t2++] = talkerAlias[2U * i + 2U];
else
m_TA[t2++] = '?';
}
m_TA[TAsize] = 0;
break;
}

size_t TAlen = ::strlen(m_TA);
LogMessage("DMR Talker Alias (Data Format %u, Received %u/%u char): '%s'", TAformat, TAlen, TAsize, m_TA);

if (TAlen > TAsize) {
if (TAlen < 29U)
strcat(m_TA, " ?");
else
strcpy(m_TA + 28U, " ?");
}

return TAlen >= TAsize;
unsigned int taFormat = (m_buf[0] >> 6U) & 0x03U;
unsigned int taSize = (m_buf[0] >> 1U) & 0x1FU;
::strcpy(m_ta, "(could not decode)");

switch (taFormat) {
case 0U: { // 7 bit
::memset(m_ta, 0x00U, sizeof(m_ta));

unsigned char* b = m_buf;
unsigned int t1 = 0U;
unsigned int t2 = 0U;
unsigned char c = 0U;

for (unsigned int i = 0U; (i < 32U) && (t2 < taSize); i++) {
for (int j = 7; j >= 0; j--) {
c = (c << 1U) | (b[i] >> j);

if (++t1 == 7U) {
if (i > 0U)
m_ta[t2++] = c & 0x7FU;

t1 = 0U;
c = 0U;
}
}
}

m_ta[taSize] = 0;
}
break;

case 1U: // ISO 8 bit
case 2U: // UTF8
::memcpy(m_ta, m_buf + 1U, sizeof(m_ta));
break;

case 3U: { // UTF16 poor man's conversion
unsigned int t2 = 0U;
::memset(&m_ta, 0x00U, sizeof(m_ta));

for (unsigned int i = 0U; (i < 15U) && (t2 < taSize); i++) {
if (m_buf[2U * i + 1U] == 0)
m_ta[t2++] = m_buf[2U * i + 2U];
else
m_ta[t2++] = '?';
}

m_ta[taSize] = 0;
}
break;
}

size_t taLen = ::strlen(m_ta);

if (taLen == taSize)
LogMessage("DMR Slot %u, Talker Alias \"%s\"", m_slotNo, m_ta);

LogDebug("DMR Slot %u, Talker Alias (Data Format %u, Received %u/%u char): '%s'", m_slotNo, taFormat, taLen, taSize, m_ta);

if (taLen > taSize) {
if (taLen < 29U)
::strcat(m_ta, " ?");
else
::strcpy(m_ta + 28U, " ?");
}

return taLen >= taSize;
}

Loading

0 comments on commit d709c2b

Please sign in to comment.