Skip to content

Commit 15217fc

Browse files
committed
Initial commit
1 parent 4a3bd1c commit 15217fc

File tree

4 files changed

+29
-28
lines changed

4 files changed

+29
-28
lines changed

include/GatewayClient.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <string_view>
1111
#include <unordered_map>
1212

13+
#include <freertos/task.h>
14+
1315
namespace OpenShock {
1416
class GatewayClient {
1517
DISABLE_COPY(GatewayClient);
@@ -27,14 +29,14 @@ namespace OpenShock {
2729
bool sendMessageTXT(std::string_view data);
2830
bool sendMessageBIN(const uint8_t* data, std::size_t length);
2931

30-
bool loop();
31-
3232
private:
33+
void _loop();
3334
void _setState(GatewayClientState state);
3435
void _sendBootStatus();
3536
void _handleEvent(WStype_t type, uint8_t* payload, std::size_t length);
3637

3738
WebSocketsClient m_webSocket;
3839
GatewayClientState m_state;
40+
TaskHandle_t m_loopTask;
3941
};
4042
} // namespace OpenShock

include/GatewayConnectionManager.h

-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,4 @@ namespace OpenShock::GatewayConnectionManager {
1717

1818
bool SendMessageTXT(std::string_view data);
1919
bool SendMessageBIN(const uint8_t* data, std::size_t length);
20-
21-
void Update();
2220
} // namespace OpenShock::GatewayConnectionManager

src/GatewayClient.cpp

+25-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <freertos/FreeRTOS.h>
2+
13
#include "GatewayClient.h"
24

35
const char* const TAG = "GatewayClient";
@@ -11,6 +13,8 @@ const char* const TAG = "GatewayClient";
1113
#include "serialization/WSGateway.h"
1214
#include "Time.h"
1315
#include "util/CertificateUtils.h"
16+
#include "util/FnProxy.h"
17+
#include "util/TaskUtils.h"
1418
#include "VisualStateManager.h"
1519

1620
using namespace OpenShock;
@@ -20,6 +24,7 @@ static bool s_bootStatusSent = false;
2024
GatewayClient::GatewayClient(const std::string& authToken)
2125
: m_webSocket()
2226
, m_state(GatewayClientState::Disconnected)
27+
, m_loopTask(nullptr)
2328
{
2429
OS_LOGD(TAG, "Creating GatewayClient");
2530

@@ -36,6 +41,9 @@ GatewayClient::~GatewayClient()
3641
_setState(GatewayClientState::Disconnected);
3742

3843
OS_LOGD(TAG, "Destroying GatewayClient");
44+
if (m_loopTask != nullptr) {
45+
vTaskDelete(m_loopTask);
46+
}
3947
m_webSocket.disconnect();
4048
}
4149

@@ -45,6 +53,17 @@ void GatewayClient::connect(const char* lcgFqdn)
4553
return;
4654
}
4755

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+
4867
_setState(GatewayClientState::Connecting);
4968

5069
//
@@ -91,21 +110,15 @@ bool GatewayClient::sendMessageBIN(const uint8_t* data, std::size_t length)
91110
return m_webSocket.sendBIN(data, length);
92111
}
93112

94-
bool GatewayClient::loop()
113+
void GatewayClient::_loop()
95114
{
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
106118
}
107119

108-
return true;
120+
m_loopTask = nullptr; // Clear the task handle
121+
vTaskDelete(nullptr); // Delete the task
109122
}
110123

111124
void GatewayClient::_setState(GatewayClientState state)

src/main.cpp

-12
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,8 @@ void setup()
111111
}
112112
}
113113

114-
void main_app(void* arg)
115-
{
116-
while (true) {
117-
OpenShock::GatewayConnectionManager::Update();
118-
119-
vTaskDelay(5); // 5 ticks update interval
120-
}
121-
}
122-
123114
void loop()
124115
{
125-
// Start the main task
126-
OpenShock::TaskUtils::TaskCreateExpensive(main_app, "main_app", 8192, nullptr, 1, nullptr); // PROFILED: 6KB stack usage
127-
128116
// Kill the loop task (Arduino is stinky)
129117
vTaskDelete(nullptr);
130118
}

0 commit comments

Comments
 (0)