summaryrefslogtreecommitdiff
path: root/minion
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-05-14 12:57:01 +0200
committerHampusM <hampus@hampusmat.com>2022-05-14 12:57:01 +0200
commit39e2964b0315faeac0c9f8431334ba10093b9490 (patch)
tree9cae9425845b32eac5c0b5c1e93601f09e718a21 /minion
parentcd00813c0740930d389f935f0c2d7d8a11eef02d (diff)
refactor(minion): create network connection class
Diffstat (limited to 'minion')
-rw-r--r--minion/src/gymnasiearbete.cpp20
-rw-r--r--minion/src/http/request.cpp12
-rw-r--r--minion/src/http/request.hpp8
-rw-r--r--minion/src/network_connection.cpp18
-rw-r--r--minion/src/network_connection.hpp19
-rw-r--r--minion/src/wifi_module.cpp31
-rw-r--r--minion/src/wifi_module.hpp15
7 files changed, 86 insertions, 37 deletions
diff --git a/minion/src/gymnasiearbete.cpp b/minion/src/gymnasiearbete.cpp
index 092250a..b31cf81 100644
--- a/minion/src/gymnasiearbete.cpp
+++ b/minion/src/gymnasiearbete.cpp
@@ -74,17 +74,20 @@ void setup()
void loop()
{
- const auto request = wifi_module.read_incoming_request();
+ auto request = wifi_module.read_incoming_request();
- if (request.connection_id() == -1)
+ auto connection = request.connection();
+
+ if (connection.id() == -1)
{
return;
}
- const auto connection_id = request.connection_id();
-
Serial.print("Connection ID: ");
- Serial.println(static_cast<unsigned int>(connection_id));
+ Serial.println(connection.id());
+
+ Serial.print("Connection closed: ");
+ Serial.println(connection.is_closed() ? "true" : "false");
Serial.print("Request method: ");
Serial.println(http_request_method_strs[static_cast<size_t>(request.method())]);
@@ -98,9 +101,12 @@ void loop()
Serial.print("\nData: ");
Serial.println(request.data());
- wifi_module.send_response(connection_id, 200U, "hello there!");
+ wifi_module.send_response(connection, 200U, "hello there!");
+
+ wifi_module.close_connection(connection);
- wifi_module.close_connection(connection_id);
+ Serial.print("Connection closed: ");
+ Serial.println(connection.is_closed() ? "true" : "false");
delay(SECOND_IN_MILLIS);
}
diff --git a/minion/src/http/request.cpp b/minion/src/http/request.cpp
index 9baf7cc..650f57f 100644
--- a/minion/src/http/request.cpp
+++ b/minion/src/http/request.cpp
@@ -55,14 +55,14 @@ auto str_to_http_request_method(const char *http_request_method_str) -> HTTPRequ
}
HTTPRequest::HTTPRequest(
- int connection_id,
+ NetworkConnection connection,
HTTPRequestMethod method,
char *http_version, // NOLINT(bugprone-easily-swappable-parameters)
char *path,
int data_length,
char *data
) noexcept
- : _connection_id(connection_id),
+ : _connection(connection),
_method(method),
_http_version(http_version),
_path(path),
@@ -72,7 +72,7 @@ HTTPRequest::HTTPRequest(
}
HTTPRequest::HTTPRequest(HTTPRequest &&other) noexcept
- : _connection_id(other._connection_id),
+ : _connection(other._connection),
_method(other._method),
_http_version(other._http_version),
_path(other._path),
@@ -87,9 +87,9 @@ HTTPRequest::~HTTPRequest() noexcept
free(_data);
}
-auto HTTPRequest::connection_id() const noexcept -> int
+auto HTTPRequest::connection() noexcept -> NetworkConnection &
{
- return _connection_id;
+ return _connection;
}
auto HTTPRequest::method() const noexcept -> HTTPRequestMethod
@@ -135,7 +135,7 @@ HTTPRequest HTTPRequest::create_invalid() noexcept
}
HTTPRequest::HTTPRequest() noexcept
- : _connection_id(-1),
+ : _connection(-1),
_method(HTTPRequestMethod(-1)),
_http_version(""),
_path(""),
diff --git a/minion/src/http/request.hpp b/minion/src/http/request.hpp
index 8524dc6..dae66f2 100644
--- a/minion/src/http/request.hpp
+++ b/minion/src/http/request.hpp
@@ -1,5 +1,7 @@
#pragma once
+#include "network_connection.hpp"
+
#include <stddef.h>
enum HTTPRequestMethod
@@ -25,7 +27,7 @@ class HTTPRequest
{
public:
explicit HTTPRequest(
- int connection_id,
+ NetworkConnection connection,
HTTPRequestMethod method,
char *http_version,
char *path,
@@ -39,7 +41,7 @@ public:
~HTTPRequest() noexcept;
- auto connection_id() const noexcept -> int;
+ auto connection() noexcept -> NetworkConnection &;
auto method() const noexcept -> HTTPRequestMethod;
@@ -58,7 +60,7 @@ public:
static HTTPRequest create_invalid() noexcept;
private:
- const int _connection_id;
+ NetworkConnection _connection;
const HTTPRequestMethod _method;
char *_http_version;
char *_path;
diff --git a/minion/src/network_connection.cpp b/minion/src/network_connection.cpp
new file mode 100644
index 0000000..1f5fdae
--- /dev/null
+++ b/minion/src/network_connection.cpp
@@ -0,0 +1,18 @@
+#include "network_connection.hpp"
+
+NetworkConnection::NetworkConnection(int8_t id) noexcept : _id(id), _is_closed(false) {}
+
+int8_t NetworkConnection::id() const noexcept
+{
+ return _id;
+}
+
+bool NetworkConnection::is_closed() const noexcept
+{
+ return _is_closed;
+}
+
+void NetworkConnection::set_is_closed(bool is_closed) noexcept
+{
+ _is_closed = is_closed;
+}
diff --git a/minion/src/network_connection.hpp b/minion/src/network_connection.hpp
new file mode 100644
index 0000000..89094b8
--- /dev/null
+++ b/minion/src/network_connection.hpp
@@ -0,0 +1,19 @@
+#pragma once
+
+#include <stdint.h>
+
+class NetworkConnection
+{
+public:
+ NetworkConnection(int8_t id) noexcept;
+
+ int8_t id() const noexcept;
+
+ bool is_closed() const noexcept;
+
+ void set_is_closed(bool is_closed) noexcept;
+
+private:
+ const int8_t _id;
+ bool _is_closed;
+};
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 <Arduino.h>
@@ -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<int>(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<int8_t>(connection_id);
}
size_t WiFiModule::_read_request_data_length() noexcept
diff --git a/minion/src/wifi_module.hpp b/minion/src/wifi_module.hpp
index bac5998..6adaf05 100644
--- a/minion/src/wifi_module.hpp
+++ b/minion/src/wifi_module.hpp
@@ -1,6 +1,7 @@
#pragma once
#include "http/request.hpp"
+#include "network_connection.hpp"
#include <SoftwareSerial.h>
#include <stddef.h>
@@ -91,15 +92,17 @@ public:
/**
* Reads a incoming HTTP request.
*
- * @returns A request. Has a connection ID of -1 if reading the request failed.
+ * @returns A request. Has a connection with a ID of -1 if reading the request failed.
*/
auto read_incoming_request() noexcept -> HTTPRequest;
- auto close_connection(size_t connection_id) noexcept -> bool;
+ auto close_connection(NetworkConnection &connection) noexcept -> bool;
- auto
- send_response(size_t connection_id, size_t status_code, const char *data) noexcept
- -> bool;
+ auto send_response(
+ const NetworkConnection &connection,
+ size_t status_code,
+ const char *data
+ ) noexcept -> bool;
private:
SoftwareSerial _serial;
@@ -113,7 +116,7 @@ private:
*/
auto _send_serial(const char *command) noexcept -> bool;
- size_t _read_connection_id() noexcept;
+ int8_t _read_connection_id() noexcept;
size_t _read_request_data_length() noexcept;