1
+ #include < freertos/FreeRTOS.h>
2
+
1
3
#include " GatewayClient.h"
2
4
3
5
const char * const TAG = " GatewayClient" ;
@@ -11,6 +13,8 @@ const char* const TAG = "GatewayClient";
11
13
#include " serialization/WSGateway.h"
12
14
#include " Time.h"
13
15
#include " util/CertificateUtils.h"
16
+ #include " util/FnProxy.h"
17
+ #include " util/TaskUtils.h"
14
18
#include " VisualStateManager.h"
15
19
16
20
using namespace OpenShock ;
@@ -20,6 +24,7 @@ static bool s_bootStatusSent = false;
20
24
GatewayClient::GatewayClient (const std::string& authToken)
21
25
: m_webSocket()
22
26
, m_state(GatewayClientState::Disconnected)
27
+ , m_loopTask(nullptr )
23
28
{
24
29
OS_LOGD (TAG, " Creating GatewayClient" );
25
30
@@ -36,6 +41,9 @@ GatewayClient::~GatewayClient()
36
41
_setState (GatewayClientState::Disconnected);
37
42
38
43
OS_LOGD (TAG, " Destroying GatewayClient" );
44
+ if (m_loopTask != nullptr ) {
45
+ vTaskDelete (m_loopTask);
46
+ }
39
47
m_webSocket.disconnect ();
40
48
}
41
49
@@ -45,6 +53,17 @@ void GatewayClient::connect(const char* lcgFqdn)
45
53
return ;
46
54
}
47
55
56
+ if (m_loopTask != nullptr ) {
57
+ vTaskDelete (m_loopTask);
58
+ }
59
+
60
+ esp_err_t err;
61
+ err = TaskUtils::TaskCreateExpensive (&Util::FnProxy<&GatewayClient::_loop>, " GatewayClientLoop" , 8192 , this , 1 , &m_loopTask); // TODO: Profile stack size
62
+ if (err != ESP_OK) {
63
+ OS_LOGE (TAG, " Failed to create GatewayClient loop task: %s" , esp_err_to_name (err));
64
+ return ;
65
+ }
66
+
48
67
_setState (GatewayClientState::Connecting);
49
68
50
69
//
@@ -91,21 +110,15 @@ bool GatewayClient::sendMessageBIN(const uint8_t* data, std::size_t length)
91
110
return m_webSocket.sendBIN (data, length);
92
111
}
93
112
94
- bool GatewayClient::loop ()
113
+ void GatewayClient::_loop ()
95
114
{
96
- if (m_state == GatewayClientState::Disconnected) {
97
- return false ;
98
- }
99
-
100
- m_webSocket.loop ();
101
-
102
- // We are still in the process of connecting or disconnecting
103
- if (m_state != GatewayClientState::Connected) {
104
- // return true to indicate that we are still busy
105
- return true ;
115
+ while (m_state != GatewayClientState::Disconnected) {
116
+ m_webSocket.loop ();
117
+ vTaskDelay (pdMS_TO_TICKS (10 )); // 100 Hz update rate
106
118
}
107
119
108
- return true ;
120
+ m_loopTask = nullptr ; // Clear the task handle
121
+ vTaskDelete (nullptr ); // Delete the task
109
122
}
110
123
111
124
void GatewayClient::_setState (GatewayClientState state)
0 commit comments