QGCUASFileManager.h 4.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 
 This file is part of the QGROUNDCONTROL project
 
 QGROUNDCONTROL is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 QGROUNDCONTROL is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
 
 ======================================================================*/

24 25 26 27
#ifndef QGCUASFILEMANAGER_H
#define QGCUASFILEMANAGER_H

#include <QObject>
28

29 30 31 32 33 34 35
#include "UASInterface.h"

class QGCUASFileManager : public QObject
{
    Q_OBJECT
public:
    QGCUASFileManager(QObject* parent, UASInterface* uas);
36 37 38 39 40
    
    /// These methods are only used for testing purposes.
    bool _sendCmdTestAck(void) { return _sendOpcodeOnlyCmd(kCmdNone, kCOAck); };
    bool _sendCmdTestNoAck(void) { return _sendOpcodeOnlyCmd(kCmdTestNoAck, kCOAck); };
    bool _sendCmdReset(void) { return _sendOpcodeOnlyCmd(kCmdReset, kCOAck); };
41 42

signals:
Lorenz Meier's avatar
Lorenz Meier committed
43
    void statusMessage(const QString& msg);
44
    void resetStatusMessages();
45
    void errorMessage(const QString& ms);
46 47

public slots:
Lorenz Meier's avatar
Lorenz Meier committed
48
    void receiveMessage(LinkInterface* link, mavlink_message_t message);
49
    void nothingMessage();
Lorenz Meier's avatar
Lorenz Meier committed
50 51
    void listRecursively(const QString &from);
    void downloadPath(const QString& from, const QString& to);
52 53

protected:
Lorenz Meier's avatar
Lorenz Meier committed
54 55 56 57 58 59 60 61 62 63
    struct RequestHeader
        {
            uint8_t		magic;
            uint8_t		session;
            uint8_t		opcode;
            uint8_t		size;
            uint32_t	crc32;
            uint32_t	offset;
        };

64 65 66 67 68 69 70 71
    struct Request
    {
        struct RequestHeader hdr;
        // The entire Request must fit into the data member of the mavlink_encapsulated_data_t structure. We use as many leftover bytes
        // after we use up space for the RequestHeader for the data portion of the Request.
        uint8_t data[sizeof(((mavlink_encapsulated_data_t*)0)->data) - sizeof(RequestHeader)];
    };

none's avatar
none committed
72 73
    enum Opcode
        {
74
            kCmdNone,       // ignored, always acked
none's avatar
none committed
75
            kCmdTerminate,	// releases sessionID, closes file
76 77 78 79 80 81 82
            kCmdReset,      // terminates all sessions
            kCmdList,       // list files in <path> from <offset>
            kCmdOpen,       // opens <path> for reading, returns <session>
            kCmdRead,       // reads <size> bytes from <offset> in <session>
            kCmdCreate,     // creates <path> for writing, returns <session>
            kCmdWrite,      // appends <size> bytes at <offset> in <session>
            kCmdRemove,     // remove file (only if created by server?)
none's avatar
none committed
83 84

            kRspAck,
85 86 87
            kRspNak,
            
            kCmdTestNoAck,  // ignored, ack not sent back, for testing only, should timeout waiting for ack
none's avatar
none committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101
        };

    enum ErrorCode
        {
            kErrNone,
            kErrNoRequest,
            kErrNoSession,
            kErrSequence,
            kErrNotDir,
            kErrNotFile,
            kErrEOF,
            kErrNotAppend,
            kErrTooBig,
            kErrIO,
102 103 104
            kErrPerm,
            kErrUnknownCommand,
            kErrCrc
none's avatar
none committed
105 106 107 108 109 110
        };


    enum OperationState
        {
            kCOIdle,    // not doing anything
111
            kCOAck,     // waiting for an Ack
none's avatar
none committed
112 113
            kCOList,    // waiting for a List response
        };
114 115 116 117 118 119 120 121 122 123 124
    
    
protected slots:
    void _ackTimeout(void);
    
protected:
    bool _sendOpcodeOnlyCmd(uint8_t opcode, OperationState newOpState);
    void _setupAckTimeout(void);
    void _clearAckTimeout(void);
    void _emitErrorMessage(const QString& msg);
    void _sendRequest(Request* request);
none's avatar
none committed
125 126 127 128

    void sendList();
    void listDecode(const uint8_t *data, unsigned len);

129
    static quint32 crc32(Request* request, unsigned state = 0);
none's avatar
none committed
130
    static QString errorString(uint8_t errorCode);
131

132 133 134 135 136 137 138 139 140 141 142 143 144
    OperationState      _currentOperation;              ///> Current operation of state machine
    QTimer              _ackTimer;                      ///> Used to signal a timeout waiting for an ack
    static const int    _ackTimerTimeoutMsecs = 1000;   ///> Timeout in msecs for ack timer
    
    UASInterface* _mav;
    quint16 _encdata_seq;

    unsigned    _listOffset;    // offset for the current List operation
    QString     _listPath;      // path for the current List operation
    
    // We give MockMavlinkFileServer friend access so that it can use the data structures and opcodes
    // to build a mock mavlink file server for testing.
    friend class MockMavlinkFileServer;
145 146 147
};

#endif // QGCUASFILEMANAGER_H