2018-05-22 15:43:04 -04:00
|
|
|
/*
|
2020-03-03 10:31:59 -05: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>
|
|
|
|
|
2020-06-05 04:24:42 -04:00
|
|
|
CAPRSWriter::CAPRSWriter(const std::string& callsign, const std::string& suffix, const std::string& address, unsigned int 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(),
|
2020-06-01 06:35:02 -04:00
|
|
|
m_aprsAddress(),
|
|
|
|
m_aprsPort(port),
|
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-06-01 06:35:02 -04:00
|
|
|
m_aprsAddress = CUDPSocket::lookup(address);
|
2018-05-22 15:43:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
CAPRSWriter::~CAPRSWriter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-06 06:27:36 -05:00
|
|
|
void CAPRSWriter::setInfo(unsigned int txFrequency, unsigned int rxFrequency, const std::string& desc)
|
2018-05-22 15:43:04 -04:00
|
|
|
{
|
|
|
|
m_txFrequency = txFrequency;
|
|
|
|
m_rxFrequency = rxFrequency;
|
|
|
|
m_desc = desc;
|
|
|
|
}
|
|
|
|
|
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-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-06-04 07:23:24 -04:00
|
|
|
bool ret = m_aprsSocket.open();
|
|
|
|
if (!ret)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
LogMessage("Opened connection to the APRS Gateway");
|
|
|
|
|
2020-06-05 06:16:52 -04:00
|
|
|
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-06-01 06:35:02 -04:00
|
|
|
m_aprsSocket.write((unsigned char*)data, (unsigned int)::strlen(data), m_aprsAddress, m_aprsPort);
|
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();
|
2020-06-05 06:16:52 -04:00
|
|
|
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 %.5LfMHz %c%.4lfMHz%s%s",
|
|
|
|
(long double)(m_txFrequency) / 1000000.0F,
|
|
|
|
offset < 0.0F ? '-' : '+',
|
|
|
|
::fabs(offset), m_desc.empty() ? "" : ", ", m_desc.c_str());
|
|
|
|
} else {
|
|
|
|
::sprintf(desc, "MMDVM Voice%s%s", m_desc.empty() ? "" : ", ", m_desc.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* band = "4m";
|
|
|
|
if (m_txFrequency >= 1200000000U)
|
|
|
|
band = "1.2";
|
|
|
|
else if (m_txFrequency >= 420000000U)
|
|
|
|
band = "440";
|
|
|
|
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;
|
|
|
|
size_t pos = server.find_first_of('-');
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
server.append("-S");
|
|
|
|
else
|
|
|
|
server.append("S");
|
|
|
|
|
|
|
|
char output[500U];
|
2020-06-01 06:35:02 -04:00
|
|
|
::sprintf(output, "%s>APDG04,TCPIP*,qAC,%s:!%s%cD%s%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',
|
|
|
|
lon, (m_longitude < 0.0F) ? 'W' : 'E',
|
|
|
|
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
|
|
|
|
2020-06-24 05:42:53 -04:00
|
|
|
#if GPSD_API_MAJOR_VERSION >= 7
|
2020-06-03 07:50:03 -04:00
|
|
|
if (::gps_read(&m_gpsdData, NULL, 0) <= 0)
|
|
|
|
return;
|
2020-06-24 05:42:53 -04:00
|
|
|
#else
|
|
|
|
if (::gps_read(&m_gpsdData) <= 0)
|
|
|
|
return;
|
|
|
|
#endif
|
2020-06-03 07:50:03 -04:00
|
|
|
|
2020-06-04 03:54:12 -04:00
|
|
|
if (m_gpsdData.status != STATUS_FIX)
|
|
|
|
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 %.5LfMHz %c%.4lfMHz%s%s",
|
|
|
|
(long double)(m_txFrequency) / 1000000.0F,
|
|
|
|
offset < 0.0F ? '-' : '+',
|
|
|
|
::fabs(offset), m_desc.empty() ? "" : ", ", m_desc.c_str());
|
|
|
|
} else {
|
|
|
|
::sprintf(desc, "MMDVM Voice%s%s", m_desc.empty() ? "" : ", ", m_desc.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* band = "4m";
|
|
|
|
if (m_txFrequency >= 1200000000U)
|
|
|
|
band = "1.2";
|
|
|
|
else if (m_txFrequency >= 420000000U)
|
|
|
|
band = "440";
|
|
|
|
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;
|
|
|
|
size_t pos = server.find_first_of('-');
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
server.append("-S");
|
|
|
|
else
|
|
|
|
server.append("S");
|
|
|
|
|
|
|
|
char output[500U];
|
|
|
|
::sprintf(output, "%s>APDG03,TCPIP*,qAC,%s:!%s%cD%s%c&",
|
|
|
|
m_callsign.c_str(), server.c_str(),
|
|
|
|
lat, (rawLatitude < 0.0F) ? 'S' : 'N',
|
|
|
|
lon, (rawLongitude < 0.0F) ? 'W' : 'E');
|
|
|
|
|
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
|