27#include "moc_qgsgpsdetector.cpp"
29#if defined(QT_POSITIONING_LIB)
37#if defined( HAVE_QTSERIALPORT )
38#include <QSerialPortInfo>
49 QList< QPair<QString, QString> > devs;
52#if defined(QT_POSITIONING_LIB)
53 devs << QPair<QString, QString>( QStringLiteral(
"internalGPS" ), tr(
"internal GPS" ) );
57 devs << QPair<QString, QString>( QStringLiteral(
"localhost:2947:" ), tr(
"local gpsd" ) );
60#if defined( HAVE_QTSERIALPORT )
61 for (
const QSerialPortInfo &p : QSerialPortInfo::availablePorts() )
63 devs << QPair<QString, QString>( p.portName(), tr(
"%1: %2" ).arg( p.portName(), p.description() ) );
71 : mUseUnsafeSignals( useUnsafeSignals )
73#if defined( HAVE_QTSERIALPORT )
74 mBaudList << QSerialPort::Baud4800 << QSerialPort::Baud9600 << QSerialPort::Baud38400 << QSerialPort::Baud57600 << QSerialPort::Baud115200;
77 if ( portName.isEmpty() )
79 QgsDebugMsgLevel( QStringLiteral(
"Attempting to autodetect GPS connection" ), 2 );
80 mPortList = availablePorts();
84 QgsDebugMsgLevel( QStringLiteral(
"Attempting GPS connection for %1" ).arg( portName ), 2 );
85 mPortList << QPair<QString, QString>( portName, portName );
88 mTimeoutTimer =
new QTimer(
this );
89 mTimeoutTimer->setSingleShot(
true );
90 connect( mTimeoutTimer, &QTimer::timeout,
this, &QgsGpsDetector::connectionTimeout );
100 if ( mUseUnsafeSignals )
102 QgsDebugError( QStringLiteral(
"QgsGpsDetector::takeConnection() incorrectly called when useUnsafeSignals option is in effect" ) );
110 mConn->disconnect(
this );
116 QgsDebugMsgLevel( QStringLiteral(
"Detected GPS connection is being taken by caller" ), 2 );
120 QgsDebugError( QStringLiteral(
"Something is trying to take the GPS connection, but it doesn't exist!" ) );
124 return mConn.release();
131 QgsDebugMsgLevel( QStringLiteral(
"Destroying existing connection to attempt next configuration combination" ), 2 );
140 if ( mBaudIndex == mBaudList.size() )
146 if ( mPortIndex == mPortList.size() )
148 QgsDebugError( QStringLiteral(
"No more devices to try!" ) );
154 QgsDebugMsgLevel( QStringLiteral(
"Attempting connection to device %1 @ %2" ).arg( mPortIndex ).arg( mBaudIndex ), 2 );
156 if ( mPortList.at( mPortIndex ).first.contains(
':' ) )
158 mBaudIndex = mBaudList.size() - 1;
160 QStringList gpsParams = mPortList.at( mPortIndex ).first.split(
':' );
161 if ( gpsParams.size() < 3 )
163 QgsDebugError( QStringLiteral(
"If the port name contains a colon, then it should have more than one colon (e.g., host:port:device). Port name: %1" ).arg( mPortList.at( mPortIndex ).first ) );
169 QgsDebugMsgLevel( QStringLiteral(
"Connecting to GPSD device %1" ).arg( gpsParams.join(
',' ) ), 2 );
171 mConn = std::make_unique< QgsGpsdConnection >( gpsParams[0], gpsParams[1].toShort(), gpsParams[2] );
173 else if ( mPortList.at( mPortIndex ).first.contains( QLatin1String(
"internalGPS" ) ) )
175#if defined(QT_POSITIONING_LIB)
176 QgsDebugMsgLevel( QStringLiteral(
"Connecting to QtLocation service device" ), 2 );
177 mConn = std::make_unique< QgsQtLocationConnection >();
179 QgsDebugError( QStringLiteral(
"QT_POSITIONING_LIB not found and mPortList matches internalGPS, this should never happen" ) );
180 qWarning(
"QT_POSITIONING_LIB not found and mPortList matches internalGPS, this should never happen" );
185#if defined( HAVE_QTSERIALPORT )
186 auto serial = std::make_unique< QSerialPort >( mPortList.at( mPortIndex ).first );
188 serial->setBaudRate( mBaudList[ mBaudIndex ] );
190 serial->setFlowControl( QgsGpsDetector::settingsGpsFlowControl->value() );
191 serial->setParity( QgsGpsDetector::settingsGpsParity->value() );
192 serial->setDataBits( QgsGpsDetector::settingsGpsDataBits->value() );
193 serial->setStopBits( QgsGpsDetector::settingsGpsStopBits->value() );
195 QgsDebugMsgLevel( QStringLiteral(
"Connecting to serial GPS device %1 (@ %2)" ).arg( mPortList.at( mPortIndex ).first ).arg( mBaudList[ mBaudIndex ] ), 2 );
197 if ( serial->open( QIODevice::ReadOnly ) )
199 QgsDebugMsgLevel( QStringLiteral(
"Successfully opened, have a port connection ready" ), 2 );
200 mConn = std::make_unique< QgsNmeaConnection >( serial.release() );
204 QgsDebugError( QStringLiteral(
"Serial port could NOT be opened" ) );
207 QgsDebugError( QStringLiteral(
"QT5SERIALPORT not found and mPortList matches serial port, this should never happen" ) );
208 qWarning(
"QT5SERIALPORT not found and mPortList matches serial port, this should never happen" );
214 QgsDebugError( QStringLiteral(
"Got to end of connection handling loop, but have no connection!" ) );
218 QgsDebugMsgLevel( QStringLiteral(
"Have a connection, now listening for messages" ), 2 );
221 if ( mUseUnsafeSignals )
227 mTimeoutTimer->start( 2000 );
236 mTimeoutTimer->stop();
239 QgsDebugError( QStringLiteral(
"Got information, but CONNECTION WAS DESTROYED EXTERNALLY!" ) );
244 mTimeoutTimer->stop();
250 QgsDebugMsgLevel( QStringLiteral(
"Connection status IS GPSDataReceived" ), 2 );
252 if ( mUseUnsafeSignals )
269 QgsDebugMsgLevel( QStringLiteral(
"Connection status is NOT GPSDataReceived. It is %1" ).arg( mConn->status() ), 2 );
273void QgsGpsDetector::connectionTimeout()
275 QgsDebugMsgLevel( QStringLiteral(
"No data received within max listening time" ), 2 );
281 QgsDebugError( QStringLiteral(
"CONNECTION WAS DESTROYED EXTERNALLY!" ) );
284 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)