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 #ifndef QNMEAPOSITIONINFOSOURCE_P_H
40 #define QNMEAPOSITIONINFOSOURCE_P_H
41 
42 //
43 //  W A R N I N G
44 //  -------------
45 //
46 // This file is not part of the Qt API.  It exists purely as an
47 // implementation detail.  This header file may change from version to
48 // version without notice, or even be removed.
49 //
50 // We mean it.
51 //
52 
53 #include "qnmeapositioninfosource.h"
54 #include "qgeopositioninfo.h"
55 
56 #include <QObject>
57 #include <QQueue>
58 #include <QPointer>
59 #include <QtCore/qtimer.h>
60 
61 QT_BEGIN_NAMESPACE
62 
63 class QBasicTimer;
64 class QTimerEvent;
65 class QTimer;
66 
67 class QNmeaReader;
68 struct QPendingGeoPositionInfo
69 {
70     QGeoPositionInfo info;
71     bool hasFix;
72 };
73 
74 
75 class QNmeaPositionInfoSourcePrivate : public QObject
76 {
77     Q_OBJECT
78 public:
79     QNmeaPositionInfoSourcePrivate(QNmeaPositionInfoSource *parent, QNmeaPositionInfoSource::UpdateMode updateMode);
80     ~QNmeaPositionInfoSourcePrivate();
81 
82     void startUpdates();
83     void stopUpdates();
84     void requestUpdate(int msec);
85 
86     bool parsePosInfoFromNmeaData(const char *data,
87                                   int size,
88                                   QGeoPositionInfo *posInfo,
89                                   bool *hasFix);
90 
91     void notifyNewUpdate(QGeoPositionInfo *update, bool fixStatus);
92 
93     QNmeaPositionInfoSource::UpdateMode m_updateMode;
94     QPointer<QIODevice> m_device;
95     QGeoPositionInfo m_lastUpdate;
96     bool m_invokedStart;
97     QGeoPositionInfoSource::Error m_positionError;
98     double m_userEquivalentRangeError;
99 
100 public Q_SLOTS:
101     void readyRead();
102 
103 protected:
104     void timerEvent(QTimerEvent *event);
105 
106 private Q_SLOTS:
107     void emitPendingUpdate();
108     void sourceDataClosed();
109     void updateRequestTimeout();
110 
111 private:
112     bool openSourceDevice();
113     bool initialize();
114     void prepareSourceDevice();
115     void emitUpdated(const QGeoPositionInfo &update);
116 
117     QNmeaPositionInfoSource *m_source;
118     QNmeaReader *m_nmeaReader;
119     QGeoPositionInfo m_pendingUpdate;
120     QDate m_currentDate;
121     QBasicTimer *m_updateTimer; // the timer used in startUpdates()
122     QTimer *m_requestTimer; // the timer used in requestUpdate()
123     qreal m_horizontalAccuracy;
124     qreal m_verticalAccuracy;
125     bool m_noUpdateLastInterval;
126     bool m_updateTimeoutSent;
127     bool m_connectedReadyRead;
128 };
129 
130 
131 class QNmeaReader
132 {
133 public:
QNmeaReader(QNmeaPositionInfoSourcePrivate * sourcePrivate)134     explicit QNmeaReader(QNmeaPositionInfoSourcePrivate *sourcePrivate)
135             : m_proxy(sourcePrivate) {}
~QNmeaReader()136     virtual ~QNmeaReader() {}
137 
138     virtual void readAvailableData() = 0;
139 
140 protected:
141     QNmeaPositionInfoSourcePrivate *m_proxy;
142 };
143 
144 
145 class QNmeaRealTimeReader : public QNmeaReader
146 {
147 public:
148     explicit QNmeaRealTimeReader(QNmeaPositionInfoSourcePrivate *sourcePrivate);
149     virtual void readAvailableData();
150     void notifyNewUpdate();
151 
152     // Data members
153     QGeoPositionInfo m_update;
154     QDateTime m_lastPushedTS;
155     bool m_updateParsed = false;
156     bool m_hasFix = false;
157     QTimer m_timer;
158     int m_pushDelay = -1;
159 };
160 
161 
162 class QNmeaSimulatedReader : public QObject, public QNmeaReader
163 {
164     Q_OBJECT
165 public:
166     explicit QNmeaSimulatedReader(QNmeaPositionInfoSourcePrivate *sourcePrivate);
167     ~QNmeaSimulatedReader();
168     virtual void readAvailableData();
169 
170 protected:
171     virtual void timerEvent(QTimerEvent *event);
172 
173 private Q_SLOTS:
174     void simulatePendingUpdate();
175 
176 private:
177     bool setFirstDateTime();
178     void processNextSentence();
179 
180     QQueue<QPendingGeoPositionInfo> m_pendingUpdates;
181     QByteArray m_nextLine;
182     int m_currTimerId;
183     bool m_hasValidDateTime;
184 };
185 
186 QT_END_NAMESPACE
187 
188 #endif
189