MockLinkMissionItemHandler.h 5.44 KB
Newer Older
1
/*=====================================================================
2

3
 QGroundControl Open Source Ground Control Station
4

5
 (c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
6

7
 This file is part of the QGROUNDCONTROL project
8

9 10 11 12
 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.
13

14 15 16 17
 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.
18

19 20
 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
21

22 23
 ======================================================================*/

Don Gagne's avatar
Don Gagne committed
24 25
#ifndef MockLinkMissionItemHandler_H
#define MockLinkMissionItemHandler_H
26 27

#include <QObject>
Don Gagne's avatar
Don Gagne committed
28
#include <QMap>
29
#include <QTimer>
30 31

#include "QGCMAVLink.h"
Don Gagne's avatar
Don Gagne committed
32
#include "QGCLoggingCategory.h"
33
#include "MAVLinkProtocol.h"
34

Don Gagne's avatar
Don Gagne committed
35 36 37
class MockLink;

Q_DECLARE_LOGGING_CATEGORY(MockLinkMissionItemHandlerLog)
38 39 40 41

class MockLinkMissionItemHandler : public QObject
{
    Q_OBJECT
42

43
public:
44
    MockLinkMissionItemHandler(MockLink* mockLink, MAVLinkProtocol* mavlinkProtocol);
45 46 47 48
    ~MockLinkMissionItemHandler();
    
    // Prepares for destruction on correct thread
    void shutdown(void);
49

50 51
    /// @brief Called to handle mission item related messages. All messages should be passed to this method.
    ///         It will handle the appropriate set.
Don Gagne's avatar
Don Gagne committed
52 53
    /// @return true: message handled
    bool handleMessage(const mavlink_message_t& msg);
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 88 89 90 91
    
    typedef enum {
        FailNone,                           // No failures
        FailReadRequestListNoResponse,      // Don't send MISSION_COUNT in response to MISSION_REQUEST_LIST
        FailReadRequest0NoResponse,         // Don't send MISSION_ITEM in response to MISSION_REQUEST item 0
        FailReadRequest1NoResponse,         // Don't send MISSION_ITEM in response to MISSION_REQUEST item 1
        FailReadRequest0IncorrectSequence,  // Respond to MISSION_REQUEST 0 with incorrect sequence number in  MISSION_ITEM
        FailReadRequest1IncorrectSequence,  // Respond to MISSION_REQUEST 1 with incorrect sequence number in  MISSION_ITEM
        FailReadRequest0ErrorAck,           // Respond to MISSION_REQUEST 0 with MISSION_ACK error
        FailReadRequest1ErrorAck,           // Respond to MISSION_REQUEST 1 bogus MISSION_ACK error
        FailWriteRequest0NoResponse,        // Don't respond to MISSION_COUNT with MISSION_REQUEST 0
        FailWriteRequest1NoResponse,        // Don't respond to MISSION_ITEM 0 with MISSION_REQUEST 1
        FailWriteRequest0IncorrectSequence, // Respond to MISSION_COUNT 0 with MISSION_REQUEST with wrong sequence number
        FailWriteRequest1IncorrectSequence, // Respond to MISSION_ITEM 0 with MISSION_REQUEST with wrong sequence number
        FailWriteRequest0ErrorAck,          // Respond to MISSION_COUNT 0 with MISSION_ACK error
        FailWriteRequest1ErrorAck,          // Respond to MISSION_ITEM 0 with MISSION_ACK error
        FailWriteFinalAckNoResponse,        // Don't send the final MISSION_ACK
        FailWriteFinalAckErrorAck,          // Send an error as the final MISSION_ACK
        FailWriteFinalAckMissingRequests,   // Send the MISSION_ACK before all items have been requested
    } FailureMode_t;

    /// Sets a failure mode for unit testing
    ///     @param failureMode Type of failure to simulate
    ///     @param firstTimeOnly true: fail first call, success subsequent calls, false: fail all calls
    void setMissionItemFailureMode(FailureMode_t failureMode, bool firstTimeOnly);
    
    /// Called to send a MISSION_ACK message while the MissionManager is in idle state
    void sendUnexpectedMissionAck(MAV_MISSION_RESULT ackType);
    
    /// Called to send a MISSION_ITEM message while the MissionManager is in idle state
    void sendUnexpectedMissionItem(void);
    
    /// Called to send a MISSION_REQUEST message while the MissionManager is in idle state
    void sendUnexpectedMissionRequest(void);
    
    /// Reset the state of the MissionItemHandler to no items, no transactions in progress.
    void reset(void) { _missionItems.clear(); }

Don Gagne's avatar
Don Gagne committed
92 93
    void setSendHomePositionOnEmptyList(bool sendHomePositionOnEmptyList) { _sendHomePositionOnEmptyList = sendHomePositionOnEmptyList; }

94 95
private slots:
    void _missionItemResponseTimeout(void);
96

97
private:
Don Gagne's avatar
Don Gagne committed
98 99 100 101 102
    void _handleMissionRequestList(const mavlink_message_t& msg);
    void _handleMissionRequest(const mavlink_message_t& msg);
    void _handleMissionItem(const mavlink_message_t& msg);
    void _handleMissionCount(const mavlink_message_t& msg);
    void _requestNextMissionItem(int sequenceNumber);
103 104
    void _sendAck(MAV_MISSION_RESULT ackType);
    void _startMissionItemResponseTimer(void);
105

Don Gagne's avatar
Don Gagne committed
106 107 108 109 110 111 112 113
private:
    MockLink* _mockLink;
    
    int _writeSequenceCount;    ///< Numbers of items about to be written
    int _writeSequenceIndex;    ///< Current index being reqested
    
    typedef QMap<uint16_t, mavlink_mission_item_t>   MissionList_t;
    MissionList_t   _missionItems;
114
    
115 116 117 118 119
    QTimer*             _missionItemResponseTimer;
    FailureMode_t       _failureMode;
    bool                _failureFirstTimeOnly;
    bool                _sendHomePositionOnEmptyList;
    MAVLinkProtocol*    _mavlinkProtocol;
120 121
};

Don Gagne's avatar
Don Gagne committed
122
#endif