MAVLinkLogManager.h 10.7 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:
109
    MAVLinkLogManager    (QGCApplication* app, QGCToolbox* toolbox);
Gus Grubba's avatar
Gus Grubba committed
110
    ~MAVLinkLogManager   ();
Gus Grubba's avatar
Gus Grubba committed
111

112 113 114
    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)
115 116
    Q_PROPERTY(QString              feedback            READ    feedback            WRITE setFeedback           NOTIFY feedbackChanged)
    Q_PROPERTY(QString              videoURL            READ    videoURL            WRITE setVideoURL           NOTIFY videoURLChanged)
117 118
    Q_PROPERTY(bool                 enableAutoUpload    READ    enableAutoUpload    WRITE setEnableAutoUpload   NOTIFY enableAutoUploadChanged)
    Q_PROPERTY(bool                 enableAutoStart     READ    enableAutoStart     WRITE setEnableAutoStart    NOTIFY enableAutoStartChanged)
119
    Q_PROPERTY(bool                 deleteAfterUpload   READ    deleteAfterUpload   WRITE setDeleteAfterUpload  NOTIFY deleteAfterUploadChanged)
120
    Q_PROPERTY(bool                 publicLog           READ    publicLog           WRITE setPublicLog          NOTIFY publicLogChanged)
121
    Q_PROPERTY(bool                 uploading           READ    uploading                                       NOTIFY uploadingChanged)
122 123 124
    Q_PROPERTY(bool                 logRunning          READ    logRunning                                      NOTIFY logRunningChanged)
    Q_PROPERTY(bool                 canStartLog         READ    canStartLog                                     NOTIFY canStartLogChanged)
    Q_PROPERTY(QmlObjectListModel*  logFiles            READ    logFiles                                        NOTIFY logFilesChanged)
125 126
    Q_PROPERTY(int                  windSpeed           READ    windSpeed           WRITE setWindSpeed          NOTIFY windSpeedChanged)
    Q_PROPERTY(QString              rating              READ    rating              WRITE setRating             NOTIFY ratingChanged)
Gus Grubba's avatar
Gus Grubba committed
127 128 129 130

    Q_INVOKABLE void uploadLog      ();
    Q_INVOKABLE void deleteLog      ();
    Q_INVOKABLE void cancelUpload   ();
131 132
    Q_INVOKABLE void startLogging   ();
    Q_INVOKABLE void stopLogging    ();
Gus Grubba's avatar
Gus Grubba committed
133 134 135 136

    QString     emailAddress        () { return _emailAddress; }
    QString     description         () { return _description; }
    QString     uploadURL           () { return _uploadURL; }
137 138
    QString     feedback            () { return _feedback; }
    QString     videoURL            () { return _videoURL; }
139 140
    bool        enableAutoUpload    () { return _enableAutoUpload; }
    bool        enableAutoStart     () { return _enableAutoStart; }
141
    bool        uploading                ();
142
    bool        logRunning          () { return _logRunning; }
143
    bool        canStartLog         () { return _vehicle != nullptr && !_logginDenied; }
144
    bool        deleteAfterUpload   () { return _deleteAfterUpload; }
145 146 147
    bool        publicLog           () { return _publicLog; }
    int         windSpeed           () { return _windSpeed; }
    QString     rating              () { return _rating; }
148
    QString     logExtension        () { return _ulogExtension; }
Gus Grubba's avatar
Gus Grubba committed
149 150 151 152 153 154

    QmlObjectListModel* logFiles    () { return &_logFiles; }

    void        setEmailAddress     (QString email);
    void        setDescription      (QString description);
    void        setUploadURL        (QString url);
155 156
    void        setFeedback         (QString feedback);
    void        setVideoURL         (QString url);
157 158
    void        setEnableAutoUpload (bool enable);
    void        setEnableAutoStart  (bool enable);
159
    void        setDeleteAfterUpload(bool enable);
160 161 162
    void        setWindSpeed        (int speed);
    void        setRating           (QString rate);
    void        setPublicLog        (bool publicLog);
Gus Grubba's avatar
Gus Grubba committed
163 164 165 166 167 168 169 170

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

signals:
    void emailAddressChanged        ();
    void descriptionChanged         ();
    void uploadURLChanged           ();
171
    void feedbackChanged            ();
172 173
    void enableAutoUploadChanged    ();
    void enableAutoStartChanged     ();
Gus Grubba's avatar
Gus Grubba committed
174 175
    void logFilesChanged            ();
    void selectedCountChanged       ();
Gus Grubba's avatar
Gus Grubba committed
176
    void uploadingChanged           ();
Gus Grubba's avatar
Gus Grubba committed
177 178 179 180
    void readyRead                  (QByteArray data);
    void failed                     ();
    void succeed                    ();
    void abortUpload                ();
181 182
    void logRunningChanged          ();
    void canStartLogChanged         ();
183
    void deleteAfterUploadChanged   ();
184 185 186 187
    void windSpeedChanged           ();
    void ratingChanged              ();
    void videoURLChanged            ();
    void publicLogChanged           ();
Gus Grubba's avatar
Gus Grubba committed
188 189 190 191 192

private slots:
    void _uploadFinished            ();
    void _dataAvailable             ();
    void _uploadProgress            (qint64 bytesSent, qint64 bytesTotal);
193
    void _activeVehicleChanged      (Vehicle* vehicle);
Gus Grubba's avatar
Gus Grubba committed
194
    void _mavlinkLogData            (Vehicle* vehicle, uint8_t target_system, uint8_t target_component, uint16_t sequence, uint8_t first_message, QByteArray data, bool acked);
195
    void _armedChanged              (bool armed);
196
    void _mavCommandResult          (int vehicleId, int component, int command, int result, bool noReponseFromVehicle);
Gus Grubba's avatar
Gus Grubba committed
197 198 199 200

private:
    bool _sendLog                   (const QString& logFile);
    bool _processUploadResponse     (int http_code, QByteArray &data);
Gus Grubba's avatar
Gus Grubba committed
201 202
    bool _createNewLog              ();
    int  _getFirstSelected          ();
Gus Grubba's avatar
Gus Grubba committed
203 204
    void _insertNewLog              (MAVLinkLogFiles* newLog);
    void _deleteLog                 (MAVLinkLogFiles* log);
205
    void _discardLog                ();
Gus Grubba's avatar
Gus Grubba committed
206
    QString _makeFilename           (const QString& baseName);
Gus Grubba's avatar
Gus Grubba committed
207 208 209 210 211

private:
    QString                 _description;
    QString                 _emailAddress;
    QString                 _uploadURL;
212
    QString                 _feedback;
Gus Grubba's avatar
Gus Grubba committed
213
    QString                 _logPath;
214
    QString                 _videoURL;
215 216
    bool                    _enableAutoUpload;
    bool                    _enableAutoStart;
Gus Grubba's avatar
Gus Grubba committed
217 218
    QNetworkAccessManager*  _nam;
    QmlObjectListModel      _logFiles;
Gus Grubba's avatar
Gus Grubba committed
219
    MAVLinkLogFiles*        _currentLogfile;
220 221
    Vehicle*                _vehicle;
    bool                    _logRunning;
Gus Grubba's avatar
Gus Grubba committed
222
    bool                    _loggingDisabled;
Gus Grubba's avatar
Gus Grubba committed
223
    MAVLinkLogProcessor*    _logProcessor;
224
    bool                    _deleteAfterUpload;
225 226 227
    int                     _windSpeed;
    QString                 _rating;
    bool                    _publicLog;
228
    QString                 _ulogExtension;
229
    bool                    _logginDenied;
230

Gus Grubba's avatar
Gus Grubba committed
231 232 233
};

#endif