QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
qgsgpsdetector.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgpsdetector.cpp - description
3 --------------------
4 begin : January 13th, 2009
5 copyright : (C) 2009 by Juergen E. Fischer
6 email : jef at norbit dot de
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsgpsdetector.h"
19
20#include "qgsgpsconnection.h"
21#include "qgsgpsdconnection.h"
22#include "qgslogger.h"
23#include "qgsnmeaconnection.h"
25#include "qgssettingstree.h"
26
27#include <QString>
28
29#include "moc_qgsgpsdetector.cpp"
30
31using namespace Qt::StringLiterals;
32
33#if defined(QT_POSITIONING_LIB)
35#endif
36
37#include <QStringList>
38#include <QFileInfo>
39#include <QTimer>
40
41#if defined( HAVE_QTSERIALPORT )
42#include <QSerialPortInfo>
43#include <QSerialPort>
44
45const QgsSettingsEntryEnumFlag<QSerialPort::FlowControl> *QgsGpsDetector::settingsGpsFlowControl = new QgsSettingsEntryEnumFlag<QSerialPort::FlowControl>( u"flow-control"_s, QgsSettingsTree::sTreeGps, QSerialPort::NoFlowControl );
46const QgsSettingsEntryEnumFlag<QSerialPort::StopBits> *QgsGpsDetector::settingsGpsStopBits = new QgsSettingsEntryEnumFlag<QSerialPort::StopBits>( u"stop-bits"_s, QgsSettingsTree::sTreeGps, QSerialPort::OneStop );
47const QgsSettingsEntryEnumFlag<QSerialPort::DataBits> *QgsGpsDetector::settingsGpsDataBits = new QgsSettingsEntryEnumFlag<QSerialPort::DataBits>( u"data-bits"_s, QgsSettingsTree::sTreeGps, QSerialPort::Data8 );
48const QgsSettingsEntryEnumFlag<QSerialPort::Parity> *QgsGpsDetector::settingsGpsParity = new QgsSettingsEntryEnumFlag<QSerialPort::Parity>( u"parity"_s, QgsSettingsTree::sTreeGps, QSerialPort::NoParity );
49#endif
50
51QList< QPair<QString, QString> > QgsGpsDetector::availablePorts()
52{
53 QList< QPair<QString, QString> > devs;
54
55 // try local QtLocation first
56#if defined(QT_POSITIONING_LIB)
57 devs << QPair<QString, QString>( u"internalGPS"_s, tr( "internal GPS" ) );
58#endif
59
60 // try local gpsd first
61 devs << QPair<QString, QString>( u"localhost:2947:"_s, tr( "local gpsd" ) );
62
63 // try serial ports
64#if defined( HAVE_QTSERIALPORT )
65 for ( const QSerialPortInfo &p : QSerialPortInfo::availablePorts() )
66 {
67 devs << QPair<QString, QString>( p.portName(), tr( "%1: %2" ).arg( p.portName(), p.description() ) );
68 }
69#endif
70
71 return devs;
72}
73
74QgsGpsDetector::QgsGpsDetector( const QString &portName, bool useUnsafeSignals )
75 : mUseUnsafeSignals( useUnsafeSignals )
76{
77#if defined( HAVE_QTSERIALPORT )
78 mBaudList << QSerialPort::Baud4800 << QSerialPort::Baud9600 << QSerialPort::Baud38400 << QSerialPort::Baud57600 << QSerialPort::Baud115200; //add 57600 for SXBlueII GPS unit
79#endif
80
81 if ( portName.isEmpty() )
82 {
83 QgsDebugMsgLevel( u"Attempting to autodetect GPS connection"_s, 2 );
84 mPortList = availablePorts();
85 }
86 else
87 {
88 QgsDebugMsgLevel( u"Attempting GPS connection for %1"_s.arg( portName ), 2 );
89 mPortList << QPair<QString, QString>( portName, portName );
90 }
91
92 mTimeoutTimer = new QTimer( this );
93 mTimeoutTimer->setSingleShot( true );
94 connect( mTimeoutTimer, &QTimer::timeout, this, &QgsGpsDetector::connectionTimeout );
95}
96
98{
99 QgsDebugMsgLevel( u"Destroying GPS detector"_s, 2 );
100}
101
103{
104 if ( mUseUnsafeSignals )
105 {
106 QgsDebugError( u"QgsGpsDetector::takeConnection() incorrectly called when useUnsafeSignals option is in effect"_s );
107 return nullptr;
108 }
109
110 if ( mConn )
111 {
112 // this is NOT the detectors connection anymore, so disconnect all signals from the connection
113 // to the detector so that there's no unwanted interaction with the detector
114 mConn->disconnect( this );
115 }
116
117#ifdef QGISDEBUG
118 if ( mConn )
119 {
120 QgsDebugMsgLevel( u"Detected GPS connection is being taken by caller"_s, 2 );
121 }
122 else
123 {
124 QgsDebugError( u"Something is trying to take the GPS connection, but it doesn't exist!"_s );
125 }
126#endif
127
128 return mConn.release();
129}
130
132{
133 if ( mConn )
134 {
135 QgsDebugMsgLevel( u"Destroying existing connection to attempt next configuration combination"_s, 2 );
136 mConn.reset();
137 }
138
139 QgsDebugMsgLevel( u"Trying to find a connection..."_s, 2 );
140
141 while ( !mConn )
142 {
143 mBaudIndex++;
144 if ( mBaudIndex == mBaudList.size() )
145 {
146 mBaudIndex = 0;
147 mPortIndex++;
148 }
149
150 if ( mPortIndex == mPortList.size() )
151 {
152 QgsDebugError( u"No more devices to try!"_s );
153 emit detectionFailed();
154 deleteLater();
155 return;
156 }
157
158 QgsDebugMsgLevel( u"Attempting connection to device %1 @ %2"_s.arg( mPortIndex ).arg( mBaudIndex ), 2 );
159
160 if ( mPortList.at( mPortIndex ).first.contains( ':' ) )
161 {
162 mBaudIndex = mBaudList.size() - 1;
163
164 QStringList gpsParams = mPortList.at( mPortIndex ).first.split( ':' );
165 if ( gpsParams.size() < 3 )
166 {
167 QgsDebugError( u"If the port name contains a colon, then it should have more than one colon (e.g., host:port:device). Port name: %1"_s.arg( mPortList.at( mPortIndex ).first ) );
168 emit detectionFailed();
169 deleteLater();
170 return;
171 }
172
173 QgsDebugMsgLevel( u"Connecting to GPSD device %1"_s.arg( gpsParams.join( ',' ) ), 2 );
174
175 mConn = std::make_unique< QgsGpsdConnection >( gpsParams[0], gpsParams[1].toShort(), gpsParams[2] );
176 }
177 else if ( mPortList.at( mPortIndex ).first.contains( "internalGPS"_L1 ) )
178 {
179#if defined(QT_POSITIONING_LIB)
180 QgsDebugMsgLevel( u"Connecting to QtLocation service device"_s, 2 );
181 mConn = std::make_unique< QgsQtLocationConnection >();
182#else
183 QgsDebugError( u"QT_POSITIONING_LIB not found and mPortList matches internalGPS, this should never happen"_s );
184 qWarning( "QT_POSITIONING_LIB not found and mPortList matches internalGPS, this should never happen" );
185#endif
186 }
187 else
188 {
189#if defined(HAVE_QTSERIALPORT)
190 auto serial = std::make_unique< QSerialPort >( mPortList.at( mPortIndex ).first );
191
192 serial->setBaudRate( mBaudList[ mBaudIndex ] );
193
194 serial->setFlowControl( QgsGpsDetector::settingsGpsFlowControl->value() );
195 serial->setParity( QgsGpsDetector::settingsGpsParity->value() );
196 serial->setDataBits( QgsGpsDetector::settingsGpsDataBits->value() );
197 serial->setStopBits( QgsGpsDetector::settingsGpsStopBits->value() );
198
199 QgsDebugMsgLevel( u"Connecting to serial GPS device %1 (@ %2)"_s.arg( mPortList.at( mPortIndex ).first ).arg( mBaudList[ mBaudIndex ] ), 2 );
200
201 if ( serial->open( QIODevice::ReadOnly ) )
202 {
203 QgsDebugMsgLevel( u"Successfully opened, have a port connection ready"_s, 2 );
204 mConn = std::make_unique< QgsNmeaConnection >( serial.release() );
205 }
206 else
207 {
208 QgsDebugError( u"Serial port could NOT be opened"_s );
209 }
210#else
211 QgsDebugError( u"QTSERIALPORT not found and mPortList matches serial port, this should never happen"_s );
212 qWarning( "QTSERIALPORT not found and mPortList matches serial port, this should never happen" );
213#endif
214 }
215
216 if ( !mConn )
217 {
218 QgsDebugError( u"Got to end of connection handling loop, but have no connection!"_s );
219 }
220 }
221
222 QgsDebugMsgLevel( u"Have a connection, now listening for messages"_s, 2 );
223
224 connect( mConn.get(), &QgsGpsConnection::stateChanged, this, qOverload< const QgsGpsInformation & >( &QgsGpsDetector::detected ) );
225 if ( mUseUnsafeSignals )
226 {
227 connect( mConn.get(), &QObject::destroyed, this, &QgsGpsDetector::connDestroyed );
228 }
229
230 // leave 2s to pickup a valid string
231 mTimeoutTimer->start( 2000 );
232}
233
235{
236 QgsDebugMsgLevel( u"Detected information"_s, 2 );
237
238 if ( !mConn )
239 {
240 mTimeoutTimer->stop();
241
242 // advance if connection was destroyed
243 QgsDebugError( u"Got information, but CONNECTION WAS DESTROYED EXTERNALLY!"_s );
244 advance();
245 }
246 else if ( mConn->status() == QgsGpsConnection::GPSDataReceived )
247 {
248 mTimeoutTimer->stop();
249 // stop listening for state changed signals, we've already validated this connection and don't want subsequent calls
250 // to QgsGpsDetector::detected being made
251 disconnect( mConn.get(), &QgsGpsConnection::stateChanged, this, qOverload< const QgsGpsInformation & >( &QgsGpsDetector::detected ) );
252
253 // signal detected
254 QgsDebugMsgLevel( u"Connection status IS GPSDataReceived"_s, 2 );
255
256 if ( mUseUnsafeSignals )
257 {
258 // let's hope there's a single, unique connection to this signal... otherwise... boom!
260 emit detected( mConn.release() );
262 }
263 else
264 {
265 emit connectionDetected();
266 }
267
268 deleteLater();
269 }
270 else
271 {
272 // don't stop timeout, we keep waiting to see if later we get the desired connection status...
273 QgsDebugMsgLevel( u"Connection status is NOT GPSDataReceived. It is %1"_s.arg( mConn->status() ), 2 );
274 }
275}
276
277void QgsGpsDetector::connectionTimeout()
278{
279 QgsDebugMsgLevel( u"No data received within max listening time"_s, 2 );
280 advance();
281}
282
284{
285 QgsDebugError( u"CONNECTION WAS DESTROYED EXTERNALLY!"_s );
286
287 // WTF? This whole class needs re-writing...
288 if ( obj == mConn.get() )
289 {
290 mConn.release(); // cppcheck-suppress ignoredReturnValue
291 }
292}
Abstract base class for connections to a GPS device.
void stateChanged(const QgsGpsInformation &info)
Emitted whenever the GPS state is changed.
~QgsGpsDetector() override
void connDestroyed(QObject *)
void detected(const QgsGpsInformation &)
static QList< QPair< QString, QString > > availablePorts()
QgsGpsConnection * takeConnection()
Returns the detected GPS connection, and removes it from the detector.
void connectionDetected()
Emitted when a GPS connection is successfully detected.
QgsGpsDetector(const QString &portName=QString(), bool useUnsafeSignals=true)
Constructor for QgsGpsDetector.
void detectionFailed()
Emitted when the detector could not find a valid GPS connection.
Encapsulates information relating to a GPS position fix.
A template class for enum and flag settings entry.
static QgsSettingsTreeNode * sTreeGps
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:7475
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7474
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63
#define QgsDebugError(str)
Definition qgslogger.h:59