summaryrefslogtreecommitdiff
path: root/minion/src/wifi_module.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'minion/src/wifi_module.cpp')
-rw-r--r--minion/src/wifi_module.cpp80
1 files changed, 34 insertions, 46 deletions
diff --git a/minion/src/wifi_module.cpp b/minion/src/wifi_module.cpp
index 718065d..70fd6f9 100644
--- a/minion/src/wifi_module.cpp
+++ b/minion/src/wifi_module.cpp
@@ -5,8 +5,8 @@
#include <Arduino.h>
#include <string.h>
-WiFiModule::WiFiModule(uint8_t receive_pin, uint8_t transmit_pin) noexcept
- : _serial(receive_pin, transmit_pin)
+WiFiModule::WiFiModule(const WiFiModuleOptions &options) noexcept
+ : _serial(options.receive_pin, options.transmit_pin)
{
}
@@ -15,7 +15,7 @@ void WiFiModule::begin(size_t baudrate) noexcept
_serial.begin(baudrate);
}
-int WiFiModule::get_available() noexcept
+auto WiFiModule::get_available() noexcept -> int
{
return _serial.available();
}
@@ -34,7 +34,7 @@ void WiFiModule::reset() noexcept
);
}
-bool WiFiModule::connect(const char *ssid, const char *password) noexcept
+auto WiFiModule::connect(const char *ssid, const char *password) noexcept -> bool
{
const char cmd[] = "AT+CWJAP";
@@ -64,21 +64,14 @@ bool WiFiModule::connect(const char *ssid, const char *password) noexcept
const auto response_status = _read(20000U, response);
- if (response_status != ResponseStatus::OK)
- {
- Serial.print("Connection command responded with status ");
- Serial.println(response_status);
- return false;
- }
-
- return true;
+ return response_status == ResponseStatus::OK;
}
void WiFiModule::set_wifi_mode(WifiMode wifi_mode) noexcept
{
const char cmd[] = "AT+CWMODE_CUR";
- auto command_length = strlen(cmd) + 2U + 1U;
+ const auto command_length = strlen(cmd) + 2U + 1U;
auto *command = util::malloc<char>(command_length);
@@ -90,7 +83,7 @@ void WiFiModule::set_wifi_mode(WifiMode wifi_mode) noexcept
char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";
- _read(1500U, response);
+ _read(TIMEOUT_SHORT, response);
}
void WiFiModule::set_multiple_connections_enabled(bool is_enabled) noexcept
@@ -109,7 +102,7 @@ void WiFiModule::set_multiple_connections_enabled(bool is_enabled) noexcept
char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";
- _read(1500U, response);
+ _read(TIMEOUT_SHORT, response);
// Serial.println(response);
}
@@ -132,23 +125,14 @@ void WiFiModule::set_echo_enabled(bool is_enabled) noexcept
const auto response_status = _read(1500U, response);
- if (response_status == ResponseStatus::OK)
- {
- Serial.print("Turned ");
- Serial.print(is_enabled ? "on" : "off");
- Serial.println(" AT commands echo");
- }
- else
- {
- Serial.print("Failed to turn ");
- Serial.print(is_enabled ? "on" : "off");
- Serial.println(" AT commands echo");
- }
+ Serial.print(response_status == ResponseStatus::OK ? "Turned " : "Failed to turn ");
+ Serial.print(is_enabled ? "on" : "off");
+ Serial.println(" AT commands echo");
}
void WiFiModule::create_tcp_server(size_t port) noexcept
{
- const auto cmd = "AT+CIPSERVER";
+ const auto *cmd = "AT+CIPSERVER";
auto command_length = strlen(cmd) + 7U + 1U;
@@ -162,12 +146,12 @@ void WiFiModule::create_tcp_server(size_t port) noexcept
char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";
- _read(4000U, response);
+ _read(TIMEOUT_LONG, response);
// Serial.println(response);
}
-bool WiFiModule::test() noexcept
+auto WiFiModule::test() noexcept -> bool
{
const auto send_success = _send_serial("AT");
@@ -188,9 +172,9 @@ bool WiFiModule::test() noexcept
return strcmp(response, "") != 0;
}
-const char *WiFiModule::get_local_ip(char *local_ip_out) noexcept
+auto WiFiModule::get_local_ip(char *local_ip_out) noexcept -> const char *
{
- const auto command = "AT+CIFSR";
+ const auto *command = "AT+CIFSR";
const auto send_success = _send_serial(command);
@@ -240,15 +224,19 @@ const char *WiFiModule::get_local_ip(char *local_ip_out) noexcept
return local_ip_out;
}
-bool WiFiModule::has_incoming_request() noexcept
+auto WiFiModule::has_incoming_request() noexcept -> bool
{
- return get_available() != 0 && _serial.find("+IPD,");
+ char request_prefix[] = "+IPD,";
+
+ return get_available() != 0 && _serial.find(request_prefix);
}
-size_t WiFiModule::read_incoming_request(char *raw_request_out) noexcept
+auto WiFiModule::read_incoming_request(char *raw_request_out) noexcept -> size_t
{
+ const auto min_available_bytes = 5;
+
// Wait for the data buffer a bit
- while (get_available() < 5)
+ while (get_available() < min_available_bytes)
{
}
@@ -259,7 +247,7 @@ size_t WiFiModule::read_incoming_request(char *raw_request_out) noexcept
Serial.print("Data length: ");
Serial.print(data_length);
- auto read_bytes = 0U;
+ auto read_bytes = 0;
const auto start_time = millis();
while (read_bytes != data_length && (start_time + 10000) > millis())
@@ -282,9 +270,9 @@ size_t WiFiModule::read_incoming_request(char *raw_request_out) noexcept
return connection_id;
}
-bool WiFiModule::close_connection(size_t connection_id) noexcept
+auto WiFiModule::close_connection(size_t connection_id) noexcept -> bool
{
- const auto cmd = "AT+CIPCLOSE";
+ const auto *cmd = "AT+CIPCLOSE";
auto command_length = strlen(cmd) + 50U + 1U;
@@ -321,9 +309,9 @@ bool WiFiModule::close_connection(size_t connection_id) noexcept
return true;
}
-bool WiFiModule::send(size_t connection_id, const char *data)
+auto WiFiModule::send(size_t connection_id, const char *data) noexcept -> bool
{
- const auto cmd = "AT+CIPSEND";
+ const auto *cmd = "AT+CIPSEND";
const auto data_length = strlen(data);
@@ -345,18 +333,18 @@ bool WiFiModule::send(size_t connection_id, const char *data)
char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";
- _read(4000U, response);
+ _read(TIMEOUT_LONG, response);
_serial.print(data);
strcpy(response, "");
- _read(4000U, response);
+ _read(TIMEOUT_LONG, response);
return true;
}
-bool WiFiModule::_send_serial(const char *command) noexcept
+auto WiFiModule::_send_serial(const char *command) noexcept -> bool
{
auto full_command_length = strlen(command) + 2U + 1U;
@@ -379,7 +367,7 @@ bool WiFiModule::_send_serial(const char *command) noexcept
return true;
}
-ResponseStatus WiFiModule::_read(uint64_t timeout, char *response_out) noexcept
+auto WiFiModule::_read(uint64_t timeout, char *response_out) noexcept -> ResponseStatus
{
const auto start_time = millis();
@@ -422,7 +410,7 @@ ResponseStatus WiFiModule::_read(uint64_t timeout, char *response_out) noexcept
return status;
}
-char WiFiModule::_read_byte() noexcept
+auto WiFiModule::_read_byte() noexcept -> char
{
return static_cast<char>(_serial.read());
}