diff --git a/src/Utilities/StateMachine.cc b/src/Utilities/StateMachine.cc index 6741b8b0549..9b1f79ef476 100644 --- a/src/Utilities/StateMachine.cc +++ b/src/Utilities/StateMachine.cc @@ -8,54 +8,58 @@ ****************************************************************************/ #include "StateMachine.h" +#include "QGCLoggingCategory.h" -StateMachine::StateMachine(void) +QGC_LOGGING_CATEGORY(StateMachineLog, "qgc.utilities.statemachine"); + +StateMachine::StateMachine(QObject *parent) + : QObject(parent) { + // qCDebug(StateMachineLog) << Q_FUNC_INFO << this; +} +StateMachine::~StateMachine() +{ + // qCDebug(StateMachineLog) << Q_FUNC_INFO << this; } -void StateMachine::start(void) +void StateMachine::start() { _active = true; advance(); } -void StateMachine::advance(void) +void StateMachine::advance() { - if (_active) { - _stateIndex++; - if (_stateIndex < stateCount()) { - (*rgStates()[_stateIndex])(this); - } else { - _active = false; - statesCompleted(); - } + if (!_active) { + return; } -} -void StateMachine::move(StateFn stateFn) -{ - if (_active) { - for (int i=0; i #include +Q_DECLARE_LOGGING_CATEGORY(StateMachineLog) + class StateMachine : public QObject { Q_OBJECT public: - typedef void (*StateFn)(StateMachine* stateMachine); + StateMachine(QObject *parent = nullptr); + ~StateMachine(); - StateMachine(void); + typedef void (*StateFn)(StateMachine *stateMachine); /// Start the state machine with the first step - void start(void); + void start(); /// Advance the state machine to the next state and call the state function - virtual void advance(void); + virtual void advance(); /// Move the state machine to the specified state and call the state function void move(StateFn stateFn); - StateFn currentState(void) const; + StateFn currentState() const; /// @return The number of states in the rgStates array - virtual int stateCount(void) const = 0; + virtual int stateCount() const = 0; /// @return Array of states to execute - virtual const StateFn* rgStates(void) const = 0; + virtual const StateFn *rgStates() const = 0; /// Called when all states have completed - virtual void statesCompleted(void) const; + virtual void statesCompleted() const {}; bool active() const { return _active; } protected: - bool _active = false; - int _stateIndex = -1; + bool _active = false; + int _stateIndex = -1; }; diff --git a/src/Vehicle/Components/ComponentInformationManager.cc b/src/Vehicle/Components/ComponentInformationManager.cc index 86a416c2ab6..60f69d5ba94 100644 --- a/src/Vehicle/Components/ComponentInformationManager.cc +++ b/src/Vehicle/Components/ComponentInformationManager.cc @@ -23,21 +23,29 @@ #include -QGC_LOGGING_CATEGORY(ComponentInformationManagerLog, "ComponentInformationManagerLog") +QGC_LOGGING_CATEGORY(ComponentInformationManagerLog, "qgc.vehicle.components.componentinformationmanager") -ComponentInformationManager::ComponentInformationManager(Vehicle* vehicle) - : _vehicle (vehicle) - , _requestTypeStateMachine (this) +ComponentInformationManager::ComponentInformationManager(Vehicle *vehicle, QObject *parent) + : StateMachine(parent) + , _vehicle(vehicle) + , _requestTypeStateMachine(this) , _cachedFileDownload(new QGCCachedFileDownload(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QLatin1String("/QGCCompInfoFileDownloadCache"), this)) , _fileCache(ComponentInformationCache::defaultInstance()) , _translation(new ComponentInformationTranslation(this, _cachedFileDownload)) { + // qCDebug(ComponentInformationManagerLog) << Q_FUNC_INFO << this; + _compInfoMap[MAV_COMP_ID_AUTOPILOT1][COMP_METADATA_TYPE_GENERAL] = new CompInfoGeneral (MAV_COMP_ID_AUTOPILOT1, vehicle, this); _compInfoMap[MAV_COMP_ID_AUTOPILOT1][COMP_METADATA_TYPE_PARAMETER] = new CompInfoParam (MAV_COMP_ID_AUTOPILOT1, vehicle, this); _compInfoMap[MAV_COMP_ID_AUTOPILOT1][COMP_METADATA_TYPE_EVENTS] = new CompInfoEvents (MAV_COMP_ID_AUTOPILOT1, vehicle, this); _compInfoMap[MAV_COMP_ID_AUTOPILOT1][COMP_METADATA_TYPE_ACTUATORS] = new CompInfoActuators (MAV_COMP_ID_AUTOPILOT1, vehicle, this); } +ComponentInformationManager::~ComponentInformationManager() +{ + // qCDebug(ComponentInformationManagerLog) << Q_FUNC_INFO << this; +} + int ComponentInformationManager::stateCount(void) const { return _cStates; @@ -165,10 +173,16 @@ QString ComponentInformationManager::_getFileCacheTag(int compInfoType, uint32_t } -RequestMetaDataTypeStateMachine::RequestMetaDataTypeStateMachine(ComponentInformationManager* compMgr) - : _compMgr(compMgr) +RequestMetaDataTypeStateMachine::RequestMetaDataTypeStateMachine(ComponentInformationManager *compMgr, QObject *parent) + : StateMachine(parent) + , _compMgr(compMgr) { + // qCDebug(RequestMetaDataTypeStateMachineLog) << Q_FUNC_INFO << this; +} +RequestMetaDataTypeStateMachine::~RequestMetaDataTypeStateMachine() +{ + // qCDebug(RequestMetaDataTypeStateMachineLog) << Q_FUNC_INFO << this; } void RequestMetaDataTypeStateMachine::request(CompInfo* compInfo) diff --git a/src/Vehicle/Components/ComponentInformationManager.h b/src/Vehicle/Components/ComponentInformationManager.h index e81e82bf83d..5ac7a7fb6f3 100644 --- a/src/Vehicle/Components/ComponentInformationManager.h +++ b/src/Vehicle/Components/ComponentInformationManager.h @@ -15,6 +15,7 @@ #include #include +Q_DECLARE_LOGGING_CATEGORY(RequestMetaDataTypeStateMachineLog) Q_DECLARE_LOGGING_CATEGORY(ComponentInformationManagerLog) class Vehicle; @@ -31,7 +32,8 @@ class RequestMetaDataTypeStateMachine : public StateMachine Q_OBJECT public: - RequestMetaDataTypeStateMachine(ComponentInformationManager* compMgr); + RequestMetaDataTypeStateMachine(ComponentInformationManager *compMgr, QObject *parent = nullptr); + ~RequestMetaDataTypeStateMachine(); void request (CompInfo* compInfo); QString typeToString(void); @@ -93,28 +95,29 @@ class ComponentInformationManager : public StateMachine Q_OBJECT public: - static constexpr int cachedFileMaxAgeSec = 3 * 24 * 3600; ///< 3 days - - ComponentInformationManager(Vehicle* vehicle); + ComponentInformationManager(Vehicle *vehicle, QObject *parent = nullptr); + ~ComponentInformationManager(); - typedef void (*RequestAllCompleteFn)(void* requestAllCompleteFnData); + typedef void (*RequestAllCompleteFn)(void *requestAllCompleteFnData); - void requestAllComponentInformation (RequestAllCompleteFn requestAllCompletFn, void * requestAllCompleteFnData); - Vehicle* vehicle (void) { return _vehicle; } - CompInfoParam* compInfoParam (uint8_t compId); - CompInfoGeneral* compInfoGeneral (uint8_t compId); + void requestAllComponentInformation(RequestAllCompleteFn requestAllCompletFn, void * requestAllCompleteFnData); + Vehicle *vehicle() { return _vehicle; } + CompInfoParam *compInfoParam(uint8_t compId); + CompInfoGeneral *compInfoGeneral(uint8_t compId); // Overrides from StateMachine - int stateCount (void) const final; - const StateFn* rgStates (void) const final; + int stateCount() const final; + const StateFn *rgStates() const final; - ComponentInformationCache& fileCache() { return _fileCache; } - ComponentInformationTranslation* translation() { return _translation; } + ComponentInformationCache &fileCache() { return _fileCache; } + ComponentInformationTranslation *translation() { return _translation; } float progress() const; void advance() override; + static constexpr int cachedFileMaxAgeSec = 3 * 24 * 3600; ///< 3 days + signals: void progressUpdate(float progress); diff --git a/src/Vehicle/InitialConnectStateMachine.cc b/src/Vehicle/InitialConnectStateMachine.cc index 971c2c461bd..b74ca40d608 100644 --- a/src/Vehicle/InitialConnectStateMachine.cc +++ b/src/Vehicle/InitialConnectStateMachine.cc @@ -20,18 +20,25 @@ #include "RallyPointManager.h" #include "QGCLoggingCategory.h" -QGC_LOGGING_CATEGORY(InitialConnectStateMachineLog, "InitialConnectStateMachineLog") +QGC_LOGGING_CATEGORY(InitialConnectStateMachineLog, "qgc.vehicle.initialconnectstatemachine") -InitialConnectStateMachine::InitialConnectStateMachine(Vehicle* vehicle) - : _vehicle(vehicle) +InitialConnectStateMachine::InitialConnectStateMachine(Vehicle *vehicle, QObject *parent) + : StateMachine(parent) + , _vehicle(vehicle) { - static_assert(sizeof(_rgStates)/sizeof(_rgStates[0]) == sizeof(_rgProgressWeights)/sizeof(_rgProgressWeights[0]), - "array size mismatch"); + static_assert(std::size(_rgStates) == std::size(_rgProgressWeights), "array size mismatch"); _progressWeightTotal = 0; for (int i = 0; i < _cStates; ++i) { _progressWeightTotal += _rgProgressWeights[i]; } + + // qCDebug(InitialConnectStateMachineLog) << Q_FUNC_INFO << this; +} + +InitialConnectStateMachine::~InitialConnectStateMachine() +{ + // qCDebug(InitialConnectStateMachineLog) << Q_FUNC_INFO << this; } int InitialConnectStateMachine::stateCount(void) const diff --git a/src/Vehicle/InitialConnectStateMachine.h b/src/Vehicle/InitialConnectStateMachine.h index 5427d70a5bc..012b5143b75 100644 --- a/src/Vehicle/InitialConnectStateMachine.h +++ b/src/Vehicle/InitialConnectStateMachine.h @@ -17,14 +17,13 @@ Q_DECLARE_LOGGING_CATEGORY(InitialConnectStateMachineLog) -class Vehicle; - class InitialConnectStateMachine : public StateMachine { Q_OBJECT public: - InitialConnectStateMachine(Vehicle* vehicle); + InitialConnectStateMachine(Vehicle *vehicle, QObject *parent = nullptr); + ~InitialConnectStateMachine(); // Overrides from StateMachine int stateCount (void) const final; diff --git a/src/Vehicle/Vehicle.cc b/src/Vehicle/Vehicle.cc index a9a8c00665e..a5b7d963f78 100644 --- a/src/Vehicle/Vehicle.cc +++ b/src/Vehicle/Vehicle.cc @@ -276,8 +276,8 @@ void Vehicle::_commonInit() connect(_missionManager, &MissionManager::newMissionItemsAvailable, _trajectoryPoints, &TrajectoryPoints::clear); _standardModes = new StandardModes (this, this); - _componentInformationManager = new ComponentInformationManager (this); - _initialConnectStateMachine = new InitialConnectStateMachine (this); + _componentInformationManager = new ComponentInformationManager (this, this); + _initialConnectStateMachine = new InitialConnectStateMachine (this, this); _ftpManager = new FTPManager (this); _vehicleLinkManager = new VehicleLinkManager (this); diff --git a/test/qgcunittest/UnitTest.cc b/test/qgcunittest/UnitTest.cc index 7ad51a37803..3bc84ea56ba 100644 --- a/test/qgcunittest/UnitTest.cc +++ b/test/qgcunittest/UnitTest.cc @@ -78,6 +78,7 @@ void UnitTest::init() { _initCalled = true; + MultiVehicleManager::instance()->init(); LinkManager::instance()->setConnectionsAllowed(); // Force offline vehicle back to defaults