summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-05-30 13:11:38 +0200
committerHampusM <hampus@hampusmat.com>2022-05-30 13:11:38 +0200
commitec20c707ea677c9b2c8e585b78c9837a2f6e7372 (patch)
tree6499124c7f7cd1d3da12afb60ab9f65f7782d228
parentd95ce9a08977b82fe6f331f110c218044b9f41cb (diff)
refactor(minion): make reading wifi module more reliable
-rw-r--r--minion/src/util.cpp12
-rw-r--r--minion/src/util.hpp12
-rw-r--r--minion/src/wifi_module.cpp15
3 files changed, 27 insertions, 12 deletions
diff --git a/minion/src/util.cpp b/minion/src/util.cpp
index 15cf840..708e47d 100644
--- a/minion/src/util.cpp
+++ b/minion/src/util.cpp
@@ -3,22 +3,22 @@
namespace util
{
-auto str_ends_with(const char *str, const char *other_str) noexcept -> bool
+auto str_ends_with(const char *target, size_t target_end, const char *other) noexcept
+ -> bool
{
- if (str == nullptr || other_str == nullptr)
+ if (target == nullptr || other == nullptr)
{
return false;
}
- auto str_length = strlen(str);
- auto other_str_length = strlen(other_str);
+ const auto other_str_length = strlen(other);
- if (other_str_length > str_length)
+ if (other_str_length > target_end)
{
return false;
}
- return strncmp(str + str_length - other_str_length, other_str, other_str_length) == 0;
+ return strncmp(target + target_end - other_str_length, other, other_str_length) == 0;
}
void substr(const char *str, const char *end, char *dest) noexcept
diff --git a/minion/src/util.hpp b/minion/src/util.hpp
index 0dbb03f..2955cf1 100644
--- a/minion/src/util.hpp
+++ b/minion/src/util.hpp
@@ -13,7 +13,15 @@ auto malloc(size_t size) noexcept -> Type *
return static_cast<Type *>(::malloc(size));
}
-auto str_ends_with(const char *str, const char *other_str) noexcept -> bool;
+/**
+ * Returns whether or not a string ends with the content of another string.
+ *
+ * @param target The string to compare the end of.
+ * @param target_end The end position of the target string.
+ * @param other The string to compare with.
+ */
+auto str_ends_with(const char *target, size_t target_end, const char *other) noexcept
+ -> bool;
/**
* Extracts a portion of a string.
@@ -32,7 +40,7 @@ void substr(const char *str, const char *end, char *dest) noexcept;
* @param str_one The first string.
* @param str_two The second string.
*
- * @returns Whether or not the two string are the same.
+ * @returns Whether or not the two strings contain the same content.
*/
auto streq(const char *str_one, const char *str_two) noexcept -> bool;
diff --git a/minion/src/wifi_module.cpp b/minion/src/wifi_module.cpp
index 5174168..953e9fa 100644
--- a/minion/src/wifi_module.cpp
+++ b/minion/src/wifi_module.cpp
@@ -498,22 +498,29 @@ auto WiFiModule::_read(uint64_t timeout, char *response_out) noexcept
{
char character = _read_byte();
+ if (has_end)
+ {
+ continue;
+ }
+
+ const auto end_pos = index + 1U;
+
response_out[index] = character;
- response_out[index + 1] = '\0';
+ response_out[end_pos] = '\0';
- if (util::str_ends_with(response_out, "OK"))
+ if (util::str_ends_with(response_out, end_pos, "OK"))
{
status = WiFiModuleResponseStatus::OK;
has_end = true;
}
- if (util::str_ends_with(response_out, "ERROR"))
+ if (util::str_ends_with(response_out, end_pos, "ERROR"))
{
status = WiFiModuleResponseStatus::ERROR;
has_end = true;
}
- if (util::str_ends_with(response_out, "FAIL"))
+ if (util::str_ends_with(response_out, end_pos, "FAIL"))
{
status = WiFiModuleResponseStatus::FAIL;
has_end = true;