From 39e2964b0315faeac0c9f8431334ba10093b9490 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 14 May 2022 12:57:01 +0200 Subject: refactor(minion): create network connection class --- minion/src/wifi_module.cpp | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'minion/src/wifi_module.cpp') diff --git a/minion/src/wifi_module.cpp b/minion/src/wifi_module.cpp index dbcd737..2322582 100644 --- a/minion/src/wifi_module.cpp +++ b/minion/src/wifi_module.cpp @@ -1,5 +1,6 @@ #include "wifi_module.hpp" +#include "network_connection.hpp" #include "util.hpp" #include @@ -244,7 +245,7 @@ auto WiFiModule::read_incoming_request() noexcept -> HTTPRequest { } - const auto connection_id = _read_connection_id(); + const auto connection = NetworkConnection(_read_connection_id()); auto request_data_length = _read_request_data_length(); @@ -281,7 +282,7 @@ auto WiFiModule::read_incoming_request() noexcept -> HTTPRequest _read_bytes(request_data, request_data_length, TIMEOUT_LONG); return HTTPRequest( - static_cast(connection_id), + connection, request_method, http_version, request_path, @@ -290,7 +291,7 @@ auto WiFiModule::read_incoming_request() noexcept -> HTTPRequest ); } -auto WiFiModule::close_connection(size_t connection_id) noexcept -> bool +auto WiFiModule::close_connection(NetworkConnection &connection) noexcept -> bool { const auto *cmd = "AT+CIPCLOSE"; @@ -304,7 +305,7 @@ auto WiFiModule::close_connection(size_t connection_id) noexcept -> bool return false; } - snprintf(command, command_length, "%s=%u", cmd, connection_id); + snprintf(command, command_length, "%s=%u", cmd, connection.id()); _send_serial(command); @@ -314,23 +315,23 @@ auto WiFiModule::close_connection(size_t connection_id) noexcept -> bool const auto response_status = _read(4000U, response); - if (response_status == WiFiModuleResponseStatus::OK) - { - Serial.print("Closed connection to "); - Serial.println(connection_id); - } - else + if (response_status != WiFiModuleResponseStatus::OK) { Serial.print("Failed to close connection to "); - Serial.println(connection_id); + Serial.println(connection.id()); return false; } + connection.set_is_closed(true); + + Serial.print("Closed connection to "); + Serial.println(connection.id()); + return true; } auto WiFiModule::send_response( - size_t connection_id, + const NetworkConnection &connection, size_t status_code, const char *data ) noexcept -> bool @@ -350,7 +351,7 @@ auto WiFiModule::send_response( return false; } - snprintf(command, command_length, "%s=%u,%u", cmd, connection_id, data_length); + snprintf(command, command_length, "%s=%u,%u", cmd, connection.id(), data_length); _send_serial(command); @@ -396,14 +397,14 @@ auto WiFiModule::_send_serial(const char *command) noexcept -> bool return true; } -size_t WiFiModule::_read_connection_id() noexcept +int8_t WiFiModule::_read_connection_id() noexcept { const auto connection_id = _serial.read() - ASCII_TO_CHAR; // Skip the comma _serial.read(); - return connection_id; + return static_cast(connection_id); } size_t WiFiModule::_read_request_data_length() noexcept -- cgit v1.2.3-18-g5258