-
Notifications
You must be signed in to change notification settings - Fork 91
/
GPSD.cpp
132 lines (106 loc) · 2.89 KB
/
GPSD.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright (C) 2018,2020 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 "GPSD.h"
#if defined(USE_GPSD)
#include <cstdio>
#include <cassert>
#include <cstring>
#include <cmath>
CGPSD::CGPSD(const std::string& address, const std::string& port) :
m_gpsdAddress(address),
m_gpsdPort(port),
m_gpsdData(),
m_idTimer(1000U, 60U),
m_networks(),
m_aprs(NULL)
{
assert(!address.empty());
assert(!port.empty());
}
CGPSD::~CGPSD()
{
}
void CGPSD::addNetwork(CDMRNetwork* network)
{
assert(network != NULL);
m_networks.push_back(network);
}
void CGPSD::setAPRS(CAPRSWriter* aprs)
{
assert(aprs != NULL);
m_aprs = aprs;
}
bool CGPSD::open()
{
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));
return false;
}
::gps_stream(&m_gpsdData, WATCH_ENABLE | WATCH_JSON, NULL);
LogMessage("Connected to GPSD");
m_idTimer.start();
return true;
}
void CGPSD::clock(unsigned int ms)
{
m_idTimer.clock(ms);
if (m_idTimer.hasExpired()) {
sendReport();
m_idTimer.start();
}
}
void CGPSD::close()
{
::gps_stream(&m_gpsdData, WATCH_DISABLE, NULL);
::gps_close(&m_gpsdData);
}
void CGPSD::sendReport()
{
if (!::gps_waiting(&m_gpsdData, 0))
return;
#if GPSD_API_MAJOR_VERSION >= 7
if (::gps_read(&m_gpsdData, NULL, 0) <= 0)
return;
#else
if (::gps_read(&m_gpsdData) <= 0)
return;
#endif
#if GPSD_API_MAJOR_VERSION >= 10
if (m_gpsdData.fix.status != STATUS_FIX)
#else
if (m_gpsdData.status != STATUS_FIX)
#endif
return;
bool latlonSet = (m_gpsdData.set & LATLON_SET) == LATLON_SET;
if (!latlonSet)
return;
bool altitudeSet = (m_gpsdData.set & ALTITUDE_SET) == ALTITUDE_SET;
float latitude = float(m_gpsdData.fix.latitude);
float longitude = float(m_gpsdData.fix.longitude);
#if GPSD_API_MAJOR_VERSION >= 9
float altitude = float(m_gpsdData.fix.altMSL);
#else
float altitude = float(m_gpsdData.fix.altitude);
#endif
if (m_aprs != NULL)
m_aprs->setLocation(latitude, longitude, altitudeSet ? altitude : 0.0F);
for (std::vector<CDMRNetwork*>::const_iterator it = m_networks.begin(); it != m_networks.end(); ++it)
(*it)->writeHomePosition(latitude, longitude);
}
#endif