19#include "moc_qgsgpsdetector.cpp"
27#if defined(QT_POSITIONING_LIB)
35#if defined( HAVE_QTSERIALPORT )
36#include <QSerialPortInfo>
47 QList< QPair<QString, QString> > devs;
50#if defined(QT_POSITIONING_LIB)
51 devs << QPair<QString, QString>( QStringLiteral(
"internalGPS" ), tr(
"internal GPS" ) );
55 devs << QPair<QString, QString>( QStringLiteral(
"localhost:2947:" ), tr(
"local gpsd" ) );
58#if defined( HAVE_QTSERIALPORT )
59 for (
const QSerialPortInfo &p : QSerialPortInfo::availablePorts() )
61 devs << QPair<QString, QString>( p.portName(), tr(
"%1: %2" ).arg( p.portName(), p.description() ) );
69 : mUseUnsafeSignals( useUnsafeSignals )
71#if defined( HAVE_QTSERIALPORT )
72 mBaudList << QSerialPort::Baud4800 << QSerialPort::Baud9600 << QSerialPort::Baud38400 << QSerialPort::Baud57600 << QSerialPort::Baud115200;
75 if ( portName.isEmpty() )
77 QgsDebugMsgLevel( QStringLiteral(
"Attempting to autodetect GPS connection" ), 2 );
82 QgsDebugMsgLevel( QStringLiteral(
"Attempting GPS connection for %1" ).arg( portName ), 2 );
83 mPortList << QPair<QString, QString>( portName, portName );
86 mTimeoutTimer =
new QTimer(
this );
87 mTimeoutTimer->setSingleShot(
true );
88 connect( mTimeoutTimer, &QTimer::timeout,
this, &QgsGpsDetector::connectionTimeout );
98 if ( mUseUnsafeSignals )
100 QgsDebugError( QStringLiteral(
"QgsGpsDetector::takeConnection() incorrectly called when useUnsafeSignals option is in effect" ) );
108 mConn->disconnect(
this );
114 QgsDebugMsgLevel( QStringLiteral(
"Detected GPS connection is being taken by caller" ), 2 );
118 QgsDebugError( QStringLiteral(
"Something is trying to take the GPS connection, but it doesn't exist!" ) );
122 return mConn.release();
129 QgsDebugMsgLevel( QStringLiteral(
"Destroying existing connection to attempt next configuration combination" ), 2 );
138 if ( mBaudIndex == mBaudList.size() )
144 if ( mPortIndex == mPortList.size() )
146 QgsDebugError( QStringLiteral(
"No more devices to try!" ) );
152 QgsDebugMsgLevel( QStringLiteral(
"Attempting connection to device %1 @ %2" ).arg( mPortIndex ).arg( mBaudIndex ), 2 );
154 if ( mPortList.at( mPortIndex ).first.contains(
':' ) )
156 mBaudIndex = mBaudList.size() - 1;
158 QStringList gpsParams = mPortList.at( mPortIndex ).first.split(
':' );
160 Q_ASSERT( gpsParams.size() >= 3 );
161 QgsDebugMsgLevel( QStringLiteral(
"Connecting to GPSD device %1" ).arg( gpsParams.join(
',' ) ), 2 );
163 mConn = std::make_unique< QgsGpsdConnection >( gpsParams[0], gpsParams[1].toShort(), gpsParams[2] );
165 else if ( mPortList.at( mPortIndex ).first.contains( QLatin1String(
"internalGPS" ) ) )
167#if defined(QT_POSITIONING_LIB)
168 QgsDebugMsgLevel( QStringLiteral(
"Connecting to QtLocation service device" ), 2 );
169 mConn = std::make_unique< QgsQtLocationConnection >();
171 QgsDebugError( QStringLiteral(
"QT_POSITIONING_LIB not found and mPortList matches internalGPS, this should never happen" ) );
172 qWarning(
"QT_POSITIONING_LIB not found and mPortList matches internalGPS, this should never happen" );
177#if defined( HAVE_QTSERIALPORT )
178 std::unique_ptr< QSerialPort > serial = std::make_unique< QSerialPort >( mPortList.at( mPortIndex ).first );
180 serial->setBaudRate( mBaudList[ mBaudIndex ] );
182 serial->setFlowControl( QgsGpsDetector::settingsGpsFlowControl->value() );
183 serial->setParity( QgsGpsDetector::settingsGpsParity->value() );
184 serial->setDataBits( QgsGpsDetector::settingsGpsDataBits->value() );
185 serial->setStopBits( QgsGpsDetector::settingsGpsStopBits->value() );
187 QgsDebugMsgLevel( QStringLiteral(
"Connecting to serial GPS device %1 (@ %2)" ).arg( mPortList.at( mPortIndex ).first ).arg( mBaudList[ mBaudIndex ] ), 2 );
189 if ( serial->open( QIODevice::ReadOnly ) )
191 QgsDebugMsgLevel( QStringLiteral(
"Successfully opened, have a port connection ready" ), 2 );
192 mConn = std::make_unique< QgsNmeaConnection >( serial.release() );
196 QgsDebugError( QStringLiteral(
"Serial port could NOT be opened" ) );
199 QgsDebugError( QStringLiteral(
"QT5SERIALPORT not found and mPortList matches serial port, this should never happen" ) );
200 qWarning(
"QT5SERIALPORT not found and mPortList matches serial port, this should never happen" );
206 QgsDebugError( QStringLiteral(
"Got to end of connection handling loop, but have no connection!" ) );
210 QgsDebugMsgLevel( QStringLiteral(
"Have a connection, now listening for messages" ), 2 );
213 if ( mUseUnsafeSignals )
219 mTimeoutTimer->start( 2000 );
228 mTimeoutTimer->stop();
231 QgsDebugError( QStringLiteral(
"Got information, but CONNECTION WAS DESTROYED EXTERNALLY!" ) );
236 mTimeoutTimer->stop();
242 QgsDebugMsgLevel( QStringLiteral(
"Connection status IS GPSDataReceived" ), 2 );
244 if ( mUseUnsafeSignals )
261 QgsDebugMsgLevel( QStringLiteral(
"Connection status is NOT GPSDataReceived. It is %1" ).arg( mConn->status() ), 2 );
265void QgsGpsDetector::connectionTimeout()
267 QgsDebugMsgLevel( QStringLiteral(
"No data received within max listening time" ), 2 );
273 QgsDebugError( QStringLiteral(
"CONNECTION WAS DESTROYED EXTERNALLY!" ) );
276 if ( obj == mConn.get() )
Abstract base class for connection 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)