Skip to content

Commit a210a70

Browse files
adding the possibility to schedule execution of tasks
1 parent cdd7dcd commit a210a70

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

UNOR4USBBridge/at_handler.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ CClientWrapper CAtHandler::getClient(int sock) {
6969
void CAtHandler::run() {
7070
/* -------------------------------------------------------------------------- */
7171
at_srv.run();
72+
73+
// execute all the pending tasks
74+
75+
// it is intended for the tasks to add other tasks in queue after being executed,
76+
// saving the current size of the queue
77+
auto size = tasks.size();
78+
for(int i=0; i<size; i++) {
79+
auto task = tasks.front();
80+
tasks.pop();
81+
if(task) {
82+
task();
83+
}
84+
}
85+
7286
vTaskDelay(1);
7387
}
7488

UNOR4USBBridge/at_handler.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "chAT.hpp"
55
#include "WiFi.h"
66
#include "Server.h"
7+
#include <queue>
78

89
#include "WiFiClient.h"
910
#include <WiFiClientSecure.h>
@@ -66,6 +67,10 @@ class CAtHandler {
6667
std::vector<std::uint8_t> clients_ca[MAX_CLIENT_AVAILABLE];
6768
std::vector<std::uint8_t> clients_cert_pem[MAX_CLIENT_AVAILABLE];
6869
std::vector<std::uint8_t> clients_key_pem[MAX_CLIENT_AVAILABLE];
70+
71+
// this vector is a list of function to be called in the run function
72+
std::queue<std::function<void()>> tasks;
73+
6974
int udps_num = 0;
7075
int servers_num = 0;
7176
int clientsToServer_num = 0;
@@ -90,6 +95,10 @@ class CAtHandler {
9095
void add_cmds_preferences();
9196
void add_cmds_se();
9297
public:
98+
inline void addTask(std::function<void()> task) {
99+
tasks.push(task);
100+
}
101+
93102
/* Used by cmds_se */
94103
std::vector<std::uint8_t> se_buf;
95104

0 commit comments

Comments
 (0)