NXDNClients/NXDNGateway/GPSHandler.cpp

253 lines
6.6 KiB
C++
Raw Normal View History

2018-05-22 15:43:04 -04:00
/*
* Copyright (C) 2018,2020 by Jonathan Naylor G4KLX
2018-05-22 15:43:04 -04:00
*
* 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 "GPSHandler.h"
#include "Utils.h"
#include <cstdint>
#include <cstdio>
#include <cassert>
#include <cstring>
const unsigned char NXDN_DATA_TYPE_GPS = 0x06U;
const unsigned int NXDN_DATA_LENGTH = 20U;
2018-05-23 02:32:39 -04:00
const unsigned int NXDN_DATA_MAX_LENGTH = 16U * NXDN_DATA_LENGTH;
2018-05-22 15:43:04 -04:00
2018-11-06 06:27:36 -05:00
CGPSHandler::CGPSHandler(const std::string& callsign, const std::string& suffix, CAPRSWriter* writer) :
2018-05-22 15:43:04 -04:00
m_callsign(callsign),
2018-11-06 06:27:36 -05:00
m_writer(writer),
2018-05-22 15:43:04 -04:00
m_data(NULL),
m_length(0U),
m_source(),
m_suffix(suffix)
2018-05-22 15:43:04 -04:00
{
assert(!callsign.empty());
2018-11-06 06:27:36 -05:00
assert(writer != NULL);
2018-05-22 15:43:04 -04:00
2018-05-23 02:32:39 -04:00
m_data = new unsigned char[NXDN_DATA_MAX_LENGTH];
2018-05-22 15:43:04 -04:00
reset();
}
CGPSHandler::~CGPSHandler()
{
delete[] m_data;
}
void CGPSHandler::processHeader(const std::string& source)
{
2018-05-22 16:04:07 -04:00
reset();
2018-05-22 15:43:04 -04:00
m_source = source;
}
void CGPSHandler::processData(const unsigned char* data)
{
assert(data != NULL);
::memcpy(m_data + m_length, data + 1U, NXDN_DATA_LENGTH);
m_length += NXDN_DATA_LENGTH;
2020-05-02 12:50:39 -04:00
// The packet frame number and block number are zero to indicate the last section of the
// data transmission.
2018-05-22 15:43:04 -04:00
if (data[0U] == 0x00U) {
bool ret = processIcom();
if (!ret)
2020-05-02 12:50:39 -04:00
processKenwood();
reset();
2018-05-22 15:43:04 -04:00
}
}
void CGPSHandler::processEnd()
{
reset();
}
void CGPSHandler::reset()
{
::memset(m_data, 0x00U, NXDN_DATA_MAX_LENGTH);
m_length = 0U;
m_source.clear();
}
bool CGPSHandler::processIcom()
2018-05-22 15:43:04 -04:00
{
if (m_data[0U] != NXDN_DATA_TYPE_GPS)
return false;
2018-05-22 15:43:04 -04:00
if (::memcmp(m_data + 1U, "$G", 2U) != 0)
return false;
2018-05-22 15:43:04 -04:00
if (::strchr((char*)(m_data + 1U), '*') == NULL)
return false;
// From here onwards we have something that looks like Icom GPS data
2018-05-22 15:43:04 -04:00
if (!checkXOR())
return true;
2018-05-22 15:43:04 -04:00
if (::memcmp(m_data + 4U, "RMC", 3U) != 0) {
CUtils::dump("Unhandled NMEA sentence", (unsigned char*)(m_data + 1U), m_length - 1U);
return true;
2018-05-22 15:43:04 -04:00
}
// Parse the $GxRMC string into tokens
char* pRMC[20U];
::memset(pRMC, 0x00U, 20U * sizeof(char*));
unsigned int nRMC = 0U;
char* p = NULL;
2018-05-23 02:32:39 -04:00
char* d = (char*)(m_data + 1U);
2018-11-05 02:48:23 -05:00
while ((p = ::strtok(d, ",\r\n")) != NULL && nRMC < 20U) {
2018-05-22 15:43:04 -04:00
pRMC[nRMC++] = p;
d = NULL;
}
// Is there any position data?
if (pRMC[3U] == NULL || pRMC[4U] == NULL || pRMC[5U] == NULL || pRMC[6U] == NULL || ::strlen(pRMC[3U]) == 0U || ::strlen(pRMC[4U]) == 0U || ::strlen(pRMC[5U]) == 0 || ::strlen(pRMC[6U]) == 0)
return true;
2018-05-22 15:43:04 -04:00
// Is it a valid GPS fix?
if (::strcmp(pRMC[2U], "A") != 0)
return true;
double latitude = ::atof(pRMC[3U]);
double longitude = ::atof(pRMC[5U]);
2018-05-22 15:43:04 -04:00
std::string source = m_source;
if (!m_suffix.empty()) {
source.append("-");
source.append(m_suffix.substr(0U, 1U));
}
2018-05-22 15:43:04 -04:00
char output[300U];
if (pRMC[7U] != NULL && pRMC[8U] != NULL && ::strlen(pRMC[7U]) > 0U && ::strlen(pRMC[8U]) > 0U) {
int bearing = ::atoi(pRMC[8U]);
int speed = ::atoi(pRMC[7U]);
2020-06-01 06:35:02 -04:00
::sprintf(output, "%s>APDPRS,NXDN*,qAR,%s:!%07.2lf%s/%08.2lf%sr%03d/%03d Icom IDAS via MMDVM\r\n",
source.c_str(), m_callsign.c_str(), latitude, pRMC[4U], longitude, pRMC[6U], bearing, speed);
2018-05-22 15:43:04 -04:00
} else {
2020-06-01 06:35:02 -04:00
::sprintf(output, "%s>APDPRS,NXDN*,qAR,%s:!%07.2lf%s/%08.2lf%sr Icom IDAS via MMDVM\r\n",
source.c_str(), m_callsign.c_str(), latitude, pRMC[4U], longitude, pRMC[6U]);
2018-05-22 15:43:04 -04:00
}
2018-11-06 06:27:36 -05:00
m_writer->write(output);
return true;
2018-05-22 15:43:04 -04:00
}
bool CGPSHandler::checkXOR() const
{
2018-05-23 02:32:39 -04:00
char* p1 = ::strchr((char*)m_data, '$');
char* p2 = ::strchr((char*)m_data, '*');
2018-05-22 15:43:04 -04:00
unsigned char res = 0U;
2018-05-23 02:32:39 -04:00
for (char* q = p1 + 1U; q < p2; q++)
2018-05-22 15:43:04 -04:00
res ^= *q;
char buffer[10U];
::sprintf(buffer, "%02X", res);
return ::memcmp(buffer, p2 + 1U, 2U) == 0;
}
2020-05-01 13:44:52 -04:00
bool CGPSHandler::processKenwood()
{
enum {
GPS_FULL,
GPS_SHORT,
GPS_VERY_SHORT
} type;
switch (m_data[0U]) {
case 0x00U:
2020-05-01 13:44:52 -04:00
if (m_length < 38U)
return false;
type = GPS_FULL;
break;
case 0x01U:
2020-05-01 13:44:52 -04:00
if (m_length < 17U)
return false;
type = GPS_SHORT;
break;
case 0x02U:
2020-05-01 13:44:52 -04:00
if (m_length < 15U)
return false;
type = GPS_VERY_SHORT;
break;
default:
2020-05-01 13:44:52 -04:00
return true;
}
2020-05-02 12:50:39 -04:00
unsigned char north = 'N';
unsigned int latAfter = 0x7FFFU;
unsigned int latBefore = 0xFFFFU;
unsigned char east = 'E';
unsigned int lonAfter = 0x7FFFU;
unsigned int lonBefore = 0xFFFFU;
if (type == GPS_VERY_SHORT) {
north = (m_data[7U] & 0x01U) == 0x00U ? 'N' : 'S';
latAfter = ((m_data[7U] & 0xFEU) >> 1) | (m_data[8U] << 7);
latBefore = (m_data[10U] << 8) | m_data[9U];
2020-05-02 12:50:39 -04:00
east = (m_data[11U] & 0x01U) == 0x00U ? 'E' : 'W';
lonAfter = ((m_data[11U] & 0xFEU) >> 1) | (m_data[12U] << 7);
lonBefore = (m_data[13U] << 8) | m_data[13U];
} else {
north = (m_data[9U] & 0x01U) == 0x00U ? 'N' : 'S';
latAfter = ((m_data[9U] & 0xFEU) >> 1) | (m_data[10U] << 7);
latBefore = (m_data[12U] << 8) | m_data[11U];
east = (m_data[13U] & 0x01U) == 0x00U ? 'E' : 'W';
lonAfter = ((m_data[13U] & 0xFEU) >> 1) | (m_data[14U] << 7);
lonBefore = (m_data[16U] << 8) | m_data[15U];
}
2020-05-02 12:50:39 -04:00
if (latAfter == 0x7FFFU || latBefore == 0xFFFFU || lonAfter == 0x7FFFU || lonBefore == 0xFFFFU)
2020-05-01 13:44:52 -04:00
return true;
unsigned int course = 0xFFFFU;
unsigned int speedBefore = 0x3FFU;
unsigned int speedAfter = 0x0FU;
if (type == GPS_FULL) {
2020-05-02 12:50:39 -04:00
course = (m_data[23U] << 4) | (m_data[24U] & 0x0FU);
speedBefore = ((m_data[25U] & 0xF0U) << 2) | (m_data[26U] & 0x3FU);
speedAfter = m_data[25U] & 0x0FU;
}
std::string source = m_source;
if (!m_suffix.empty()) {
source.append("-");
source.append(m_suffix.substr(0U, 1U));
}
char output[300U];
if (course != 0xFFFFU && speedBefore != 0x3FFU && speedAfter != 0x0FU) {
2020-06-01 06:35:02 -04:00
::sprintf(output, "%s>APDPRS,NXDN*,qAR,%s:!%04u.%02u%c/%05u.%02u%cr%03d/%03d Kenwood NEXEDGE via MMDVM\r\n",
2020-05-02 12:50:39 -04:00
source.c_str(), m_callsign.c_str(), latBefore, latAfter / 100U, north, lonBefore, lonAfter / 100U, east, course / 10U, speedBefore);
2020-05-01 13:44:52 -04:00
} else {
2020-06-01 06:35:02 -04:00
::sprintf(output, "%s>APDPRS,NXDN*,qAR,%s:!%04u.%02u%c/%05u.%02u%cr Kenwood NEXEDGE via MMDVM\r\n",
2020-05-02 12:50:39 -04:00
source.c_str(), m_callsign.c_str(), latBefore, latAfter / 100U, north, lonBefore, lonAfter / 100U, east);
}
2020-05-04 08:48:54 -04:00
m_writer->write(output);
2020-05-01 13:44:52 -04:00
return true;
}