Firmware  0.8.0
Loading...
Searching...
No Matches
utility.hpp
Go to the documentation of this file.
1// Copyright (C) 2025 Vincent Hamp
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
21
22#pragma once
23
24#include <bit>
25#include <charconv>
26#include <dcc/dcc.hpp>
27#include <functional>
28#include <iterator>
29#include <memory>
30#include <numeric>
31#include <span>
32#include <string_view>
33#include "intf/http/message.hpp"
34#include "log.h"
35
37
38bool validate_json(std::string_view json);
39
40// https://stackoverflow.com/questions/81870/is-it-possible-to-print-a-variables-type-in-standard-c
41template<typename T>
42consteval std::string_view type_name() {
43 using namespace std;
44#if defined(__clang__)
45 string_view p{__PRETTY_FUNCTION__};
46 return string_view(data(p) + 34, size(p) - 34 - 1);
47#elif defined(__GNUC__)
48 string_view p{__PRETTY_FUNCTION__};
49# if __cplusplus < 201402
50 return string_view(data(p) + 36, size(p) - 36 - 1);
51# else
52 return string_view(data(p) + 49, p.find(';', 49) - 49);
53# endif
54#elif defined(_MSC_VER)
55 string_view p{__FUNCSIG__};
56 return string_view(data(p) + 84, size(p) - 84 - 7);
57#endif
58}
59
60std::optional<dcc::Address::value_type> uri2address(std::string_view uri);
61
62std::optional<dcc::Address> uri2loco_address(std::string_view uri);
63
65template<std::output_iterator<char> OutputIt>
66OutputIt decode_uri(std::string_view uri, OutputIt out) {
67 auto first{begin(uri)};
68 auto const last{cend(uri)};
69 while (first < last) {
70 auto c{*first++};
71 if (c == '+') c = ' ';
72 else if (c == '%') {
73 std::from_chars(first, first + 2, c, 16);
74 first += 2;
75 }
76 *out++ = c;
77 }
78 return out;
79}
80
83
85esp_err_t ipc_call_blocking(BaseType_t core_id, esp_err_t (*f)());
86
88template<typename... Ts>
89auto httpd_sess_trigger_close(Ts&&... ts) {
90 return httpd_sess_trigger_close(intf::http::handle, std::forward<Ts>(ts)...);
91}
92
95 return httpd_queue_work(
97 [](void* arg) {
98 auto msg{static_cast<intf::http::Message*>(arg)};
99
100 // Wrap message in httpd_ws_frame_t
101 httpd_ws_frame_t frame{
102 .type = msg->type,
103 .payload = data(msg->payload),
104 .len = size(msg->payload),
105 };
106 if (auto const err{httpd_ws_send_frame_async(
107 intf::http::handle, msg->sock_fd, &frame)})
108 LOGD("httpd_ws_send_frame_async failed %s", esp_err_to_name(err));
109
110 // Delete
111 delete msg;
112 },
113 msg);
114}
Log macros.
#define LOGD(...)
Definition log.h:46
HTTP websocket message.
httpd_handle_t handle
Handle to server instance.
Definition config.hpp:496
Definition message.hpp:29
httpd_ws_type_t type
Definition message.hpp:31
int sock_fd
Definition message.hpp:30
std::vector< uint8_t > payload
Definition message.hpp:32
std::optional< dcc::Address > uri2loco_address(std::string_view uri)
Definition utility.cpp:78
void esp_delayed_restart()
Definition utility.cpp:54
bool validate_json(std::string_view json)
Definition utility.cpp:60
auto httpd_sess_trigger_close(Ts &&... ts)
Definition utility.hpp:89
auto httpd_queue_work(intf::http::Message *msg)
Definition utility.hpp:94
uint32_t http_receive_timeout2ms()
Definition utility.cpp:88
OutputIt decode_uri(std::string_view uri, OutputIt out)
https://rosettacode.org/wiki/URL_decoding#C
Definition utility.hpp:66
esp_err_t ipc_call_blocking(BaseType_t core_id, esp_err_t(*f)())
Definition utility.cpp:94
consteval std::string_view type_name()
Definition utility.hpp:42
std::optional< dcc::Address::value_type > uri2address(std::string_view uri)
Definition utility.cpp:67