1
0
Fork 0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
YSFClients/YSFGateway/GPS.cpp

239 lines
6.5 KiB

9 years ago
/*
* Copyright (C) 2016 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
* 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 "GPS.h"
#include "YSFPayload.h"
#include "YSFDefines.h"
#include "Utils.h"
#include "Log.h"
#include <cstdio>
#include <cassert>
#include <cstring>
const unsigned char NULL_GPS[] = {0x47U, 0x63U};
const unsigned char SHRT_GPS[] = {0x22U, 0x62U};
const unsigned char LONG_GPS[] = {0x47U, 0x64U};
9 years ago
CGPS::CGPS(const std::string& hostname, unsigned int port, const std::string& password) :
m_hostname(hostname),
m_port(port),
m_password(password),
m_buffer(NULL),
m_dt1(false),
m_dt2(false),
m_sent(false)
{
assert(port > 0U);
m_buffer = new unsigned char[20U];
}
CGPS::~CGPS()
{
delete[] m_buffer;
}
void CGPS::data(const unsigned char* source, const unsigned char* data, unsigned char fi, unsigned char dt, unsigned char fn)
{
if (m_sent)
return;
// In case we had the data but not the source callsign
if (m_dt1 && m_dt2) {
transmitGPS(source);
return;
}
CYSFPayload payload;
if (fi == YSF_FI_COMMUNICATIONS && dt == YSF_DT_VD_MODE1) {
if (fn == 3U && !m_dt1) {
bool valid = payload.readVDMode1Data(data, m_buffer);
if (valid) {
m_dt1 = true;
m_dt2 = true;
// If no GPS data then mark it as complete for this transmission
if (::memcmp(m_buffer + 1U, NULL_GPS, 2U) == 0) {
9 years ago
CUtils::dump("Null GPS data received", m_buffer, 20U);
m_sent = true;
}
if (::memcmp(m_buffer + 1U, SHRT_GPS, 2U) == 0) {
9 years ago
CUtils::dump("Short GPS data received", m_buffer, 20U);
transmitGPS(source);
}
if (::memcmp(m_buffer + 1U, LONG_GPS, 2U) == 0) {
9 years ago
CUtils::dump("Long GPS data received", m_buffer, 20U);
transmitGPS(source);
}
}
}
} else if (fi == YSF_FI_COMMUNICATIONS && dt == YSF_DT_VD_MODE2) {
if (fn == 6U && !m_dt1) {
bool valid = payload.readVDMode2Data(data, m_buffer + 0U);
if (valid) {
m_dt1 = true;
// If no GPS data then mark it as complete for this transmission
if (::memcmp(m_buffer + 1U, NULL_GPS, 2U) == 0) {
9 years ago
CUtils::dump("Null GPS data received", m_buffer, 20U);
m_sent = true;
m_dt2 = true;
}
}
}
if (fn == 7U && !m_dt2) {
bool valid = payload.readVDMode2Data(data, m_buffer + 10U);
if (valid) {
m_dt2 = true;
if (::memcmp(m_buffer + 1U, SHRT_GPS, 2U) == 0) {
9 years ago
CUtils::dump("Short GPS data received", m_buffer, 20U);
transmitGPS(source);
}
if (::memcmp(m_buffer + 1U, LONG_GPS, 2U) == 0) {
9 years ago
CUtils::dump("Long GPS data received", m_buffer, 20U);
transmitGPS(source);
}
}
}
}
}
void CGPS::reset()
{
m_dt1 = false;
m_dt2 = false;
m_sent = false;
}
void CGPS::transmitGPS(const unsigned char* source)
{
// We don't know who its from!
if (::memcmp(source, " ", YSF_CALLSIGN_LENGTH) == 0)
return;
for (unsigned int i = 5U; i < 11U; i++) {
unsigned char b = m_buffer[i] & 0xF0U;
if (b != 0x50U && b != 0x30U)
return; // error/unknown
}
unsigned int tens = m_buffer[5U] & 0x0FU;
unsigned int units = m_buffer[6U] & 0x0FU;
unsigned int lat_deg = (tens * 10U) + units;
if (tens > 9U || units > 9U || lat_deg > 89U)
return; // error/unknown
tens = m_buffer[7U] & 0x0FU;
units = m_buffer[8U] & 0x0FU;
unsigned int lat_min = (tens * 10U) + units;
if (tens > 9U || units > 9U || lat_min > 59U)
return; // error/unknown
tens = m_buffer[9U] & 0x0FU;
units = m_buffer[10U] & 0x0FU;
unsigned int lat_min_frac = (tens * 10U) + units;
if (tens > 9U || units > 10U || lat_min_frac > 99U) // units > 10 ??? .. more buggy Yaesu firmware ?
return; // error/unknown
int lat_dir;
unsigned char b = m_buffer[6U] & 0xF0U; // currently a guess
if (b == 0x30U)
lat_dir = 1; // N
else if (b == 0x50U)
lat_dir = -1; // S
else
return; // error/unknown
unsigned int lon_deg;
b = m_buffer[9U] & 0xF0U;
if (b == 0x50U) {
// lon deg 0 to 9, and 100 to 179
b = m_buffer[11U];
if (b >= 0x76U && b <= 0x7FU)
lon_deg = b - 0x76U; // 0 to 9
else if (b >= 0x6CU && b <= 0x75U)
lon_deg = 100U + (b - 0x6CU); // 100 to 109
else if (b >= 0x16U && b <= 0x6BU)
lon_deg = 110U + (b - 0x16U); // 110 to 179
else
return; // error/unknown
} else if (b == 0x30U) {
// lon deg 10 to 99
b = m_buffer[11U];
if (b >= 0x26U && b <= 0x7FU)
lon_deg = 10U + (b - 0x26U); // 10 to 99
else
return; // error/unknown
} else {
return; // error/unknown
}
unsigned int lon_min;
b = m_buffer[12U];
if (b >= 0x58U && b <= 0x61U)
lon_min = b - 0x58U; // 0 to 9
else if (b >= 0x26U && b <= 0x57U)
lon_min = 10U + (b - 0x26U); // 10 to 59
else
return; // error/unknown
unsigned int lon_min_frac;
b = m_buffer[13U];
if (b >= 0x1CU && b <= 0x7FU)
lon_min_frac = b - 0x1CU;
else
return; // error/unknown
int lon_dir;
b = m_buffer[10U] & 0xF0U;
if (b == 0x30U)
lon_dir = 1; // E
else if (b == 0x50U)
lon_dir = -1; // W
else
return; // error/unknown
unsigned int lat_sec = lat_min_frac * 60U;
lat_sec = (lat_sec + (lat_sec % 100U)) / 100U; // with rounding
unsigned int lon_sec = lon_min_frac * 60U;
lon_sec = (lon_sec + (lon_sec % 100U)) / 100U; // with rounding
// >= 0 is north, < 0 is south
float latitude = lat_deg + ((lat_min + ((float)lat_min_frac * 0.01F)) * (1.0F / 60.0F));
latitude *= lat_dir;
// >= 0 is east, < 0 is west
float longitude = lon_deg + ((lon_min + ((float)lon_min_frac * 0.01F)) * (1.0F / 60.0F));
longitude *= lon_dir;
9 years ago
m_sent = true;
}
void CGPS::clock(unsigned int ms)
{
}