oveRTOS C API
Embedded RTOS framework — build system, configuration, and portable C API
Loading...
Searching...
No Matches
net_httpd.h
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
9#ifndef OVE_NET_HTTPD_H
10#define OVE_NET_HTTPD_H
11
25#include "ove/types.h"
26#include "ove/net.h"
27#include "ove_config.h"
28
29#include <stddef.h>
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35#ifdef CONFIG_OVE_NET_HTTPD
36
38#define OVE_HTTPD_MAX_ROUTES 16
39
41#define OVE_HTTPD_MAX_SEGMENTS 8
42
44typedef struct ove_httpd_req ove_httpd_req_t;
45
47typedef struct ove_httpd_resp ove_httpd_resp_t;
48
54
58typedef struct {
59 uint16_t port;
62
73
77void ove_httpd_stop(void);
78
87int ove_httpd_route(const char *method, const char *path, ove_httpd_handler_t handler);
88
95
105
106/* ── Optional module hooks ──────────────────────────────────── */
107
108#ifdef CONFIG_OVE_AUDIO
109struct ove_audio_graph;
112#endif
113
114#ifdef CONFIG_OVE_INFER
117#endif
118
119/* ── Request accessors ───────────────────────────────────────── */
120
123
126
129
132
135
145const char *ove_httpd_req_segment(ove_httpd_req_t *req, int idx);
146
147/* ── Response helpers ────────────────────────────────────────── */
148
150int ove_httpd_resp_json(ove_httpd_resp_t *resp, int status, const char *json);
151
153int ove_httpd_resp_html(ove_httpd_resp_t *resp, int status, const char *html, size_t len);
154
156int ove_httpd_resp_send(ove_httpd_resp_t *resp, int status, const char *content_type,
157 const void *body, size_t len);
158
160int ove_httpd_resp_send_gz(ove_httpd_resp_t *resp, int status, const char *content_type,
161 const void *body, size_t len);
162
164int ove_httpd_resp_error(ove_httpd_resp_t *resp, int status, const char *message);
165
166/* ── WebSocket support ──────────────────────────────────────── */
167
168#ifdef CONFIG_OVE_NET_HTTPD_WS
169
171typedef struct ove_httpd_ws_conn ove_httpd_ws_conn_t;
172
179typedef void (*ove_httpd_ws_handler_t)(ove_httpd_ws_conn_t *conn, const void *data, size_t len);
180
186
199int ove_httpd_ws_route(const char *path, ove_httpd_ws_handler_t on_message,
201
210int ove_httpd_ws_send(ove_httpd_ws_conn_t *conn, const void *data, size_t len);
211
220int ove_httpd_ws_broadcast(const char *path, const void *data, size_t len);
221
224
225/* ── Internal — called by httpd accept loop ──────────────────────────── */
226
228int ove_httpd_ws_is_upgrade(const char *headers);
229
236int ove_httpd_ws_handshake(const char *headers, size_t headers_len, const char *path,
237 ove_socket_t sock, ove_socket_storage_t *storage);
238
241
242#endif /* CONFIG_OVE_NET_HTTPD_WS */
243
244/* ── Log hook ────────────────────────────────────────────────── */
245
251void ove_httpd_log_append(const char *line);
252
253#else /* !CONFIG_OVE_NET_HTTPD */
254
256static inline int ove_httpd_start(const void *cfg)
257{
258 (void)cfg;
260}
261static inline void ove_httpd_stop(void)
262{
263}
264static inline void ove_httpd_register_builtin_routes(void)
265{
266}
267static inline void ove_httpd_log_append(const char *line)
268{
269 (void)line;
270}
273#endif /* CONFIG_OVE_NET_HTTPD */
274
275#ifdef __cplusplus
276}
277#endif
278
281#endif /* OVE_NET_HTTPD_H */
int ove_httpd_ws_is_upgrade(const char *headers)
Return non-zero if the HTTP headers request a WebSocket upgrade.
int ove_httpd_resp_html(ove_httpd_resp_t *resp, int status, const char *html, size_t len)
Send an HTML response.
void ove_httpd_set_netif(ove_netif_t netif)
Set the network interface used by built-in dashboard routes.
int(* ove_httpd_handler_t)(ove_httpd_req_t *req, ove_httpd_resp_t *resp)
Route handler callback.
Definition net_httpd.h:53
const char * ove_httpd_req_query(ove_httpd_req_t *req)
Get the query string after '?' (or NULL).
struct ove_httpd_req ove_httpd_req_t
Opaque HTTP request.
Definition net_httpd.h:44
int ove_httpd_ws_broadcast(const char *path, const void *data, size_t len)
Broadcast a text message to all WebSocket connections on a path.
const char * ove_httpd_req_segment(ove_httpd_req_t *req, int idx)
Get a path segment by index.
void ove_httpd_stop(void)
Stop the HTTP server and close the listening socket.
void(* ove_httpd_ws_handler_t)(ove_httpd_ws_conn_t *conn, const void *data, size_t len)
WebSocket message callback.
Definition net_httpd.h:179
void ove_httpd_ws_poll(void)
Drive the WS subsystem from the httpd task's poll loop.
void(* ove_httpd_ws_close_handler_t)(ove_httpd_ws_conn_t *conn)
WebSocket close callback.
Definition net_httpd.h:185
int ove_httpd_ws_route(const char *path, ove_httpd_ws_handler_t on_message, ove_httpd_ws_close_handler_t on_close)
Register a WebSocket route.
int ove_httpd_resp_error(ove_httpd_resp_t *resp, int status, const char *message)
Send a JSON error response.
int ove_httpd_resp_send(ove_httpd_resp_t *resp, int status, const char *content_type, const void *body, size_t len)
Send a response with arbitrary content type.
size_t ove_httpd_req_body_len(ove_httpd_req_t *req)
Get the request body length.
const char * ove_httpd_req_method(ove_httpd_req_t *req)
Get the HTTP method string ("GET" or "POST").
const char * ove_httpd_req_path(ove_httpd_req_t *req)
Get the full request path (e.g. "/api/leds/0").
int ove_httpd_ws_send(ove_httpd_ws_conn_t *conn, const void *data, size_t len)
Send a text message to a WebSocket connection.
int ove_httpd_ws_active_count(void)
Return the number of active WebSocket connections.
struct ove_httpd_ws_conn ove_httpd_ws_conn_t
Opaque WebSocket connection handle.
Definition net_httpd.h:171
int ove_httpd_start(const ove_httpd_config_t *cfg)
Start the HTTP server.
void ove_httpd_set_audio_graph(struct ove_audio_graph *g)
Set the audio graph for /api/audio/stats.
const char * ove_httpd_req_body(ove_httpd_req_t *req)
Get the request body (or NULL).
void ove_httpd_log_append(const char *line)
Feed a log line into the httpd log ring buffer.
int ove_httpd_ws_handshake(const char *headers, size_t headers_len, const char *path, ove_socket_t sock, ove_socket_storage_t *storage)
Complete the WebSocket upgrade handshake on sock.
int ove_httpd_resp_json(ove_httpd_resp_t *resp, int status, const char *json)
Send a JSON response.
int ove_httpd_resp_send_gz(ove_httpd_resp_t *resp, int status, const char *content_type, const void *body, size_t len)
Send a pre-gzipped response (adds Content-Encoding: gzip).
int ove_httpd_route(const char *method, const char *path, ove_httpd_handler_t handler)
Register a route handler.
struct ove_httpd_resp ove_httpd_resp_t
Opaque HTTP response.
Definition net_httpd.h:47
void ove_httpd_set_model(ove_model_t model)
Set the ML model for /api/infer/stats.
void ove_httpd_register_builtin_routes(void)
Register built-in dashboard routes (/api/info, /api/leds, etc.).
struct ove_netif * ove_netif_t
Opaque handle for a network interface.
Definition types.h:250
struct ove_socket * ove_socket_t
Opaque handle for a network socket.
Definition types.h:247
struct ove_model * ove_model_t
Opaque handle for an ML inference model session.
Definition types.h:244
@ OVE_ERR_NOT_SUPPORTED
Definition types.h:98
Audio processing graph instance.
Definition audio.h:96
HTTP server configuration.
Definition net_httpd.h:58
int max_body_size
Definition net_httpd.h:60
uint16_t port
Definition net_httpd.h:59