From 0529ac97653bc535c5c1db832d258f661d69309c Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 16 May 2022 13:49:54 +0200 Subject: refactor(minion): improve network code readability --- minion/src/http/request.cpp | 6 +++--- minion/src/http/request.hpp | 2 +- minion/src/network_connection.cpp | 9 ++++++--- minion/src/network_connection.hpp | 6 +++--- minion/src/wifi_module.cpp | 26 +++++++++++--------------- minion/src/wifi_module.hpp | 13 ++++++++++--- 6 files changed, 34 insertions(+), 28 deletions(-) (limited to 'minion/src') diff --git a/minion/src/http/request.cpp b/minion/src/http/request.cpp index 650f57f..6a3f722 100644 --- a/minion/src/http/request.cpp +++ b/minion/src/http/request.cpp @@ -129,14 +129,14 @@ auto HTTPRequest::operator=(HTTPRequest &&other) noexcept -> HTTPRequest & return *this; } -HTTPRequest HTTPRequest::create_invalid() noexcept +auto HTTPRequest::create_invalid() noexcept -> HTTPRequest { - return HTTPRequest(); + return {}; } HTTPRequest::HTTPRequest() noexcept : _connection(-1), - _method(HTTPRequestMethod(-1)), + _method(HTTPRequestMethod::GET), _http_version(""), _path(""), _data_length(0), diff --git a/minion/src/http/request.hpp b/minion/src/http/request.hpp index dae66f2..fc95473 100644 --- a/minion/src/http/request.hpp +++ b/minion/src/http/request.hpp @@ -57,7 +57,7 @@ public: auto operator=(HTTPRequest &&other) noexcept -> HTTPRequest &; - static HTTPRequest create_invalid() noexcept; + static auto create_invalid() noexcept -> HTTPRequest; private: NetworkConnection _connection; diff --git a/minion/src/network_connection.cpp b/minion/src/network_connection.cpp index 1f5fdae..3d20113 100644 --- a/minion/src/network_connection.cpp +++ b/minion/src/network_connection.cpp @@ -1,13 +1,16 @@ #include "network_connection.hpp" -NetworkConnection::NetworkConnection(int8_t id) noexcept : _id(id), _is_closed(false) {} +NetworkConnection::NetworkConnection(int8_t connection_id) noexcept + : _id(connection_id), _is_closed(false) +{ +} -int8_t NetworkConnection::id() const noexcept +auto NetworkConnection::id() const noexcept -> int8_t { return _id; } -bool NetworkConnection::is_closed() const noexcept +auto NetworkConnection::is_closed() const noexcept -> bool { return _is_closed; } diff --git a/minion/src/network_connection.hpp b/minion/src/network_connection.hpp index 89094b8..4cefb39 100644 --- a/minion/src/network_connection.hpp +++ b/minion/src/network_connection.hpp @@ -5,11 +5,11 @@ class NetworkConnection { public: - NetworkConnection(int8_t id) noexcept; + explicit NetworkConnection(int8_t connection_id) noexcept; - int8_t id() const noexcept; + auto id() const noexcept -> int8_t; - bool is_closed() const noexcept; + auto is_closed() const noexcept -> bool; void set_is_closed(bool is_closed) noexcept; diff --git a/minion/src/wifi_module.cpp b/minion/src/wifi_module.cpp index 2322582..ea509df 100644 --- a/minion/src/wifi_module.cpp +++ b/minion/src/wifi_module.cpp @@ -39,7 +39,8 @@ auto WiFiModule::connect_to_wifi(const char *ssid, const char *password) noexcep { const char cmd[] = "AT+CWJAP"; - auto command_length = strlen(cmd) + strlen(ssid) + strlen(password) + 6U + 1U; + auto command_length = + WIFI_CONNECT_COMMAND_BASE_LENGTH + strlen(cmd) + strlen(ssid) + strlen(password); auto *command = util::malloc(command_length); @@ -104,8 +105,6 @@ void WiFiModule::set_multiple_connections_enabled(bool is_enabled) noexcept char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = ""; _read(TIMEOUT_SHORT, response); - - // Serial.println(response); } void WiFiModule::set_echo_enabled(bool is_enabled) noexcept @@ -137,7 +136,7 @@ void WiFiModule::create_tcp_server(size_t port) noexcept { const auto *cmd = "AT+CIPSERVER"; - auto command_length = strlen(cmd) + 7U + 1U; + auto command_length = CREATE_TCP_SERVER_COMMAND_BASE_LENGTH + strlen(cmd); auto *command = util::malloc(command_length); @@ -150,8 +149,6 @@ void WiFiModule::create_tcp_server(size_t port) noexcept char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = ""; _read(TIMEOUT_MEDIUM, response); - - // Serial.println(response); } auto WiFiModule::test() noexcept -> bool @@ -295,7 +292,7 @@ auto WiFiModule::close_connection(NetworkConnection &connection) noexcept -> boo { const auto *cmd = "AT+CIPCLOSE"; - auto command_length = strlen(cmd) + 50U + 1U; + auto command_length = CLOSE_CONNECTION_COMMAND_BASE_LENGTH + strlen(cmd); auto *command = util::malloc(command_length); @@ -341,7 +338,7 @@ auto WiFiModule::send_response( const auto data_length = strlen(RESPONSE_HTTP_VERSION) + 1 + RESPONSE_STATUS_CODE_LENGTH + 4 + strlen(data); - auto command_length = strlen(cmd) + 50U + data_length + 1U; + auto command_length = SEND_RESPONSE_COMMAND_BASE_LENGTH + strlen(cmd) + data_length; auto *command = util::malloc(command_length); @@ -382,6 +379,7 @@ auto WiFiModule::_send_serial(const char *command) noexcept -> bool if (full_command == nullptr) { + // NOLINTNEXTLINE(readability-simplify-boolean-expr) return false; } @@ -389,15 +387,12 @@ auto WiFiModule::_send_serial(const char *command) noexcept -> bool _serial.print(full_command); - // Serial.print("Sent command: "); - // Serial.println(full_command); - free(full_command); return true; } -int8_t WiFiModule::_read_connection_id() noexcept +auto WiFiModule::_read_connection_id() noexcept -> int8_t { const auto connection_id = _serial.read() - ASCII_TO_CHAR; @@ -407,16 +402,17 @@ int8_t WiFiModule::_read_connection_id() noexcept return static_cast(connection_id); } -size_t WiFiModule::_read_request_data_length() noexcept +auto WiFiModule::_read_request_data_length() noexcept -> size_t { char data_length_buf[REQUEST_DATA_LENGTH_BUF_SIZE] = ""; _read_to(&data_length_buf[0], sizeof(data_length_buf) - 1U, ':', TIMEOUT_SHORT); - return static_cast(strtoul(data_length_buf, nullptr, 10)); + return static_cast(strtoul(data_length_buf, nullptr, NUMBER_BASE)); } -HTTPRequestMethod WiFiModule::_read_request_method(size_t &request_data_length) noexcept +auto WiFiModule::_read_request_method(size_t &request_data_length) noexcept + -> HTTPRequestMethod { char request_method_buf[REQUEST_METHOD_STR_MAX_LENGTH] = ""; diff --git a/minion/src/wifi_module.hpp b/minion/src/wifi_module.hpp index 6adaf05..51e90f0 100644 --- a/minion/src/wifi_module.hpp +++ b/minion/src/wifi_module.hpp @@ -14,6 +14,11 @@ constexpr auto REQUEST_METHOD_STR_MAX_LENGTH = 10U; constexpr auto REQUEST_PATH_MAX_LENGTH = 64U; constexpr auto REQUEST_HTTP_VERSION_MAX_LENGTH = 10U; +constexpr auto WIFI_CONNECT_COMMAND_BASE_LENGTH = 7U; +constexpr auto CREATE_TCP_SERVER_COMMAND_BASE_LENGTH = 8U; +constexpr auto CLOSE_CONNECTION_COMMAND_BASE_LENGTH = 51U; +constexpr auto SEND_RESPONSE_COMMAND_BASE_LENGTH = 51U; + constexpr auto ASCII_TO_CHAR = 48U; constexpr auto TIMEOUT_SHORT = 1500U; @@ -23,6 +28,8 @@ constexpr auto TIMEOUT_LONG = 10000U; constexpr auto RESPONSE_HTTP_VERSION = "HTTP/1.1"; constexpr auto RESPONSE_STATUS_CODE_LENGTH = 3U; +constexpr auto NUMBER_BASE = 10U; + enum WifiMode { Station = 1, @@ -116,11 +123,11 @@ private: */ auto _send_serial(const char *command) noexcept -> bool; - int8_t _read_connection_id() noexcept; + auto _read_connection_id() noexcept -> int8_t; - size_t _read_request_data_length() noexcept; + auto _read_request_data_length() noexcept -> size_t; - HTTPRequestMethod _read_request_method(size_t &request_data_length) noexcept; + auto _read_request_method(size_t &request_data_length) noexcept -> HTTPRequestMethod; /** * Reads from the wifi module until it responds with a status. -- cgit v1.2.3-18-g5258