MockMavlinkFileServer.cc 10.8 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
/*=====================================================================
 
 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"

26 27 28 29 30 31 32 33 34
const MockMavlinkFileServer::ErrorMode_t MockMavlinkFileServer::rgFailureModes[] = {
    MockMavlinkFileServer::errModeNoResponse,
    MockMavlinkFileServer::errModeNakResponse,
    MockMavlinkFileServer::errModeNoSecondResponse,
    MockMavlinkFileServer::errModeNakSecondResponse,
    MockMavlinkFileServer::errModeBadCRC,
};
const size_t MockMavlinkFileServer::cFailureModes = sizeof(MockMavlinkFileServer::rgFailureModes) / sizeof(MockMavlinkFileServer::rgFailureModes[0]);

35 36
const MockMavlinkFileServer::FileTestCase MockMavlinkFileServer::rgFileTestCases[MockMavlinkFileServer::cFileTestCases] = {
    // File fits one Read Ack packet, partially filling data
37
    { "partial.qgc",    sizeof(((QGCUASFileManager::Request*)0)->data) - 1,     false },
38
    // File fits one Read Ack packet, exactly filling all data
39
    { "exact.qgc",      sizeof(((QGCUASFileManager::Request*)0)->data),         true },
40
    // File is larger than a single Read Ack packets, requires multiple Reads
41
    { "multi.qgc",      sizeof(((QGCUASFileManager::Request*)0)->data) + 1,     true },
42
};
43

44
// We only support a single fixed session
45 46
const uint8_t MockMavlinkFileServer::_sessionId = 1;

47 48
MockMavlinkFileServer::MockMavlinkFileServer(void) :
    _errMode(errModeNone)
49 50 51 52
{

}

53 54 55
/// @brief Handles List command requests. Only supports root folder paths.
///         File list returned is set using the setFileList method.
void MockMavlinkFileServer::_listCommand(QGCUASFileManager::Request* request)
56
{
57 58
    // FIXME: Does not support directories that span multiple packets
    
Don Gagne's avatar
Don Gagne committed
59 60 61
    QGCUASFileManager::Request  ackResponse;
    QString                     path;

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    // We only support root path
    path = (char *)&request->data[0];
    if (!path.isEmpty() && path != "/") {
        _sendNak(QGCUASFileManager::kErrNotDir);
        return;
    }
    
    // Offset requested is past the end of the list
    if (request->hdr.offset > (uint32_t)_fileList.size()) {
        _sendNak(QGCUASFileManager::kErrEOF);
        return;
    }
    
    ackResponse.hdr.magic = 'f';
    ackResponse.hdr.opcode = QGCUASFileManager::kRspAck;
    ackResponse.hdr.session = 0;
78
    ackResponse.hdr.offset = request->hdr.offset;
79
    ackResponse.hdr.size = 0;
80

81 82 83 84 85
    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++) {
86 87 88
            strcpy(bufPtr, _fileList[i].toStdString().c_str());
            size_t cchFilename = strlen(bufPtr);
			Q_ASSERT(cchFilename);
89 90 91
            ackResponse.hdr.size += cchFilename + 1;
            bufPtr += cchFilename + 1;
        }
92 93

        _emitResponse(&ackResponse);
94 95 96 97 98 99 100
    } else if (_errMode == errModeNakSecondResponse) {
        // Nak error all subsequent requests
        _sendNak(QGCUASFileManager::kErrPerm);
        return;
    } else if (_errMode == errModeNoSecondResponse) {
        // No response for all subsequent requests
        return;
101
    } else {
102 103
        // FIXME: Does not support directories that span multiple packets
        _sendNak(QGCUASFileManager::kErrEOF);
104 105 106
    }
}

107
/// @brief Handles Open command requests.
108 109 110 111 112
void MockMavlinkFileServer::_openCommand(QGCUASFileManager::Request* request)
{
    QGCUASFileManager::Request  response;
    QString                     path;
    
113 114
    size_t cchPath = strnlen((char *)request->data, sizeof(request->data));
    Q_ASSERT(cchPath != sizeof(request->data));
115
    Q_UNUSED(cchPath); // Fix initialized-but-not-referenced warning on release builds
116 117 118
    path = (char *)request->data;
    
    // Check path against one of our known test cases
119

120 121 122 123 124 125 126 127 128
    bool found = false;
    for (size_t i=0; i<cFileTestCases; i++) {
        if (path == rgFileTestCases[i].filename) {
            found = true;
            _readFileLength = rgFileTestCases[i].length;
            break;
        }
    }
    if (!found) {
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        _sendNak(QGCUASFileManager::kErrNotFile);
        return;
    }
    
    response.hdr.magic = 'f';
    response.hdr.opcode = QGCUASFileManager::kRspAck;
    response.hdr.session = _sessionId;
    response.hdr.size = 0;
    
    _emitResponse(&response);
}

/// @brief Handles Read command requests.
void MockMavlinkFileServer::_readCommand(QGCUASFileManager::Request* request)
{
    QGCUASFileManager::Request response;

    if (request->hdr.session != _sessionId) {
        _sendNak(QGCUASFileManager::kErrNoSession);
        return;
    }
    
151 152
    uint32_t readOffset = request->hdr.offset;  // offset into file for reading
    uint8_t cDataBytes = 0;                     // current number of data bytes used
153
    
154 155 156 157 158 159 160 161 162 163 164 165
    if (readOffset != 0) {
        // If we get here it means the client is requesting additional data past the first request
        if (_errMode == errModeNakSecondResponse) {
            // Nak error all subsequent requests
            _sendNak(QGCUASFileManager::kErrPerm);
            return;
        } else if (_errMode == errModeNoSecondResponse) {
            // No rsponse for all subsequent requests
            return;
        }
    }
    
166
    if (readOffset >= _readFileLength) {
167
        _sendNak(QGCUASFileManager::kErrEOF);
168 169 170 171
        return;
    }
    
    // Write file bytes. Data is a repeating sequence of 0x00, 0x01, .. 0xFF.
172
    for (; cDataBytes < sizeof(response.data) && readOffset < _readFileLength; readOffset++, cDataBytes++) {
173
        response.data[cDataBytes] = readOffset & 0xFF;
174 175
    }
    
176 177
    // We should always have written something, otherwise there is something wrong with the code above
    Q_ASSERT(cDataBytes);
178 179 180
    
    response.hdr.magic = 'f';
    response.hdr.session = _sessionId;
181
    response.hdr.size = cDataBytes;
182
    response.hdr.offset = request->hdr.offset;
183
    response.hdr.opcode = QGCUASFileManager::kRspAck;
184
    
185 186 187
    _emitResponse(&response);
}

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
/// @brief Handles Terminate commands
void MockMavlinkFileServer::_terminateCommand(QGCUASFileManager::Request* request)
{
    if (request->hdr.session != _sessionId) {
        _sendNak(QGCUASFileManager::kErrNoSession);
        return;
    }
    
    _sendAck();
    
    // Let our test harness know that we got a terminate command. This is used to validate the a Terminate is correctly
    // sent after an Open.
    emit terminateCommandReceived();
}

203 204 205
/// @brief Handles messages sent to the FTP server.
void MockMavlinkFileServer::sendMessage(mavlink_message_t message)
{
206
    QGCUASFileManager::Request ackResponse;
207

208
    Q_ASSERT(message.msgid == MAVLINK_MSG_ID_ENCAPSULATED_DATA);
209 210 211 212 213 214 215 216 217
    
    if (_errMode == errModeNoResponse) {
        // Don't respond to any requests, this shold cause the client to eventually timeout waiting for the ack
        return;
    } else if (_errMode == errModeNakResponse) {
        // Nak all requests, the actual error send back doesn't really matter as long as it's an error
        _sendNak(QGCUASFileManager::kErrPerm);
        return;
    }
218

Don Gagne's avatar
Don Gagne committed
219 220 221
    mavlink_encapsulated_data_t requestEncapsulatedData;
    mavlink_msg_encapsulated_data_decode(&message, &requestEncapsulatedData);
    QGCUASFileManager::Request* request = (QGCUASFileManager::Request*)&requestEncapsulatedData.data[0];
222
    
Don Gagne's avatar
Don Gagne committed
223 224 225 226
    // Validate CRC
    if (request->hdr.crc32 != QGCUASFileManager::crc32(request)) {
        _sendNak(QGCUASFileManager::kErrCrc);
    }
227

Don Gagne's avatar
Don Gagne committed
228 229 230 231 232 233 234 235 236 237
    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:
238
            // ignored, always acked
Don Gagne's avatar
Don Gagne committed
239 240 241 242 243 244 245 246 247
            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:
248 249
            _listCommand(request);
            break;
250
            
251 252 253
        case QGCUASFileManager::kCmdOpen:
            _openCommand(request);
            break;
Don Gagne's avatar
Don Gagne committed
254

255 256
        case QGCUASFileManager::kCmdRead:
            _readCommand(request);
257
            break;
258

259 260 261 262
        case QGCUASFileManager::kCmdTerminate:
            _terminateCommand(request);
            break;

Don Gagne's avatar
Don Gagne committed
263
        // Remainder of commands are NYI
264

Don Gagne's avatar
Don Gagne committed
265
        case QGCUASFileManager::kCmdCreate:
266
            // creates <path> for writing, returns <session>
Don Gagne's avatar
Don Gagne committed
267
        case QGCUASFileManager::kCmdWrite:
268
            // appends <size> bytes at <offset> in <session>
Don Gagne's avatar
Don Gagne committed
269
        case QGCUASFileManager::kCmdRemove:
270 271 272
            // remove file (only if created by server?)
        default:
            // nack for all NYI opcodes
Don Gagne's avatar
Don Gagne committed
273
            _sendNak(QGCUASFileManager::kErrUnknownCommand);
274 275 276
            break;
    }
}
Don Gagne's avatar
Don Gagne committed
277

278 279 280 281 282 283 284 285 286 287 288 289 290
/// @brief Sends an Ack
void MockMavlinkFileServer::_sendAck(void)
{
    QGCUASFileManager::Request ackResponse;
    
    ackResponse.hdr.magic = 'f';
    ackResponse.hdr.opcode = QGCUASFileManager::kRspAck;
    ackResponse.hdr.session = 0;
    ackResponse.hdr.size = 0;
    
    _emitResponse(&ackResponse);
}

291
/// @brief Sends a Nak with the specified error code.
Don Gagne's avatar
Don Gagne committed
292 293 294 295 296 297 298
void MockMavlinkFileServer::_sendNak(QGCUASFileManager::ErrorCode error)
{
    QGCUASFileManager::Request nakResponse;

    nakResponse.hdr.magic = 'f';
    nakResponse.hdr.opcode = QGCUASFileManager::kRspNak;
    nakResponse.hdr.session = 0;
299 300
    nakResponse.hdr.size = 1;
    nakResponse.data[0] = error;
Don Gagne's avatar
Don Gagne committed
301 302 303 304
    
    _emitResponse(&nakResponse);
}

305
/// @brief Emits a Request through the messageReceived signal.
Don Gagne's avatar
Don Gagne committed
306 307 308 309 310
void MockMavlinkFileServer::_emitResponse(QGCUASFileManager::Request* request)
{
    mavlink_message_t   mavlinkMessage;
    
    request->hdr.crc32 = QGCUASFileManager::crc32(request);
311 312 313 314
    if (_errMode == errModeBadCRC) {
        // Return a bad CRC
        request->hdr.crc32++;
    }
Don Gagne's avatar
Don Gagne committed
315
    
Don Gagne's avatar
Don Gagne committed
316
    mavlink_msg_encapsulated_data_pack(250, MAV_COMP_ID_IMU, &mavlinkMessage, 0 /*_encdata_seq*/, (uint8_t*)request);
Don Gagne's avatar
Don Gagne committed
317 318
    
    emit messageReceived(NULL, mavlinkMessage);
Lorenz Meier's avatar
Lorenz Meier committed
319
}