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

10

11
#pragma once
12 13

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

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

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

Q_DECLARE_LOGGING_CATEGORY(MockLinkMissionItemHandlerLog)
24 25 26 27

class MockLinkMissionItemHandler : public QObject
{
    Q_OBJECT
28

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

36 37
    /// @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
38 39
    /// @return true: message handled
    bool handleMessage(const mavlink_message_t& msg);
40 41 42 43
    
    typedef enum {
        FailNone,                           // No failures
        FailReadRequestListNoResponse,      // Don't send MISSION_COUNT in response to MISSION_REQUEST_LIST
44
        FailReadRequestListFirstResponse,   // Don't send MISSION_COUNT in response to first MISSION_REQUEST_LIST, allow subsequent to go through
45 46
        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
47
        FailReadRequest1FirstResponse,      // Don't send MISSION_ITEM in response to MISSION_REQUEST item 1 on first try, allow subsequent request to go through
48 49 50 51
        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
52 53
        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
54
        FailWriteRequest1NoResponse,        // Don't respond to MISSION_ITEM 0 with MISSION_REQUEST 1
55 56 57 58
        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
59 60 61 62 63 64 65
        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
66 67
    ///     @param failureAckResult Error to send if one the ack error modes
    void setFailureMode(FailureMode_t failureMode, MAV_MISSION_RESULT failureAckResult);
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
    void _handleMissionRequestList(const mavlink_message_t& msg);
    void _handleMissionRequest(const mavlink_message_t& msg);
89
    void _handleMissionItem(const mavlink_message_t& msg, bool missionItemInt);
Don Gagne's avatar
Don Gagne committed
90
    void _handleMissionCount(const mavlink_message_t& msg);
91
    void _handleMissionClearAll(const mavlink_message_t& msg);
Don Gagne's avatar
Don Gagne committed
92
    void _requestNextMissionItem(int sequenceNumber);
93 94
    void _sendAck(MAV_MISSION_RESULT ackType);
    void _startMissionItemResponseTimer(void);
95

Don Gagne's avatar
Don Gagne committed
96 97 98 99 100
private:
    MockLink* _mockLink;
    
    int _writeSequenceCount;    ///< Numbers of items about to be written
    int _writeSequenceIndex;    ///< Current index being reqested
101 102 103 104 105 106 107 108

    typedef struct {
        bool isIntItem;
        union {
            mavlink_mission_item_t      missionItem;
            mavlink_mission_item_int_t  missionItemInt;
        };
    } MissionItemBoth_t;
Don Gagne's avatar
Don Gagne committed
109
    
110
    typedef QMap<uint16_t, MissionItemBoth_t> MissionItemList_t;
111 112 113 114 115 116

    MAV_MISSION_TYPE    _requestType;
    MissionItemList_t   _missionItems;
    MissionItemList_t   _fenceItems;
    MissionItemList_t   _rallyItems;

117 118
    QTimer*             _missionItemResponseTimer;
    FailureMode_t       _failureMode;
119
    MAV_MISSION_RESULT  _failureAckResult;
120 121
    bool                _sendHomePositionOnEmptyList;
    MAVLinkProtocol*    _mavlinkProtocol;
122 123 124
    bool                _failReadRequestListFirstResponse;
    bool                _failReadRequest1FirstResponse;
    bool                _failWriteMissionCountFirstResponse;
125 126
};