Friendly LWM2M client
WppTaskQueue.h
Go to the documentation of this file.
1 /*
2  * WppManager.h
3  *
4  * Created on: 20 Jul 2023
5  * Author: valentin
6  */
7 
8 #ifndef WPP_TASK_QUEUE_H_
9 #define WPP_TASK_QUEUE_H_
10 
11 #include <list>
12 #include <functional>
13 
14 #include "WppTypes.h"
15 #include "WppGuard.h"
16 
17 #define WPP_TASK_MIN_DELAY_S (time_t)0
18 #define WPP_TASK_DEF_DELAY_S (time_t)1
19 #define WPP_TASK_MAX_DELAY_S (time_t)(0xFFFFFFF)
20 
21 #define WPP_TASK_DEF_CTX NULL
22 #define WPP_ERR_TASK_ID 0
23 
24 namespace wpp {
25 
26 class WppClient;
27 
50 class WppTaskQueue {
51 public:
52  using task_id_t = uint32_t;
61  using task_t = std::function<bool(WppClient&, void *)>;
62 
63 private:
64  enum TaskState : uint8_t {
65  IDLE = 1,
66  EXECUTING = 2,
67  SHOULD_BE_DELETED = 4,
68  MAX_TASK_STATE = 0xFF
69  };
70 
71  struct TaskInfo {
72  task_id_t id;
73  task_t task;
74  time_t delaySec;
75  time_t nextCallTime;
76  void *ctx = NULL;
77  size_t ctxSize = 0;
78  TaskState state;
79  };
80 
81 private:
82  WppTaskQueue();
83 
84 public:
85  ~WppTaskQueue();
86 
87  /* ------------- Tasks management ------------- */
99  static task_id_t addTask(time_t delaySec, task_t task);
100 
115  static task_id_t addTask(void *ctx, time_t delaySec, task_t task);
116 
131  static task_id_t addTaskWithCopy(const void *ctx, size_t size, time_t delaySec, task_t task);
132 
139  static size_t getTaskCnt();
140 
144  static bool isTaskExist(task_id_t id);
145 
149  static bool isTaskIdle(task_id_t id);
150  static bool isTaskExecuting(task_id_t id);
151  static bool isTaskShouldBeDeleted(task_id_t id);
152 
169  static void requestToRemoveTask(task_id_t id);
170 
187  static void requestToRemoveEachTask();
188 
193  static void hardReset();
194 
202  static time_t handleEachTask(WppClient& client);
203 
204 private:
208  void deleteFinishedTasks();
209 
213  time_t updateNextCallTimeForTasks();
214 
218  task_id_t getNextTaskId();
219 
220 private:
221  static WppGuard _handleTaskGuard;
222  static WppGuard _taskQueueGuard;
223  static WppTaskQueue _instance;
227  task_id_t _nextTaskId;
234  std::list<TaskInfo *> _tasks;
235 };
236 
237 } /* namespace wpp */
238 
239 #endif /* WPP_TASK_QUEUE_H_ */
Represents a client interface for Wpp library.
Definition: WppClient.h:37
Represents a task queue.
Definition: WppTaskQueue.h:50
std::function< bool(WppClient &, void *)> task_t
Definition: WppTaskQueue.h:61
static time_t handleEachTask(WppClient &client)
Execute each task in the queue and delete it from queue if task returns false or task state is SHOULD...
static bool isTaskShouldBeDeleted(task_id_t id)
static size_t getTaskCnt()
Returns count of tasks in the queue. Tasks count does not immediately updated after request to remove...
static void requestToRemoveEachTask()
This function does not immediately delete all tasks, it only marks them as one that should be deleted...
static bool isTaskExist(task_id_t id)
Returns true if task exists in the queue.
uint32_t task_id_t
Definition: WppTaskQueue.h:52
static bool isTaskExecuting(task_id_t id)
static task_id_t addTask(time_t delaySec, task_t task)
Add task to queue, ctx that passed to task equals to NULL.
static bool isTaskIdle(task_id_t id)
Returns true if state corresponds to function.
static task_id_t addTaskWithCopy(const void *ctx, size_t size, time_t delaySec, task_t task)
Add task to queue, ctx passed to task by pointer with copy, allocated memory will be relesed after de...
static void requestToRemoveTask(task_id_t id)
This function does not immediately delete the task, it only marks it as one that should be deleted at...
static void hardReset()
Blocks task handling, calls of other methods, and deletes all tasks from the queue,...
The WppConnection class represents a connection interface for the Wpp library.
Definition: WppClient.cpp:14