Firmware: rework string actions a bit
This commit is contained in:
parent
509909625c
commit
bbdcd9b5ec
@ -6,9 +6,11 @@
|
||||
|
||||
#include "esp_expected.hpp"
|
||||
#include "clock_core_event.hpp"
|
||||
#include "string_helpers.hpp"
|
||||
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
#include <etl/string.h>
|
||||
|
||||
#include <esp_event.h>
|
||||
#include <esp_log.h>
|
||||
@ -16,6 +18,8 @@
|
||||
#include <esp_netif_sntp.h>
|
||||
#include <esp_wifi.h>
|
||||
|
||||
#include <peripherals_hal_esp.hpp>
|
||||
|
||||
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<Context*>(pvTimerGetTimerID(timer));
|
||||
context->events->setEvent(clock_core::EventGroup::CLOCK_UPDATE);
|
||||
}
|
||||
|
||||
esp_err_t sntpInitialize()
|
||||
@ -136,6 +141,22 @@ std::expected<void, esp_err_t> 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
|
||||
{
|
||||
|
||||
16
firmware/main/string_helpers.hpp
Normal file
16
firmware/main/string_helpers.hpp
Normal 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;
|
||||
}
|
||||
@ -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.");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user