Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Nov 21, 2024
1 parent 4a3bd1c commit 15217fc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
6 changes: 4 additions & 2 deletions include/GatewayClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <string_view>
#include <unordered_map>

#include <freertos/task.h>

namespace OpenShock {
class GatewayClient {
DISABLE_COPY(GatewayClient);
Expand All @@ -27,14 +29,14 @@ namespace OpenShock {
bool sendMessageTXT(std::string_view data);
bool sendMessageBIN(const uint8_t* data, std::size_t length);

bool loop();

private:
void _loop();
void _setState(GatewayClientState state);
void _sendBootStatus();
void _handleEvent(WStype_t type, uint8_t* payload, std::size_t length);

WebSocketsClient m_webSocket;
GatewayClientState m_state;
TaskHandle_t m_loopTask;
};
} // namespace OpenShock
2 changes: 0 additions & 2 deletions include/GatewayConnectionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ namespace OpenShock::GatewayConnectionManager {

bool SendMessageTXT(std::string_view data);
bool SendMessageBIN(const uint8_t* data, std::size_t length);

void Update();
} // namespace OpenShock::GatewayConnectionManager
37 changes: 25 additions & 12 deletions src/GatewayClient.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <freertos/FreeRTOS.h>

#include "GatewayClient.h"

const char* const TAG = "GatewayClient";
Expand All @@ -11,6 +13,8 @@ const char* const TAG = "GatewayClient";
#include "serialization/WSGateway.h"
#include "Time.h"
#include "util/CertificateUtils.h"
#include "util/FnProxy.h"
#include "util/TaskUtils.h"
#include "VisualStateManager.h"

using namespace OpenShock;
Expand All @@ -20,6 +24,7 @@ static bool s_bootStatusSent = false;
GatewayClient::GatewayClient(const std::string& authToken)
: m_webSocket()
, m_state(GatewayClientState::Disconnected)
, m_loopTask(nullptr)
{
OS_LOGD(TAG, "Creating GatewayClient");

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

OS_LOGD(TAG, "Destroying GatewayClient");
if (m_loopTask != nullptr) {
vTaskDelete(m_loopTask);
}
m_webSocket.disconnect();
}

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

if (m_loopTask != nullptr) {
vTaskDelete(m_loopTask);
}

esp_err_t err;
err = TaskUtils::TaskCreateExpensive(&Util::FnProxy<&GatewayClient::_loop>, "GatewayClientLoop", 8192, this, 1, &m_loopTask); // TODO: Profile stack size
if (err != ESP_OK) {
OS_LOGE(TAG, "Failed to create GatewayClient loop task: %s", esp_err_to_name(err));
return;
}

_setState(GatewayClientState::Connecting);

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

bool GatewayClient::loop()
void GatewayClient::_loop()
{
if (m_state == GatewayClientState::Disconnected) {
return false;
}

m_webSocket.loop();

// We are still in the process of connecting or disconnecting
if (m_state != GatewayClientState::Connected) {
// return true to indicate that we are still busy
return true;
while (m_state != GatewayClientState::Disconnected) {
m_webSocket.loop();
vTaskDelay(pdMS_TO_TICKS(10)); // 100 Hz update rate
}

return true;
m_loopTask = nullptr; // Clear the task handle
vTaskDelete(nullptr); // Delete the task
}

void GatewayClient::_setState(GatewayClientState state)
Expand Down
12 changes: 0 additions & 12 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,8 @@ void setup()
}
}

void main_app(void* arg)
{
while (true) {
OpenShock::GatewayConnectionManager::Update();

vTaskDelay(5); // 5 ticks update interval
}
}

void loop()
{
// Start the main task
OpenShock::TaskUtils::TaskCreateExpensive(main_app, "main_app", 8192, nullptr, 1, nullptr); // PROFILED: 6KB stack usage

// Kill the loop task (Arduino is stinky)
vTaskDelete(nullptr);
}

1 comment on commit 15217fc

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cpp-Linter Report ⚠️

Some files did not pass the configured checks!

clang-format (v18.1.8) reports: 1 file(s) not formatted
  • src/GatewayClient.cpp

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.