oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
net_httpd.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * This file is part of oveRTOS.
7 */
8
14#pragma once
15
16#include <ove/net_httpd.h>
17#include <ove/types.hpp>
18#include <string_view>
19
20#ifdef CONFIG_OVE_NET_HTTPD
21
22namespace ove {
23
34namespace httpd {
35
46class Request {
47public:
52 explicit Request(ove_httpd_req_t *raw) : raw_(raw) {}
53
54 Request(const Request &) = delete;
55 Request &operator=(const Request &) = delete;
56 Request(Request &&) = delete;
57 Request &operator=(Request &&) = delete;
58
60 const char *method() const { return ove_httpd_req_method(raw_); }
61
63 const char *path() const { return ove_httpd_req_path(raw_); }
64
66 const char *query() const { return ove_httpd_req_query(raw_); }
67
69 const char *body() const { return ove_httpd_req_body(raw_); }
70
72 size_t body_len() const { return ove_httpd_req_body_len(raw_); }
73
83 const char *segment(int idx) const {
84 return ove_httpd_req_segment(raw_, idx);
85 }
86
91 ove_httpd_req_t *raw() const { return raw_; }
92
93private:
94 ove_httpd_req_t *raw_;
95};
96
107class Response {
108public:
113 explicit Response(ove_httpd_resp_t *raw) : raw_(raw) {}
114
115 Response(const Response &) = delete;
116 Response &operator=(const Response &) = delete;
117 Response(Response &&) = delete;
118 Response &operator=(Response &&) = delete;
119
126 int json(int status, const char *json) {
127 return ove_httpd_resp_json(raw_, status, json);
128 }
129
137 int html(int status, const char *html, size_t len) {
138 return ove_httpd_resp_html(raw_, status, html, len);
139 }
140
149 int send(int status, const char *content_type,
150 const void *body, size_t len) {
151 return ove_httpd_resp_send(raw_, status, content_type,
152 body, len);
153 }
154
163 int send_gz(int status, const char *content_type,
164 const void *body, size_t len) {
165 return ove_httpd_resp_send_gz(raw_, status, content_type,
166 body, len);
167 }
168
175 int error(int status, const char *msg) {
176 return ove_httpd_resp_error(raw_, status, msg);
177 }
178
183 ove_httpd_resp_t *raw() const { return raw_; }
184
185private:
186 ove_httpd_resp_t *raw_;
187};
188
190using Handler = ove_httpd_handler_t;
191
198struct Config {
199 uint16_t port{80};
200 int max_body_size{1024};
201};
202
212[[nodiscard]] inline int start(const Config &cfg = {}) {
213 ove_httpd_config_t c{cfg.port, cfg.max_body_size};
214 return ove_httpd_start(&c);
215}
216
220inline void stop() {
221 ove_httpd_stop();
222}
223
231[[nodiscard]] inline int route(const char *method, const char *path,
232 Handler handler) {
233 return ove_httpd_route(method, path, handler);
234}
235
241inline void register_builtin_routes() {
242 ove_httpd_register_builtin_routes();
243}
244
251inline void set_netif(ove_netif_t netif) {
252 ove_httpd_set_netif(netif);
253}
254
255#ifdef CONFIG_OVE_AUDIO
257inline void set_audio_graph(struct ove_audio_graph *g) {
258 ove_httpd_set_audio_graph(g);
259}
260#endif
261
262#ifdef CONFIG_OVE_INFER
264inline void set_model(ove_model_t model) {
265 ove_httpd_set_model(model);
266}
267#endif
268
269} /* namespace httpd */
270
271#ifdef CONFIG_OVE_NET_HTTPD_WS
272
279namespace ws {
280
288class Connection {
289public:
290 explicit Connection(ove_httpd_ws_conn_t *raw) : raw_(raw) {}
291
298 int send(const void *data, size_t len) {
299 return ove_httpd_ws_send(raw_, data, len);
300 }
301
307 int send(std::string_view sv) {
308 return ove_httpd_ws_send(raw_, sv.data(), sv.size());
309 }
310
311 ove_httpd_ws_conn_t *raw() const { return raw_; }
312
313private:
314 ove_httpd_ws_conn_t *raw_;
315};
316
318using Handler = ove_httpd_ws_handler_t;
319
321using CloseHandler = ove_httpd_ws_close_handler_t;
322
330[[nodiscard]] inline int route(const char *path,
331 Handler on_message,
332 CloseHandler on_close = nullptr) {
333 return ove_httpd_ws_route(path, on_message, on_close);
334}
335
343inline int broadcast(const char *path, const void *data, size_t len) {
344 return ove_httpd_ws_broadcast(path, data, len);
345}
346
353inline int broadcast(const char *path, std::string_view sv) {
354 return ove_httpd_ws_broadcast(path, sv.data(), sv.size());
355}
356
358inline int active_count() {
359 return ove_httpd_ws_active_count();
360}
361
362} /* namespace ws */
363
364#endif /* CONFIG_OVE_NET_HTTPD_WS */
365
366} // namespace ove
367
368#endif /* CONFIG_OVE_NET_HTTPD */
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:19
Common type definitions and concepts for the C++ wrapper layer.