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/wifi_module.cpp | 26 +++++++++++--------------- 1 file changed, 11 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 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] = ""; -- cgit v1.2.3-18-g5258