LogReplayLink.h 5.46 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
Don Gagne's avatar
Don Gagne committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22


#ifndef LogReplayLink_H
#define LogReplayLink_H

#include "LinkInterface.h"
#include "LinkConfiguration.h"
#include "MAVLinkProtocol.h"

#include <QTimer>
#include <QFile>

class LogReplayLinkConfiguration : public LinkConfiguration
{
Don Gagne's avatar
Don Gagne committed
23 24
    Q_OBJECT

Don Gagne's avatar
Don Gagne committed
25
public:
26 27 28

    Q_PROPERTY(QString  fileName    READ logFilename    WRITE setLogFilename    NOTIFY fileNameChanged)

Don Gagne's avatar
Don Gagne committed
29 30
    LogReplayLinkConfiguration(const QString& name);
    LogReplayLinkConfiguration(LogReplayLinkConfiguration* copy);
31

Don Gagne's avatar
Don Gagne committed
32
    QString logFilename(void) { return _logFilename; }
33
    void setLogFilename(const QString logFilename) { _logFilename = logFilename; emit fileNameChanged(); }
34

Don Gagne's avatar
Don Gagne committed
35 36 37
    QString logFilenameShort(void);

    // Virtuals from LinkConfiguration
38 39 40 41 42 43
    LinkType    type                    () { return LinkConfiguration::TypeLogReplay; }
    void        copyFrom                (LinkConfiguration* source);
    void        loadSettings            (QSettings& settings, const QString& root);
    void        saveSettings            (QSettings& settings, const QString& root);
    void        updateSettings          ();
    bool        isAutoConnectAllowed    () { return false; }
44
    QString     settingsURL             () { return "LogReplaySettings.qml"; }
45 46
signals:
    void fileNameChanged();
Don Gagne's avatar
Don Gagne committed
47 48 49 50 51 52 53 54 55 56 57

private:
    static const char*  _logFilenameKey;
    QString             _logFilename;
};

class LogReplayLink : public LinkInterface
{
    Q_OBJECT

    friend class LinkManager;
58

Don Gagne's avatar
Don Gagne committed
59 60 61
public:
    /// @return true: log is currently playing, false: log playback is paused
    bool isPlaying(void) { return _readTickTimer.isActive(); }
62

Don Gagne's avatar
Don Gagne committed
63 64
    /// Start replay at current position
    void play(void) { emit _playOnThread(); }
65

Don Gagne's avatar
Don Gagne committed
66 67
    /// Pause replay
    void pause(void) { emit _pauseOnThread(); }
68

Don Gagne's avatar
Don Gagne committed
69 70
    /// Move the playhead to the specified percent complete
    void movePlayhead(int percentComplete);
71

Don Gagne's avatar
Don Gagne committed
72 73
    /// Sets the acceleration factor: -100: 0.01X, 0: 1.0X, 100: 100.0X
    void setAccelerationFactor(int factor) { emit _setAccelerationFactorOnThread(factor); }
74

Don Gagne's avatar
Don Gagne committed
75 76 77 78 79 80 81 82 83 84 85 86 87
    // Virtuals from LinkInterface
    virtual QString getName(void) const { return _config->name(); }
    virtual void requestReset(void){ }
    virtual bool isConnected(void) const { return _connected; }
    virtual qint64 getConnectionSpeed(void) const { return 100000000; }
    virtual qint64 bytesAvailable(void) { return 0; }
    virtual bool isLogReplay(void) { return true; }

    // These are left unimplemented in order to cause linker errors which indicate incorrect usage of
    // connect/disconnect on link directly. All connect/disconnect calls should be made through LinkManager.
    bool connect(void);
    bool disconnect(void);

88
private slots:
89
    virtual void _writeBytes(const QByteArray bytes);
90

Don Gagne's avatar
Don Gagne committed
91 92 93 94 95 96 97
signals:
    void logFileStats(bool logTimestamped, int logDurationSecs, int binaryBaudRate);
    void playbackStarted(void);
    void playbackPaused(void);
    void playbackAtEnd(void);
    void playbackError(void);
    void playbackPercentCompleteChanged(int percentComplete);
98

Don Gagne's avatar
Don Gagne committed
99 100 101 102 103 104 105 106 107 108 109 110 111
    // Internal signals
    void _playOnThread(void);
    void _pauseOnThread(void);
    void _setAccelerationFactorOnThread(int factor);

private slots:
    void _readNextLogEntry(void);
    void _play(void);
    void _pause(void);
    void _setAccelerationFactor(int factor);

private:
    // Links are only created/destroyed by LinkManager so constructor/destructor is not public
112
    LogReplayLink(SharedLinkConfigurationPointer& config);
Don Gagne's avatar
Don Gagne committed
113
    ~LogReplayLink();
114

Don Gagne's avatar
Don Gagne committed
115 116 117 118 119 120 121
    void _replayError(const QString& errorMsg);
    quint64 _parseTimestamp(const QByteArray& bytes);
    quint64 _seekToNextMavlinkMessage(mavlink_message_t* nextMsg);
    bool _loadLogFile(void);
    void _finishPlayback(void);
    void _playbackError(void);
    void _resetPlaybackToBeginning(void);
122

Don Gagne's avatar
Don Gagne committed
123 124
    // Virtuals from LinkInterface
    virtual bool _connect(void);
Don Gagne's avatar
Don Gagne committed
125
    virtual void _disconnect(void);
126

Don Gagne's avatar
Don Gagne committed
127 128
    // Virtuals from QThread
    virtual void run(void);
129

130
    LogReplayLinkConfiguration* _logReplayConfig;
Don Gagne's avatar
Don Gagne committed
131 132 133

    bool    _connected;
    QTimer _readTickTimer;      ///< Timer which signals a read of next log record
134

Don Gagne's avatar
Don Gagne committed
135
    static const char* _errorTitle; ///< Title for communicatorError signals
136

Don Gagne's avatar
Don Gagne committed
137 138 139 140
    quint64 _logCurrentTimeUSecs;   ///< The timestamp of the next message in the log file.
    quint64 _logStartTimeUSecs;     ///< The first timestamp in the current log file.
    quint64 _logEndTimeUSecs;       ///< The last timestamp in the current log file.
    quint64 _logDurationUSecs;
141

Don Gagne's avatar
Don Gagne committed
142 143
    static const int    _defaultBinaryBaudRate = 57600;
    int                 _binaryBaudRate;        ///< Playback rate for binary log format
144

Don Gagne's avatar
Don Gagne committed
145 146
    float   _replayAccelerationFactor;  ///< Factor to apply to playback rate
    quint64 _playbackStartTimeMSecs;    ///< The time when the logfile was first played back. This is used to pace out replaying the messages to fix long-term drift/skew. 0 indicates that the player hasn't initiated playback of this log file.
147

Don Gagne's avatar
Don Gagne committed
148 149 150 151
    MAVLinkProtocol*    _mavlink;
    QFile               _logFile;
    quint64             _logFileSize;
    bool                _logTimestamped;    ///< true: Timestamped log format, false: no timestamps
152

Don Gagne's avatar
Don Gagne committed
153 154 155 156
    static const int cbTimestamp = sizeof(quint64);
};

#endif