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 <ove/error.hpp>
19#include <string_view>
20
21#ifdef CONFIG_OVE_NET_HTTPD
22
23namespace ove
24{
25
36namespace httpd
37{
38
50{
51 public:
56 explicit Request(ove_httpd_req_t *raw) : raw_(raw)
57 {
58 }
59
60 Request(const Request &) = delete;
61 Request &operator=(const Request &) = delete;
62 Request(Request &&) = delete;
63 Request &operator=(Request &&) = delete;
64
66 const char *method() const
67 {
68 return ove_httpd_req_method(raw_);
69 }
70
72 const char *path() const
73 {
74 return ove_httpd_req_path(raw_);
75 }
76
78 const char *query() const
79 {
80 return ove_httpd_req_query(raw_);
81 }
82
84 const char *body() const
85 {
86 return ove_httpd_req_body(raw_);
87 }
88
90 size_t body_len() const
91 {
92 return ove_httpd_req_body_len(raw_);
93 }
94
104 const char *segment(int idx) const
105 {
106 return ove_httpd_req_segment(raw_, idx);
107 }
108
113 ove_httpd_req_t *raw() const
114 {
115 return raw_;
116 }
117
118 private:
119 ove_httpd_req_t *raw_;
120};
121
133{
134 public:
139 explicit Response(ove_httpd_resp_t *raw) : raw_(raw)
140 {
141 }
142
143 Response(const Response &) = delete;
144 Response &operator=(const Response &) = delete;
145 Response(Response &&) = delete;
146 Response &operator=(Response &&) = delete;
147
155 [[nodiscard]] Result<void> json(int status, const char *json) noexcept
156 {
157 return from_rc(ove_httpd_resp_json(raw_, status, json));
158 }
159
168 [[nodiscard]] Result<void> html(int status, const char *html, size_t len) noexcept
169 {
170 return from_rc(ove_httpd_resp_html(raw_, status, html, len));
171 }
172
182 [[nodiscard]] Result<void> send(int status, const char *content_type, const void *body,
183 size_t len) noexcept
184 {
185 return from_rc(ove_httpd_resp_send(raw_, status, content_type, body, len));
186 }
187
197 [[nodiscard]] Result<void> send_gz(int status, const char *content_type, const void *body,
198 size_t len) noexcept
199 {
200 return from_rc(ove_httpd_resp_send_gz(raw_, status, content_type, body, len));
201 }
202
210 [[nodiscard]] Result<void> error(int status, const char *msg) noexcept
211 {
212 return from_rc(ove_httpd_resp_error(raw_, status, msg));
213 }
214
219 ove_httpd_resp_t *raw() const
220 {
221 return raw_;
222 }
223
224 private:
225 ove_httpd_resp_t *raw_;
226};
227
229using Handler = ove_httpd_handler_t;
230
237struct Config {
238 uint16_t port{80};
239 int max_body_size{1024};
240};
241
252[[nodiscard]] inline Result<void> start(const Config &cfg = {}) noexcept
253{
254 ove_httpd_config_t c{cfg.port, cfg.max_body_size};
255 return from_rc(ove_httpd_start(&c));
256}
257
261inline void stop()
262{
263 ove_httpd_stop();
264}
265
274[[nodiscard]] inline Result<void> route(const char *method, const char *path,
275 Handler handler) noexcept
276{
277 return from_rc(ove_httpd_route(method, path, handler));
278}
279
286{
287 ove_httpd_register_builtin_routes();
288}
289
296inline void set_netif(ove_netif_t netif)
297{
298 ove_httpd_set_netif(netif);
299}
300
301#ifdef CONFIG_OVE_AUDIO
303inline void set_audio_graph(struct ove_audio_graph *g)
304{
305 ove_httpd_set_audio_graph(g);
306}
307#endif
308
309#ifdef CONFIG_OVE_INFER
311inline void set_model(ove_model_t model)
312{
313 ove_httpd_set_model(model);
314}
315#endif
316
317} /* namespace httpd */
318
319#ifdef CONFIG_OVE_NET_HTTPD_WS
320
327namespace ws
328{
329
338{
339 public:
341 explicit Connection(ove_httpd_ws_conn_t *raw) : raw_(raw)
342 {
343 }
344
352 [[nodiscard]] Result<void> send(const void *data, size_t len) noexcept
353 {
354 return from_rc(ove_httpd_ws_send(raw_, data, len));
355 }
356
362 [[nodiscard]] Result<void> send(std::string_view sv) noexcept
363 {
364 return from_rc(ove_httpd_ws_send(raw_, sv.data(), sv.size()));
365 }
366
368 ove_httpd_ws_conn_t *raw() const
369 {
370 return raw_;
371 }
372
373 private:
374 ove_httpd_ws_conn_t *raw_;
375};
376
378using Handler = ove_httpd_ws_handler_t;
379
381using CloseHandler = ove_httpd_ws_close_handler_t;
382
391[[nodiscard]] inline Result<void> route(const char *path, Handler on_message,
392 CloseHandler on_close = nullptr) noexcept
393{
394 return from_rc(ove_httpd_ws_route(path, on_message, on_close));
395}
396
404inline int broadcast(const char *path, const void *data, size_t len)
405{
406 return ove_httpd_ws_broadcast(path, data, len);
407}
408
415inline int broadcast(const char *path, std::string_view sv)
416{
417 return ove_httpd_ws_broadcast(path, sv.data(), sv.size());
418}
419
421inline int active_count()
422{
423 return ove_httpd_ws_active_count();
424}
425
426} /* namespace ws */
427
428#endif /* CONFIG_OVE_NET_HTTPD_WS */
429
430} // namespace ove
431
432#endif /* CONFIG_OVE_NET_HTTPD */
Read-only view of an incoming HTTP request.
Definition net_httpd.hpp:50
size_t body_len() const
Returns the request body length in bytes.
Definition net_httpd.hpp:90
const char * query() const
Returns the query string after '?' (or NULL).
Definition net_httpd.hpp:78
Request(ove_httpd_req_t *raw)
Constructs a Request view from a raw C request pointer.
Definition net_httpd.hpp:56
const char * method() const
Returns the HTTP method string ("GET", "POST", etc.).
Definition net_httpd.hpp:66
ove_httpd_req_t * raw() const
Returns the raw oveRTOS request pointer.
Definition net_httpd.hpp:113
const char * segment(int idx) const
Returns a path segment by index.
Definition net_httpd.hpp:104
const char * body() const
Returns the request body (or NULL).
Definition net_httpd.hpp:84
const char * path() const
Returns the full request path (e.g. "/api/leds/0").
Definition net_httpd.hpp:72
Helper for building and sending an HTTP response.
Definition net_httpd.hpp:133
Result< void > html(int status, const char *html, size_t len) noexcept
Sends an HTML response.
Definition net_httpd.hpp:168
ove_httpd_resp_t * raw() const
Returns the raw oveRTOS response pointer.
Definition net_httpd.hpp:219
Result< void > send_gz(int status, const char *content_type, const void *body, size_t len) noexcept
Sends a pre-gzipped response (adds Content-Encoding: gzip).
Definition net_httpd.hpp:197
Result< void > send(int status, const char *content_type, const void *body, size_t len) noexcept
Sends a response with an arbitrary content type.
Definition net_httpd.hpp:182
Result< void > json(int status, const char *json) noexcept
Sends a JSON response.
Definition net_httpd.hpp:155
Response(ove_httpd_resp_t *raw)
Constructs a Response helper from a raw C response pointer.
Definition net_httpd.hpp:139
Result< void > error(int status, const char *msg) noexcept
Sends a JSON error response.
Definition net_httpd.hpp:210
Non-owning handle to an active WebSocket connection.
Definition net_httpd.hpp:338
Result< void > send(const void *data, size_t len) noexcept
Send a text message to this connection.
Definition net_httpd.hpp:352
Result< void > send(std::string_view sv) noexcept
Send a string_view as a text message.
Definition net_httpd.hpp:362
ove_httpd_ws_conn_t * raw() const
Returns the underlying ove_httpd_ws_conn_t * (for C-API escape hatches).
Definition net_httpd.hpp:368
Connection(ove_httpd_ws_conn_t *raw)
Wraps an opaque ove_httpd_ws_conn_t * from the handler callback.
Definition net_httpd.hpp:341
Strong ove::Error type, Result<T> alias, and std::error_code interop for the oveRTOS C++ binding.
void set_audio_graph(struct ove_audio_graph *g)
Set the audio graph for /api/audio/stats.
Definition net_httpd.hpp:303
void set_netif(ove_netif_t netif)
Associate a network interface with the dashboard routes.
Definition net_httpd.hpp:296
void register_builtin_routes()
Registers the built-in dashboard routes (/api/info, /api/leds, etc.).
Definition net_httpd.hpp:285
void set_model(ove_model_t model)
Set the ML model for /api/infer/stats.
Definition net_httpd.hpp:311
Result< void > route(const char *method, const char *path, Handler handler) noexcept
Registers a route handler.
Definition net_httpd.hpp:274
ove_httpd_handler_t Handler
Route handler callback type (same as the C typedef).
Definition net_httpd.hpp:229
Result< void > start(const Config &cfg={}) noexcept
Starts the HTTP server.
Definition net_httpd.hpp:252
void stop()
Stops the HTTP server and closes the listening socket.
Definition net_httpd.hpp:261
int active_count()
Return the number of active WebSocket connections.
Definition net_httpd.hpp:421
Result< void > route(const char *path, Handler on_message, CloseHandler on_close=nullptr) noexcept
Register a WebSocket route.
Definition net_httpd.hpp:391
ove_httpd_ws_handler_t Handler
WebSocket message handler type.
Definition net_httpd.hpp:378
ove_httpd_ws_close_handler_t CloseHandler
WebSocket close handler type.
Definition net_httpd.hpp:381
int broadcast(const char *path, const void *data, size_t len)
Broadcast a message to all WebSocket connections on a path.
Definition net_httpd.hpp:404
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:20
Result< void > from_rc(int rc) noexcept
Lifts a substrate rc-code into a Result<void>.
Definition error.hpp:254
std::expected< T, Error > Result
std::expected-based result alias.
Definition error.hpp:139
HTTP server configuration.
Definition net_httpd.hpp:237
int max_body_size
Definition net_httpd.hpp:239
uint16_t port
Definition net_httpd.hpp:238
Common type definitions and concepts for the C++ wrapper layer.