diff --git a/app/backplane/sensor_module/src/c_sensor_module.cpp b/app/backplane/sensor_module/src/c_sensor_module.cpp index 40cb8a7c..a9834ca3 100644 --- a/app/backplane/sensor_module/src/c_sensor_module.cpp +++ b/app/backplane/sensor_module/src/c_sensor_module.cpp @@ -3,6 +3,7 @@ // F-Core Tenant #include #include +#include // TODO: Blast once we have CCpuMonitorTenant inherit this #include #include LOG_MODULE_REGISTER(sensor_module); diff --git a/include/f_core/os/tenants/c_timed_tenant.h b/include/f_core/os/tenants/c_timed_tenant.h new file mode 100644 index 00000000..b5f2176d --- /dev/null +++ b/include/f_core/os/tenants/c_timed_tenant.h @@ -0,0 +1,20 @@ +#ifndef C_TIMED_TENANT_H +#define C_TIMED_TENANT_H + +#include "f_core/os/c_tenant.h" +#include "f_core/utils/c_soft_timer.h" + +class CTimedTenant : public CTenant { +public: + /** + * @brief Constructor + * @param name Name of the tenant + * @param intervalMillis Interval in milliseconds for the timeout + */ + explicit CTimedTenant(const char* name, const uint32_t intervalMillis); + +private: + CSoftTimer timer; +}; + +#endif //C_TIMED_TENANT_H \ No newline at end of file diff --git a/include/f_core/utils/c_soft_timer.h b/include/f_core/utils/c_soft_timer.h index 6cafdc59..e18b8eac 100644 --- a/include/f_core/utils/c_soft_timer.h +++ b/include/f_core/utils/c_soft_timer.h @@ -13,6 +13,17 @@ class CSoftTimer { k_timer_init(&timer, expirationFn, stopFn); } + /** + * Constructor + * @param timeoutMillis Time in milliseconds until the timer expires + * @param expirationFn Function to call when the timer expires + * @param stopFn Function to call when the timer is stopped + */ + CSoftTimer(const uint32_t timeoutMillis, k_timer_expiry_t expirationFn = nullptr, k_timer_stop_t stopFn = nullptr) { + k_timer_init(&timer, expirationFn, stopFn); + StartTimer(timeoutMillis); + } + /** * Destructor */ @@ -24,7 +35,7 @@ class CSoftTimer { * Start the timer with the given expiration time * @param millis Time in milliseconds until the timer expires */ - void StartTimer(int millis) { + void StartTimer(uint32_t millis) { // Duration (second arg) is the initial expiration time // Period (third arg) is the time set after each expiration k_timer_start(&timer, K_MSEC(millis), K_MSEC(millis)); @@ -59,7 +70,7 @@ class CSoftTimer { * @param millis Time in milliseconds until the timer expires * @param initialExpirationMillis Time in milliseconds to wait before the first expiration */ - void StartTimer(int millis, int initialExpirationMillis) { + void StartTimer(uint32_t millis, uint32_t initialExpirationMillis) { // Duration (second arg) is the initial expiration time // Period (third arg) is the time set after each expiration k_timer_start(&timer, K_MSEC(initialExpirationMillis), K_MSEC(millis)); diff --git a/lib/f_core/os/CMakeLists.txt b/lib/f_core/os/CMakeLists.txt index 7ef6af7b..4efe8181 100644 --- a/lib/f_core/os/CMakeLists.txt +++ b/lib/f_core/os/CMakeLists.txt @@ -2,6 +2,6 @@ # SPDX-License-Identifier: Apache-2.0 zephyr_library() -FILE(GLOB sources *.cpp) +FILE(GLOB sources *.cpp tenants/*.cpp) zephyr_library_sources(${sources}) diff --git a/lib/f_core/os/tenants/c_timed_tenant.cpp b/lib/f_core/os/tenants/c_timed_tenant.cpp new file mode 100644 index 00000000..72a097f9 --- /dev/null +++ b/lib/f_core/os/tenants/c_timed_tenant.cpp @@ -0,0 +1,20 @@ +#include "f_core/os/tenants/c_timed_tenant.h" + +#include "f_core/os/n_rtos.h" +#include "zephyr/logging/log.h" + +LOG_MODULE_REGISTER(CTimedTenant); + +static void timerExpirationCallback(struct k_timer* timer) { + auto* tenant = static_cast(k_timer_user_data_get(timer)); + if (tenant != nullptr) { + tenant->Run(); + } else { + LOG_ERR("Timed tenant callback with null tenant"); + } +} + +CTimedTenant::CTimedTenant(const char* name, const uint32_t intervalMillis) + : CTenant(name), timer(intervalMillis, timerExpirationCallback, nullptr) { + timer.SetUserData(this); +}