summaryrefslogtreecommitdiff
path: root/src/plugins/position/serialnmea/qnmeasatelliteinfosource.cpp
blob: 9804c4067319509646b4c54ae16724ea5fb7ed80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/****************************************************************************
**
** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtPositioning module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qnmeasatelliteinfosource_p.h"
#include <QtPositioning/private/qgeosatelliteinfo_p.h>
#include <QtPositioning/private/qgeosatelliteinfosource_p.h>
#include <QtPositioning/private/qlocationutils_p.h>

#include <QIODevice>
#include <QBasicTimer>
#include <QTimerEvent>
#include <QTimer>
#include <array>
#include <QDebug>
#include <QtCore/QtNumeric>


//QT_BEGIN_NAMESPACE

#define USE_NMEA_PIMPL 1

#if USE_NMEA_PIMPL
class QGeoSatelliteInfoPrivateNmea : public QGeoSatelliteInfoPrivate
{
public:
    QGeoSatelliteInfoPrivateNmea(const QGeoSatelliteInfoPrivate &other);
    QGeoSatelliteInfoPrivateNmea(const QGeoSatelliteInfoPrivateNmea &other);
    virtual ~QGeoSatelliteInfoPrivateNmea();
    QGeoSatelliteInfoPrivate *clone() const override;

    QList<QByteArray> nmeaSentences;
};

QGeoSatelliteInfoPrivateNmea::QGeoSatelliteInfoPrivateNmea(const QGeoSatelliteInfoPrivate &other)
:   QGeoSatelliteInfoPrivate(other)
{
}

QGeoSatelliteInfoPrivateNmea::QGeoSatelliteInfoPrivateNmea(const QGeoSatelliteInfoPrivateNmea &other)
:   QGeoSatelliteInfoPrivate(other)
{
    nmeaSentences = other.nmeaSentences;
}

QGeoSatelliteInfoPrivateNmea::~QGeoSatelliteInfoPrivateNmea() {}

QGeoSatelliteInfoPrivate *QGeoSatelliteInfoPrivateNmea::clone() const
{
    return new QGeoSatelliteInfoPrivateNmea(*this);
}
#else
typedef QGeoSatelliteInfoPrivate QGeoSatelliteInfoPrivateNmea;
#endif

class QNmeaSatelliteInfoSourcePrivate : public QObject, public QGeoSatelliteInfoSourcePrivate
{
    Q_OBJECT
public:
    QNmeaSatelliteInfoSourcePrivate(QNmeaSatelliteInfoSource *parent);
    ~QNmeaSatelliteInfoSourcePrivate();

    void startUpdates();
    void stopUpdates();
    void requestUpdate(int msec);
    void notifyNewUpdate();

public slots:
    void readyRead();
    void emitPendingUpdate();
    void sourceDataClosed();
    void updateRequestTimeout();


public:
    QGeoSatelliteInfoSource *m_source = nullptr;
    QGeoSatelliteInfoSource::Error m_satelliteError = QGeoSatelliteInfoSource::NoError;
    QPointer<QIODevice> m_device;
    struct Update {
        QList<QGeoSatelliteInfo> m_satellitesInView;
        QList<QGeoSatelliteInfo> m_satellitesInUse;
        QList<int> m_inUse; // temp buffer for GSA received before GSV
        bool m_validInView = false;
        bool m_validInUse = false;
        bool m_fresh = false;
        bool m_updatingGsv = false;
#if USE_NMEA_PIMPL
        QByteArray gsa;
        QList<QByteArray> gsv;
#endif
        void setSatellitesInView(const QList<QGeoSatelliteInfo> &inView)
        {
            m_updatingGsv = false;
            m_satellitesInView = inView;
            m_validInView = m_fresh = true;
            if (m_inUse.size()) {
                m_satellitesInUse.clear();
                m_validInUse = false;
                bool corrupt = false;
                for (const auto i: m_inUse) {
                    bool found = false;
                    for (const auto &s: m_satellitesInView) {
                        if (s.satelliteIdentifier() == i) {
                            m_satellitesInUse.append(s);
                            found = true;
                        }
                    }
                    if (!found) { // received a GSA before a GSV, but it was incorrect or something. Unrelated to this GSV at least.
                        m_satellitesInUse.clear();
                        corrupt = true;
                        break;
                    }
                }
                m_validInUse = !corrupt;
                m_inUse.clear();
            }
        }

//        void setSatellitesInUse(const QList<QGeoSatelliteInfo> &inUse)
//        {
//            m_satellitesInUse = inUse;
//            m_validInUse = true;
//            m_inUse.clear();
//        }

        bool setSatellitesInUse(const QList<int> &inUse)
        {
            m_satellitesInUse.clear();
            m_validInUse = false;
            m_inUse = inUse;
            if (m_updatingGsv) {
                m_satellitesInUse.clear();
                m_validInView =  false;
                return false;
            }
            for (const auto i: inUse) {
                bool found = false;
                for (const auto &s: m_satellitesInView) {
                    if (s.satelliteIdentifier() == i) {
                        m_satellitesInUse.append(s);
                        found = true;
                    }
                }
                if (!found) {                       // if satellites in use aren't in view, the related GSV is still to be received.
                    m_inUse = inUse;        // So clear outdated data, buffer the info, and set it later.
                    m_satellitesInUse.clear();
                    m_satellitesInView.clear();
                    m_validInView =  false;
                    return false;
                }
            }
            m_validInUse = m_fresh = true;
            return true;
        }

        void consume()
        {
            m_fresh = false;
        }

        bool isFresh()
        {
            return m_fresh;
        }

        QSet<int> inUse() const
        {
            QSet<int> res;
            for (const auto &s: m_satellitesInUse)
                res.insert(s.satelliteIdentifier());
            return res;
        }

        void clear()
        {
            m_satellitesInView.clear();
            m_satellitesInUse.clear();
            m_validInView = m_validInUse = false;
        }

        bool isValid()
        {
            return m_validInView || m_validInUse; // GSV without GSA is valid. GSA with outdated but still matching GSV also valid.
        }
    } m_pendingUpdate, m_lastUpdate;
    bool m_fresh = false;
    bool m_invokedStart = false;
    bool m_noUpdateLastInterval = false;
    bool m_updateTimeoutSent = false;
    bool m_connectedReadyRead = false;
    int m_pushDelay = 20;
    QBasicTimer *m_updateTimer = nullptr; // the timer used in startUpdates()
    QTimer *m_requestTimer = nullptr; // the timer used in requestUpdate()

protected:
    void readAvailableData();
    bool openSourceDevice();
    bool initialize();
    void prepareSourceDevice();
    bool emitUpdated(Update &update);
    void timerEvent(QTimerEvent *event) override;
};

QNmeaSatelliteInfoSourcePrivate::QNmeaSatelliteInfoSourcePrivate(QNmeaSatelliteInfoSource *parent)
:   m_source(parent)
{
}

void QNmeaSatelliteInfoSourcePrivate::notifyNewUpdate()
{
    if (m_pendingUpdate.isValid() && m_pendingUpdate.isFresh()) {
        if (m_requestTimer && m_requestTimer->isActive()) { // User called requestUpdate()
            m_requestTimer->stop();
            emitUpdated(m_pendingUpdate);
        } else if (m_invokedStart) { // user called startUpdates()
            if (m_updateTimer && m_updateTimer->isActive()) { // update interval > 0
                // for periodic updates, only want the most recent update
                if (m_noUpdateLastInterval) {
                    // if the update was invalid when timerEvent was last called, a valid update
                    // should be sent ASAP
                    emitPendingUpdate(); // m_noUpdateLastInterval handled in there.
                }
            } else { // update interval <= 0, send anything new ASAP
                m_noUpdateLastInterval = !emitUpdated(m_pendingUpdate);
            }
        }
    }
}

QNmeaSatelliteInfoSourcePrivate::~QNmeaSatelliteInfoSourcePrivate()
{
    delete m_updateTimer;
}

void QNmeaSatelliteInfoSourcePrivate::startUpdates()
{
    if (m_invokedStart)
        return;

    m_invokedStart = true;
    m_pendingUpdate.clear();
    m_noUpdateLastInterval = false;

    bool initialized = initialize();
    if (!initialized)
        return;

    // Do not support simulation just yet
//    if (m_updateMode == QNmeaPositionInfoSource::RealTimeMode)
    {
        // skip over any buffered data - we only want the newest data.
        // Don't do this in requestUpdate. In that case bufferedData is good to have/use.
        if (m_device->bytesAvailable()) {
            if (m_device->isSequential())
                m_device->readAll();
            else
                m_device->seek(m_device->bytesAvailable());
        }
    }

    if (m_updateTimer)
        m_updateTimer->stop();

    if (m_source->updateInterval() > 0) {
        if (!m_updateTimer)
            m_updateTimer = new QBasicTimer;
        m_updateTimer->start(m_source->updateInterval(), this);
    }

    if (initialized)
        prepareSourceDevice();
}

void QNmeaSatelliteInfoSourcePrivate::stopUpdates()
{
    m_invokedStart = false;
    if (m_updateTimer)
        m_updateTimer->stop();
    m_pendingUpdate.clear();
    m_noUpdateLastInterval = false;
}

void QNmeaSatelliteInfoSourcePrivate::requestUpdate(int msec)
{
    if (m_requestTimer && m_requestTimer->isActive())
        return;

    if (msec <= 0 || msec < m_source->minimumUpdateInterval()) {
        emit m_source->requestTimeout();
        return;
    }

    if (!m_requestTimer) {
        m_requestTimer = new QTimer(this);
        connect(m_requestTimer, SIGNAL(timeout()), SLOT(updateRequestTimeout()));
    }

    bool initialized = initialize();
    if (!initialized) {
        emit m_source->requestTimeout();
        return;
    }

    m_requestTimer->start(msec);
    prepareSourceDevice();
}

void QNmeaSatelliteInfoSourcePrivate::readyRead()
{
    readAvailableData();
}

void QNmeaSatelliteInfoSourcePrivate::emitPendingUpdate()
{
    if (m_pendingUpdate.isValid() && m_pendingUpdate.isFresh()) {
        m_updateTimeoutSent = false;
        m_noUpdateLastInterval = false;
        if (!emitUpdated(m_pendingUpdate))
            m_noUpdateLastInterval = true;
//        m_pendingUpdate.clear(); // Do not clear, it will be incrementally updated
    } else { // invalid or not fresh update
        if (m_noUpdateLastInterval && !m_updateTimeoutSent) {
            m_updateTimeoutSent = true;
            emit m_source->requestTimeout();
        }
        m_noUpdateLastInterval = true;
    }
}

void QNmeaSatelliteInfoSourcePrivate::sourceDataClosed()
{
    if (m_device && m_device->bytesAvailable())
        readAvailableData();
}

void QNmeaSatelliteInfoSourcePrivate::updateRequestTimeout()
{
    m_requestTimer->stop();
    emit m_source->requestTimeout();
}

void QNmeaSatelliteInfoSourcePrivate::readAvailableData()
{
    while (m_device->canReadLine()) {
        char buf[1024];
        qint64 size = m_device->readLine(buf, sizeof(buf));
        QList<int> satInUse;
        const bool satInUseParsed = QLocationUtils::getSatInUseFromNmea(buf, size, satInUse);
        if (satInUseParsed) {
            m_pendingUpdate.setSatellitesInUse(satInUse);
#if USE_NMEA_PIMPL
            m_pendingUpdate.gsa = QByteArray(buf, size);
            if (m_pendingUpdate.m_satellitesInUse.size()) {
                for (auto &s: m_pendingUpdate.m_satellitesInUse)
                    static_cast<QGeoSatelliteInfoPrivateNmea *>(QGeoSatelliteInfoPrivate::get(s))->nmeaSentences.append(m_pendingUpdate.gsa);
                for (auto &s: m_pendingUpdate.m_satellitesInView)
                    static_cast<QGeoSatelliteInfoPrivateNmea *>(QGeoSatelliteInfoPrivate::get(s))->nmeaSentences.append(m_pendingUpdate.gsa);
            }
#endif
        } else {
            const QLocationUtils::GSVParseStatus parserStatus = QLocationUtils::getSatInfoFromNmea(buf, size, m_pendingUpdate.m_satellitesInView);
            if (parserStatus == QLocationUtils::GSVPartiallyParsed) {
                m_pendingUpdate.m_updatingGsv = true;
#if USE_NMEA_PIMPL
                m_pendingUpdate.gsv.append(QByteArray(buf, size));
#endif
            } else if (parserStatus == QLocationUtils::GSVFullyParsed) {
#if USE_NMEA_PIMPL
                m_pendingUpdate.gsv.append(QByteArray(buf, size));
                for (int i = 0; i < m_pendingUpdate.m_satellitesInView.size(); i++) {
                    const QGeoSatelliteInfo &s = m_pendingUpdate.m_satellitesInView.at(i);
                    QGeoSatelliteInfoPrivateNmea *pimpl = new QGeoSatelliteInfoPrivateNmea(*QGeoSatelliteInfoPrivate::get(s));
                    pimpl->nmeaSentences.append(m_pendingUpdate.gsa);
                    pimpl->nmeaSentences.append(m_pendingUpdate.gsv);
                    m_pendingUpdate.m_satellitesInView.replace(i, QGeoSatelliteInfo(*pimpl));
                }
                m_pendingUpdate.gsv.clear();
#endif
                m_pendingUpdate.setSatellitesInView(m_pendingUpdate.m_satellitesInView);
            }
        }
    }
    notifyNewUpdate();
}

bool QNmeaSatelliteInfoSourcePrivate::openSourceDevice()
{
    if (!m_device) {
        qWarning("QNmeaSatelliteInfoSource: no QIODevice data source, call setDevice() first");
        return false;
    }

    if (!m_device->isOpen() && !m_device->open(QIODevice::ReadOnly)) {
        qWarning("QNmeaSatelliteInfoSource: cannot open QIODevice data source");
        return false;
    }

    connect(m_device, SIGNAL(aboutToClose()), SLOT(sourceDataClosed()));
    connect(m_device, SIGNAL(readChannelFinished()), SLOT(sourceDataClosed()));
    connect(m_device, SIGNAL(destroyed()), SLOT(sourceDataClosed()));

    return true;
}

bool QNmeaSatelliteInfoSourcePrivate::initialize()
{
    if (!openSourceDevice())
        return false;

    return true;
}

void QNmeaSatelliteInfoSourcePrivate::prepareSourceDevice()
{
    if (!m_connectedReadyRead) {
        connect(m_device, SIGNAL(readyRead()), SLOT(readyRead()));
        m_connectedReadyRead = true;
    }
}

bool QNmeaSatelliteInfoSourcePrivate::emitUpdated(QNmeaSatelliteInfoSourcePrivate::Update &update)
{
    bool emitted = false;
    if (!update.isFresh())
        return emitted;

    update.consume();
    const bool inUseUpdated = update.m_satellitesInUse != m_lastUpdate.m_satellitesInUse;
    const bool inViewUpdated = update.m_satellitesInView != m_lastUpdate.m_satellitesInView;


    m_lastUpdate = update;
    if (update.m_validInUse && inUseUpdated) {
        emit m_source->satellitesInUseUpdated(update.m_satellitesInUse);
        emitted = true;
    }
    if (update.m_validInView && inViewUpdated) {
        emit m_source->satellitesInViewUpdated(update.m_satellitesInView);
        emitted = true;
    }
    return emitted;
}

void QNmeaSatelliteInfoSourcePrivate::timerEvent(QTimerEvent * /*event*/)
{
    emitPendingUpdate();
}


// currently supports only realtime
QNmeaSatelliteInfoSource::QNmeaSatelliteInfoSource(QObject *parent)
:   QGeoSatelliteInfoSource(*new QNmeaSatelliteInfoSourcePrivate(this), parent)
{
    d = static_cast<QNmeaSatelliteInfoSourcePrivate *>(QGeoSatelliteInfoSourcePrivate::get(*this));
}

QNmeaSatelliteInfoSource::~QNmeaSatelliteInfoSource()
{
    // d deleted in superclass destructor
}

void QNmeaSatelliteInfoSource::setDevice(QIODevice *device)
{
    if (device != d->m_device) {
        if (!d->m_device)
            d->m_device = device;
        else
            qWarning("QNmeaPositionInfoSource: source device has already been set");
    }
}

QIODevice *QNmeaSatelliteInfoSource::device() const
{
    return d->m_device;
}

void QNmeaSatelliteInfoSource::setUpdateInterval(int msec)
{
    int interval = msec;
    if (interval != 0)
        interval = qMax(msec, minimumUpdateInterval());
    QGeoSatelliteInfoSource::setUpdateInterval(interval);
    if (d->m_invokedStart) {
        d->stopUpdates();
        d->startUpdates();
    }
}

int QNmeaSatelliteInfoSource::minimumUpdateInterval() const
{
    return 2; // Some chips are capable of over 100 updates per seconds.
}

QGeoSatelliteInfoSource::Error QNmeaSatelliteInfoSource::error() const
{
    return d->m_satelliteError;
}

void QNmeaSatelliteInfoSource::startUpdates()
{
    d->startUpdates();
}

void QNmeaSatelliteInfoSource::stopUpdates()
{
    d->stopUpdates();
}

void QNmeaSatelliteInfoSource::requestUpdate(int msec)
{
    d->requestUpdate(msec == 0 ? 60000 * 5 : msec); // 5min default timeout
}

void QNmeaSatelliteInfoSource::setError(QGeoSatelliteInfoSource::Error satelliteError)
{
    d->m_satelliteError = satelliteError;
    emit QGeoSatelliteInfoSource::error(satelliteError);
}


//QT_END_NAMESPACE

#include "qnmeasatelliteinfosource.moc"