From 6decaf83fc2b1e751876a76d72c4370b3b66a507 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 9 May 2022 14:53:39 +0200 Subject: fix(minion): properly read request data length --- minion/src/wifi_module.cpp | 28 ++++++++++++++++++++++++++-- minion/src/wifi_module.hpp | 2 ++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/minion/src/wifi_module.cpp b/minion/src/wifi_module.cpp index 70fd6f9..8b04b70 100644 --- a/minion/src/wifi_module.cpp +++ b/minion/src/wifi_module.cpp @@ -242,10 +242,17 @@ auto WiFiModule::read_incoming_request(char *raw_request_out) noexcept -> size_t const auto connection_id = _serial.read() - ASCII_TO_CHAR; - const auto data_length = _serial.read(); + // Skip the comma + _serial.read(); + + char data_length_buf[16] = { '\0' }; + + _read_buffer(&data_length_buf[0], sizeof(data_length_buf) - 1, ':'); + + auto data_length = atoi(data_length_buf); Serial.print("Data length: "); - Serial.print(data_length); + Serial.println(data_length); auto read_bytes = 0; const auto start_time = millis(); @@ -410,6 +417,23 @@ auto WiFiModule::_read(uint64_t timeout, char *response_out) noexcept -> Respons return status; } +void WiFiModule::_read_buffer(char *buffer_out, size_t length, char stop_char) noexcept +{ + byte position = 0; + + while (get_available() != 0 && position < length) + { + auto character = _read_byte(); + + if (character == stop_char) + break; + + buffer_out[position++] = character; + } + + buffer_out[position] = '\0'; +} + auto WiFiModule::_read_byte() noexcept -> char { return static_cast(_serial.read()); diff --git a/minion/src/wifi_module.hpp b/minion/src/wifi_module.hpp index 5de898e..43999d6 100644 --- a/minion/src/wifi_module.hpp +++ b/minion/src/wifi_module.hpp @@ -115,5 +115,7 @@ private: */ auto _read(uint64_t timeout, char *response_out) noexcept -> ResponseStatus; + void _read_buffer(char *buffer_out, size_t length, char stop_char) noexcept; + auto _read_byte() noexcept -> char; }; -- cgit v1.2.3-18-g5258