MockMavlinkFileServer.cc 3.13 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 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 78 79 80 81 82 83 84 85 86 87
/*=====================================================================
 
 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)
{
    Q_ASSERT(message.msgid == MAVLINK_MSG_ID_ENCAPSULATED_DATA);

    mavlink_encapsulated_data_t data;
    mavlink_msg_encapsulated_data_decode(&message, &data);
    const RequestHeader *hdr = (const RequestHeader *)&data.data[0];
    
    // FIXME: Check CRC

    switch (hdr->opcode) {
        case kCmdNone:
            // ignored, always acked
            
            RequestHeader ackHdr;
            ackHdr.magic = 'f';
            ackHdr.opcode = kRspAck;
            ackHdr.session = 0;
            ackHdr.crc32 = 0;
            ackHdr.size = 0;
            // FIXME: Add CRC
            //ackHdr.crc32 = crc32((uint8_t*)&hdr, sizeof(hdr) + hdr.size, 0);
            
            mavlink_message_t ackMessage;
            mavlink_msg_encapsulated_data_pack(250, 0, &ackMessage, 0 /*_encdata_seq*/, (uint8_t*)&ackHdr);
            emit messageReceived(NULL, ackMessage);
            break;

        case kCmdTerminate:
            // releases sessionID, closes file
        case kCmdReset:
            // terminates all sessions
        case kCmdList:
            // list files in <path> from <offset>
        case kCmdOpen:
            // opens <path> for reading, returns <session>
        case kCmdRead:
            // reads <size> bytes from <offset> in <session>
        case kCmdCreate:
            // creates <path> for writing, returns <session>
        case kCmdWrite:
            // appends <size> bytes at <offset> in <session>
        case kCmdRemove:
            // remove file (only if created by server?)
        default:
            // nack for all NYI opcodes
            
            RequestHeader nakHdr;
            nakHdr.opcode = kRspNak;
            nakHdr.magic = 'f';
            nakHdr.session = 0;
            nakHdr.crc32 = 0;
            nakHdr.size = 0;
            // FIXME: Add CRC
            //ackHdr.crc32 = crc32((uint8_t*)&hdr, sizeof(hdr) + hdr.size, 0);
            
            mavlink_message_t nakMessage;
            mavlink_msg_encapsulated_data_pack(250, 0, &nakMessage, 0 /*_encdata_seq*/, (uint8_t*)&nakHdr);
            emit messageReceived(NULL, nakMessage);
            break;
    }
}