diff options
author | HampusM <hampus@hampusmat.com> | 2022-05-30 13:06:33 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-05-30 13:06:33 +0200 |
commit | 7da6fc9f7be6c732aeae09cdcc338e1af2b5ad4c (patch) | |
tree | 6363660972ccfcc48dc4d2068ad174214e9f38b5 /minion/src/wifi_module.cpp | |
parent | 1452435a8d662bbd39f0c812cdc24ddd4ec226d3 (diff) |
refactor(minion): store string constants in flash memory
Diffstat (limited to 'minion/src/wifi_module.cpp')
-rw-r--r-- | minion/src/wifi_module.cpp | 81 |
1 files changed, 57 insertions, 24 deletions
diff --git a/minion/src/wifi_module.cpp b/minion/src/wifi_module.cpp index 6c989f9..5174168 100644 --- a/minion/src/wifi_module.cpp +++ b/minion/src/wifi_module.cpp @@ -23,21 +23,27 @@ auto WiFiModule::get_available() noexcept -> int void WiFiModule::reset() noexcept { - _send_serial("AT+RST"); + const auto send_success = _send_serial("AT+RST"); + + if (!send_success) + { + Serial.println(F("Failed to send command to reset wifi module")); + return; + } char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = ""; const auto response_status = _read(2000U, response); Serial.println( - response_status == WiFiModuleResponseStatus::OK ? "Reset wifi module" - : "Failed to reset wifi module" + response_status == WiFiModuleResponseStatus::OK ? F("Reset wifi module") + : F("Failed to reset wifi module") ); } auto WiFiModule::connect_to_wifi(const char *ssid, const char *password) noexcept -> bool { - const char cmd[] = "AT+CWJAP"; + const auto cmd = "AT+CWJAP_CUR"; auto command_length = WIFI_CONNECT_COMMAND_BASE_LENGTH + strlen(cmd) + strlen(ssid) + strlen(password); @@ -46,7 +52,9 @@ auto WiFiModule::connect_to_wifi(const char *ssid, const char *password) noexcep if (command == nullptr) { - Serial.println("Memory allocation failed for creating wifi connection command"); + Serial.println( + F("Heap memory allocation failed for creating a wifi connection command") + ); return false; } @@ -56,7 +64,8 @@ auto WiFiModule::connect_to_wifi(const char *ssid, const char *password) noexcep if (!send_success) { - Serial.println("Failed to send connection command"); + Serial.println(F("Failed to send wifi connect command")); + free(command); return false; } @@ -64,7 +73,7 @@ auto WiFiModule::connect_to_wifi(const char *ssid, const char *password) noexcep char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = ""; - const auto response_status = _read(20000U, response); + const auto response_status = _read(40000U, response); return response_status == WiFiModuleResponseStatus::OK; } @@ -77,6 +86,11 @@ void WiFiModule::set_wifi_mode(WifiMode wifi_mode) noexcept auto *command = util::malloc<char>(command_length); + if (command == nullptr) + { + return; + } + snprintf(command, command_length, "%s=%u", cmd, static_cast<int>(wifi_mode)); _send_serial(command); @@ -96,6 +110,11 @@ void WiFiModule::set_multiple_connections_enabled(bool is_enabled) noexcept auto *command = util::malloc<char>(command_length); + if (command == nullptr) + { + return; + } + snprintf(command, command_length, "%s=%u", cmd, is_enabled ? 1U : 0U); _send_serial(command); @@ -115,6 +134,11 @@ void WiFiModule::set_echo_enabled(bool is_enabled) noexcept auto *command = util::malloc<char>(command_length); + if (command == nullptr) + { + return; + } + snprintf(command, command_length, "%s%u", cmd, is_enabled ? 1U : 0U); _send_serial(command); @@ -126,10 +150,11 @@ void WiFiModule::set_echo_enabled(bool is_enabled) noexcept const auto response_status = _read(1500U, response); Serial.print( - response_status == WiFiModuleResponseStatus::OK ? "Turned " : "Failed to turn " + response_status == WiFiModuleResponseStatus::OK ? F("Turned ") + : F("Failed to turn ") ); - Serial.print(is_enabled ? "on" : "off"); - Serial.println(" AT commands echo"); + Serial.print(is_enabled ? F("on") : F("off")); + Serial.println(F(" AT commands echo")); } void WiFiModule::create_tcp_server(size_t port) noexcept @@ -140,6 +165,11 @@ void WiFiModule::create_tcp_server(size_t port) noexcept auto *command = util::malloc<char>(command_length); + if (command == nullptr) + { + return; + } + snprintf(command, command_length, "%s=1,%u", cmd, port); _send_serial(command); @@ -162,7 +192,7 @@ auto WiFiModule::test() noexcept -> bool char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = ""; - const auto response_status = _read(5000U, response); + const auto response_status = _read(8000U, response); if (response_status == WiFiModuleResponseStatus::TIMEOUT) { @@ -174,9 +204,7 @@ auto WiFiModule::test() noexcept -> bool auto WiFiModule::get_local_ip(char *local_ip_out) noexcept -> const char * { - const auto *command = "AT+CIFSR"; - - const auto send_success = _send_serial(command); + const auto send_success = _send_serial("AT+CIFSR"); if (!send_success) { @@ -300,7 +328,9 @@ auto WiFiModule::close_connection(NetworkConnection &connection) noexcept -> boo if (command == nullptr) { - Serial.println("Memory allocation failed for creating close connection command"); + Serial.println( + F("Heap memory allocation failed for creating close connection command") + ); return false; } @@ -316,14 +346,15 @@ auto WiFiModule::close_connection(NetworkConnection &connection) noexcept -> boo if (response_status != WiFiModuleResponseStatus::OK) { - Serial.print("Failed to close connection to "); + Serial.print(F("Failed to close connection to ")); Serial.println(connection.id()); + Serial.println(response); return false; } connection.set_is_closed(true); - Serial.print("Closed connection to "); + Serial.print(F("Closed connection to ")); Serial.println(connection.id()); return true; @@ -346,7 +377,7 @@ auto WiFiModule::send_response( tot_headers_lengths += strlen(headers[index]) + 2U; } - const auto data_length = strlen(RESPONSE_HTTP_VERSION) + 1 + + const auto data_length = strlen_P(RESPONSE_HTTP_VERSION) + 1 + RESPONSE_STATUS_CODE_LENGTH + 4 + strlen(body) + tot_headers_lengths; @@ -356,7 +387,9 @@ auto WiFiModule::send_response( if (command == nullptr) { - Serial.println("Memory allocation failed for creating send response command"); + Serial.println( + F("Heap memory allocation failed for creating send response command") + ); return false; } @@ -370,19 +403,19 @@ auto WiFiModule::send_response( _read(TIMEOUT_MEDIUM, response); - _serial.print(RESPONSE_HTTP_VERSION); - _serial.print(" "); + _serial.print(reinterpret_cast<const __FlashStringHelper *>(RESPONSE_HTTP_VERSION)); + _serial.print(F(" ")); _serial.print(status_code); - _serial.print("\r\n"); + _serial.print(F("\r\n")); // Print headers for (size_t index = 0U; index < headers_cnt; index++) { _serial.print(headers[index]); - _serial.print("\r\n"); + _serial.print(F("\r\n")); } - _serial.print("\r\n"); + _serial.print(F("\r\n")); _serial.print(body); strcpy(response, ""); |