mirror of
https://github.com/mudita/MuditaOS.git
synced 2026-01-25 22:40:26 -05:00
110 lines
2.7 KiB
C++
110 lines
2.7 KiB
C++
#pragma once
|
|
|
|
#include "Common.hpp"
|
|
#include "Message.hpp"
|
|
#include "Mailbox.hpp"
|
|
#include "Bus.hpp"
|
|
#include <memory>
|
|
#include <queue>
|
|
#include <vector>
|
|
#include <string>
|
|
#include <algorithm>
|
|
#include <stdbool.h>
|
|
#include "thread.hpp"
|
|
#include "timer.hpp"
|
|
|
|
namespace sys
|
|
{
|
|
|
|
class ServiceTimer : public Timer
|
|
{
|
|
public:
|
|
ServiceTimer(const std::string &name, TickType_t tick, bool isPeriodic, uint32_t idx, Service *service);
|
|
|
|
uint32_t GetId() const
|
|
{
|
|
return m_id;
|
|
}
|
|
static uint32_t GetNextUniqueID()
|
|
{
|
|
return ++m_timers_unique_idx;
|
|
}
|
|
|
|
void Run() override;
|
|
|
|
private:
|
|
bool m_isPeriodic;
|
|
TickType_t m_interval;
|
|
uint32_t m_id;
|
|
Service *m_service;
|
|
static uint32_t m_timers_unique_idx;
|
|
};
|
|
|
|
class Service : public cpp_freertos::Thread, public std::enable_shared_from_this<Service>
|
|
{
|
|
public:
|
|
Service(std::string name,
|
|
std::string parent = "",
|
|
uint32_t stackDepth = 4096,
|
|
ServicePriority priority = ServicePriority::Idle);
|
|
|
|
virtual ~Service();
|
|
|
|
void StartService();
|
|
|
|
// Create service timer
|
|
uint32_t CreateTimer(TickType_t interval, bool isPeriodic, const std::string &name = "");
|
|
// Reload service timer
|
|
void ReloadTimer(uint32_t id);
|
|
// Delete timer
|
|
void DeleteTimer(uint32_t id);
|
|
void setTimerPeriod(uint32_t id, uint32_t period);
|
|
/**
|
|
* @brief Stops a timer with specified ID
|
|
* @param id ID of the timer;
|
|
*/
|
|
void stopTimer(uint32_t id);
|
|
|
|
// Invoked when service received data message
|
|
virtual Message_t DataReceivedHandler(DataMessage *msg, ResponseMessage *resp) = 0;
|
|
|
|
// Invoked when timer ticked
|
|
virtual void TickHandler(uint32_t id){};
|
|
|
|
// Invoked during initialization
|
|
virtual ReturnCodes InitHandler() = 0;
|
|
|
|
// Invoked upon receiving close request
|
|
virtual ReturnCodes DeinitHandler() = 0;
|
|
|
|
virtual ReturnCodes SwitchPowerModeHandler(const ServicePowerMode mode) = 0;
|
|
|
|
void CloseHandler();
|
|
|
|
std::string parent;
|
|
|
|
std::vector<BusChannels> busChannels;
|
|
|
|
Mailbox<std::shared_ptr<Message>> mailbox;
|
|
|
|
uint32_t pingTimestamp;
|
|
|
|
bool isReady;
|
|
|
|
std::vector<std::pair<uint64_t, uint32_t>> staleUniqueMsg;
|
|
|
|
protected:
|
|
bool enableRunLoop;
|
|
|
|
// TODO R.B. I've removed this private tag to be able to create application class
|
|
// private:
|
|
|
|
virtual void Run();
|
|
|
|
std::vector<std::unique_ptr<ServiceTimer>> timersList;
|
|
|
|
friend class ServiceTimer;
|
|
};
|
|
|
|
} // namespace sys
|