Combined watchdog code with latest changes.

This commit is contained in:
dario
2024-01-03 14:55:16 +01:00
parent 2494b4f0c1
commit 05ddf1fb03
7 changed files with 250 additions and 10 deletions

View File

@@ -1,6 +1,9 @@
#ifndef STA_TACOS_CONFIGS_DEFAULT_HPP
#define STA_TACOS_CONFIGS_DEFAULT_HPP
// Enable the watchdog provided by TACOS.
#define STA_TACOS_WATCHDOG_ENABLED
// Generally, we assume the TACOS threads to have the highest priorties.
#define STA_TACOS_MANAGER_PRIORITY osPriorityHigh
#define STA_TACOS_STATEMACHINE_PRIORITY osPriorityHigh

View File

@@ -17,6 +17,7 @@
#include <set>
#include <list>
#include <vector>
#include <memory>
#include <sta/tacos/thread.hpp>
@@ -59,10 +60,16 @@ namespace sta
*/
void registerThread(std::shared_ptr<TacosThread> thread, std::list<uint16_t> states);
/**
* @brief Get the Active Threads object
*
* @return std::vector<std::shared_ptr<TacosThread>>
*/
std::vector<std::shared_ptr<TacosThread>> getActiveThreads();
void init() override;
void func() override;
private:
static Manager* _instance;
@@ -105,7 +112,7 @@ namespace sta
*
* @ingroup tacos_manager
*/
std::set<std::shared_ptr<TacosThread>> threads_[STA_TACOS_NUM_STATES];
std::vector<std::shared_ptr<TacosThread>> threads_[STA_TACOS_NUM_STATES];
};
} // namespace tacos
} // namespace sta

View File

@@ -22,6 +22,45 @@ namespace sta
{
namespace tacos
{
/**
* @brief A status flags for the watchdog.
*
*/
enum class ThreadStatus
{
/**
* @brief This thread wants to be ignored by the watchdog.
*
*/
IGNORED,
/**
* @brief The thread terminated regularly. The watchdog will not try to restart this thread.
*
*/
STOPPED,
/**
* @brief The thread's status is unknown. If the watchdog encounters this status, it will try to
* restart the affected thread.
*
*/
UNKNOWN,
/**
* @brief The thread is running as intended. The watchdog will set this flag back to UNKNOWN.
*
*/
RUNNING,
/**
* @brief The thread is waiting and might not send a heartbeat signal in a while. The watchdog will
* not do anything in this case.
*
*/
WAITING
};
/**
* @brief Abstract class for thread implementations in Tacos.
*
@@ -87,8 +126,37 @@ namespace sta
*/
virtual void cleanup();
private:
#ifdef STA_TACOS_WATCHDOG_ENABLED
#define BLOCKING(stmt) waiting(); stmt heartbeat()
protected:
/**
* @brief
*
*/
void heartbeat();
/**
* @brief Set the thread's status to waiting. Should be called before executing
*
*/
void waiting();
public:
/**
* @brief Get the current status of the thread.
*
* @return ThreadStatus
*/
ThreadStatus getStatus();
/**
* @brief Reset the thread's status to UNKNOWN. Should only be called by the Watchdog.
*
*/
void resetStatus();
#endif // STA_TACOS_WATCHDOG_ENABLED
private:
/**
* @brief Static function to pass to RTOS to run as a thread. Calls the loop function implemented here.
*/
@@ -98,6 +166,9 @@ namespace sta
osThreadId_t instance_;
osThreadAttr_t attribs_;
bool running_;
#ifdef STA_TACOS_WATCHDOG_ENABLED
ThreadStatus status_;
#endif // STA_TACOS_WATCHDOG_ENABLED
};
}
}

View File

@@ -0,0 +1,74 @@
#ifndef STA_TACOS_WATCHDOG_HPP
#define STA_TACOS_WATCHDOG_HPP
#include <sta/config.hpp>
#ifdef STA_TACOS_WATCHDOG_ENABLED
#ifndef STA_TACOS_WATCHDOG_PRIORITY
# error "TACOS watchdog priority was not specified!"
#endif // STA_TACOS_WATCHDOG_PRIORITY
#ifndef STA_TACOS_WATCHDOG_FREQUENCY
# error "TACOS watchdog frequency was not specified!"
#endif // STA_TACOS_WATCHDOG_FREQUENCY
#include <sta/tacos/thread.hpp>
namespace sta
{
namespace tacos
{
/**
* @brief Watchdog class for TACOS using singleton pattern.
*
*/
class Watchdog: public TacosThread
{
public:
/**
* @brief Getter for the singleton instance. Constructs the instance if no exists.
*
* @return Watchdog*
*/
static Watchdog* instance()
{
static CGuard g;
if (!_instance)
{
// Create the watchdog singleton instance.
Watchdog::_instance = new Watchdog();
}
return _instance;
}
void func() override;
private:
static Watchdog* _instance;
class CGuard
{
public:
~CGuard()
{
if( NULL != Watchdog::_instance )
{
delete Watchdog::_instance;
Watchdog::_instance = NULL;
}
}
};
Watchdog(const Watchdog&);
Watchdog();
};
} // namespace tacos
} // namespace sta
#endif // STA_TACOS_WATCHDOG_ENABLED
#endif // STA_TACOS_WATCHDOG_HPP