ComponentInformationManager.h 3.34 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 88 89
/****************************************************************************
 *
 * (c) 2009-2020 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.
 *
 ****************************************************************************/

#pragma once

#include "QGCLoggingCategory.h"
#include "QGCMAVLink.h"
#include "StateMachine.h"

Q_DECLARE_LOGGING_CATEGORY(ComponentInformationManagerLog)

class Vehicle;
class ComponentInformationManager;

class RequestMetaDataTypeStateMachine : public StateMachine
{
public:
    RequestMetaDataTypeStateMachine(ComponentInformationManager* compMgr);

    void request(COMP_METADATA_TYPE type);
    ComponentInformationManager* compMgr(void) { return _compMgr; }
    QString typeToString(void);

    // Overrides from StateMachine
    int             stateCount      (void) const final;
    const StateFn*  rgStates        (void) const final;
    void            statesCompleted (void) const final;

private:
    static void _stateRequestCompInfo           (StateMachine* stateMachine);
    static void _stateRequestMetaDataJson       (StateMachine* stateMachine);
    static void _stateRequestTranslationJson    (StateMachine* stateMachine);

    ComponentInformationManager*    _compMgr;
    COMP_METADATA_TYPE              _type = COMP_METADATA_TYPE_VERSION;

    static StateFn                  _rgStates[];
    static int                      _cStates;
};

class ComponentInformationManager : public StateMachine
{
public:
    ComponentInformationManager(Vehicle* vehicle);

    typedef struct {
        uint32_t        metadataUID;
        QString         metadataURI;
        uint32_t        translationUID;
        QString         translationURI;
    } ComponentInformation_t;

    typedef void (*RequestAllCompleteFn)(void* requestAllCompleteFnData);

    void        requestAllComponentInformation  (RequestAllCompleteFn requestAllCompletFn, void * requestAllCompleteFnData);
    Vehicle*    vehicle                         (void) { return _vehicle; }

    // These methods should only be called by RequestMetaDataTypeStateMachine
    void _componentInformationReceived(const mavlink_message_t& message);
    void _stateRequestCompInfoComplete(void);

    // Overrides from StateMachine
    int             stateCount  (void) const final;
    const StateFn*  rgStates    (void) const final;

private:
    static void _stateRequestCompInfoVersion        (StateMachine* stateMachine);
    static void _stateRequestCompInfoParam          (StateMachine* stateMachine);
    static void _stateRequestAllCompInfoComplete    (StateMachine* stateMachine);

    Vehicle*                        _vehicle                    = nullptr;
    RequestMetaDataTypeStateMachine _requestTypeStateMachine;
    bool                            _versionCompInfoAvailable   = false;
    ComponentInformation_t          _versionCompInfo;
    bool                            _paramCompInfoAvailable     = false;
    ComponentInformation_t          _parameterCompInfo;
    QList<COMP_METADATA_TYPE>       _supportedMetaDataTypes;
    RequestAllCompleteFn            _requestAllCompleteFn       = nullptr;
    void*                           _requestAllCompleteFnData   = nullptr;

    static StateFn                  _rgStates[];
    static int                      _cStates;
};