MockMavlinkFileServer.cc 5.3 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 24 25 26 27
/*=====================================================================
 
 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/>.
 
 ======================================================================*/

#include "MockMavlinkFileServer.h"

void MockMavlinkFileServer::sendMessage(mavlink_message_t message)
{
Don Gagne's avatar
Don Gagne committed
28 29 30
    QGCUASFileManager::Request  ackResponse;
    QString                     path;

31 32
    Q_ASSERT(message.msgid == MAVLINK_MSG_ID_ENCAPSULATED_DATA);

Don Gagne's avatar
Don Gagne committed
33 34 35
    mavlink_encapsulated_data_t requestEncapsulatedData;
    mavlink_msg_encapsulated_data_decode(&message, &requestEncapsulatedData);
    QGCUASFileManager::Request* request = (QGCUASFileManager::Request*)&requestEncapsulatedData.data[0];
36
    
Don Gagne's avatar
Don Gagne committed
37 38 39 40
    // Validate CRC
    if (request->hdr.crc32 != QGCUASFileManager::crc32(request)) {
        _sendNak(QGCUASFileManager::kErrCrc);
    }
41

Don Gagne's avatar
Don Gagne committed
42 43 44 45 46 47 48 49 50 51
    switch (request->hdr.opcode) {
        case QGCUASFileManager::kCmdTestNoAck:
            // ignored, ack not sent back, for testing only
            break;
            
        case QGCUASFileManager::kCmdReset:
            // terminates all sessions
            // Fall through to send back Ack

        case QGCUASFileManager::kCmdNone:
52
            // ignored, always acked
Don Gagne's avatar
Don Gagne committed
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
            ackResponse.hdr.magic = 'f';
            ackResponse.hdr.opcode = QGCUASFileManager::kRspAck;
            ackResponse.hdr.session = 0;
            ackResponse.hdr.crc32 = 0;
            ackResponse.hdr.size = 0;
            _emitResponse(&ackResponse);
            break;

        case QGCUASFileManager::kCmdList:
            // We only support root path
            path = (char *)&request->data[0];
            if (!path.isEmpty() && path != "/") {
                _sendNak(QGCUASFileManager::kErrNotDir);
                break;
            }
            
            if (request->hdr.offset > (uint32_t)_fileList.size()) {
                _sendNak(QGCUASFileManager::kErrEOF);
                break;
            }
            
            ackResponse.hdr.magic = 'f';
            ackResponse.hdr.opcode = QGCUASFileManager::kRspAck;
            ackResponse.hdr.session = 0;
            ackResponse.hdr.size = 0;
78
            
Don Gagne's avatar
Don Gagne committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
            if (request->hdr.offset == 0) {
                // Requesting first batch of file names
                Q_ASSERT(_fileList.size());
                char *bufPtr = (char *)&ackResponse.data[0];
                for (int i=0; i<_fileList.size(); i++) {
                    const char *filename = _fileList[i].toStdString().c_str();
                    size_t cchFilename = strlen(filename);
                    strcpy(bufPtr, filename);
                    ackResponse.hdr.size += cchFilename + 1;
                    bufPtr += cchFilename + 1;
                }
                
                // Final double termination
                *bufPtr = 0;
                ackResponse.hdr.size++;

            } else {
                // All filenames fit in first ack, send final null terminated ack
                ackResponse.data[0] = 0;
                ackResponse.hdr.size = 1;
            }
100
            
Don Gagne's avatar
Don Gagne committed
101 102
            _emitResponse(&ackResponse);

103
            break;
Don Gagne's avatar
Don Gagne committed
104 105
            
        // Remainder of commands are NYI
106

Don Gagne's avatar
Don Gagne committed
107
        case QGCUASFileManager::kCmdTerminate:
108
            // releases sessionID, closes file
Don Gagne's avatar
Don Gagne committed
109
        case QGCUASFileManager::kCmdOpen:
110
            // opens <path> for reading, returns <session>
Don Gagne's avatar
Don Gagne committed
111
        case QGCUASFileManager::kCmdRead:
112
            // reads <size> bytes from <offset> in <session>
Don Gagne's avatar
Don Gagne committed
113
        case QGCUASFileManager::kCmdCreate:
114
            // creates <path> for writing, returns <session>
Don Gagne's avatar
Don Gagne committed
115
        case QGCUASFileManager::kCmdWrite:
116
            // appends <size> bytes at <offset> in <session>
Don Gagne's avatar
Don Gagne committed
117
        case QGCUASFileManager::kCmdRemove:
118 119 120
            // remove file (only if created by server?)
        default:
            // nack for all NYI opcodes
Don Gagne's avatar
Don Gagne committed
121
            _sendNak(QGCUASFileManager::kErrUnknownCommand);
122 123 124
            break;
    }
}
Don Gagne's avatar
Don Gagne committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

void MockMavlinkFileServer::_sendNak(QGCUASFileManager::ErrorCode error)
{
    QGCUASFileManager::Request nakResponse;

    nakResponse.hdr.magic = 'f';
    nakResponse.hdr.opcode = QGCUASFileManager::kRspNak;
    nakResponse.hdr.session = 0;
    nakResponse.hdr.size = sizeof(nakResponse.data[0]);
    nakResponse.data[0] = error;
    
    _emitResponse(&nakResponse);
}

void MockMavlinkFileServer::_emitResponse(QGCUASFileManager::Request* request)
{
    mavlink_message_t   mavlinkMessage;
    
    request->hdr.crc32 = QGCUASFileManager::crc32(request);
    
    mavlink_msg_encapsulated_data_pack(250, 0, &mavlinkMessage, 0 /*_encdata_seq*/, (uint8_t*)request);
    
    emit messageReceived(NULL, mavlinkMessage);
}