diff --git a/firmware/main/clock_core.cpp b/firmware/main/clock_core.cpp index 6d1b938..ec888f5 100644 --- a/firmware/main/clock_core.cpp +++ b/firmware/main/clock_core.cpp @@ -6,9 +6,11 @@ #include "esp_expected.hpp" #include "clock_core_event.hpp" +#include "string_helpers.hpp" #include #include +#include #include #include @@ -16,6 +18,8 @@ #include #include +#include + namespace { @@ -79,9 +83,10 @@ void sntpEventHandler(struct timeval*) Context::getContext().events->setEvent(clock_core::EventGroup::SNTP_SYNCED); } -void timerEventHandler(TimerHandle_t) +void timerEventHandler(TimerHandle_t timer) { - Context::getContext().events->setEvent(clock_core::EventGroup::CLOCK_UPDATE); + auto context = static_cast(pvTimerGetTimerID(timer)); + context->events->setEvent(clock_core::EventGroup::CLOCK_UPDATE); } esp_err_t sntpInitialize() @@ -136,6 +141,22 @@ std::expected wifiInitialize(const char* wifi_ssid, const char* return {}; } +etl::string<6> getCurrentTimeString() +{ + std::time_t time_now; + std::tm time_info{}; + std::time(&time_now); + localtime_r(&time_now, &time_info); + + etl::string<6> buffer; + wrapUnsafeStringOps(buffer, [&time_info](char* string, const int available) + { + std::strftime(string, available, "%H:%M", &time_info); + }); + + return buffer; +} + } namespace clock_core @@ -149,7 +170,7 @@ void run(const char* wifi_ssid, const char* wifi_password) auto clock_timer = xTimerCreate("clock_core", pdMS_TO_TICKS(60'000), pdTRUE, - nullptr, + &Context::getContext(), timerEventHandler); if (const auto res = wifiInitialize(wifi_ssid, wifi_password); @@ -185,14 +206,8 @@ void run(const char* wifi_ssid, const char* wifi_password) } else if (bits & EventGroup::CLOCK_UPDATE) { - char time_buf[64]; - std::time_t time_now; - std::tm time_info{}; - std::time(&time_now); - localtime_r(&time_now, &time_info); - - std::strftime(time_buf, sizeof(time_buf), "%c", &time_info); - ESP_LOGI(TAG, "Current time: %s", time_buf); + const auto time = getCurrentTimeString(); + ESP_LOGI(TAG, "Current time: %s", time.c_str()); } else { diff --git a/firmware/main/string_helpers.hpp b/firmware/main/string_helpers.hpp new file mode 100644 index 0000000..9955c27 --- /dev/null +++ b/firmware/main/string_helpers.hpp @@ -0,0 +1,16 @@ +// +// Created by erki on 16/02/24. +// + +#pragma once + +template +auto wrapUnsafeStringOps(T& string, F&& operation) +{ + string.initialize_free_space(); + + operation(string.data_end(), string.available()); + + string.trim_to_terminator(); + return string; +} diff --git a/firmware/main/wifi_provisioner.cpp b/firmware/main/wifi_provisioner.cpp index c406b62..bcceb65 100644 --- a/firmware/main/wifi_provisioner.cpp +++ b/firmware/main/wifi_provisioner.cpp @@ -88,7 +88,8 @@ esp_err_t rootPostHandler_(httpd_req_t* req) }; etl::string<100> content_type; - if (const auto err = httpd_req_get_hdr_value_str(req, "Content-Type", content_type.data(), content_type.max_size() - 1); + content_type.initialize_free_space(); + if (const auto err = httpd_req_get_hdr_value_str(req, "Content-Type", content_type.data_end(), content_type.available() - 1); err != ESP_OK) { dispatch_error("Cannot parse header.");