mirror of
https://github.com/eclipse-openvehicle-api/openvehicle-api.git
synced 2026-02-05 15:18:45 +00:00
42
sdv_services/task_timer/CMakeLists.txt
Normal file
42
sdv_services/task_timer/CMakeLists.txt
Normal file
@@ -0,0 +1,42 @@
|
||||
# Define project
|
||||
project(task_timer VERSION 1.0 LANGUAGES CXX)
|
||||
|
||||
# Define target
|
||||
add_library(task_timer SHARED "tasktimer.h" "tasktimer.cpp")
|
||||
|
||||
target_link_options(task_timer PRIVATE)
|
||||
if (WIN32)
|
||||
target_link_libraries(task_timer ${CMAKE_THREAD_LIBS_INIT} Winmm.lib)
|
||||
elseif (UNIX)
|
||||
target_link_libraries(task_timer ${CMAKE_THREAD_LIBS_INIT} rt)
|
||||
endif()
|
||||
set_target_properties(task_timer PROPERTIES PREFIX "")
|
||||
set_target_properties(task_timer PROPERTIES SUFFIX ".sdv")
|
||||
|
||||
# Build dependencies
|
||||
add_dependencies(task_timer CompileCoreIDL)
|
||||
|
||||
# Appending the service in the service list
|
||||
set(SDV_Service_List ${SDV_Service_List} task_timer PARENT_SCOPE)
|
||||
|
||||
|
||||
# Define project
|
||||
project(simulation_task_timer VERSION 1.0 LANGUAGES CXX)
|
||||
|
||||
# Define target
|
||||
add_library(simulation_task_timer SHARED "simulationtasktimer.h" "simulationtasktimer.cpp")
|
||||
|
||||
target_link_options(simulation_task_timer PRIVATE)
|
||||
if (WIN32)
|
||||
target_link_libraries(simulation_task_timer ${CMAKE_THREAD_LIBS_INIT} Winmm.lib)
|
||||
elseif (UNIX)
|
||||
target_link_libraries(simulation_task_timer ${CMAKE_THREAD_LIBS_INIT} rt)
|
||||
endif()
|
||||
set_target_properties(simulation_task_timer PROPERTIES PREFIX "")
|
||||
set_target_properties(simulation_task_timer PROPERTIES SUFFIX ".sdv")
|
||||
|
||||
# Build dependencies
|
||||
add_dependencies(simulation_task_timer CompileCoreIDL)
|
||||
|
||||
# Appending the service in the service list
|
||||
set(SDV_Service_List ${SDV_Service_List} task_timer simulation_task_timer PARENT_SCOPE)
|
||||
142
sdv_services/task_timer/simulationtasktimer.cpp
Normal file
142
sdv_services/task_timer/simulationtasktimer.cpp
Normal file
@@ -0,0 +1,142 @@
|
||||
#include "simulationtasktimer.h"
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
|
||||
CSimulationTimer::CSimulationTimer(CSimulationTaskTimerService& rtimersvc, uint32_t uiPeriod, sdv::core::ITaskExecute* pExecute) :
|
||||
m_rtimersvc(rtimersvc), m_pExecute(pExecute)
|
||||
{
|
||||
if (!pExecute) return;
|
||||
|
||||
m_uiInitializedPeriod = static_cast<uint64_t>(uiPeriod) * 1000;
|
||||
m_uiPeriod = static_cast<uint64_t>(uiPeriod) * 1000;
|
||||
if(m_uiInitializedPeriod != 0)
|
||||
m_bRunning = true;
|
||||
}
|
||||
|
||||
void CSimulationTimer::DestroyObject()
|
||||
{
|
||||
// Delete the object
|
||||
m_rtimersvc.RemoveTimer(this);
|
||||
}
|
||||
|
||||
CSimulationTimer::operator bool() const
|
||||
{
|
||||
return m_bRunning;
|
||||
}
|
||||
|
||||
void CSimulationTimer::SimulationStep(uint64_t uiSimulationStep)
|
||||
{
|
||||
while (uiSimulationStep > m_uiPeriod)
|
||||
{
|
||||
uiSimulationStep -= m_uiPeriod;
|
||||
if (m_pExecute) m_pExecute->Execute();
|
||||
m_uiPeriod = m_uiInitializedPeriod;
|
||||
}
|
||||
|
||||
m_uiPeriod -= uiSimulationStep;
|
||||
if (m_uiPeriod == 0)
|
||||
{
|
||||
if (m_pExecute) m_pExecute->Execute();
|
||||
m_uiPeriod = m_uiInitializedPeriod;
|
||||
}
|
||||
}
|
||||
|
||||
//#ifdef _WIN32
|
||||
//void CSimulationTimer::ExecuteCallback()
|
||||
//{
|
||||
// if (m_rtimersvc.GetStatus() != sdv::EObjectStatus::running) return;
|
||||
// if (!m_pExecute) return;
|
||||
// if (!m_bPrioritySet)
|
||||
// SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
|
||||
// m_bPrioritySet = true;
|
||||
//
|
||||
// if (m_pExecute) m_pExecute->Execute();
|
||||
//}
|
||||
//#endif
|
||||
|
||||
CSimulationTaskTimerService::CSimulationTaskTimerService()
|
||||
{
|
||||
}
|
||||
|
||||
CSimulationTaskTimerService::~CSimulationTaskTimerService()
|
||||
{
|
||||
}
|
||||
|
||||
void CSimulationTaskTimerService::Initialize(/*in*/ const sdv::u8string& /*ssObjectConfig*/)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
m_eObjectStatus = sdv::EObjectStatus::initializing;
|
||||
SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
|
||||
|
||||
// set up time resolution and maybe some other things
|
||||
timeBeginPeriod(1);
|
||||
#endif
|
||||
m_eObjectStatus = sdv::EObjectStatus::initialized;
|
||||
}
|
||||
|
||||
sdv::EObjectStatus CSimulationTaskTimerService::GetStatus() const
|
||||
{
|
||||
return m_eObjectStatus;
|
||||
}
|
||||
|
||||
void CSimulationTaskTimerService::SetOperationMode(/*in*/ sdv::EOperationMode eMode)
|
||||
{
|
||||
switch (eMode)
|
||||
{
|
||||
case sdv::EOperationMode::configuring:
|
||||
if (m_eObjectStatus == sdv::EObjectStatus::running || m_eObjectStatus == sdv::EObjectStatus::initialized)
|
||||
m_eObjectStatus = sdv::EObjectStatus::configuring;
|
||||
break;
|
||||
case sdv::EOperationMode::running:
|
||||
if (m_eObjectStatus == sdv::EObjectStatus::configuring || m_eObjectStatus == sdv::EObjectStatus::initialized)
|
||||
m_eObjectStatus = sdv::EObjectStatus::running;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void CSimulationTaskTimerService::Shutdown()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
m_eObjectStatus = sdv::EObjectStatus::shutdown_in_progress;
|
||||
timeEndPeriod(1);
|
||||
#endif
|
||||
m_eObjectStatus = sdv::EObjectStatus::destruction_pending;
|
||||
}
|
||||
|
||||
sdv::IInterfaceAccess* CSimulationTaskTimerService::CreateTimer(uint32_t uiPeriod, sdv::IInterfaceAccess* pTask)
|
||||
{
|
||||
if (m_eObjectStatus != sdv::EObjectStatus::configuring) return nullptr;
|
||||
if (!uiPeriod) return nullptr;
|
||||
if (!pTask) return nullptr;
|
||||
sdv::core::ITaskExecute* pExecute = pTask->GetInterface<sdv::core::ITaskExecute>();
|
||||
if (!pExecute) return nullptr;
|
||||
|
||||
std::unique_lock<std::mutex> lock(m_mtxTasks);
|
||||
auto ptrTimer = std::make_unique<CSimulationTimer>(*this, uiPeriod, pExecute);
|
||||
// Ignore cppcheck warning; normally the returned pointer should always have a value at this stage (otherwise an
|
||||
// exception was triggered).
|
||||
// cppcheck-suppress knownConditionTrueFalse
|
||||
if (!ptrTimer)
|
||||
return nullptr;
|
||||
CSimulationTimer* pObject = ptrTimer.get();
|
||||
if (pObject)
|
||||
m_mapTasks.try_emplace(pObject, std::move(ptrTimer));
|
||||
return pObject;
|
||||
}
|
||||
|
||||
void CSimulationTaskTimerService::SimulationStep(uint64_t uiSimulationStep)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(m_mtxTasks);
|
||||
for (auto it = m_mapTasks.begin(); it != m_mapTasks.end(); it++)
|
||||
{
|
||||
it->first->SimulationStep(uiSimulationStep);
|
||||
}
|
||||
}
|
||||
|
||||
void CSimulationTaskTimerService::RemoveTimer(CSimulationTimer* pTimer)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(m_mtxTasks);
|
||||
m_mapTasks.erase(pTimer);
|
||||
}
|
||||
194
sdv_services/task_timer/simulationtasktimer.h
Normal file
194
sdv_services/task_timer/simulationtasktimer.h
Normal file
@@ -0,0 +1,194 @@
|
||||
/**
|
||||
* @file simulationtasktimer.h
|
||||
* @author Sudipta Babu Durjoy FRD DISS21 (mailto:sudipta.durjoy@zf.com)
|
||||
* @brief
|
||||
* @version 1.0
|
||||
* @date 2023-05-24
|
||||
*
|
||||
* @copyright Copyright ZF Friedrichshafen AG (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef SIMULATION_TASK_TIMER_H
|
||||
#define SIMULATION_TASK_TIMER_H
|
||||
|
||||
#include <interfaces/core.h>
|
||||
#include <interfaces/timer.h>
|
||||
#include <support/interface_ptr.h>
|
||||
#include <support/component_impl.h>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <fstream>
|
||||
|
||||
#ifdef _WIN32
|
||||
// Resolve conflict
|
||||
#pragma push_macro("interface")
|
||||
#undef interface
|
||||
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
|
||||
#include <WinSock2.h>
|
||||
#include <Windows.h>
|
||||
#include <timeapi.h>
|
||||
|
||||
// Resolve conflict
|
||||
#pragma pop_macro("interface")
|
||||
#ifdef GetClassInfo
|
||||
#undef GetClassInfo
|
||||
#endif
|
||||
#elif defined __unix__
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <mutex>
|
||||
#include <time.h>
|
||||
#include <sys/times.h>
|
||||
#else
|
||||
#error OS is not supported!
|
||||
#endif
|
||||
|
||||
// Forward declaration
|
||||
class CSimulationTaskTimerService;
|
||||
|
||||
/**
|
||||
* @brief Timer object managing the lifetime of the timer.
|
||||
*/
|
||||
class CSimulationTimer : public sdv::IInterfaceAccess, public sdv::IObjectDestroy, public sdv::core::ITimerSimulationStep
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor
|
||||
* @param[in] rtimersvc Reference to the task timer service.
|
||||
* @param[in] uiPeriod The period of the task timer (must not be 0) in ms.
|
||||
* @param[in] pExecute Pointer to the interface containing the execution function.
|
||||
*/
|
||||
CSimulationTimer(CSimulationTaskTimerService& rtimersvc, uint32_t uiPeriod, sdv::core::ITaskExecute* pExecute);
|
||||
|
||||
// Interface map
|
||||
BEGIN_SDV_INTERFACE_MAP()
|
||||
SDV_INTERFACE_ENTRY(sdv::IObjectDestroy)
|
||||
SDV_INTERFACE_ENTRY(sdv::core::ITimerSimulationStep)
|
||||
END_SDV_INTERFACE_MAP()
|
||||
|
||||
/**
|
||||
* @brief Destroy the object. Overload of sdv::IObjectDestroy::DestroyObject.
|
||||
*/
|
||||
virtual void DestroyObject() override;
|
||||
|
||||
/**
|
||||
* @brief Method to set the time which has past from the last simulation step.
|
||||
* @param[in] uiSimulationStep the time in microseconds which has past from the last simulation step.
|
||||
*/
|
||||
virtual void SimulationStep(/*in*/ uint64_t uiSimulationStep) override;
|
||||
|
||||
/**
|
||||
* @brief Operator returning info about the validity of the timer.
|
||||
*/
|
||||
operator bool() const;
|
||||
|
||||
private:
|
||||
//#ifdef _WIN32
|
||||
// /**
|
||||
// * @brief Execution function called by the timer.
|
||||
// */
|
||||
// void ExecuteCallback();
|
||||
//
|
||||
//#endif
|
||||
|
||||
sdv::CLifetimeCookie m_cookie = sdv::CreateLifetimeCookie(); ///< Lifetime cookie to manage the module lifetime.
|
||||
CSimulationTaskTimerService& m_rtimersvc; ///< Reference to the task timer service
|
||||
sdv::core::ITaskExecute* m_pExecute = nullptr; ///< Pointer to the execution callback interface.
|
||||
uint64_t m_uiInitializedPeriod = 0; ///< Period in microseconds when timer is initialized.
|
||||
uint64_t m_uiPeriod = 0; ///< Decreasing period counter, if 0 execution is called.
|
||||
bool m_bRunning = false; ///< When set, the timer is running.
|
||||
bool m_bPrioritySet = false; ///< When set, the priority of the task was increased.
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Task timer class to execute task periodically
|
||||
*/
|
||||
class CSimulationTaskTimerService : public sdv::CSdvObject, public sdv::IObjectControl, public sdv::core::ITaskTimer,
|
||||
public sdv::core::ITimerSimulationStep
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor configure and initializes the timer.
|
||||
*/
|
||||
CSimulationTaskTimerService();
|
||||
|
||||
/**
|
||||
* @brief Destructor cleans up the timer if there is no active task and delete it after.
|
||||
*/
|
||||
virtual ~CSimulationTaskTimerService() override;
|
||||
|
||||
// Interface map
|
||||
BEGIN_SDV_INTERFACE_MAP()
|
||||
SDV_INTERFACE_ENTRY(sdv::IObjectControl)
|
||||
SDV_INTERFACE_ENTRY(sdv::core::ITaskTimer)
|
||||
SDV_INTERFACE_ENTRY(sdv::core::ITimerSimulationStep)
|
||||
END_SDV_INTERFACE_MAP()
|
||||
|
||||
// Object declarations
|
||||
DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::SystemObject)
|
||||
DECLARE_OBJECT_CLASS_NAME("SimulationTaskTimerService")
|
||||
DECLARE_OBJECT_SINGLETON()
|
||||
|
||||
/**
|
||||
* @brief Initialize the object. Overload of sdv::IObjectControl::Initialize.
|
||||
* @param[in] ssObjectConfig Optional configuration string.
|
||||
*/
|
||||
virtual void Initialize(/*in*/ const sdv::u8string& ssObjectConfig) override;
|
||||
|
||||
/**
|
||||
* @brief Get the current status of the object. Overload of sdv::IObjectControl::GetStatus.
|
||||
* @return Return the current status of the object.
|
||||
*/
|
||||
virtual sdv::EObjectStatus GetStatus() const override;
|
||||
|
||||
/**
|
||||
* @brief Set the component operation mode. Overload of sdv::IObjectControl::SetOperationMode.
|
||||
* @param[in] eMode The operation mode, the component should run in.
|
||||
*/
|
||||
virtual void SetOperationMode(/*in*/ sdv::EOperationMode eMode) override;
|
||||
|
||||
/**
|
||||
* @brief Shutdown called before the object is destroyed. Overload of sdv::IObjectControl::Shutdown.
|
||||
* @attention Implement calls to other SDV objects here as this is no longer considered safe during the destructor of the object!
|
||||
* After a call to shutdown any threads/callbacks/etc that could call other SDV objects need to have been stopped.
|
||||
* The SDV object itself is to remain in a state where it can respond to calls to its interfaces as other objects may still call it during the shutdown sequence!
|
||||
* Any subsequent call to GetStatus should return EObjectStatus::destruction_pending
|
||||
*/
|
||||
virtual void Shutdown() override;
|
||||
|
||||
/**
|
||||
* @brief Method to execute the user-defined task periodically until ShutdownTask is called.
|
||||
* @param[in] uiPeriod The time period in milliseconds in which the task should executed.
|
||||
* @param[in] pTask Interface to the task object exposing the ITaskExecute interface. The object must be kept alive
|
||||
* until the timer has been destroyed.
|
||||
* @return Returns an interface to the task timer object. Use sdv::IObjectDestroy to terminate the timer.
|
||||
*/
|
||||
virtual sdv::IInterfaceAccess* CreateTimer(uint32_t uiPeriod, sdv::IInterfaceAccess* pTask) override;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Method to set the time which has past from the last simulation step.
|
||||
* @param[in] uiSimulationStep the time in microseconds which has past from the last simulation step.
|
||||
*/
|
||||
virtual void SimulationStep(/*in*/ uint64_t uiSimulationStep) override;
|
||||
|
||||
/**
|
||||
* @brief Remove the timer from from the timer map.
|
||||
* @param[in] pTimer Pointer to the timer object to remove.
|
||||
*/
|
||||
void RemoveTimer(CSimulationTimer* pTimer);
|
||||
|
||||
private:
|
||||
sdv::EObjectStatus m_eObjectStatus = sdv::EObjectStatus::initialization_pending; ///< Object operation status
|
||||
std::mutex m_mtxTasks; ///< Mutex for tasks
|
||||
std::map<CSimulationTimer*, std::unique_ptr<CSimulationTimer>> m_mapTasks; ///< Set to get the active tasks
|
||||
};
|
||||
|
||||
DEFINE_SDV_OBJECT(CSimulationTaskTimerService)
|
||||
|
||||
#endif // !define SIMULATION_TASK_TIMER_H
|
||||
186
sdv_services/task_timer/tasktimer.cpp
Normal file
186
sdv_services/task_timer/tasktimer.cpp
Normal file
@@ -0,0 +1,186 @@
|
||||
#include "tasktimer.h"
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
|
||||
CTimer::CTimer(CTaskTimerService& rtimersvc, uint32_t uiPeriod, sdv::core::ITaskExecute* pExecute) :
|
||||
m_rtimersvc(rtimersvc), m_pExecute(pExecute)
|
||||
{
|
||||
if (!pExecute) return;
|
||||
|
||||
#ifdef _WIN32
|
||||
// Use a lambda function without capture as function pointer.
|
||||
// NOTE: Casting is unsafe... but a necessity when working with Windows API functions.
|
||||
m_uiTimerID = timeSetEvent(uiPeriod, 0, [](UINT, UINT, DWORD_PTR dwUser, DWORD_PTR, DWORD_PTR)
|
||||
{
|
||||
CTimer* pTimer = reinterpret_cast<CTimer*>(dwUser);
|
||||
pTimer->ExecuteCallback();
|
||||
}, reinterpret_cast<DWORD_PTR>(this), TIME_PERIODIC | TIME_KILL_SYNCHRONOUS);
|
||||
#elif defined __unix__
|
||||
// Configure the signal event for timer expiration
|
||||
sigevent sev{};
|
||||
sev.sigev_notify = SIGEV_THREAD;
|
||||
sev.sigev_notify_function = [](sigval sv)
|
||||
{
|
||||
CTimer* pTimer = reinterpret_cast<CTimer*>(sv.sival_ptr);
|
||||
std::unique_lock<std::mutex> lock(pTimer->m_mtxExecution);
|
||||
if (!pTimer->m_bRunning) return;
|
||||
sdv::core::ITaskExecute* pExecuteLocal = reinterpret_cast<sdv::core::ITaskExecute*>(pTimer->m_pExecute);
|
||||
pExecuteLocal->Execute();
|
||||
};
|
||||
sev.sigev_value.sival_ptr = this;
|
||||
sev.sigev_notify_attributes = nullptr;
|
||||
|
||||
// Create the timer
|
||||
if (timer_create(CLOCK_MONOTONIC, &sev, &m_timerid) != 0)
|
||||
return;
|
||||
|
||||
//split up period in full seconds and remaining nanoseconds
|
||||
//find out how many full seconds period contains
|
||||
uint32_t uiFullSeconds = uiPeriod / 1000;
|
||||
//determine remainder in nanoseconds
|
||||
uint32_t uiRemainderNS = (uiPeriod % 1000)*1000000;
|
||||
|
||||
// Configure the timer
|
||||
itimerspec its{};
|
||||
its.it_value.tv_sec = uiFullSeconds;
|
||||
its.it_value.tv_nsec = uiRemainderNS;
|
||||
its.it_interval.tv_sec = uiFullSeconds;
|
||||
its.it_interval.tv_nsec = uiRemainderNS;
|
||||
|
||||
// Start the timer
|
||||
if (timer_settime(m_timerid, 0, &its, nullptr) != 0)
|
||||
{
|
||||
timer_delete(m_timerid);
|
||||
return;
|
||||
}
|
||||
|
||||
m_bRunning = true;
|
||||
#endif
|
||||
}
|
||||
|
||||
void CTimer::DestroyObject()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
// Terminate the timer
|
||||
if (m_uiTimerID)
|
||||
timeKillEvent(m_uiTimerID);
|
||||
m_uiTimerID = 0ul;
|
||||
#elif defined __unix__
|
||||
// Terminate the timer
|
||||
if (m_bRunning)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(m_mtxExecution);
|
||||
m_bRunning = false;
|
||||
timer_delete(m_timerid);
|
||||
m_timerid = 0ul;
|
||||
lock.unlock();
|
||||
|
||||
// Sleep 20ms to allow all outstanding tasks to end.
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(20));
|
||||
}
|
||||
#endif
|
||||
|
||||
// Delete the object
|
||||
m_rtimersvc.RemoveTimer(this);
|
||||
}
|
||||
|
||||
CTimer::operator bool() const
|
||||
{
|
||||
#ifdef _WIN32
|
||||
return m_uiTimerID ? true : false;
|
||||
#elif defined __unix__
|
||||
return m_bRunning;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
void CTimer::ExecuteCallback()
|
||||
{
|
||||
if (m_rtimersvc.GetStatus() != sdv::EObjectStatus::running) return;
|
||||
if (!m_pExecute) return;
|
||||
if (!m_bPrioritySet)
|
||||
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
|
||||
m_bPrioritySet = true;
|
||||
if (m_pExecute) m_pExecute->Execute();
|
||||
}
|
||||
#endif
|
||||
|
||||
CTaskTimerService::CTaskTimerService()
|
||||
{
|
||||
}
|
||||
|
||||
CTaskTimerService::~CTaskTimerService()
|
||||
{
|
||||
}
|
||||
|
||||
void CTaskTimerService::Initialize(/*in*/ const sdv::u8string& /*ssObjectConfig*/)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
m_eObjectStatus = sdv::EObjectStatus::initializing;
|
||||
SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
|
||||
|
||||
// set up time resolution and maybe some other things
|
||||
timeBeginPeriod(1);
|
||||
#endif
|
||||
|
||||
m_eObjectStatus = sdv::EObjectStatus::initialized;
|
||||
}
|
||||
|
||||
sdv::EObjectStatus CTaskTimerService::GetStatus() const
|
||||
{
|
||||
return m_eObjectStatus;
|
||||
}
|
||||
|
||||
void CTaskTimerService::SetOperationMode(/*in*/ sdv::EOperationMode eMode)
|
||||
{
|
||||
switch (eMode)
|
||||
{
|
||||
case sdv::EOperationMode::configuring:
|
||||
if (m_eObjectStatus == sdv::EObjectStatus::running || m_eObjectStatus == sdv::EObjectStatus::initialized)
|
||||
m_eObjectStatus = sdv::EObjectStatus::configuring;
|
||||
break;
|
||||
case sdv::EOperationMode::running:
|
||||
if (m_eObjectStatus == sdv::EObjectStatus::configuring || m_eObjectStatus == sdv::EObjectStatus::initialized)
|
||||
m_eObjectStatus = sdv::EObjectStatus::running;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void CTaskTimerService::Shutdown()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
m_eObjectStatus = sdv::EObjectStatus::shutdown_in_progress;
|
||||
timeEndPeriod(1);
|
||||
#endif
|
||||
m_eObjectStatus = sdv::EObjectStatus::destruction_pending;
|
||||
}
|
||||
|
||||
sdv::IInterfaceAccess* CTaskTimerService::CreateTimer(uint32_t uiPeriod, sdv::IInterfaceAccess* pTask)
|
||||
{
|
||||
if (m_eObjectStatus != sdv::EObjectStatus::configuring) return nullptr;
|
||||
|
||||
if (!uiPeriod) return nullptr;
|
||||
if (!pTask) return nullptr;
|
||||
sdv::core::ITaskExecute* pExecute = pTask->GetInterface<sdv::core::ITaskExecute>();
|
||||
if (!pExecute) return nullptr;
|
||||
|
||||
std::unique_lock<std::mutex> lock(m_mtxTasks);
|
||||
auto ptrTimer = std::make_unique<CTimer>(*this, uiPeriod, pExecute);
|
||||
// Ignore cppcheck warning; normally the returned pointer should always have a value at this stage (otherwise an
|
||||
// exception was triggered).
|
||||
// cppcheck-suppress knownConditionTrueFalse
|
||||
if (!ptrTimer)
|
||||
return nullptr;
|
||||
CTimer* pObject = ptrTimer.get();
|
||||
if (pObject)
|
||||
m_mapTasks.try_emplace(pObject, std::move(ptrTimer));
|
||||
return pObject;
|
||||
}
|
||||
|
||||
void CTaskTimerService::RemoveTimer(CTimer* pTimer)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(m_mtxTasks);
|
||||
m_mapTasks.erase(pTimer);
|
||||
}
|
||||
181
sdv_services/task_timer/tasktimer.h
Normal file
181
sdv_services/task_timer/tasktimer.h
Normal file
@@ -0,0 +1,181 @@
|
||||
/**
|
||||
* @file tasktimer.h
|
||||
* @author Sudipta Babu Durjoy FRD DISS21 (mailto:sudipta.durjoy@zf.com)
|
||||
* @brief
|
||||
* @version 1.0
|
||||
* @date 2023-05-24
|
||||
*
|
||||
* @copyright Copyright ZF Friedrichshafen AG (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef TASK_TIMER_H
|
||||
#define TASK_TIMER_H
|
||||
|
||||
#include <interfaces/core.h>
|
||||
#include <interfaces/timer.h>
|
||||
#include <support/interface_ptr.h>
|
||||
#include <support/component_impl.h>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <fstream>
|
||||
|
||||
#ifdef _WIN32
|
||||
// Resolve conflict
|
||||
#pragma push_macro("interface")
|
||||
#undef interface
|
||||
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
|
||||
#include <WinSock2.h>
|
||||
#include <Windows.h>
|
||||
#include <timeapi.h>
|
||||
|
||||
// Resolve conflict
|
||||
#pragma pop_macro("interface")
|
||||
#ifdef GetClassInfo
|
||||
#undef GetClassInfo
|
||||
#endif
|
||||
#elif defined __unix__
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <mutex>
|
||||
#include <time.h>
|
||||
#include <sys/times.h>
|
||||
#else
|
||||
#error OS is not supported!
|
||||
#endif
|
||||
|
||||
// Forward declaration
|
||||
class CTaskTimerService;
|
||||
|
||||
/**
|
||||
* @brief Timer object managing the lifetime of the timer.
|
||||
*/
|
||||
class CTimer : public sdv::IInterfaceAccess, public sdv::IObjectDestroy
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor
|
||||
* @param[in] rtimersvc Reference to the task timer service.
|
||||
* @param[in] uiPeriod The period of the task timer (must not be 0) in ms.
|
||||
* @param[in] pExecute Pointer to the interface containing the execution function.
|
||||
*/
|
||||
CTimer(CTaskTimerService& rtimersvc, uint32_t uiPeriod, sdv::core::ITaskExecute* pExecute);
|
||||
|
||||
// Interface map
|
||||
BEGIN_SDV_INTERFACE_MAP()
|
||||
SDV_INTERFACE_ENTRY(sdv::IObjectDestroy)
|
||||
END_SDV_INTERFACE_MAP()
|
||||
|
||||
/**
|
||||
* @brief Destroy the object. Overload of sdv::IObjectDestroy::DestroyObject.
|
||||
*/
|
||||
virtual void DestroyObject() override;
|
||||
|
||||
/**
|
||||
* @brief Operator returning info about the validity of the timer.
|
||||
*/
|
||||
operator bool() const;
|
||||
|
||||
private:
|
||||
#ifdef _WIN32
|
||||
/**
|
||||
* @brief Execution function called by the timer.
|
||||
*/
|
||||
void ExecuteCallback();
|
||||
#endif
|
||||
|
||||
sdv::CLifetimeCookie m_cookie = sdv::CreateLifetimeCookie(); ///< Lifetime cookie to manage the module lifetime.
|
||||
CTaskTimerService& m_rtimersvc; ///< Reference to the task timer service
|
||||
sdv::core::ITaskExecute* m_pExecute = nullptr; ///< Pointer to the execution callback interface.
|
||||
#ifdef _WIN32
|
||||
UINT m_uiTimerID = 0ul; ///< Timer ID
|
||||
bool m_bPrioritySet = false; ///< When set, the priority of the task was increased.
|
||||
#elif defined __unix__
|
||||
timer_t m_timerid = 0; ///< Timer ID.
|
||||
bool m_bRunning = false; ///< When set, the timer is running.
|
||||
std::mutex m_mtxExecution; ///< Prevent killing the timer when in execution.
|
||||
#endif
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Task timer class to execute task periodically
|
||||
*/
|
||||
class CTaskTimerService : public sdv::CSdvObject, public sdv::IObjectControl, public sdv::core::ITaskTimer
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor configure and initializes the timer.
|
||||
*/
|
||||
CTaskTimerService();
|
||||
|
||||
/**
|
||||
* @brief Destructor cleans up the timer if there is no active task and delete it after.
|
||||
*/
|
||||
virtual ~CTaskTimerService() override;
|
||||
|
||||
// Interface map
|
||||
BEGIN_SDV_INTERFACE_MAP()
|
||||
SDV_INTERFACE_ENTRY(sdv::IObjectControl)
|
||||
SDV_INTERFACE_ENTRY(sdv::core::ITaskTimer)
|
||||
END_SDV_INTERFACE_MAP()
|
||||
|
||||
// Object declarations
|
||||
DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::SystemObject)
|
||||
DECLARE_OBJECT_CLASS_NAME("TaskTimerService")
|
||||
DECLARE_OBJECT_SINGLETON()
|
||||
|
||||
/**
|
||||
* @brief Initialize the object. Overload of sdv::IObjectControl::Initialize.
|
||||
* @param[in] ssObjectConfig Optional configuration string.
|
||||
*/
|
||||
virtual void Initialize(/*in*/ const sdv::u8string& ssObjectConfig) override;
|
||||
|
||||
/**
|
||||
* @brief Get the current status of the object. Overload of sdv::IObjectControl::GetStatus.
|
||||
* @return Return the current status of the object.
|
||||
*/
|
||||
virtual sdv::EObjectStatus GetStatus() const override;
|
||||
|
||||
/**
|
||||
* @brief Set the component operation mode. Overload of sdv::IObjectControl::SetOperationMode.
|
||||
* @param[in] eMode The operation mode, the component should run in.
|
||||
*/
|
||||
virtual void SetOperationMode(/*in*/ sdv::EOperationMode eMode) override;
|
||||
|
||||
/**
|
||||
* @brief Shutdown called before the object is destroyed. Overload of sdv::IObjectControl::Shutdown.
|
||||
* @attention Implement calls to other SDV objects here as this is no longer considered safe during the destructor of the object!
|
||||
* After a call to shutdown any threads/callbacks/etc that could call other SDV objects need to have been stopped.
|
||||
* The SDV object itself is to remain in a state where it can respond to calls to its interfaces as other objects may still call it during the shutdown sequence!
|
||||
* Any subsequent call to GetStatus should return EObjectStatus::destruction_pending
|
||||
*/
|
||||
virtual void Shutdown() override;
|
||||
|
||||
/**
|
||||
* @brief Method to execute the user-defined task periodically until ShutdownTask is called.
|
||||
* @param[in] uiPeriod The time period in milliseconds in which the task should executed.
|
||||
* @param[in] pTask Interface to the task object exposing the ITaskExecute interface. The object must be kept alive
|
||||
* until the timer has been destroyed.
|
||||
* @return Returns an interface to the task timer object. Use sdv::IObjectDestroy to terminate the timer.
|
||||
*/
|
||||
virtual sdv::IInterfaceAccess* CreateTimer(uint32_t uiPeriod, sdv::IInterfaceAccess* pTask) override;
|
||||
|
||||
/**
|
||||
* @brief Remove the timer from from the timer map.
|
||||
* @param[in] pTimer Pointer to the timer object to remove.
|
||||
*/
|
||||
void RemoveTimer(CTimer* pTimer);
|
||||
|
||||
private:
|
||||
sdv::EObjectStatus m_eObjectStatus = sdv::EObjectStatus::initialization_pending; ///< Object operation status
|
||||
std::mutex m_mtxTasks; ///< Mutex for tasks
|
||||
std::map<CTimer*, std::unique_ptr<CTimer>> m_mapTasks; ///< Set to get the active tasks
|
||||
};
|
||||
|
||||
DEFINE_SDV_OBJECT(CTaskTimerService)
|
||||
|
||||
#endif // !define TASK_TIMER_H
|
||||
Reference in New Issue
Block a user