MAVLinkLogManager.h 9 KB
Newer Older
Gus Grubba's avatar
Gus Grubba committed
1 2 3 4 5 6 7 8 9 10
/****************************************************************************
 *
 *   (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.
 *
 ****************************************************************************/


Gus Grubba's avatar
Gus Grubba committed
11 12
#ifndef MAVLinkLogManager_H
#define MAVLinkLogManager_H
Gus Grubba's avatar
Gus Grubba committed
13 14 15 16 17 18

#include <QObject>

#include "QmlObjectListModel.h"
#include "QGCLoggingCategory.h"
#include "QGCToolbox.h"
19
#include "Vehicle.h"
Gus Grubba's avatar
Gus Grubba committed
20

Gus Grubba's avatar
Gus Grubba committed
21
Q_DECLARE_LOGGING_CATEGORY(MAVLinkLogManagerLog)
Gus Grubba's avatar
Gus Grubba committed
22 23

class QNetworkAccessManager;
Gus Grubba's avatar
Gus Grubba committed
24
class MAVLinkLogManager;
Gus Grubba's avatar
Gus Grubba committed
25 26

//-----------------------------------------------------------------------------
Gus Grubba's avatar
Gus Grubba committed
27
class MAVLinkLogFiles : public QObject
Gus Grubba's avatar
Gus Grubba committed
28 29 30
{
    Q_OBJECT
public:
Gus Grubba's avatar
Gus Grubba committed
31
    MAVLinkLogFiles    (MAVLinkLogManager* manager, const QString& filePath, bool newFile = false);
Gus Grubba's avatar
Gus Grubba committed
32 33

    Q_PROPERTY(QString      name        READ    name                                CONSTANT)
34
    Q_PROPERTY(quint32      size        READ    size                                NOTIFY sizeChanged)
Gus Grubba's avatar
Gus Grubba committed
35 36 37
    Q_PROPERTY(bool         selected    READ    selected    WRITE setSelected       NOTIFY selectedChanged)
    Q_PROPERTY(bool         uploading   READ    uploading                           NOTIFY uploadingChanged)
    Q_PROPERTY(qreal        progress    READ    progress                            NOTIFY progressChanged)
38
    Q_PROPERTY(bool         writing     READ    writing                             NOTIFY writingChanged)
Gus Grubba's avatar
Gus Grubba committed
39
    Q_PROPERTY(bool         uploaded    READ    uploaded                            NOTIFY uploadedChanged)
Gus Grubba's avatar
Gus Grubba committed
40 41 42 43 44 45

    QString     name                () { return _name; }
    quint32     size                () { return _size; }
    bool        selected            () { return _selected; }
    bool        uploading           () { return _uploading; }
    qreal       progress            () { return _progress; }
46
    bool        writing             () { return _writing; }
Gus Grubba's avatar
Gus Grubba committed
47
    bool        uploaded            () { return _uploaded; }
Gus Grubba's avatar
Gus Grubba committed
48 49 50 51

    void        setSelected         (bool selected);
    void        setUploading        (bool uploading);
    void        setProgress         (qreal progress);
52 53
    void        setWriting          (bool writing);
    void        setSize             (quint32 size);
Gus Grubba's avatar
Gus Grubba committed
54
    void        setUploaded         (bool uploaded);
Gus Grubba's avatar
Gus Grubba committed
55 56

signals:
57
    void        sizeChanged         ();
Gus Grubba's avatar
Gus Grubba committed
58 59 60
    void        selectedChanged     ();
    void        uploadingChanged    ();
    void        progressChanged     ();
61
    void        writingChanged      ();
Gus Grubba's avatar
Gus Grubba committed
62
    void        uploadedChanged     ();
Gus Grubba's avatar
Gus Grubba committed
63 64

private:
Gus Grubba's avatar
Gus Grubba committed
65
    MAVLinkLogManager*  _manager;
Gus Grubba's avatar
Gus Grubba committed
66 67 68 69 70
    QString             _name;
    quint32             _size;
    bool                _selected;
    bool                _uploading;
    qreal               _progress;
71
    bool                _writing;
Gus Grubba's avatar
Gus Grubba committed
72
    bool                _uploaded;
Gus Grubba's avatar
Gus Grubba committed
73 74
};

75
//-----------------------------------------------------------------------------
Gus Grubba's avatar
Gus Grubba committed
76
class MAVLinkLogProcessor
77 78
{
public:
Gus Grubba's avatar
Gus Grubba committed
79 80
    MAVLinkLogProcessor();
    ~MAVLinkLogProcessor();
Gus Grubba's avatar
Gus Grubba committed
81 82
    void                close       ();
    bool                valid       ();
Gus Grubba's avatar
Gus Grubba committed
83 84
    bool                create      (MAVLinkLogManager *manager, const QString path, uint8_t id);
    MAVLinkLogFiles*    record      () { return _record; }
Gus Grubba's avatar
Gus Grubba committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    QString             fileName    () { return _fileName; }
    bool                processStreamData(uint16_t _sequence, uint8_t first_message, QByteArray data);
private:
    bool                _checkSequence(uint16_t seq, int &num_drops);
    QByteArray          _writeUlogMessage(QByteArray &data);
    void                _writeData(void* data, int len);
private:
    FILE*               _fd;
    quint32             _written;
    int                 _sequence;
    int                 _numDrops;
    bool                _gotHeader;
    bool                _error;
    QByteArray          _ulogMessage;
    QString             _fileName;
Gus Grubba's avatar
Gus Grubba committed
100
    MAVLinkLogFiles*    _record;
101 102 103
};

//-----------------------------------------------------------------------------
Gus Grubba's avatar
Gus Grubba committed
104
class MAVLinkLogManager : public QGCTool
Gus Grubba's avatar
Gus Grubba committed
105 106 107 108
{
    Q_OBJECT

public:
Gus Grubba's avatar
Gus Grubba committed
109 110
    MAVLinkLogManager    (QGCApplication* app);
    ~MAVLinkLogManager   ();
Gus Grubba's avatar
Gus Grubba committed
111

112 113 114 115 116
    Q_PROPERTY(QString              emailAddress        READ    emailAddress        WRITE setEmailAddress       NOTIFY emailAddressChanged)
    Q_PROPERTY(QString              description         READ    description         WRITE setDescription        NOTIFY descriptionChanged)
    Q_PROPERTY(QString              uploadURL           READ    uploadURL           WRITE setUploadURL          NOTIFY uploadURLChanged)
    Q_PROPERTY(bool                 enableAutoUpload    READ    enableAutoUpload    WRITE setEnableAutoUpload   NOTIFY enableAutoUploadChanged)
    Q_PROPERTY(bool                 enableAutoStart     READ    enableAutoStart     WRITE setEnableAutoStart    NOTIFY enableAutoStartChanged)
117 118
    Q_PROPERTY(bool                 deleteAfterUpload   READ    deleteAfterUpload   WRITE setDeleteAfterUpload  NOTIFY deleteAfterUploadChanged)
    Q_PROPERTY(bool                 uploading           READ    uploading                                       NOTIFY uploadingChanged)
119 120 121
    Q_PROPERTY(bool                 logRunning          READ    logRunning                                      NOTIFY logRunningChanged)
    Q_PROPERTY(bool                 canStartLog         READ    canStartLog                                     NOTIFY canStartLogChanged)
    Q_PROPERTY(QmlObjectListModel*  logFiles            READ    logFiles                                        NOTIFY logFilesChanged)
Gus Grubba's avatar
Gus Grubba committed
122 123 124 125

    Q_INVOKABLE void uploadLog      ();
    Q_INVOKABLE void deleteLog      ();
    Q_INVOKABLE void cancelUpload   ();
126 127
    Q_INVOKABLE void startLogging   ();
    Q_INVOKABLE void stopLogging    ();
Gus Grubba's avatar
Gus Grubba committed
128 129 130 131

    QString     emailAddress        () { return _emailAddress; }
    QString     description         () { return _description; }
    QString     uploadURL           () { return _uploadURL; }
132 133
    bool        enableAutoUpload    () { return _enableAutoUpload; }
    bool        enableAutoStart     () { return _enableAutoStart; }
134
    bool        uploading                ();
135 136
    bool        logRunning          () { return _logRunning; }
    bool        canStartLog         () { return _vehicle != NULL; }
137
    bool        deleteAfterUpload   () { return _deleteAfterUpload; }
Gus Grubba's avatar
Gus Grubba committed
138 139 140 141 142 143

    QmlObjectListModel* logFiles    () { return &_logFiles; }

    void        setEmailAddress     (QString email);
    void        setDescription      (QString description);
    void        setUploadURL        (QString url);
144 145
    void        setEnableAutoUpload (bool enable);
    void        setEnableAutoStart  (bool enable);
146
    void        setDeleteAfterUpload(bool enable);
Gus Grubba's avatar
Gus Grubba committed
147 148 149 150 151 152 153 154

    // Override from QGCTool
    void        setToolbox          (QGCToolbox *toolbox);

signals:
    void emailAddressChanged        ();
    void descriptionChanged         ();
    void uploadURLChanged           ();
155 156
    void enableAutoUploadChanged    ();
    void enableAutoStartChanged     ();
Gus Grubba's avatar
Gus Grubba committed
157 158
    void logFilesChanged            ();
    void selectedCountChanged       ();
Gus Grubba's avatar
Gus Grubba committed
159
    void uploadingChanged           ();
Gus Grubba's avatar
Gus Grubba committed
160 161 162 163
    void readyRead                  (QByteArray data);
    void failed                     ();
    void succeed                    ();
    void abortUpload                ();
164 165
    void logRunningChanged          ();
    void canStartLogChanged         ();
166
    void deleteAfterUploadChanged   ();
Gus Grubba's avatar
Gus Grubba committed
167 168 169 170 171

private slots:
    void _uploadFinished            ();
    void _dataAvailable             ();
    void _uploadProgress            (qint64 bytesSent, qint64 bytesTotal);
172
    void _activeVehicleChanged      (Vehicle* vehicle);
Gus Grubba's avatar
Gus Grubba committed
173
    void _mavlinkLogData            (Vehicle* vehicle, uint8_t target_system, uint8_t target_component, uint16_t sequence, uint8_t first_message, QByteArray data, bool acked);
174
    void _armedChanged              (bool armed);
175 176
    void _commandLongAck            (uint8_t compID, uint16_t command, uint8_t result);
    void _processCmdAck             ();
Gus Grubba's avatar
Gus Grubba committed
177 178 179 180

private:
    bool _sendLog                   (const QString& logFile);
    bool _processUploadResponse     (int http_code, QByteArray &data);
Gus Grubba's avatar
Gus Grubba committed
181 182
    bool _createNewLog              ();
    int  _getFirstSelected          ();
Gus Grubba's avatar
Gus Grubba committed
183 184
    void _insertNewLog              (MAVLinkLogFiles* newLog);
    void _deleteLog                 (MAVLinkLogFiles* log);
185
    void _discardLog                ();
Gus Grubba's avatar
Gus Grubba committed
186
    QString _makeFilename           (const QString& baseName);
Gus Grubba's avatar
Gus Grubba committed
187 188 189 190 191 192

private:
    QString                 _description;
    QString                 _emailAddress;
    QString                 _uploadURL;
    QString                 _logPath;
193 194
    bool                    _enableAutoUpload;
    bool                    _enableAutoStart;
Gus Grubba's avatar
Gus Grubba committed
195 196
    QNetworkAccessManager*  _nam;
    QmlObjectListModel      _logFiles;
Gus Grubba's avatar
Gus Grubba committed
197
    MAVLinkLogFiles*        _currentLogfile;
198 199
    Vehicle*                _vehicle;
    bool                    _logRunning;
Gus Grubba's avatar
Gus Grubba committed
200
    bool                    _loggingDisabled;
Gus Grubba's avatar
Gus Grubba committed
201
    MAVLinkLogProcessor*    _logProcessor;
202
    bool                    _deleteAfterUpload;
203 204
    int                     _loggingCmdTryCount;
    QTimer                  _ackTimer;
Gus Grubba's avatar
Gus Grubba committed
205 206 207
};

#endif