Files
MuditaOS/module-sys/Service/Worker.hpp
2020-07-23 23:02:05 +02:00

90 lines
2.8 KiB
C++

#pragma once
#include "Service.hpp"
#include <memory>
#include <string>
namespace sys
{
class WorkerQueueInfo
{
public:
std::string name;
int elementSize;
int length;
};
struct WorkerCommand
{
uint32_t command = 0;
uint32_t *data = NULL;
};
/*
* @brief Worker is a wrapper for freeRTOS task used to separate sysmanager environment from
* the rest of the system. Its purpose is to handle asynchronous events like IRQ or timers.
* Flow of creating worker is as follows:
* - create new Worker object and provide pointer to the service that owns the worker,
* - call init method and provide list of parameters to create queues. Those queues can be later
* used to wake up the worker.
* - call run method to start the worker.
* Flow for closing the worker is as follows:
* - call stop method - task will send a close confirmation to the service after exiting its main loop
* - call deinit to destroy all queues ued by the worker
* - delete the object.
*
*/
class Worker
{
protected:
const static uint32_t SERVICE_QUEUE_LENGTH = 10;
const static uint32_t SERVICE_QUEUE_SIZE = sizeof(WorkerCommand);
const std::string SERVICE_QUEUE_NAME = "ServiceQueue";
sys::Service *service;
// queue used by service to send commands to service.
xQueueHandle serviceQueue;
std::vector<xQueueHandle> queues;
QueueSetHandle_t queueSet;
std::map<xQueueHandle, std::string> queueNameMap;
xTaskHandle taskHandle;
friend void workerTaskFunction(void *ptr);
public:
Worker(sys::Service *service);
virtual ~Worker() = default;
/**
* This function is responsible for creating all queues provided in the constructor.
* When all queues are created this method creates set of queues.
*/
virtual bool init(std::list<WorkerQueueInfo> queues = std::list<WorkerQueueInfo>());
virtual bool deinit();
/**
* This method starts RTOS thread that waits for incoming queue events.
*/
virtual bool run();
/**
* Sends stop command to worker.
*/
virtual bool stop();
/**
* This method is called from thread when new message arrives in queue.
* @param queueID Index of the queue in the queues vector.
*/
virtual bool handleMessage(uint32_t queueID);
/**
* @brief Sends command and pointer to data to worker
*/
virtual bool send(uint32_t cmd, uint32_t *data);
/**
* @brief Returns handle to queue by its name
*/
virtual xQueueHandle getQueueByName(std::string);
};
} /* namespace sys */