Skip to content

Commit

Permalink
Move event_handlers to message_handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Oct 25, 2024
1 parent b1d1cb8 commit 868fed6
Show file tree
Hide file tree
Showing 21 changed files with 80 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <cstdint>

namespace OpenShock::EventHandlers::WebSocket {
namespace OpenShock::MessageHandlers::WebSocket {
void HandleGatewayBinary(const uint8_t* data, std::size_t len);
void HandleLocalBinary(uint8_t socketId, const uint8_t* data, std::size_t len);
}
File renamed without changes.
File renamed without changes.
31 changes: 20 additions & 11 deletions src/CaptivePortalInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
const char* const TAG = "CaptivePortalInstance";

#include "CommandHandler.h"
#include "event_handlers/WebSocket.h"
#include "GatewayConnectionManager.h"
#include "Logging.h"
#include "message_handlers/WebSocket.h"
#include "serialization/WSLocal.h"
#include "util/FnProxy.h"
#include "util/HexUtils.h"
Expand All @@ -29,7 +29,8 @@ const uint32_t WEBSOCKET_UPDATE_INTERVAL = 10; // 10ms / 100Hz

using namespace OpenShock;

const esp_partition_t* _getStaticPartition() {
const esp_partition_t* _getStaticPartition()
{
const esp_partition_t* partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, "static0");
if (partition != nullptr) {
return partition;
Expand All @@ -43,7 +44,8 @@ const esp_partition_t* _getStaticPartition() {
return nullptr;
}

const char* _getPartitionHash() {
const char* _getPartitionHash()
{
const esp_partition_t* partition = _getStaticPartition();
if (partition == nullptr) {
return nullptr;
Expand All @@ -63,7 +65,8 @@ CaptivePortalInstance::CaptivePortalInstance()
, m_socketDeFragger(std::bind(&CaptivePortalInstance::handleWebSocketEvent, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4))
, m_fileSystem()
, m_dnsServer()
, m_taskHandle(nullptr) {
, m_taskHandle(nullptr)
{
m_socketServer.onEvent(std::bind(&WebSocketDeFragger::handler, &m_socketDeFragger, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
m_socketServer.begin();
m_socketServer.enableHeartbeat(WEBSOCKET_PING_INTERVAL, WEBSOCKET_PING_TIMEOUT, WEBSOCKET_PING_RETRIES);
Expand Down Expand Up @@ -130,7 +133,8 @@ discord.gg/OpenShock
}
}

CaptivePortalInstance::~CaptivePortalInstance() {
CaptivePortalInstance::~CaptivePortalInstance()
{
if (m_taskHandle != nullptr) {
vTaskDelete(m_taskHandle);
m_taskHandle = nullptr;
Expand All @@ -141,15 +145,17 @@ CaptivePortalInstance::~CaptivePortalInstance() {
m_dnsServer.stop();
}

void CaptivePortalInstance::task() {
void CaptivePortalInstance::task()
{
while (true) {
m_socketServer.loop();
// instance->m_dnsServer.processNextRequest();
vTaskDelay(pdMS_TO_TICKS(WEBSOCKET_UPDATE_INTERVAL));
}
}

void CaptivePortalInstance::handleWebSocketClientConnected(uint8_t socketId) {
void CaptivePortalInstance::handleWebSocketClientConnected(uint8_t socketId)
{
OS_LOGD(TAG, "WebSocket client #%u connected from %s", socketId, m_socketServer.remoteIP(socketId).toString().c_str());

WiFiNetwork connectedNetwork;
Expand All @@ -166,15 +172,18 @@ void CaptivePortalInstance::handleWebSocketClientConnected(uint8_t socketId) {
Serialization::Local::SerializeWiFiNetworksEvent(Serialization::Types::WifiNetworkEventType::Discovered, networks, std::bind(&CaptivePortalInstance::sendMessageBIN, this, socketId, std::placeholders::_1, std::placeholders::_2));
}

void CaptivePortalInstance::handleWebSocketClientDisconnected(uint8_t socketId) {
void CaptivePortalInstance::handleWebSocketClientDisconnected(uint8_t socketId)
{
OS_LOGD(TAG, "WebSocket client #%u disconnected", socketId);
}

void CaptivePortalInstance::handleWebSocketClientError(uint8_t socketId, uint16_t code, const char* message) {
void CaptivePortalInstance::handleWebSocketClientError(uint8_t socketId, uint16_t code, const char* message)
{
OS_LOGE(TAG, "WebSocket client #%u error %u: %s", socketId, code, message);
}

void CaptivePortalInstance::handleWebSocketEvent(uint8_t socketId, WebSocketMessageType type, const uint8_t* payload, std::size_t length) {
void CaptivePortalInstance::handleWebSocketEvent(uint8_t socketId, WebSocketMessageType type, const uint8_t* payload, std::size_t length)
{
switch (type) {
case WebSocketMessageType::Connected:
handleWebSocketClientConnected(socketId);
Expand All @@ -186,7 +195,7 @@ void CaptivePortalInstance::handleWebSocketEvent(uint8_t socketId, WebSocketMess
OS_LOGE(TAG, "Message type is not supported");
break;
case WebSocketMessageType::Binary:
EventHandlers::WebSocket::HandleLocalBinary(socketId, payload, length);
MessageHandlers::WebSocket::HandleLocalBinary(socketId, payload, length);
break;
case WebSocketMessageType::Error:
handleWebSocketClientError(socketId, length, reinterpret_cast<const char*>(payload));
Expand Down
4 changes: 2 additions & 2 deletions src/GatewayClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const char* const TAG = "GatewayClient";

#include "Common.h"
#include "config/Config.h"
#include "event_handlers/WebSocket.h"
#include "events/Events.h"
#include "Logging.h"
#include "message_handlers/WebSocket.h"
#include "OtaUpdateManager.h"
#include "serialization/WSGateway.h"
#include "Time.h"
Expand Down Expand Up @@ -203,7 +203,7 @@ void GatewayClient::_handleEvent(WStype_t type, uint8_t* payload, std::size_t le
OS_LOGV(TAG, "Received pong from API");
break;
case WStype_BIN:
EventHandlers::WebSocket::HandleGatewayBinary(payload, length);
MessageHandlers::WebSocket::HandleGatewayBinary(payload, length);
break;
case WStype_FRAGMENT_BIN_START:
OS_LOGE(TAG, "Received binary fragment start from API, this is not supported!");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "event_handlers/WebSocket.h"
#include "message_handlers/WebSocket.h"

const char* const TAG = "ServerMessageHandlers";

#include "event_handlers/impl/WSGateway.h"
#include "message_handlers/impl/WSGateway.h"

#include "Logging.h"

Expand Down Expand Up @@ -34,7 +34,8 @@ static std::array<Handlers::HandlerType, HANDLER_COUNT> s_serverHandlers = []()
return handlers;
}();

void EventHandlers::WebSocket::HandleGatewayBinary(const uint8_t* data, std::size_t len) {
void MessageHandlers::WebSocket::HandleGatewayBinary(const uint8_t* data, std::size_t len)
{
// Deserialize
auto msg = flatbuffers::GetRoot<Schemas::GatewayToHubMessage>(data);
if (msg == nullptr) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "event_handlers/WebSocket.h"
#include "message_handlers/WebSocket.h"

const char* const TAG = "LocalMessageHandlers";

#include "event_handlers/impl/WSLocal.h"
#include "Logging.h"
#include "message_handlers/impl/WSLocal.h"

#include "serialization/_fbs/LocalToHubMessage_generated.h"

Expand Down Expand Up @@ -39,7 +39,8 @@ static std::array<Handlers::HandlerType, HANDLER_COUNT> s_localHandlers = []() {
return handlers;
}();

void EventHandlers::WebSocket::HandleLocalBinary(uint8_t socketId, const uint8_t* data, std::size_t len) {
void MessageHandlers::WebSocket::HandleLocalBinary(uint8_t socketId, const uint8_t* data, std::size_t len)
{
// Deserialize
auto msg = flatbuffers::GetRoot<Schemas::LocalToHubMessage>(data);
if (msg == nullptr) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "event_handlers/impl/WSGateway.h"
#include "message_handlers/impl/WSGateway.h"

const char* const TAG = "ServerMessageHandlers";

Expand All @@ -9,7 +9,8 @@ const char* const TAG = "ServerMessageHandlers";

using namespace OpenShock::MessageHandlers::Server;

void _Private::HandleCaptivePortalConfig(const OpenShock::Serialization::Gateway::GatewayToHubMessage* root) {
void _Private::HandleCaptivePortalConfig(const OpenShock::Serialization::Gateway::GatewayToHubMessage* root)
{
auto msg = root->payload_as_CaptivePortalConfig();
if (msg == nullptr) {
OS_LOGE(TAG, "Payload cannot be parsed as CaptivePortalConfig");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "event_handlers/impl/WSGateway.h"
#include "message_handlers/impl/WSGateway.h"

const char* const TAG = "ServerMessageHandlers";

Expand All @@ -10,7 +10,8 @@ const char* const TAG = "ServerMessageHandlers";

using namespace OpenShock::MessageHandlers::Server;

void _Private::HandleOtaInstall(const OpenShock::Serialization::Gateway::GatewayToHubMessage* root) {
void _Private::HandleOtaInstall(const OpenShock::Serialization::Gateway::GatewayToHubMessage* root)
{
auto msg = root->payload_as_OtaInstall();
if (msg == nullptr) {
OS_LOGE(TAG, "Payload cannot be parsed as OtaInstall");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "event_handlers/impl/WSGateway.h"
#include "message_handlers/impl/WSGateway.h"

const char* const TAG = "ServerMessageHandlers";

Expand All @@ -10,7 +10,8 @@ const char* const TAG = "ServerMessageHandlers";

using namespace OpenShock::MessageHandlers::Server;

void _Private::HandleShockerCommandList(const OpenShock::Serialization::Gateway::GatewayToHubMessage* root) {
void _Private::HandleShockerCommandList(const OpenShock::Serialization::Gateway::GatewayToHubMessage* root)
{
auto msg = root->payload_as_ShockerCommandList();
if (msg == nullptr) {
OS_LOGE(TAG, "Payload cannot be parsed as ShockerCommandList");
Expand All @@ -26,9 +27,9 @@ void _Private::HandleShockerCommandList(const OpenShock::Serialization::Gateway:
OS_LOGV(TAG, "Received command list from API (%u commands)", commands->size());

for (auto command : *commands) {
uint16_t id = command->id();
uint8_t intensity = command->intensity();
uint16_t durationMs = command->duration();
uint16_t id = command->id();
uint8_t intensity = command->intensity();
uint16_t durationMs = command->duration();
OpenShock::ShockerModelType model = command->model();
OpenShock::ShockerCommandType type = command->type();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include "event_handlers/impl/WSGateway.h"
#include "message_handlers/impl/WSGateway.h"

const char* const TAG = "ServerMessageHandlers";

#include "Logging.h"

using namespace OpenShock::MessageHandlers::Server;

void _Private::HandleInvalidMessage(const OpenShock::Serialization::Gateway::GatewayToHubMessage* root) {
void _Private::HandleInvalidMessage(const OpenShock::Serialization::Gateway::GatewayToHubMessage* root)
{
if (root == nullptr) {
OS_LOGE(TAG, "Message cannot be parsed");
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "event_handlers/impl/WSLocal.h"
#include "message_handlers/impl/WSLocal.h"

const char* const TAG = "LocalMessageHandlers";

Expand All @@ -8,7 +8,8 @@ const char* const TAG = "LocalMessageHandlers";

#include <cstdint>

void serializeAccountLinkCommandResult(uint8_t socketId, OpenShock::Serialization::Local::AccountLinkResultCode result) {
void serializeAccountLinkCommandResult(uint8_t socketId, OpenShock::Serialization::Local::AccountLinkResultCode result)
{
flatbuffers::FlatBufferBuilder builder(1024); // TODO: Determine a good size

auto responseOffset = OpenShock::Serialization::Local::CreateAccountLinkCommandResult(builder, result);
Expand All @@ -25,7 +26,8 @@ void serializeAccountLinkCommandResult(uint8_t socketId, OpenShock::Serializatio

using namespace OpenShock::MessageHandlers::Local;

void _Private::HandleAccountLinkCommand(uint8_t socketId, const OpenShock::Serialization::Local::LocalToHubMessage* root) {
void _Private::HandleAccountLinkCommand(uint8_t socketId, const OpenShock::Serialization::Local::LocalToHubMessage* root)
{
auto msg = root->payload_as_AccountLinkCommand();
if (msg == nullptr) {
OS_LOGE(TAG, "Payload cannot be parsed as AccountLinkCommand");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "event_handlers/impl/WSLocal.h"
#include "message_handlers/impl/WSLocal.h"

const char* const TAG = "LocalMessageHandlers";

Expand All @@ -9,7 +9,8 @@ const char* const TAG = "LocalMessageHandlers";

using namespace OpenShock::MessageHandlers::Local;

void _Private::HandleAccountUnlinkCommand(uint8_t socketId, const OpenShock::Serialization::Local::LocalToHubMessage* root) {
void _Private::HandleAccountUnlinkCommand(uint8_t socketId, const OpenShock::Serialization::Local::LocalToHubMessage* root)
{
(void)socketId;

auto msg = root->payload_as_AccountUnlinkCommand();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "event_handlers/impl/WSLocal.h"
#include "message_handlers/impl/WSLocal.h"

#include "CaptivePortal.h"
#include "CommandHandler.h"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "event_handlers/impl/WSLocal.h"
#include "message_handlers/impl/WSLocal.h"

const char* const TAG = "LocalMessageHandlers";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "event_handlers/impl/WSLocal.h"
#include "message_handlers/impl/WSLocal.h"

const char* const TAG = "LocalMessageHandlers";

Expand All @@ -10,9 +10,10 @@ const char* const TAG = "LocalMessageHandlers";

using namespace OpenShock::MessageHandlers::Local;

void _Private::HandleWiFiNetworkConnectCommand(uint8_t socketId, const OpenShock::Serialization::Local::LocalToHubMessage* root) {
void _Private::HandleWiFiNetworkConnectCommand(uint8_t socketId, const OpenShock::Serialization::Local::LocalToHubMessage* root)
{
(void)socketId;

auto msg = root->payload_as_WifiNetworkConnectCommand();
if (msg == nullptr) {
OS_LOGE(TAG, "Payload cannot be parsed as WiFiNetworkConnectCommand");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "event_handlers/impl/WSLocal.h"
#include "message_handlers/impl/WSLocal.h"

const char* const TAG = "LocalMessageHandlers";

Expand All @@ -10,9 +10,10 @@ const char* const TAG = "LocalMessageHandlers";

using namespace OpenShock::MessageHandlers::Local;

void _Private::HandleWiFiNetworkDisconnectCommand(uint8_t socketId, const OpenShock::Serialization::Local::LocalToHubMessage* root) {
void _Private::HandleWiFiNetworkDisconnectCommand(uint8_t socketId, const OpenShock::Serialization::Local::LocalToHubMessage* root)
{
(void)socketId;

auto msg = root->payload_as_WifiNetworkDisconnectCommand();
if (msg == nullptr) {
OS_LOGE(TAG, "Payload cannot be parsed as WiFiNetworkDisconnectCommand");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "event_handlers/impl/WSLocal.h"
#include "message_handlers/impl/WSLocal.h"

const char* const TAG = "LocalMessageHandlers";

Expand All @@ -10,9 +10,10 @@ const char* const TAG = "LocalMessageHandlers";

using namespace OpenShock::MessageHandlers::Local;

void _Private::HandleWiFiNetworkForgetCommand(uint8_t socketId, const OpenShock::Serialization::Local::LocalToHubMessage* root) {
void _Private::HandleWiFiNetworkForgetCommand(uint8_t socketId, const OpenShock::Serialization::Local::LocalToHubMessage* root)
{
(void)socketId;

auto msg = root->payload_as_WifiNetworkForgetCommand();
if (msg == nullptr) {
OS_LOGE(TAG, "Payload cannot be parsed as WiFiNetworkForgetCommand");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "event_handlers/impl/WSLocal.h"
#include "message_handlers/impl/WSLocal.h"

const char* const TAG = "LocalMessageHandlers";

Expand All @@ -10,9 +10,10 @@ const char* const TAG = "LocalMessageHandlers";

using namespace OpenShock::MessageHandlers::Local;

void _Private::HandleWiFiNetworkSaveCommand(uint8_t socketId, const OpenShock::Serialization::Local::LocalToHubMessage* root) {
void _Private::HandleWiFiNetworkSaveCommand(uint8_t socketId, const OpenShock::Serialization::Local::LocalToHubMessage* root)
{
(void)socketId;

auto msg = root->payload_as_WifiNetworkSaveCommand();
if (msg == nullptr) {
OS_LOGE(TAG, "Payload cannot be parsed as WiFiNetworkSaveCommand");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "event_handlers/impl/WSLocal.h"
#include "message_handlers/impl/WSLocal.h"

const char* const TAG = "LocalMessageHandlers";

Expand All @@ -7,9 +7,10 @@ const char* const TAG = "LocalMessageHandlers";

using namespace OpenShock::MessageHandlers::Local;

void _Private::HandleWiFiScanCommand(uint8_t socketId, const OpenShock::Serialization::Local::LocalToHubMessage* root) {
void _Private::HandleWiFiScanCommand(uint8_t socketId, const OpenShock::Serialization::Local::LocalToHubMessage* root)
{
(void)socketId;

auto msg = root->payload_as_WifiScanCommand();
if (msg == nullptr) {
OS_LOGE(TAG, "Payload cannot be parsed as WiFiScanCommand");
Expand Down
Loading

0 comments on commit 868fed6

Please sign in to comment.