2018-12-08 14:09:59 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015,2016,2017,2018 Jonathan Naylor, G4KLX
|
|
|
|
* Copyright (C) 2018 by Shawn Chain, BG5HHP
|
|
|
|
*
|
|
|
|
* 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 "DMRTA.h"
|
|
|
|
#include "Log.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
2018-12-08 20:24:05 +00:00
|
|
|
#include <cassert>
|
2018-12-08 14:09:59 +00:00
|
|
|
|
|
|
|
CDMRTA::CDMRTA() :
|
|
|
|
m_TA(),
|
2018-12-09 09:24:11 +00:00
|
|
|
m_buf()
|
2018-12-08 14:09:59 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-08 20:24:05 +00:00
|
|
|
CDMRTA::~CDMRTA()
|
|
|
|
{
|
2018-12-08 14:09:59 +00:00
|
|
|
}
|
|
|
|
|
2018-12-09 09:24:11 +00:00
|
|
|
bool CDMRTA::add(unsigned int blockId, const unsigned char* data, unsigned int len)
|
2018-12-08 20:24:05 +00:00
|
|
|
{
|
|
|
|
assert(data != NULL);
|
2018-12-09 15:49:15 +00:00
|
|
|
if (blockId > 3) {
|
|
|
|
// invalid block id
|
|
|
|
reset();
|
|
|
|
return false;
|
2018-12-09 09:24:11 +00:00
|
|
|
}
|
|
|
|
|
2018-12-09 15:49:15 +00:00
|
|
|
unsigned int offset = blockId * 7;
|
|
|
|
|
2018-12-09 09:24:11 +00:00
|
|
|
if (offset + len >= sizeof(m_buf)) {
|
2018-12-08 14:09:59 +00:00
|
|
|
// buffer overflow
|
|
|
|
reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-09 09:24:11 +00:00
|
|
|
::memcpy(m_buf + offset, data, len);
|
2018-12-08 14:09:59 +00:00
|
|
|
|
2018-12-09 15:49:15 +00:00
|
|
|
return decodeTA();
|
2018-12-08 14:09:59 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 20:24:05 +00:00
|
|
|
const unsigned char* CDMRTA::get()
|
|
|
|
{
|
2018-12-08 14:09:59 +00:00
|
|
|
return (unsigned char*)m_TA;
|
|
|
|
}
|
|
|
|
|
2018-12-08 20:24:05 +00:00
|
|
|
void CDMRTA::reset()
|
|
|
|
{
|
2018-12-08 14:09:59 +00:00
|
|
|
::memset(m_TA, 0, sizeof(m_TA));
|
|
|
|
::memset(m_buf, 0, sizeof(m_buf));
|
|
|
|
}
|
|
|
|
|
2018-12-09 15:49:15 +00:00
|
|
|
bool CDMRTA::decodeTA()
|
2018-12-08 20:24:05 +00:00
|
|
|
{
|
2018-12-08 14:09:59 +00:00
|
|
|
unsigned char *b;
|
|
|
|
unsigned char c;
|
|
|
|
int j;
|
2018-12-08 20:24:05 +00:00
|
|
|
unsigned int i, t1, t2;
|
2018-12-08 14:09:59 +00:00
|
|
|
|
|
|
|
unsigned char* talkerAlias = m_buf;
|
|
|
|
|
2018-12-08 20:24:05 +00:00
|
|
|
unsigned int TAformat = (talkerAlias[0] >> 6U) & 0x03U;
|
|
|
|
unsigned int TAsize = (talkerAlias[0] >> 1U) & 0x1FU;
|
2019-11-05 09:01:49 +00:00
|
|
|
::memcpy(m_TA, "(could not decode)", sizeof(m_TA));
|
2018-12-08 14:09:59 +00:00
|
|
|
|
|
|
|
switch (TAformat) {
|
|
|
|
case 0U: // 7 bit
|
|
|
|
::memset(m_TA, 0, sizeof(m_TA));
|
2018-12-08 20:24:05 +00:00
|
|
|
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;
|
2018-12-08 14:09:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-08 20:24:05 +00:00
|
|
|
m_TA[TAsize] = 0;
|
2018-12-08 14:09:59 +00:00
|
|
|
break;
|
2018-12-08 20:24:05 +00:00
|
|
|
|
2018-12-08 14:09:59 +00:00
|
|
|
case 1U: // ISO 8 bit
|
|
|
|
case 2U: // UTF8
|
2019-11-05 09:01:49 +00:00
|
|
|
::memcpy(m_TA, talkerAlias + 1U, sizeof(m_TA));
|
2018-12-08 14:09:59 +00:00
|
|
|
break;
|
2018-12-08 20:24:05 +00:00
|
|
|
|
2018-12-08 14:09:59 +00:00
|
|
|
case 3U: // UTF16 poor man's conversion
|
|
|
|
t2=0;
|
2018-12-08 20:24:05 +00:00
|
|
|
::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];
|
2018-12-08 14:09:59 +00:00
|
|
|
else
|
|
|
|
m_TA[t2++] = '?';
|
|
|
|
}
|
2018-12-08 20:24:05 +00:00
|
|
|
m_TA[TAsize] = 0;
|
2018-12-08 14:09:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-09 15:49:15 +00:00
|
|
|
size_t TAlen = ::strlen(m_TA);
|
|
|
|
LogMessage("DMR Talker Alias (Data Format %u, Received %u/%u char): '%s'", TAformat, TAlen, TAsize, m_TA);
|
2018-12-08 20:24:05 +00:00
|
|
|
|
2018-12-09 15:49:15 +00:00
|
|
|
if (TAlen > TAsize) {
|
|
|
|
if (TAlen < 29U)
|
2019-11-05 09:01:49 +00:00
|
|
|
strcat(m_TA, " ?");
|
2018-12-08 14:09:59 +00:00
|
|
|
else
|
2019-11-05 09:01:49 +00:00
|
|
|
strcpy(m_TA + 28U, " ?");
|
2018-12-08 14:09:59 +00:00
|
|
|
}
|
2018-12-09 15:49:15 +00:00
|
|
|
|
|
|
|
return TAlen >= TAsize;
|
2018-12-08 20:24:05 +00:00
|
|
|
}
|