1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtPositioning module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qgeopositioninfosourcefactory_serialnmea.h"
41#include <QtPositioning/qnmeapositioninfosource.h>
42#include <QtSerialPort/qserialport.h>
43#include <QtSerialPort/qserialportinfo.h>
44#include <QtCore/qloggingcategory.h>
45#include <QSet>
46#include "qiopipe_p.h"
47#include <QSharedPointer>
48#include "qnmeasatelliteinfosource_p.h"
49
50Q_LOGGING_CATEGORY(lcSerial, "qt.positioning.serialnmea")
51
52class IODeviceContainer
53{
54public:
55 IODeviceContainer() {}
56 IODeviceContainer(IODeviceContainer const&) = delete;
57 void operator=(IODeviceContainer const&) = delete;
58
59 QSharedPointer<QIOPipe> serial(const QString &portName)
60 {
61 if (m_serialPorts.contains(akey: portName)) {
62 m_serialPorts[portName].refs++;
63 QIOPipe *endPipe = new QIOPipe(m_serialPorts[portName].proxy);
64 m_serialPorts[portName].proxy->addChildPipe(childPipe: endPipe);
65 return QSharedPointer<QIOPipe>(endPipe);
66 }
67 IODevice device;
68 QSerialPort *port = new QSerialPort(portName);
69 port->setBaudRate(baudRate: 4800);
70 qCDebug(lcSerial) << "Opening serial port" << portName;
71 if (!port->open(mode: QIODevice::ReadOnly)) {
72 qWarning(msg: "serialnmea: Failed to open %s", qPrintable(portName));
73 delete port;
74 return {};
75 }
76 qCDebug(lcSerial) << "Opened successfully";
77 device.device = port;
78 device.refs = 1;
79 device.proxy = new QIOPipe(port, QIOPipe::ProxyPipe);
80 m_serialPorts[portName] = device;
81 QIOPipe *endPipe = new QIOPipe(device.proxy);
82 device.proxy->addChildPipe(childPipe: endPipe);
83 return QSharedPointer<QIOPipe>(endPipe);
84 }
85
86 void releaseSerial(const QString &portName, QSharedPointer<QIOPipe> &pipe) {
87 if (!m_serialPorts.contains(akey: portName))
88 return;
89
90 pipe.clear(); // make sure to release the pipe returned by getSerial, or else, if there are still refs, data will be leaked through it
91 IODevice &device = m_serialPorts[portName];
92 if (device.refs > 1) {
93 device.refs--;
94 return;
95 }
96
97 IODevice taken = m_serialPorts.take(akey: portName);
98 taken.device->deleteLater();
99 }
100
101private:
102
103 struct IODevice {
104 QIODevice *device = nullptr;
105 QIOPipe *proxy = nullptr; // adding client pipes as children of proxy allows to dynamically add clients to one device.
106 unsigned int refs = 1;
107 };
108
109 QMap<QString, IODevice> m_serialPorts;
110};
111
112Q_GLOBAL_STATIC(IODeviceContainer, deviceContainer)
113
114
115class NmeaSource : public QNmeaPositionInfoSource
116{
117public:
118 explicit NmeaSource(QObject *parent, const QVariantMap &parameters);
119 ~NmeaSource() override;
120 bool isValid() const { return !m_port.isNull(); }
121
122private:
123 QSharedPointer<QIOPipe> m_port;
124 QString m_portName;
125};
126
127NmeaSource::NmeaSource(QObject *parent, const QVariantMap &parameters)
128 : QNmeaPositionInfoSource(RealTimeMode, parent)
129{
130 QByteArray requestedPort;
131 if (parameters.contains(QStringLiteral("serialnmea.serial_port")))
132 requestedPort = parameters.value(QStringLiteral("serialnmea.serial_port")).toString().toLatin1();
133 else
134 requestedPort = qgetenv(varName: "QT_NMEA_SERIAL_PORT");
135 QString portName;
136 if (requestedPort.isEmpty()) {
137 const QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts();
138 qCDebug(lcSerial) << "Found" << ports.count() << "serial ports";
139 if (ports.isEmpty()) {
140 qWarning(msg: "serialnmea: No serial ports found");
141 return;
142 }
143
144 // Try to find a well-known device.
145 QSet<int> supportedDevices;
146 supportedDevices << 0x67b; // GlobalSat (BU-353S4 and probably others)
147 supportedDevices << 0xe8d; // Qstarz MTK II
148 for (const QSerialPortInfo& port : ports) {
149 if (port.hasVendorIdentifier() && supportedDevices.contains(value: port.vendorIdentifier())) {
150 portName = port.portName();
151 break;
152 }
153 }
154
155 if (portName.isEmpty()) {
156 qWarning(msg: "serialnmea: No known GPS device found. Specify the COM port via QT_NMEA_SERIAL_PORT.");
157 return;
158 }
159 m_portName = portName;
160 } else {
161 m_portName = QString::fromUtf8(str: requestedPort);
162 }
163
164 m_port = deviceContainer->serial(portName: m_portName);
165 if (!m_port)
166 return;
167
168 setDevice(m_port.data());
169}
170
171NmeaSource::~NmeaSource()
172{
173 deviceContainer->releaseSerial(portName: m_portName, pipe&: m_port);
174}
175
176
177
178class NmeaSatelliteSource : public QNmeaSatelliteInfoSource
179{
180public:
181 NmeaSatelliteSource(QObject *parent, const QVariantMap &parameters)
182 : QNmeaSatelliteInfoSource(parent)
183 {
184 QByteArray requestedPort;
185 if (parameters.contains(QStringLiteral("serialnmea.serial_port")))
186 requestedPort = parameters.value(QStringLiteral("serialnmea.serial_port")).toString().toLatin1();
187 else
188 requestedPort = qgetenv(varName: "QT_NMEA_SERIAL_PORT");
189 QString portName;
190 if (requestedPort.isEmpty()) {
191 const QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts();
192 qCDebug(lcSerial) << "Found" << ports.count() << "serial ports";
193 if (ports.isEmpty()) {
194 qWarning(msg: "serialnmea: No serial ports found");
195 return;
196 }
197
198 // Try to find a well-known device.
199 QSet<int> supportedDevices;
200 supportedDevices << 0x67b; // GlobalSat (BU-353S4 and probably others)
201 supportedDevices << 0xe8d; // Qstarz MTK II
202 foreach (const QSerialPortInfo& port, ports) {
203 if (port.hasVendorIdentifier() && supportedDevices.contains(value: port.vendorIdentifier())) {
204 portName = port.portName();
205 break;
206 }
207 }
208
209 if (portName.isEmpty()) {
210 qWarning(msg: "serialnmea: No known GPS device found. Specify the COM port via QT_NMEA_SERIAL_PORT.");
211 return;
212 }
213 m_portName = portName;
214 } else {
215 m_portName = QString::fromUtf8(str: requestedPort);
216 }
217
218 m_port = deviceContainer->serial(portName: m_portName);
219 if (!m_port)
220 return;
221
222 setDevice(m_port.data());
223 }
224
225 ~NmeaSatelliteSource()
226 {
227 deviceContainer->releaseSerial(portName: m_portName, pipe&: m_port);
228 }
229
230 bool isValid() const { return !m_port.isNull(); }
231
232private:
233 QSharedPointer<QIOPipe> m_port;
234 QString m_portName;
235};
236
237QGeoPositionInfoSource *QGeoPositionInfoSourceFactorySerialNmea::positionInfoSource(QObject *parent)
238{
239 return positionInfoSourceWithParameters(parent, parameters: QVariantMap());
240}
241
242QGeoSatelliteInfoSource *QGeoPositionInfoSourceFactorySerialNmea::satelliteInfoSource(QObject *parent)
243{
244 return satelliteInfoSourceWithParameters(parent, parameters: QVariantMap());
245}
246
247QGeoAreaMonitorSource *QGeoPositionInfoSourceFactorySerialNmea::areaMonitor(QObject *parent)
248{
249 return areaMonitorWithParameters(parent, parameters: QVariantMap());
250}
251
252QGeoPositionInfoSource *QGeoPositionInfoSourceFactorySerialNmea::positionInfoSourceWithParameters(QObject *parent, const QVariantMap &parameters)
253{
254 QScopedPointer<NmeaSource> src(new NmeaSource(parent, parameters));
255 return src->isValid() ? src.take() : nullptr;
256}
257
258QGeoSatelliteInfoSource *QGeoPositionInfoSourceFactorySerialNmea::satelliteInfoSourceWithParameters(QObject *parent, const QVariantMap &parameters)
259{
260 QScopedPointer<NmeaSatelliteSource> src(new NmeaSatelliteSource(parent, parameters));
261 return src->isValid() ? src.take() : nullptr;
262}
263
264QGeoAreaMonitorSource *QGeoPositionInfoSourceFactorySerialNmea::areaMonitorWithParameters(QObject *parent, const QVariantMap &parameters)
265{
266 Q_UNUSED(parent);
267 Q_UNUSED(parameters)
268 return nullptr;
269}
270

source code of qtlocation/src/plugins/position/serialnmea/qgeopositioninfosourcefactory_serialnmea.cpp