diff options
author | HampusM <hampus@hampusmat.com> | 2022-05-08 23:04:16 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-05-08 23:05:36 +0200 |
commit | eddaed4e597ce2bc7fa179ce6a15b90951579c6e (patch) | |
tree | 0e696aaab4cb24f58582efce1982cce47832cb4f /minion/src/wifi_module.cpp | |
parent | 2809f92eeb8b727e20167fe82e4cb9c3627d4870 (diff) |
feat(minion): implement respond to HTTP requests
Diffstat (limited to 'minion/src/wifi_module.cpp')
-rw-r--r-- | minion/src/wifi_module.cpp | 118 |
1 files changed, 117 insertions, 1 deletions
diff --git a/minion/src/wifi_module.cpp b/minion/src/wifi_module.cpp index 2e25dc9..718065d 100644 --- a/minion/src/wifi_module.cpp +++ b/minion/src/wifi_module.cpp @@ -162,7 +162,7 @@ void WiFiModule::create_tcp_server(size_t port) noexcept char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = ""; - _read(1500U, response); + _read(4000U, response); // Serial.println(response); } @@ -240,6 +240,122 @@ const char *WiFiModule::get_local_ip(char *local_ip_out) noexcept return local_ip_out; } +bool WiFiModule::has_incoming_request() noexcept +{ + return get_available() != 0 && _serial.find("+IPD,"); +} + +size_t WiFiModule::read_incoming_request(char *raw_request_out) noexcept +{ + // Wait for the data buffer a bit + while (get_available() < 5) + { + } + + const auto connection_id = _serial.read() - ASCII_TO_CHAR; + + const auto data_length = _serial.read(); + + Serial.print("Data length: "); + Serial.print(data_length); + + auto read_bytes = 0U; + const auto start_time = millis(); + + while (read_bytes != data_length && (start_time + 10000) > millis()) + { + if (get_available() == 0) + { + continue; + } + + char character = _read_byte(); + + strncat(raw_request_out, &character, 1U); + + read_bytes++; + } + + // Skip the comma at the beginning + raw_request_out++; + + return connection_id; +} + +bool WiFiModule::close_connection(size_t connection_id) noexcept +{ + const auto cmd = "AT+CIPCLOSE"; + + auto command_length = strlen(cmd) + 50U + 1U; + + auto *command = util::malloc<char>(command_length); + + if (command == nullptr) + { + Serial.println("Memory allocation failed for creating close connection command"); + return false; + } + + snprintf(command, command_length, "%s=%u", cmd, connection_id); + + _send_serial(command); + + free(command); + + char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = ""; + + const auto response_status = _read(4000U, response); + + if (response_status == ResponseStatus::OK) + { + Serial.print("Closed connection to "); + Serial.println(connection_id); + } + else + { + Serial.print("Failed to close connection to "); + Serial.println(connection_id); + return false; + } + + return true; +} + +bool WiFiModule::send(size_t connection_id, const char *data) +{ + const auto cmd = "AT+CIPSEND"; + + const auto data_length = strlen(data); + + auto command_length = strlen(cmd) + 50U + data_length + 1U; + + auto *command = util::malloc<char>(command_length); + + if (command == nullptr) + { + Serial.println("Memory allocation failed for creating close connection command"); + return false; + } + + snprintf(command, command_length, "%s=%u,%u", cmd, connection_id, data_length); + + _send_serial(command); + + free(command); + + char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = ""; + + _read(4000U, response); + + _serial.print(data); + + strcpy(response, ""); + + _read(4000U, response); + + return true; +} + bool WiFiModule::_send_serial(const char *command) noexcept { auto full_command_length = strlen(command) + 2U + 1U; |