From 40f8bb884df80ebd67a4aabd33bb256a20a913e1 Mon Sep 17 00:00:00 2001 From: erki Date: Sun, 14 Jan 2024 20:37:23 +0200 Subject: [PATCH] Firmware: add ETL subcomponent --- .gitmodules | 3 ++ firmware/components/etlcpp/CMakeLists.txt | 1 + firmware/components/etlcpp/etl | 1 + .../components/etlcpp/include/etl_profile.h | 21 +++++++++ firmware/main/CMakeLists.txt | 2 +- firmware/main/wifi_provisioner.cpp | 43 +++++++++++++++++++ 6 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 firmware/components/etlcpp/CMakeLists.txt create mode 160000 firmware/components/etlcpp/etl create mode 100644 firmware/components/etlcpp/include/etl_profile.h diff --git a/.gitmodules b/.gitmodules index 6dbf2f6..4d0781a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "clock-pcb/vfd-lib"] path = clock-pcb/vfd-lib url = https://github.com/jh1995/KiCad-libs.git +[submodule "firmware/components/etlcpp/etl"] + path = firmware/components/etlcpp/etl + url = https://github.com/ETLCPP/etl.git diff --git a/firmware/components/etlcpp/CMakeLists.txt b/firmware/components/etlcpp/CMakeLists.txt new file mode 100644 index 0000000..1d037a4 --- /dev/null +++ b/firmware/components/etlcpp/CMakeLists.txt @@ -0,0 +1 @@ +idf_component_register(INCLUDE_DIRS "include" "etl/include") diff --git a/firmware/components/etlcpp/etl b/firmware/components/etlcpp/etl new file mode 160000 index 0000000..0f1840a --- /dev/null +++ b/firmware/components/etlcpp/etl @@ -0,0 +1 @@ +Subproject commit 0f1840a70d11a3d317afa55b8361829b93c8eeb8 diff --git a/firmware/components/etlcpp/include/etl_profile.h b/firmware/components/etlcpp/include/etl_profile.h new file mode 100644 index 0000000..076eecc --- /dev/null +++ b/firmware/components/etlcpp/include/etl_profile.h @@ -0,0 +1,21 @@ +// +// Created by erki on 13/01/24. +// + +#ifndef ETL_COMPILER_H +#define ETL_COMPILER_H + +#define ETL_COMPILER_GCC +#define ETL_CPP23_SUPPORTED 1 + +#if !defined(CONFIG_OPTIMIZATION_ASSERTIONS_ENABLED) +#define ETL_NO_CHECKS +#endif + +#if defined(CONFIG_CXX_EXCEPTIONS) +#define ETL_THROW_EXCEPTIONS +#endif + +#include "etl/profiles/gcc_generic.h" + +#endif // ETL_COMPILER_H diff --git a/firmware/main/CMakeLists.txt b/firmware/main/CMakeLists.txt index 6cd1b77..38db32b 100644 --- a/firmware/main/CMakeLists.txt +++ b/firmware/main/CMakeLists.txt @@ -1,6 +1,6 @@ idf_component_register( SRCS "main.cpp" "wifi_provisioner.cpp" INCLUDE_DIRS "" - REQUIRES nvs_flash esp_wifi esp_http_server + REQUIRES nvs_flash esp_wifi esp_http_server etlcpp EMBED_FILES "${CMAKE_CURRENT_LIST_DIR}/static/index.html" ) diff --git a/firmware/main/wifi_provisioner.cpp b/firmware/main/wifi_provisioner.cpp index 2dfe1a9..aba92d5 100644 --- a/firmware/main/wifi_provisioner.cpp +++ b/firmware/main/wifi_provisioner.cpp @@ -9,6 +9,8 @@ #include "esp_mac.h" #include "esp_log.h" +#include + #define TRY(x) ({ \ const auto& _x = (x); \ if (!_x) { \ @@ -68,8 +70,35 @@ esp_err_t rootGetHandler_(httpd_req_t* req) esp_err_t rootPostHandler_(httpd_req_t* req) { + etl::string<1024> content; + + content.initialize_free_space(); + const std::size_t size = MIN(req->content_len, content.max_size() - 1); + + if (const int ret = httpd_req_recv(req, content.data_end(), size); + ret <= 0) + { + if (ret == HTTPD_SOCK_ERR_TIMEOUT) + httpd_resp_send_408(req); + + return ESP_FAIL; + } + + content.trim_to_terminator(); + + etl::string<100> header; + if (const auto err = httpd_req_get_hdr_value_str(req, "Content-Type", header.data(), header.max_size() - 1); + err != ESP_OK) + { + ESP_LOGE(TAG, "Error reading content header."); + return ESP_FAIL; + } //httpd_query_key_value + ESP_LOGI(TAG, "Data from post: %s, %s", header.data(), content.data()); + + const char resp[] = "URI POST Response"; + httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN); return ESP_OK; } @@ -171,6 +200,13 @@ std::expected WifiProvisioner::initializeCaptivePorta .user_ctx = this }; + static httpd_uri_t uri_post = { + .uri = "/configure", + .method = HTTP_POST, + .handler = rootPostHandler_, + .user_ctx = this + }; + if (const auto err = httpd_register_uri_handler(server, &uri_main); err != ESP_OK) { @@ -178,5 +214,12 @@ std::expected WifiProvisioner::initializeCaptivePorta return std::unexpected(err); } + if (const auto err = httpd_register_uri_handler(server, &uri_post); + err != ESP_OK) + { + httpd_stop(server); + return std::unexpected(err); + } + return server; }