Skip to content

Commit edce667

Browse files
committed
Replace MobileGPS with gpsd.
1 parent 1dc91b9 commit edce667

15 files changed

+222
-177
lines changed

Conf.cpp

+16-16
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ enum SECTION {
5757
SECTION_OLED,
5858
SECTION_LCDPROC,
5959
SECTION_LOCK_FILE,
60-
SECTION_MOBILE_GPS,
60+
SECTION_GPSD,
6161
SECTION_REMOTE_CONTROL
6262
};
6363

@@ -283,9 +283,9 @@ m_lcdprocUTC(false),
283283
m_lcdprocDimOnIdle(false),
284284
m_lockFileEnabled(false),
285285
m_lockFileName(),
286-
m_mobileGPSEnabled(false),
287-
m_mobileGPSAddress(),
288-
m_mobileGPSPort(0U),
286+
m_gpsdEnabled(false),
287+
m_gpsdAddress(),
288+
m_gpsdPort(),
289289
m_remoteControlEnabled(false),
290290
m_remoteControlPort(0U)
291291
{
@@ -367,8 +367,8 @@ bool CConf::read()
367367
section = SECTION_LCDPROC;
368368
else if (::strncmp(buffer, "[Lock File]", 11U) == 0)
369369
section = SECTION_LOCK_FILE;
370-
else if (::strncmp(buffer, "[Mobile GPS]", 12U) == 0)
371-
section = SECTION_MOBILE_GPS;
370+
else if (::strncmp(buffer, "[GPSD]", 6U) == 0)
371+
section = SECTION_GPSD;
372372
else if (::strncmp(buffer, "[Remote Control]", 16U) == 0)
373373
section = SECTION_REMOTE_CONTROL;
374374
else
@@ -950,13 +950,13 @@ bool CConf::read()
950950
m_lockFileEnabled = ::atoi(value) == 1;
951951
else if (::strcmp(key, "File") == 0)
952952
m_lockFileName = value;
953-
} else if (section == SECTION_MOBILE_GPS) {
953+
} else if (section == SECTION_GPSD) {
954954
if (::strcmp(key, "Enable") == 0)
955-
m_mobileGPSEnabled = ::atoi(value) == 1;
955+
m_gpsdEnabled = ::atoi(value) == 1;
956956
else if (::strcmp(key, "Address") == 0)
957-
m_mobileGPSAddress = value;
957+
m_gpsdAddress = value;
958958
else if (::strcmp(key, "Port") == 0)
959-
m_mobileGPSPort = (unsigned int)::atoi(value);
959+
m_gpsdPort = value;
960960
} else if (section == SECTION_REMOTE_CONTROL) {
961961
if (::strcmp(key, "Enable") == 0)
962962
m_remoteControlEnabled = ::atoi(value) == 1;
@@ -2071,19 +2071,19 @@ std::string CConf::getLockFileName() const
20712071
return m_lockFileName;
20722072
}
20732073

2074-
bool CConf::getMobileGPSEnabled() const
2074+
bool CConf::getGPSDEnabled() const
20752075
{
2076-
return m_mobileGPSEnabled;
2076+
return m_gpsdEnabled;
20772077
}
20782078

2079-
std::string CConf::getMobileGPSAddress() const
2079+
std::string CConf::getGPSDAddress() const
20802080
{
2081-
return m_mobileGPSAddress;
2081+
return m_gpsdAddress;
20822082
}
20832083

2084-
unsigned int CConf::getMobileGPSPort() const
2084+
std::string CConf::getGPSDPort() const
20852085
{
2086-
return m_mobileGPSPort;
2086+
return m_gpsdPort;
20872087
}
20882088

20892089
bool CConf::getRemoteControlEnabled() const

Conf.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,10 @@ class CConf
306306
bool getLockFileEnabled() const;
307307
std::string getLockFileName() const;
308308

309-
// The Mobile GPS section
310-
bool getMobileGPSEnabled() const;
311-
std::string getMobileGPSAddress() const;
312-
unsigned int getMobileGPSPort() const;
309+
// The GPSD section
310+
bool getGPSDEnabled() const;
311+
std::string getGPSDAddress() const;
312+
std::string getGPSDPort() const;
313313

314314
// The Remote Control section
315315
bool getRemoteControlEnabled() const;
@@ -565,9 +565,9 @@ class CConf
565565
bool m_lockFileEnabled;
566566
std::string m_lockFileName;
567567

568-
bool m_mobileGPSEnabled;
569-
std::string m_mobileGPSAddress;
570-
unsigned int m_mobileGPSPort;
568+
bool m_gpsdEnabled;
569+
std::string m_gpsdAddress;
570+
std::string m_gpsdPort;
571571

572572
bool m_remoteControlEnabled;
573573
unsigned int m_remoteControlPort;

GPSD.cpp

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (C) 2018,2020 by Jonathan Naylor G4KLX
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17+
*/
18+
19+
#include "GPSD.h"
20+
21+
#if defined(USE_GPS)
22+
23+
#include <cstdio>
24+
#include <cassert>
25+
#include <cstring>
26+
#include <cmath>
27+
28+
CGPSD::CGPSD(const std::string& address, const std::string& port, CDMRNetwork* network) :
29+
m_gpsdAddress(address),
30+
m_gpsdPort(port),
31+
m_network(network),
32+
m_gpsdData(),
33+
m_idTimer(1000U, 60U)
34+
{
35+
assert(!address.empty());
36+
assert(!port.empty());
37+
assert(network != NULL);
38+
}
39+
40+
CGPSD::~CGPSD()
41+
{
42+
}
43+
44+
bool CGPSD::open()
45+
{
46+
int ret = ::gps_open(m_gpsdAddress.c_str(), m_gpsdPort.c_str(), &m_gpsdData);
47+
if (ret != 0) {
48+
LogError("Error when opening access to gpsd - %d - %s", errno, ::gps_errstr(errno));
49+
return false;
50+
}
51+
52+
::gps_stream(&m_gpsdData, WATCH_ENABLE | WATCH_JSON, NULL);
53+
54+
LogMessage("Connected to GPSD");
55+
56+
m_idTimer.start();
57+
58+
return true;
59+
}
60+
61+
void CGPSD::clock(unsigned int ms)
62+
{
63+
m_idTimer.clock(ms);
64+
65+
if (m_idTimer.hasExpired()) {
66+
sendReport();
67+
m_idTimer.start();
68+
}
69+
}
70+
71+
void CGPSD::close()
72+
{
73+
::gps_stream(&m_gpsdData, WATCH_DISABLE, NULL);
74+
::gps_close(&m_gpsdData);
75+
}
76+
77+
void CGPSD::sendReport()
78+
{
79+
if (!::gps_waiting(&m_gpsdData, 0))
80+
return;
81+
82+
#if GPSD_API_MAJOR_VERSION >= 7
83+
if (::gps_read(&m_gpsdData, NULL, 0) <= 0)
84+
return;
85+
#else
86+
if (::gps_read(&m_gpsdData) <= 0)
87+
return;
88+
#endif
89+
90+
if (m_gpsdData.status != STATUS_FIX)
91+
return;
92+
93+
bool latlonSet = (m_gpsdData.set & LATLON_SET) == LATLON_SET;
94+
if (!latlonSet)
95+
return;
96+
97+
float latitude = float(m_gpsdData.fix.latitude);
98+
float longitude = float(m_gpsdData.fix.longitude);
99+
100+
m_network->writeHomePosition(latitude, longitude);
101+
}
102+
103+
#endif
104+

MobileGPS.h GPSD.h

+17-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2018 by Jonathan Naylor G4KLX
2+
* Copyright (C) 2018,2020 by Jonathan Naylor G4KLX
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -16,32 +16,22 @@
1616
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1717
*/
1818

19-
#ifndef MobileGPS_H
20-
#define MobileGPS_H
19+
#ifndef GPSD_H
20+
#define GPSD_H
21+
22+
#if defined(USE_GPS)
2123

2224
#include "DMRNetwork.h"
23-
#include "UDPSocket.h"
2425
#include "Timer.h"
2526

2627
#include <string>
2728

28-
#if !defined(_WIN32) && !defined(_WIN64)
29-
#include <netdb.h>
30-
#include <sys/time.h>
31-
#include <sys/types.h>
32-
#include <sys/socket.h>
33-
#include <unistd.h>
34-
#include <netinet/in.h>
35-
#include <arpa/inet.h>
36-
#include <errno.h>
37-
#else
38-
#include <winsock.h>
39-
#endif
29+
#include <gps.h>
4030

41-
class CMobileGPS {
31+
class CGPSD {
4232
public:
43-
CMobileGPS(const std::string& address, unsigned int port, CDMRNetwork* network);
44-
~CMobileGPS();
33+
CGPSD(const std::string& address, const std::string& port, CDMRNetwork* network);
34+
~CGPSD();
4535

4636
bool open();
4737

@@ -50,14 +40,16 @@ class CMobileGPS {
5040
void close();
5141

5242
private:
53-
CTimer m_idTimer;
54-
in_addr m_address;
55-
unsigned int m_port;
56-
CUDPSocket m_socket;
57-
CDMRNetwork* m_network;
43+
std::string m_gpsdAddress;
44+
std::string m_gpsdPort;
45+
CDMRNetwork* m_network;
46+
struct gps_data_t m_gpsdData;
47+
CTimer m_idTimer;
5848

59-
bool pollGPS();
6049
void sendReport();
6150
};
6251

6352
#endif
53+
54+
#endif
55+

MMDVM.ini

+2-2
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,10 @@ UTC=0
288288
Enable=0
289289
File=/tmp/MMDVM_Active.lck
290290

291-
[Mobile GPS]
291+
[GPSD]
292292
Enable=0
293293
Address=127.0.0.1
294-
Port=7834
294+
Port=2947
295295

296296
[Remote Control]
297297
Enable=0

MMDVMHost.cpp

+25-17
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ m_id(0U),
161161
m_cwCallsign(),
162162
m_lockFileEnabled(false),
163163
m_lockFileName(),
164-
m_mobileGPS(NULL),
164+
#if defined(USE_GPS)
165+
m_gpsd(NULL),
166+
#endif
165167
m_remoteControl(NULL),
166168
m_fixedMode(false)
167169
{
@@ -1004,8 +1006,10 @@ int CMMDVMHost::run()
10041006
if (m_pocsagNetwork != NULL)
10051007
m_pocsagNetwork->clock(ms);
10061008

1007-
if (m_mobileGPS != NULL)
1008-
m_mobileGPS->clock(ms);
1009+
#if defined(USE_GPS)
1010+
if (m_gpsd != NULL)
1011+
m_gpsd->clock(ms);
1012+
#endif
10091013

10101014
m_cwIdTimer.clock(ms);
10111015
if (m_cwIdTimer.isRunning() && m_cwIdTimer.hasExpired()) {
@@ -1082,10 +1086,12 @@ int CMMDVMHost::run()
10821086
m_display->close();
10831087
delete m_display;
10841088

1085-
if (m_mobileGPS != NULL) {
1086-
m_mobileGPS->close();
1087-
delete m_mobileGPS;
1089+
#if defined(USE_GPS)
1090+
if (m_gpsd != NULL) {
1091+
m_gpsd->close();
1092+
delete m_gpsd;
10881093
}
1094+
#endif
10891095

10901096
if (m_ump != NULL) {
10911097
m_ump->close();
@@ -1392,23 +1398,25 @@ bool CMMDVMHost::createDMRNetwork()
13921398
return false;
13931399
}
13941400

1395-
bool mobileGPSEnabled = m_conf.getMobileGPSEnabled();
1396-
if (mobileGPSEnabled) {
1397-
std::string mobileGPSAddress = m_conf.getMobileGPSAddress();
1398-
unsigned int mobileGPSPort = m_conf.getMobileGPSPort();
1401+
#if defined(USE_GPS)
1402+
bool gpsdEnabled = m_conf.getGPSDEnabled();
1403+
if (gpsdEnabled) {
1404+
std::string gpsdAddress = m_conf.getGPSDAddress();
1405+
std::string gpsdPort = m_conf.getGPSDPort();
13991406

1400-
LogInfo("Mobile GPS Parameters");
1401-
LogInfo(" Address: %s", mobileGPSAddress.c_str());
1402-
LogInfo(" Port: %u", mobileGPSPort);
1407+
LogInfo("GPSD Parameters");
1408+
LogInfo(" Address: %s", gpsdAddress.c_str());
1409+
LogInfo(" Port: %s", gpsdPort.c_str());
14031410

1404-
m_mobileGPS = new CMobileGPS(address, port, m_dmrNetwork);
1411+
m_gpsd = new CGPSD(gpsdAddress, gpsdPort, m_dmrNetwork);
14051412

1406-
ret = m_mobileGPS->open();
1413+
ret = m_gpsd->open();
14071414
if (!ret) {
1408-
delete m_mobileGPS;
1409-
m_mobileGPS = NULL;
1415+
delete m_gpsd;
1416+
m_gpsd = NULL;
14101417
}
14111418
}
1419+
#endif
14121420

14131421
m_dmrNetwork->enable(true);
14141422

MMDVMHost.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
#include "P25Network.h"
3535
#include "DMRNetwork.h"
3636
#include "DMRLookup.h"
37-
#include "MobileGPS.h"
3837
#include "Display.h"
3938
#include "Timer.h"
4039
#include "Modem.h"
4140
#include "Conf.h"
41+
#include "GPSD.h"
4242
#include "UMP.h"
4343

4444
#include <string>
@@ -102,7 +102,9 @@ class CMMDVMHost
102102
std::string m_cwCallsign;
103103
bool m_lockFileEnabled;
104104
std::string m_lockFileName;
105-
CMobileGPS* m_mobileGPS;
105+
#if defined(USE_GPS)
106+
CGPSD* m_gpsd;
107+
#endif
106108
CRemoteControl* m_remoteControl;
107109
bool m_fixedMode;
108110

0 commit comments

Comments
 (0)