5#include <QtPositioning/QNmeaPositionInfoSource>
6#include <QtPositioning/QNmeaSatelliteInfoSource>
7#include <QtNetwork/QTcpSocket>
8#include <QLoggingCategory>
12#include <QSharedPointer>
15#ifdef QT_NMEA_PLUGIN_HAS_SERIALPORT
16# include <QtSerialPort/QSerialPort>
17# include <QtSerialPort/QSerialPortInfo>
32#ifdef QT_NMEA_PLUGIN_HAS_SERIALPORT
43class IODeviceContainer
46 IODeviceContainer() {}
47 IODeviceContainer(IODeviceContainer
const&) =
delete;
48 void operator=(IODeviceContainer
const&) =
delete;
50 QSharedPointer<QIOPipe> serial(
const QString &portName,
qint32 baudRate)
52 if (m_serialPorts.contains(portName)) {
53 m_serialPorts[portName].refs++;
55 m_serialPorts[portName].proxy->addChildPipe(endPipe);
56 return QSharedPointer<QIOPipe>(endPipe);
59 QSerialPort *
port =
new QSerialPort(portName);
60 port->setBaudRate(baudRate);
61 qCDebug(lcNmea) <<
"Opening serial port" << portName <<
"with baudrate" << baudRate;
67 qCDebug(lcNmea) <<
"Opened successfully";
71 m_serialPorts[portName] =
device;
73 device.proxy->addChildPipe(endPipe);
74 return QSharedPointer<QIOPipe>(endPipe);
77 void releaseSerial(
const QString &portName, QSharedPointer<QIOPipe> &pipe)
79 if (!m_serialPorts.contains(portName))
83 IODevice &
device = m_serialPorts[portName];
89 IODevice taken = m_serialPorts.take(portName);
90 taken.device->deleteLater();
99 unsigned int refs = 1;
102 QMap<QString, IODevice> m_serialPorts;
153 void addSerialDevice(
const QString &requestedPort,
quint32 baudRate);
157 QSharedPointer<QIOPipe> m_dataSource;
158 QScopedPointer<QFile> m_fileSource;
159 QScopedPointer<QTcpSocket> m_socket;
177#ifdef QT_NMEA_PLUGIN_HAS_SERIALPORT
178 if (deviceContainer.exists())
179 deviceContainer->releaseSerial(m_sourceName, m_dataSource);
198 qWarning() <<
"Connection failed! QAbstractSocket::SocketError" <<
error;
205void NmeaSource::processParameters(
const NmeaParameters ¶meters)
209 connectSocket(parameters.
source);
217#ifdef QT_NMEA_PLUGIN_HAS_SERIALPORT
221 if (requestedPort.isEmpty()) {
222 const QList<QSerialPortInfo>
ports = QSerialPortInfo::availablePorts();
223 qCDebug(lcNmea) <<
"Found" <<
ports.size() <<
"serial ports";
224 if (
ports.isEmpty()) {
225 qWarning(
"nmea: No serial ports found");
230 QSet<int> supportedDevices;
231 supportedDevices << 0x67b;
232 supportedDevices << 0xe8d;
233 for (
const QSerialPortInfo&
port :
ports) {
234 if (
port.hasVendorIdentifier() && supportedDevices.contains(
port.vendorIdentifier())) {
235 portName =
port.portName();
240 if (portName.isEmpty()) {
241 qWarning(
"nmea: No known GPS device found.");
244 portName = requestedPort;
246 portName.remove(0, 7);
252void NmeaSource::addSerialDevice(
const QString &requestedPort,
quint32 baudRate)
254#ifdef QT_NMEA_PLUGIN_HAS_SERIALPORT
255 m_sourceName = tryFindSerialDevice(requestedPort);
259 m_dataSource = deviceContainer->serial(m_sourceName, baudRate);
268 qWarning() <<
"Plugin was built without serialport support!"
269 << requestedPort <<
"cannot be used!";
281 m_fileSource.
reset();
287 qCDebug(lcNmea) <<
"Opened successfully";
325 void parseSimulationSource(
const QString &localFileName);
327 QSharedPointer<QIOPipe> m_port;
328 QScopedPointer<QFile> m_file;
329 QScopedPointer<QTcpSocket> m_socket;
357#ifdef QT_NMEA_PLUGIN_HAS_SERIALPORT
358 if (deviceContainer.exists())
359 deviceContainer->releaseSerial(m_sourceName, m_port);
378 qWarning() <<
"Connection failed! QAbstractSocket::SocketError" <<
error;
385void NmeaSatelliteSource::processRealtimeParameters(
const NmeaParameters ¶meters)
397 this, &NmeaSatelliteSource::onSocketError);
406#ifdef QT_NMEA_PLUGIN_HAS_SERIALPORT
408 m_sourceName = tryFindSerialDevice(
source);
412 m_port = deviceContainer->serial(m_sourceName, parameters.
baudRate);
420 qWarning() <<
"Plugin was built without serialport support!"
421 <<
source <<
"cannot be used!";
426void NmeaSatelliteSource::parseSimulationSource(
const QString &localFileName)
429 m_sourceName = localFileName;
431 qCDebug(lcNmea) <<
"Opening file" << localFileName;
438 qCDebug(lcNmea) <<
"Opened successfully";
458 localFileName.remove(0, 7);
460 localFileName.remove(0, 7);
462 localFileName.remove(0, 5);
465 localFileName.remove(0, 1);
471 return isLocalFile ? localFileName :
QString();
486 std::unique_ptr<NmeaSource>
src =
nullptr;
489 if (localFileName.isEmpty())
490 src = std::make_unique<NmeaSource>(
parent, parameters);
492 src = std::make_unique<NmeaSource>(
parent, localFileName);
494 return (
src &&
src->isValid()) ?
src.release() :
nullptr;
499 std::unique_ptr<NmeaSatelliteSource>
src =
nullptr;
502 if (localFileName.isEmpty()) {
504 src = std::make_unique<NmeaSatelliteSource>(
parent, parameters);
507 src = std::make_unique<NmeaSatelliteSource>(
parent, localFileName, parameters);
509 return (
src &&
src->isValid()) ?
src.release() :
nullptr;
521#include "moc_qgeopositioninfosourcefactory_nmea.cpp"
522#include "qgeopositioninfosourcefactory_nmea.moc"
IOBluetoothDevice * device
NmeaSatelliteSource(QObject *parent, const QVariantMap ¶meters)
NmeaSource(QObject *parent, const QVariantMap ¶meters)
void errorOccurred(QAbstractSocket::SocketError)
void close() override
Closes the I/O device for the socket and calls disconnectFromHost() to close the socket's connection.
SocketError
This enum describes the socket errors that can occur.
virtual void connectToHost(const QString &hostName, quint16 port, OpenMode mode=ReadWrite, NetworkLayerProtocol protocol=AnyIPProtocol)
Attempts to make a connection to hostName on the given port.
QFILE_MAYBE_NODISCARD bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
bool exists() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
QGeoAreaMonitorSource * areaMonitor(QObject *parent, const QVariantMap ¶meters) override
Returns a new QGeoAreaMonitorSource associated with this plugin with parent parent,...
Q_PLUGIN_METADATA(IID "org.qt-project.qt.position.sourcefactory/6.0" FILE "plugin.json") public QGeoSatelliteInfoSource * satelliteInfoSource(QObject *parent, const QVariantMap ¶meters) override
Returns a new QGeoSatelliteInfoSource associated with this plugin with parent parent,...
virtual QGeoPositionInfoSource * positionInfoSource(QObject *parent, const QVariantMap ¶meters)=0
Returns a new QGeoPositionInfoSource associated with this plugin with parent parent,...
\inmodule QtCore \reentrant
T value(const Key &key, const T &defaultValue=T()) const
Error error() const override
\reimp
void setError(QGeoPositionInfoSource::Error positionError)
void setDevice(QIODevice *source)
Sets the NMEA data source to device.
UpdateMode
Defines the available update modes.
bool setBackendProperty(const QString &name, const QVariant &value) override
\reimp
static QString SimulationUpdateInterval
\variable QNmeaSatelliteInfoSource::SimulationUpdateInterval
void setDevice(QIODevice *source)
Sets the NMEA data source to device.
Error error() const override
\reimp
void setError(QGeoSatelliteInfoSource::Error satelliteError)
QObject * parent() const
Returns a pointer to the parent object.
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
T * data() const noexcept
Returns the value of the pointer referenced by this object.
bool isNull() const noexcept
Returns true if this object refers to \nullptr.
void reset(T *other=nullptr) noexcept(noexcept(Cleanup::cleanup(std::declval< T * >())))
Deletes the existing object it is pointing to (if any), and sets its pointer to other.
bool isNull() const noexcept
Returns true if this object refers to \nullptr.
T * data() const noexcept
Returns the value of the pointer referenced by this object.
\macro QT_RESTRICTED_CAST_FROM_ASCII
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
The QTcpSocket class provides a TCP socket.
QString host(ComponentFormattingOptions=FullyDecoded) const
Returns the host of the URL if it is defined; otherwise an empty string is returned.
int port(int defaultPort=-1) const
int toInt(bool *ok=nullptr) const
Returns the variant as an int if the variant has userType() \l QMetaType::Int, \l QMetaType::Bool,...
QString toString() const
Returns the variant as a QString if the variant has a userType() including, but not limited to:
Combined button and popup list for selecting options.
DBusConnection const char DBusError * error
const EGLAttrib EGLOutputPortEXT * ports
static QT_BEGIN_NAMESPACE const auto sourceParameterName
static QString checkSourceIsFile(const QString &source)
static const auto socketScheme
static QString extractLocalFileName(const QVariantMap ¶meters)
static constexpr auto defaultBaudRate
static const auto baudRateParameterName
static const auto serialScheme
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
#define Q_LOGGING_CATEGORY(name,...)
#define qCDebug(category,...)
GLsizei GLsizei GLchar * source
#define qPrintable(string)
#define QStringLiteral(str)
QUrl url("example.com")
[constructor-url-reference]
NmeaParameters(const QVariantMap ¶meters)
\inmodule QtCore \reentrant