NXDNClients/NXDNGateway/APRSWriter.cpp

345 lines
8.5 KiB
C++
Raw Normal View History

2018-05-22 15:43:04 -04:00
/*
* Copyright (C) 2010-2014,2016,2017,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 "APRSWriter.h"
2020-06-03 07:24:29 -04:00
#include "Log.h"
2018-05-22 15:43:04 -04:00
#include <cstdio>
#include <cassert>
#include <cstring>
#include <cmath>
CAPRSWriter::CAPRSWriter(const std::string& callsign, const std::string& suffix, const std::string& address, unsigned short port, bool debug) :
2018-11-06 06:27:36 -05:00
m_idTimer(1000U),
2018-05-22 15:43:04 -04:00
m_callsign(callsign),
2020-06-05 04:24:42 -04:00
m_debug(debug),
2018-05-22 15:43:04 -04:00
m_txFrequency(0U),
m_rxFrequency(0U),
m_latitude(0.0F),
m_longitude(0.0F),
m_height(0),
2019-04-10 15:15:16 -04:00
m_desc(),
m_symbol(),
2020-09-03 10:12:03 -04:00
m_aprsAddr(),
2020-09-06 08:45:06 -04:00
m_aprsAddrLen(0U),
2020-06-03 07:24:29 -04:00
m_aprsSocket()
2020-06-21 12:43:31 -04:00
#if defined(USE_GPSD)
2020-06-03 07:24:29 -04:00
,m_gpsdEnabled(false),
m_gpsdAddress(),
2020-06-04 07:23:24 -04:00
m_gpsdPort(),
2020-06-03 07:24:29 -04:00
m_gpsdData()
#endif
2018-05-22 15:43:04 -04:00
{
assert(!callsign.empty());
assert(!address.empty());
assert(port > 0U);
if (!suffix.empty()) {
m_callsign.append("-");
m_callsign.append(suffix.substr(0U, 1U));
}
2020-09-06 08:45:06 -04:00
if (CUDPSocket::lookup(address, port, m_aprsAddr, m_aprsAddrLen) != 0)
m_aprsAddrLen = 0U;
2018-05-22 15:43:04 -04:00
}
CAPRSWriter::~CAPRSWriter()
{
}
void CAPRSWriter::setInfo(unsigned int txFrequency, unsigned int rxFrequency, const std::string& desc, const std::string& symbol)
2018-05-22 15:43:04 -04:00
{
m_txFrequency = txFrequency;
m_rxFrequency = rxFrequency;
m_desc = desc;
m_symbol = symbol;
2018-05-22 15:43:04 -04:00
}
2018-11-06 06:27:36 -05:00
void CAPRSWriter::setStaticLocation(float latitude, float longitude, int height)
{
m_latitude = latitude;
m_longitude = longitude;
m_height = height;
}
2020-06-03 07:24:29 -04:00
void CAPRSWriter::setGPSDLocation(const std::string& address, const std::string& port)
2018-11-06 06:27:36 -05:00
{
2020-06-21 12:43:31 -04:00
#if defined(USE_GPSD)
2018-11-06 06:27:36 -05:00
assert(!address.empty());
2020-06-03 07:24:29 -04:00
assert(!port.empty());
2018-11-06 06:27:36 -05:00
2020-06-03 07:24:29 -04:00
m_gpsdEnabled = true;
m_gpsdAddress = address;
m_gpsdPort = port;
#endif
2018-11-06 06:27:36 -05:00
}
2018-05-22 15:43:04 -04:00
bool CAPRSWriter::open()
{
2020-09-06 08:45:06 -04:00
if (m_aprsAddrLen == 0U) {
LogError("Unable to resolve the address of the APRS Gateway");
return false;
}
2020-06-21 12:43:31 -04:00
#if defined(USE_GPSD)
2020-06-03 07:24:29 -04:00
if (m_gpsdEnabled) {
int ret = ::gps_open(m_gpsdAddress.c_str(), m_gpsdPort.c_str(), &m_gpsdData);
if (ret != 0) {
LogError("Error when opening access to gpsd - %d - %s", errno, ::gps_errstr(errno));
2018-11-06 06:27:36 -05:00
return false;
}
2020-06-03 07:24:29 -04:00
::gps_stream(&m_gpsdData, WATCH_ENABLE | WATCH_JSON, NULL);
2020-06-04 07:23:24 -04:00
LogMessage("Connected to GPSD");
2018-11-06 06:27:36 -05:00
}
2020-06-03 07:24:29 -04:00
#endif
2020-09-06 08:45:06 -04:00
bool ret = m_aprsSocket.open(m_aprsAddr);
2020-06-04 07:23:24 -04:00
if (!ret)
return false;
LogMessage("Opened connection to the APRS Gateway");
m_idTimer.setTimeout(60U);
m_idTimer.start();
2020-06-04 07:23:24 -04:00
return true;
2018-05-22 15:43:04 -04:00
}
void CAPRSWriter::write(const char* data)
{
assert(data != NULL);
2020-06-05 04:24:42 -04:00
if (m_debug)
LogDebug("APRS ==> %s", data);
2020-09-03 10:12:03 -04:00
m_aprsSocket.write((unsigned char*)data, (unsigned int)::strlen(data), m_aprsAddr, m_aprsAddrLen);
2018-05-22 15:43:04 -04:00
}
void CAPRSWriter::clock(unsigned int ms)
{
m_idTimer.clock(ms);
2020-06-21 12:43:31 -04:00
#if defined(USE_GPSD)
2020-06-03 07:24:29 -04:00
if (m_gpsdEnabled) {
2018-11-06 06:27:36 -05:00
if (m_idTimer.hasExpired()) {
2020-06-03 07:24:29 -04:00
sendIdFrameMobile();
2018-11-06 06:27:36 -05:00
m_idTimer.start();
}
} else {
2020-06-03 07:24:29 -04:00
#endif
2018-11-06 06:27:36 -05:00
if (m_idTimer.hasExpired()) {
sendIdFrameFixed();
m_idTimer.setTimeout(20U * 60U);
2018-11-06 06:27:36 -05:00
m_idTimer.start();
}
2020-06-21 12:43:31 -04:00
#if defined(USE_GPSD)
2018-05-22 15:43:04 -04:00
}
2020-06-03 07:24:29 -04:00
#endif
2018-05-22 15:43:04 -04:00
}
void CAPRSWriter::close()
{
2020-06-01 06:35:02 -04:00
m_aprsSocket.close();
2018-11-06 06:27:36 -05:00
2020-06-21 12:43:31 -04:00
#if defined(USE_GPSD)
2020-06-03 07:24:29 -04:00
if (m_gpsdEnabled) {
::gps_stream(&m_gpsdData, WATCH_DISABLE, NULL);
::gps_close(&m_gpsdData);
2020-06-01 06:35:02 -04:00
}
2020-06-03 07:24:29 -04:00
#endif
2018-11-06 06:27:36 -05:00
}
void CAPRSWriter::sendIdFrameFixed()
2018-05-22 15:43:04 -04:00
{
// Default values aren't passed on
if (m_latitude == 0.0F && m_longitude == 0.0F)
return;
char desc[200U];
if (m_txFrequency != 0U) {
float offset = float(int(m_rxFrequency) - int(m_txFrequency)) / 1000000.0F;
::sprintf(desc, "MMDVM Voice (NXDN) %.5LfMHz %c%.4lfMHz%s%s",
2018-05-22 15:43:04 -04:00
(long double)(m_txFrequency) / 1000000.0F,
offset < 0.0F ? '-' : '+',
::fabs(offset), m_desc.empty() ? "" : ", ", m_desc.c_str());
} else {
::sprintf(desc, "MMDVM Voice (NXDN)%s%s", m_desc.empty() ? "" : ", ", m_desc.c_str());
2018-05-22 15:43:04 -04:00
}
const char* band = "4m";
if (m_txFrequency >= 1200000000U)
band = "23cm/1.2GHz";
2018-05-22 15:43:04 -04:00
else if (m_txFrequency >= 420000000U)
band = "70cm";
2018-05-22 15:43:04 -04:00
else if (m_txFrequency >= 144000000U)
band = "2m";
else if (m_txFrequency >= 50000000U)
band = "6m";
else if (m_txFrequency >= 28000000U)
band = "10m";
double tempLat = ::fabs(m_latitude);
double tempLong = ::fabs(m_longitude);
double latitude = ::floor(tempLat);
double longitude = ::floor(tempLong);
latitude = (tempLat - latitude) * 60.0 + latitude * 100.0;
longitude = (tempLong - longitude) * 60.0 + longitude * 100.0;
char lat[20U];
::sprintf(lat, "%07.2lf", latitude);
char lon[20U];
::sprintf(lon, "%08.2lf", longitude);
std::string server = m_callsign;
std::string symbol = m_symbol;
2018-05-22 15:43:04 -04:00
size_t pos = server.find_first_of('-');
if (pos == std::string::npos)
server.append("-S");
else
server.append("S");
if (symbol.empty())
symbol.append("D&");
2018-05-22 15:43:04 -04:00
char output[500U];
::sprintf(output, "%s>APDG04,TCPIP*,qAC,%s:!%s%c%c%s%c%c/A=%06.0f%s %s\r\n",
2018-05-22 15:43:04 -04:00
m_callsign.c_str(), server.c_str(),
lat, (m_latitude < 0.0F) ? 'S' : 'N', symbol[0],
lon, (m_longitude < 0.0F) ? 'W' : 'E', symbol[1],
2018-05-22 15:43:04 -04:00
float(m_height) * 3.28F, band, desc);
2020-06-05 04:24:42 -04:00
if (m_debug)
LogDebug("APRS ==> %s", output);
2020-06-01 06:35:02 -04:00
write(output);
2018-11-06 06:27:36 -05:00
}
2018-05-22 15:43:04 -04:00
2020-06-21 12:43:31 -04:00
#if defined(USE_GPSD)
2018-11-06 06:27:36 -05:00
void CAPRSWriter::sendIdFrameMobile()
{
2020-06-03 07:24:29 -04:00
if (!::gps_waiting(&m_gpsdData, 0))
return;
2018-11-06 06:27:36 -05:00
#if GPSD_API_MAJOR_VERSION >= 7
2020-06-03 07:50:03 -04:00
if (::gps_read(&m_gpsdData, NULL, 0) <= 0)
return;
#else
if (::gps_read(&m_gpsdData) <= 0)
return;
#endif
2020-06-03 07:50:03 -04:00
#if GPSD_API_MAJOR_VERSION >= 10
2023-02-13 12:54:56 -05:00
if (m_gpsdData.fix.status != STATUS_FIX)
#else
if (m_gpsdData.status != STATUS_FIX)
#endif
2020-06-04 03:54:12 -04:00
return;
2020-06-03 07:24:29 -04:00
bool latlonSet = (m_gpsdData.set & LATLON_SET) == LATLON_SET;
bool altitudeSet = (m_gpsdData.set & ALTITUDE_SET) == ALTITUDE_SET;
bool velocitySet = (m_gpsdData.set & SPEED_SET) == SPEED_SET;
bool bearingSet = (m_gpsdData.set & TRACK_SET) == TRACK_SET;
2018-11-06 06:27:36 -05:00
2020-06-03 07:24:29 -04:00
if (!latlonSet)
2018-11-06 06:27:36 -05:00
return;
2020-06-03 07:24:29 -04:00
float rawLatitude = float(m_gpsdData.fix.latitude);
float rawLongitude = float(m_gpsdData.fix.longitude);
2020-06-24 06:04:51 -04:00
#if GPSD_API_MAJOR_VERSION >= 9
2020-06-03 07:24:29 -04:00
float rawAltitude = float(m_gpsdData.fix.altMSL);
2020-06-24 06:04:51 -04:00
#else
float rawAltitude = float(m_gpsdData.fix.altitude);
#endif
2020-06-03 07:24:29 -04:00
float rawVelocity = float(m_gpsdData.fix.speed);
float rawBearing = float(m_gpsdData.fix.track);
2018-11-06 06:27:36 -05:00
char desc[200U];
if (m_txFrequency != 0U) {
float offset = float(int(m_rxFrequency) - int(m_txFrequency)) / 1000000.0F;
::sprintf(desc, "MMDVM Voice (NXDN) %.5LfMHz %c%.4lfMHz%s%s",
2018-11-06 06:27:36 -05:00
(long double)(m_txFrequency) / 1000000.0F,
offset < 0.0F ? '-' : '+',
::fabs(offset), m_desc.empty() ? "" : ", ", m_desc.c_str());
} else {
::sprintf(desc, "MMDVM Voice (NXDN)%s%s", m_desc.empty() ? "" : ", ", m_desc.c_str());
2018-11-06 06:27:36 -05:00
}
const char* band = "4m";
if (m_txFrequency >= 1200000000U)
band = "23cm/1.2GHz";
2018-11-06 06:27:36 -05:00
else if (m_txFrequency >= 420000000U)
band = "70cm";
2018-11-06 06:27:36 -05:00
else if (m_txFrequency >= 144000000U)
band = "2m";
else if (m_txFrequency >= 50000000U)
band = "6m";
else if (m_txFrequency >= 28000000U)
band = "10m";
double tempLat = ::fabs(rawLatitude);
double tempLong = ::fabs(rawLongitude);
double latitude = ::floor(tempLat);
double longitude = ::floor(tempLong);
latitude = (tempLat - latitude) * 60.0 + latitude * 100.0;
longitude = (tempLong - longitude) * 60.0 + longitude * 100.0;
char lat[20U];
::sprintf(lat, "%07.2lf", latitude);
char lon[20U];
::sprintf(lon, "%08.2lf", longitude);
std::string server = m_callsign;
std::string symbol = m_symbol;
2018-11-06 06:27:36 -05:00
size_t pos = server.find_first_of('-');
if (pos == std::string::npos)
server.append("-S");
else
server.append("S");
if (symbol.empty())
symbol.append("D&");
2018-11-06 06:27:36 -05:00
char output[500U];
::sprintf(output, "%s>APDG03,TCPIP*,qAC,%s:!%s%c%c%s%c%c",
2018-11-06 06:27:36 -05:00
m_callsign.c_str(), server.c_str(),
lat, (rawLatitude < 0.0F) ? 'S' : 'N', symbol[0],
lon, (rawLongitude < 0.0F) ? 'W' : 'E',symbol[0]);
2018-11-06 06:27:36 -05:00
2020-06-03 07:24:29 -04:00
if (bearingSet && velocitySet)
2018-11-06 06:27:36 -05:00
::sprintf(output + ::strlen(output), "%03.0f/%03.0f", rawBearing, rawVelocity * 0.539957F);
2020-06-03 07:24:29 -04:00
if (altitudeSet)
::sprintf(output + ::strlen(output), "/A=%06.0f", float(rawAltitude) * 3.28F);
::sprintf(output + ::strlen(output), "%s %s\r\n", band, desc);
2018-11-06 06:27:36 -05:00
2020-06-05 04:24:42 -04:00
if (m_debug)
LogDebug("APRS ==> %s", output);
2020-06-01 06:35:02 -04:00
write(output);
2018-05-22 15:43:04 -04:00
}
2020-06-21 12:43:31 -04:00
#endif