From 5681411c65664e3cfc07a2043173ed791abe83e8 Mon Sep 17 00:00:00 2001 From: dario Date: Sun, 7 Jan 2024 00:41:32 +0100 Subject: [PATCH] Added first implementation --- include/sta/config.hpp | 29 +++++++++ include/sta/tasty/config.hpp | 6 ++ include/sta/tasty/tasks/dummy.hpp | 26 +++++++++ include/sta/tasty/tasks/supervisor.hpp | 37 ++++++++++++ include/sta/tasty/tasks/toggle.hpp | 32 ++++++++++ include/sta/tasty/utils.hpp | 34 +++++++++++ src/cases/case1.cpp | 81 ++++++++++++++++++++++++++ src/cases/case2.cpp | 18 ++++++ src/startup.cpp | 32 ++++++++++ src/tasks/dummy.cpp | 32 ++++++++++ src/tasks/supervisor.cpp | 27 +++++++++ src/tasks/toggle.cpp | 46 +++++++++++++++ src/utils.cpp | 18 ++++++ 13 files changed, 418 insertions(+) create mode 100644 include/sta/config.hpp create mode 100644 include/sta/tasty/config.hpp create mode 100644 include/sta/tasty/tasks/dummy.hpp create mode 100644 include/sta/tasty/tasks/supervisor.hpp create mode 100644 include/sta/tasty/tasks/toggle.hpp create mode 100644 include/sta/tasty/utils.hpp create mode 100644 src/cases/case1.cpp create mode 100644 src/cases/case2.cpp create mode 100644 src/startup.cpp create mode 100644 src/tasks/dummy.cpp create mode 100644 src/tasks/supervisor.cpp create mode 100644 src/tasks/toggle.cpp create mode 100644 src/utils.cpp diff --git a/include/sta/config.hpp b/include/sta/config.hpp new file mode 100644 index 0000000..8a5a73f --- /dev/null +++ b/include/sta/config.hpp @@ -0,0 +1,29 @@ +/* + * config.hpp + * + * Created on: Dec 31, 2023 + * Author: Dario + */ + +#ifndef STA_CONFIG_HPP +#define STA_CONFIG_HPP + +#include + +// Doesn't really do too much right now. Has to be added for successful compilation. +#define STA_PRINTF_USE_STDLIB + +// Enable debug serial output and assertions. +#define STA_ASSERT_FORCE +#define STA_DEBUGGING_ENABLED + +// Settings for the rtos-utils +#define STA_RTOS_SYSTEM_EVENTS_ENABLE + +// Use the default configs for TACOS +#include + +#define STA_TACOS_WATCHDOG_FREQUENCY 1000 +#define STA_TACOS_NUM_STATES 3 + +#endif // STA_CONFIG_HPP diff --git a/include/sta/tasty/config.hpp b/include/sta/tasty/config.hpp new file mode 100644 index 0000000..d5a91be --- /dev/null +++ b/include/sta/tasty/config.hpp @@ -0,0 +1,6 @@ +#ifndef STA_TASTY_CONFIG_HPP +#define STA_TASTY_CONFIG_HPP + +#define TASTY_CASE_1 + +#endif // STA_TASTY_CONFIG_HPP diff --git a/include/sta/tasty/tasks/dummy.hpp b/include/sta/tasty/tasks/dummy.hpp new file mode 100644 index 0000000..e1803ac --- /dev/null +++ b/include/sta/tasty/tasks/dummy.hpp @@ -0,0 +1,26 @@ +/* + * Dummy.hpp + * + * Created on: Dec 31, 2023 + * Author: Dario + */ + +#ifndef INCLUDE_TASKS_DUMMY_HPP_ +#define INCLUDE_TASKS_DUMMY_HPP_ + +#include + +namespace demo +{ + class DummyThread : public sta::tacos::TacosThread + { + public: + DummyThread(const char* name); + + void init() override; + + void func() override; + }; +} + +#endif /* INCLUDE_TASKS_DUMMY_HPP_ */ diff --git a/include/sta/tasty/tasks/supervisor.hpp b/include/sta/tasty/tasks/supervisor.hpp new file mode 100644 index 0000000..a980301 --- /dev/null +++ b/include/sta/tasty/tasks/supervisor.hpp @@ -0,0 +1,37 @@ +/* + * supervisor.hpp + * + * Created on: Jan 6, 2024 + * Author: Dario + */ + +#ifndef STA_TASTY_TASKS_SUPERVISOR_HPP +#define STA_TASTY_TASKS_SUPERVISOR_HPP + +#include + +#include +#include + +namespace sta +{ + namespace tasty + { + typedef std::function TastyCheck; + + class Supervisor : public tacos::TacosThread + { + public: + Supervisor(std::list checks); + + void func() override; + private: + std::list checks_; + }; + } // namespace tasty +} // namespace sta + + + + +#endif // STA_TASTY_TASKS_SUPERVISOR_HPP diff --git a/include/sta/tasty/tasks/toggle.hpp b/include/sta/tasty/tasks/toggle.hpp new file mode 100644 index 0000000..9c5c765 --- /dev/null +++ b/include/sta/tasty/tasks/toggle.hpp @@ -0,0 +1,32 @@ +/* + * toggle.hpp + * + * Created on: Jan 1, 2024 + * Author: Dario + */ + +#ifndef INCLUDE_TASKS_TOGGLE_HPP_ +#define INCLUDE_TASKS_TOGGLE_HPP_ + +#include + +namespace sta +{ + namespace tasty + { + class ToggleThread : public sta::tacos::TacosThread + { + public: + ToggleThread(uint32_t ticks, uint32_t lockout = 0, const char* name = "toggle"); + + void init() override; + + void func() override; + private: + uint32_t ticks_; + uint32_t lockout_; + }; + } // namespace tasty +} // namespace sta + +#endif /* INCLUDE_TASKS_TOGGLE_HPP_ */ diff --git a/include/sta/tasty/utils.hpp b/include/sta/tasty/utils.hpp new file mode 100644 index 0000000..45fa224 --- /dev/null +++ b/include/sta/tasty/utils.hpp @@ -0,0 +1,34 @@ +#ifndef STA_TASTY_UTILS_HPP +#define STA_TASTY_UTILS_HPP + +#include +#ifdef STA_DEBUGGING_ENABLED + +#include + +namespace sta +{ + namespace tasty + { + /** + * @brief Print a test result via serial. + * + * @param file The file the test case was in. + * @param line The line of the test case. + * @param rslt The result of the test case. + */ + void test_case(const char * file, uint32_t line, bool rslt); + } // namespace tasty +} // namespace sta + + +/** + * @brief Assert statement for automatic testing using tasty. Sends the test result to the host via serial. + * + */ +#define STA_TASTY_ASSERT(expr) ( (void)( sta::tasty::test_case(__FILE__, __LINE__, expr) ) ) + + +#endif // STA_DEBUGGING_ENABLED + +#endif // STA_TASTY_UTILS_HPP \ No newline at end of file diff --git a/src/cases/case1.cpp b/src/cases/case1.cpp new file mode 100644 index 0000000..7fc8562 --- /dev/null +++ b/src/cases/case1.cpp @@ -0,0 +1,81 @@ + + +/** + * A simple testcase that checks if threads are correctly terminated after state transitions + * by creating the following threads: + * + * - DummyThread: A thread that does nothing and runs only for state 0. + * - ToggleThread: A thread that switches between states 0 and 1. + * - Supervisor: A thread which supervises the system's behavior. + * + * This test case fails if Dummy doesn't run in state 0 or runs in state 1. + */ + +#include +#ifdef TASTY_CASE_1 + +#include +#include +#include +#include +#include +#include + +namespace sta +{ + namespace tasty + { + class DummyThread : public tacos::TacosThread + { + public: + DummyThread() + : tacos::TacosThread("Dummy", osPriorityNormal) + {} + + void func() override + { + // Nothing to do here. + } + }; + + /** + * @brief Global pointer to the dummy thread. + * + */ + std::shared_ptr dummy; + + /** + * @brief Checker function that is called repeatedly by the supervisor thread. + * + */ + void checkDummy() + { + // Wait for the next state change to happen + tacos::Statemachine::stateChangeEvent.wait(tacos::EventFlags::ALL, osWaitForever); + + // Wait a little bit longer because Dummy needs time to terminate. + osDelay(10); + + // Test if dummy is only running if the state is 0. + if (tacos::getState() == 0) + { + STA_TASTY_ASSERT(dummy->isRunning()); + } + else + { + STA_TASTY_ASSERT(!dummy->isRunning()); + } + } + + void onTastyInit() + { + std::list checks = { &checkDummy }; + + dummy = tacos::addThread({0}); + tacos::addThread({0, 1}, checks); + tacos::addThread({0, 1}, 1000); + } + } +} // namespace sta + +#endif // TASTY_CASE_1 diff --git a/src/cases/case2.cpp b/src/cases/case2.cpp new file mode 100644 index 0000000..a50a124 --- /dev/null +++ b/src/cases/case2.cpp @@ -0,0 +1,18 @@ +#include + +#ifdef TASTY_CASE_2 + +#include + +namespace sta +{ + namespace tasty + { + void onTastyInit() + { + + } + } // namespace tasty +} // namespace sta + +#endif // TASTY_CASE_2 \ No newline at end of file diff --git a/src/startup.cpp b/src/startup.cpp new file mode 100644 index 0000000..5452f27 --- /dev/null +++ b/src/startup.cpp @@ -0,0 +1,32 @@ +/* + * startup.cpp + * + * Created on: Jan 6, 2024 + * Author: Dario + */ + +#include +#include +#include +#include + +namespace sta +{ + namespace tasty + { + STA_WEAK + void onTastyInit() + { + + } + } + + namespace tacos + { + void onManagerInit() + { + tasty::onTastyInit(); + } + } // namespace tacos +} // namespace sta + diff --git a/src/tasks/dummy.cpp b/src/tasks/dummy.cpp new file mode 100644 index 0000000..1e0b2d8 --- /dev/null +++ b/src/tasks/dummy.cpp @@ -0,0 +1,32 @@ +/* + * dummy.cpp + * + * Created on: 22 Sep 2023 + * Author: Dario + */ + +#include +#include + +#include + + +namespace demo +{ + DummyThread::DummyThread(const char* name) + : sta::tacos::TacosThread(name, osPriorityNormal) + { + + } + + void DummyThread::init() + { + STA_DEBUG_PRINTF("Thread %s was initialized", this->getName()); + } + + void DummyThread::func() + { + STA_DEBUG_PRINTLN(this->getName()); + osDelay(1000); + } +} // namespace demo \ No newline at end of file diff --git a/src/tasks/supervisor.cpp b/src/tasks/supervisor.cpp new file mode 100644 index 0000000..da6e6be --- /dev/null +++ b/src/tasks/supervisor.cpp @@ -0,0 +1,27 @@ +#include + + +#include +#include + + +namespace sta +{ + namespace tasty + { + Supervisor::Supervisor(std::list checks) + : tacos::TacosThread{"Supervisor", osPriorityNormal}, + checks_{checks} + {} + + void Supervisor::func() + { + for (TastyCheck check : checks_) + { + blocking( + check(); + ); + } + } + } // namespace tasty +} // namespace sta diff --git a/src/tasks/toggle.cpp b/src/tasks/toggle.cpp new file mode 100644 index 0000000..6ac2863 --- /dev/null +++ b/src/tasks/toggle.cpp @@ -0,0 +1,46 @@ +/* + * toggle.cpp + * + * Created on: Jan 1, 2024 + * Author: Dario + */ + +#include + +#include + + +namespace sta +{ + namespace tasty + { + ToggleThread::ToggleThread(uint32_t ticks, uint32_t lockout /* = 0 */, const char* name /* = "toggle" */) + : sta::tacos::TacosThread(name, osPriorityNormal), + ticks_{ticks}, + lockout_{lockout} + { + + } + + void ToggleThread::init() + { + + } + + void ToggleThread::func() + { + blocking( + this->sleep(ticks_); + ) + + uint16_t state = sta::tacos::getState(); + uint16_t next = 1 - state; + + sta::tacos::setState(state, next, lockout_); + } + } // namespace tasty +} // namespace sta + + + + diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..18f1e57 --- /dev/null +++ b/src/utils.cpp @@ -0,0 +1,18 @@ +#include + +#ifdef STA_DEBUGGING_ENABLED + +#include + +namespace sta +{ + namespace tasty + { + void test_case(const char * file, uint32_t line, bool rslt) + { + STA_DEBUG_PRINTF("[%s|%d|%d]", file, line, rslt); + } + } // namespace tasty +} // namespace sta + +#endif // STA_DEBUGGING_ENABLED