29#include "moc_qgsgpsdetector.cpp"
31using namespace Qt::StringLiterals;
33#if defined(QT_POSITIONING_LIB)
41#if defined( HAVE_QTSERIALPORT )
42#include <QSerialPortInfo>
53 QList< QPair<QString, QString> > devs;
56#if defined(QT_POSITIONING_LIB)
57 devs << QPair<QString, QString>( u
"internalGPS"_s, tr(
"internal GPS" ) );
61 devs << QPair<QString, QString>( u
"localhost:2947:"_s, tr(
"local gpsd" ) );
64#if defined( HAVE_QTSERIALPORT )
65 for (
const QSerialPortInfo &p : QSerialPortInfo::availablePorts() )
67 devs << QPair<QString, QString>( p.portName(), tr(
"%1: %2" ).arg( p.portName(), p.description() ) );
75 : mUseUnsafeSignals( useUnsafeSignals )
77#if defined( HAVE_QTSERIALPORT )
78 mBaudList << QSerialPort::Baud4800 << QSerialPort::Baud9600 << QSerialPort::Baud38400 << QSerialPort::Baud57600 << QSerialPort::Baud115200;
81 if ( portName.isEmpty() )
83 QgsDebugMsgLevel( u
"Attempting to autodetect GPS connection"_s, 2 );
84 mPortList = availablePorts();
88 QgsDebugMsgLevel( u
"Attempting GPS connection for %1"_s.arg( portName ), 2 );
89 mPortList << QPair<QString, QString>( portName, portName );
92 mTimeoutTimer =
new QTimer(
this );
93 mTimeoutTimer->setSingleShot(
true );
94 connect( mTimeoutTimer, &QTimer::timeout,
this, &QgsGpsDetector::connectionTimeout );
104 if ( mUseUnsafeSignals )
106 QgsDebugError( u
"QgsGpsDetector::takeConnection() incorrectly called when useUnsafeSignals option is in effect"_s );
114 mConn->disconnect(
this );
120 QgsDebugMsgLevel( u
"Detected GPS connection is being taken by caller"_s, 2 );
124 QgsDebugError( u
"Something is trying to take the GPS connection, but it doesn't exist!"_s );
128 return mConn.release();
135 QgsDebugMsgLevel( u
"Destroying existing connection to attempt next configuration combination"_s, 2 );
144 if ( mBaudIndex == mBaudList.size() )
150 if ( mPortIndex == mPortList.size() )
158 QgsDebugMsgLevel( u
"Attempting connection to device %1 @ %2"_s.arg( mPortIndex ).arg( mBaudIndex ), 2 );
160 if ( mPortList.at( mPortIndex ).first.contains(
':' ) )
162 mBaudIndex = mBaudList.size() - 1;
164 QStringList gpsParams = mPortList.at( mPortIndex ).first.split(
':' );
165 if ( gpsParams.size() < 3 )
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 ) );
173 QgsDebugMsgLevel( u
"Connecting to GPSD device %1"_s.arg( gpsParams.join(
',' ) ), 2 );
175 mConn = std::make_unique< QgsGpsdConnection >( gpsParams[0], gpsParams[1].toShort(), gpsParams[2] );
177 else if ( mPortList.at( mPortIndex ).first.contains(
"internalGPS"_L1 ) )
179#if defined(QT_POSITIONING_LIB)
181 mConn = std::make_unique< QgsQtLocationConnection >();
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" );
189#if defined(HAVE_QTSERIALPORT)
190 auto serial = std::make_unique< QSerialPort >( mPortList.at( mPortIndex ).first );
192 serial->setBaudRate( mBaudList[ mBaudIndex ] );
194 serial->setFlowControl( QgsGpsDetector::settingsGpsFlowControl->value() );
195 serial->setParity( QgsGpsDetector::settingsGpsParity->value() );
196 serial->setDataBits( QgsGpsDetector::settingsGpsDataBits->value() );
197 serial->setStopBits( QgsGpsDetector::settingsGpsStopBits->value() );
199 QgsDebugMsgLevel( u
"Connecting to serial GPS device %1 (@ %2)"_s.arg( mPortList.at( mPortIndex ).first ).arg( mBaudList[ mBaudIndex ] ), 2 );
201 if ( serial->open( QIODevice::ReadOnly ) )
203 QgsDebugMsgLevel( u
"Successfully opened, have a port connection ready"_s, 2 );
204 mConn = std::make_unique< QgsNmeaConnection >( serial.release() );
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" );
218 QgsDebugError( u
"Got to end of connection handling loop, but have no connection!"_s );
225 if ( mUseUnsafeSignals )
231 mTimeoutTimer->start( 2000 );
240 mTimeoutTimer->stop();
243 QgsDebugError( u
"Got information, but CONNECTION WAS DESTROYED EXTERNALLY!"_s );
248 mTimeoutTimer->stop();
256 if ( mUseUnsafeSignals )
273 QgsDebugMsgLevel( u
"Connection status is NOT GPSDataReceived. It is %1"_s.arg( mConn->status() ), 2 );
277void QgsGpsDetector::connectionTimeout()
288 if ( obj == mConn.get() )
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.
A template class for enum and flag settings entry.
static QgsSettingsTreeNode * sTreeGps
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)