MockLinkMissionItemHandler.h 5.3 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
9

10

Don Gagne's avatar
Don Gagne committed
11 12
#ifndef MockLinkMissionItemHandler_H
#define MockLinkMissionItemHandler_H
13 14

#include <QObject>
Don Gagne's avatar
Don Gagne committed
15
#include <QMap>
16
#include <QTimer>
17 18

#include "QGCMAVLink.h"
Don Gagne's avatar
Don Gagne committed
19
#include "QGCLoggingCategory.h"
20
#include "MAVLinkProtocol.h"
21

Don Gagne's avatar
Don Gagne committed
22 23 24
class MockLink;

Q_DECLARE_LOGGING_CATEGORY(MockLinkMissionItemHandlerLog)
25 26 27 28

class MockLinkMissionItemHandler : public QObject
{
    Q_OBJECT
29

30
public:
31
    MockLinkMissionItemHandler(MockLink* mockLink, MAVLinkProtocol* mavlinkProtocol);
32 33 34 35
    ~MockLinkMissionItemHandler();
    
    // Prepares for destruction on correct thread
    void shutdown(void);
36

37 38
    /// @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
39 40
    /// @return true: message handled
    bool handleMessage(const mavlink_message_t& msg);
41 42 43 44
    
    typedef enum {
        FailNone,                           // No failures
        FailReadRequestListNoResponse,      // Don't send MISSION_COUNT in response to MISSION_REQUEST_LIST
45
        FailReadRequestListFirstResponse,   // Don't send MISSION_COUNT in response to first MISSION_REQUEST_LIST, allow subsequent to go through
46 47
        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
48
        FailReadRequest1FirstResponse,      // Don't send MISSION_ITEM in response to MISSION_REQUEST item 1 on first try, allow subsequent request to go through
49 50 51 52
        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
53 54
        FailWriteMissionCountNoResponse,    // Don't respond to MISSION_COUNT with MISSION_REQUEST 0
        FailWriteMissionCountFirstResponse, // Don't respond to first MISSION_COUNT with MISSION_REQUEST 0, respond to subsequent MISSION_COUNT requests
55
        FailWriteRequest1NoResponse,        // Don't respond to MISSION_ITEM 0 with MISSION_REQUEST 1
56 57 58 59
        FailWriteRequest0IncorrectSequence, // Item 0 MISSION_REQUEST sent has wrong sequence number
        FailWriteRequest1IncorrectSequence, // Item 1 MISSION_REQUEST sent has wrong sequence number
        FailWriteRequest0ErrorAck,          // Instead of sending MISSION_REQUEST 0, send MISSION_ACK error
        FailWriteRequest1ErrorAck,          // Instead of sending MISSION_REQUEST 1, send MISSION_ACK error
60 61 62 63 64 65 66
        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
67
    void setMissionItemFailureMode(FailureMode_t failureMode);
68 69 70 71 72 73 74 75 76 77 78 79 80
    
    /// 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
81 82
    void setSendHomePositionOnEmptyList(bool sendHomePositionOnEmptyList) { _sendHomePositionOnEmptyList = sendHomePositionOnEmptyList; }

83 84
private slots:
    void _missionItemResponseTimeout(void);
85

86
private:
Don Gagne's avatar
Don Gagne committed
87 88 89 90 91
    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);
92 93
    void _sendAck(MAV_MISSION_RESULT ackType);
    void _startMissionItemResponseTimer(void);
94

Don Gagne's avatar
Don Gagne committed
95 96 97 98 99 100 101 102
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;
103
    
104 105 106 107
    QTimer*             _missionItemResponseTimer;
    FailureMode_t       _failureMode;
    bool                _sendHomePositionOnEmptyList;
    MAVLinkProtocol*    _mavlinkProtocol;
108 109 110
    bool                _failReadRequestListFirstResponse;
    bool                _failReadRequest1FirstResponse;
    bool                _failWriteMissionCountFirstResponse;
111 112
};

Don Gagne's avatar
Don Gagne committed
113
#endif