Firmware: rework string actions a bit

This commit is contained in:
Erki 2024-02-16 16:52:26 +02:00
parent 509909625c
commit bbdcd9b5ec
3 changed files with 44 additions and 12 deletions

View File

@ -6,9 +6,11 @@
#include "esp_expected.hpp" #include "esp_expected.hpp"
#include "clock_core_event.hpp" #include "clock_core_event.hpp"
#include "string_helpers.hpp"
#include <cstring> #include <cstring>
#include <ctime> #include <ctime>
#include <etl/string.h>
#include <esp_event.h> #include <esp_event.h>
#include <esp_log.h> #include <esp_log.h>
@ -16,6 +18,8 @@
#include <esp_netif_sntp.h> #include <esp_netif_sntp.h>
#include <esp_wifi.h> #include <esp_wifi.h>
#include <peripherals_hal_esp.hpp>
namespace namespace
{ {
@ -79,9 +83,10 @@ void sntpEventHandler(struct timeval*)
Context::getContext().events->setEvent(clock_core::EventGroup::SNTP_SYNCED); 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<Context*>(pvTimerGetTimerID(timer));
context->events->setEvent(clock_core::EventGroup::CLOCK_UPDATE);
} }
esp_err_t sntpInitialize() esp_err_t sntpInitialize()
@ -136,6 +141,22 @@ std::expected<void, esp_err_t> wifiInitialize(const char* wifi_ssid, const char*
return {}; 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 namespace clock_core
@ -149,7 +170,7 @@ void run(const char* wifi_ssid, const char* wifi_password)
auto clock_timer = xTimerCreate("clock_core", auto clock_timer = xTimerCreate("clock_core",
pdMS_TO_TICKS(60'000), pdMS_TO_TICKS(60'000),
pdTRUE, pdTRUE,
nullptr, &Context::getContext(),
timerEventHandler); timerEventHandler);
if (const auto res = wifiInitialize(wifi_ssid, wifi_password); 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) else if (bits & EventGroup::CLOCK_UPDATE)
{ {
char time_buf[64]; const auto time = getCurrentTimeString();
std::time_t time_now; ESP_LOGI(TAG, "Current time: %s", time.c_str());
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);
} }
else else
{ {

View File

@ -0,0 +1,16 @@
//
// Created by erki on 16/02/24.
//
#pragma once
template<typename T, typename F>
auto wrapUnsafeStringOps(T& string, F&& operation)
{
string.initialize_free_space();
operation(string.data_end(), string.available());
string.trim_to_terminator();
return string;
}

View File

@ -88,7 +88,8 @@ esp_err_t rootPostHandler_(httpd_req_t* req)
}; };
etl::string<100> content_type; 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) err != ESP_OK)
{ {
dispatch_error("Cannot parse header."); dispatch_error("Cannot parse header.");