oveRTOS C API
Embedded RTOS framework — build system, configuration, and portable C API
Loading...
Searching...
No Matches
net.h
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_H
10#define OVE_NET_H
11
26#include "ove/types.h"
27#include "ove_config.h"
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33/* ── Types (always visible) ──────────────────────────────────────── */
34
36#ifdef __ZIG_CIMPORT__ /* @cond ZIG_ABI */
37/* Zig @cImport uses clang which does not default to -fshort-enums on ARM.
38 * Use fixed-width types so the struct layout matches the ARM EABI ABI. */
39typedef uint8_t ove_sock_type_t;
40#define OVE_SOCK_STREAM ((ove_sock_type_t)1)
41#define OVE_SOCK_DGRAM ((ove_sock_type_t)2)
42typedef uint8_t ove_af_t;
43#define OVE_AF_INET ((ove_af_t)2)
44#define OVE_AF_INET6 ((ove_af_t)10)
45#else /* @endcond */
50
52typedef enum {
55} ove_af_t;
56#endif
57
61typedef struct {
63 uint16_t port;
64 uint8_t addr[16];
66
77
78#include "ove/storage.h"
79
80#ifdef CONFIG_OVE_NET
81
82/* ── Network interface ───────────────────────────────────────────── */
83
91int ove_netif_init(ove_netif_t *netif, ove_netif_storage_t *storage);
92
98void ove_netif_deinit(ove_netif_t netif);
99
107int ove_netif_up(ove_netif_t netif, const ove_netif_config_t *cfg);
108
114void ove_netif_down(ove_netif_t netif);
115
125int ove_netif_get_addr(ove_netif_t netif, ove_sockaddr_t *ip,
126 ove_sockaddr_t *gateway, ove_sockaddr_t *netmask);
127
128#ifdef OVE_HEAP_NET
135int ove_netif_create(ove_netif_t *netif);
136
142void ove_netif_destroy(ove_netif_t netif);
143#endif /* OVE_HEAP_NET */
144
145/* ── Sockets ─────────────────────────────────────────────────────── */
146
156int ove_socket_open(ove_socket_t *sock, ove_socket_storage_t *storage,
157 ove_af_t af, ove_sock_type_t type);
158
164void ove_socket_close(ove_socket_t sock);
165
174int ove_socket_connect(ove_socket_t sock, const ove_sockaddr_t *addr,
175 uint32_t timeout_ms);
176
184int ove_socket_bind(ove_socket_t sock, const ove_sockaddr_t *addr);
185
193int ove_socket_listen(ove_socket_t sock, int backlog);
194
204int ove_socket_accept(ove_socket_t sock, ove_socket_t *client,
205 ove_socket_storage_t *client_storage,
206 uint32_t timeout_ms);
207
217int ove_socket_send(ove_socket_t sock, const void *data, size_t len,
218 size_t *sent);
219
230int ove_socket_recv(ove_socket_t sock, void *buf, size_t len,
231 size_t *received, uint32_t timeout_ms);
232
243int ove_socket_sendto(ove_socket_t sock, const void *data, size_t len,
244 size_t *sent, const ove_sockaddr_t *dest);
245
257int ove_socket_recvfrom(ove_socket_t sock, void *buf, size_t len,
258 size_t *received, ove_sockaddr_t *src,
259 uint32_t timeout_ms);
260
261#ifdef OVE_HEAP_NET
270int ove_socket_create(ove_socket_t *sock, ove_af_t af,
271 ove_sock_type_t type);
272
278void ove_socket_destroy(ove_socket_t sock);
279#endif /* OVE_HEAP_NET */
280
281/* ── DNS ─────────────────────────────────────────────────────────── */
282
291int ove_dns_resolve(const char *hostname, ove_sockaddr_t *addr,
292 uint32_t timeout_ms);
293
294/* ── Helpers ─────────────────────────────────────────────────────── */
295
306void ove_sockaddr_ipv4(ove_sockaddr_t *addr, uint8_t a, uint8_t b,
307 uint8_t c, uint8_t d, uint16_t port);
308
309#else /* !CONFIG_OVE_NET */
310
312/* Provide dummy storage types so the disabled inline stubs compile. */
313#ifndef CONFIG_OVE_NET
314typedef struct { uint8_t _unused; } ove_socket_storage_t;
315typedef struct { uint8_t _unused; } ove_netif_storage_t;
316#endif
317
318static inline int ove_netif_init(ove_netif_t *netif, ove_netif_storage_t *storage) { (void)netif; (void)storage; return OVE_ERR_NOT_SUPPORTED; }
319static inline void ove_netif_deinit(ove_netif_t netif) { (void)netif; }
320static inline int ove_netif_up(ove_netif_t netif, const ove_netif_config_t *cfg) { (void)netif; (void)cfg; return OVE_ERR_NOT_SUPPORTED; }
321static inline void ove_netif_down(ove_netif_t netif) { (void)netif; }
322static inline int ove_netif_get_addr(ove_netif_t netif, ove_sockaddr_t *ip, ove_sockaddr_t *gw, ove_sockaddr_t *nm) { (void)netif; (void)ip; (void)gw; (void)nm; return OVE_ERR_NOT_SUPPORTED; }
323static inline int ove_socket_open(ove_socket_t *sock, ove_socket_storage_t *storage, ove_af_t af, ove_sock_type_t type) { (void)sock; (void)storage; (void)af; (void)type; return OVE_ERR_NOT_SUPPORTED; }
324static inline void ove_socket_close(ove_socket_t sock) { (void)sock; }
325static inline int ove_socket_connect(ove_socket_t sock, const ove_sockaddr_t *addr, uint32_t timeout_ms) { (void)sock; (void)addr; (void)timeout_ms; return OVE_ERR_NOT_SUPPORTED; }
326static inline int ove_socket_bind(ove_socket_t sock, const ove_sockaddr_t *addr) { (void)sock; (void)addr; return OVE_ERR_NOT_SUPPORTED; }
327static inline int ove_socket_listen(ove_socket_t sock, int backlog) { (void)sock; (void)backlog; return OVE_ERR_NOT_SUPPORTED; }
328static inline int ove_socket_accept(ove_socket_t sock, ove_socket_t *client, ove_socket_storage_t *client_storage, uint32_t timeout_ms) { (void)sock; (void)client; (void)client_storage; (void)timeout_ms; return OVE_ERR_NOT_SUPPORTED; }
329static inline int ove_socket_send(ove_socket_t sock, const void *data, size_t len, size_t *sent) { (void)sock; (void)data; (void)len; (void)sent; return OVE_ERR_NOT_SUPPORTED; }
330static inline int ove_socket_recv(ove_socket_t sock, void *buf, size_t len, size_t *received, uint32_t timeout_ms) { (void)sock; (void)buf; (void)len; (void)received; (void)timeout_ms; return OVE_ERR_NOT_SUPPORTED; }
331static inline int ove_socket_sendto(ove_socket_t sock, const void *data, size_t len, size_t *sent, const ove_sockaddr_t *dest) { (void)sock; (void)data; (void)len; (void)sent; (void)dest; return OVE_ERR_NOT_SUPPORTED; }
332static inline int ove_socket_recvfrom(ove_socket_t sock, void *buf, size_t len, size_t *received, ove_sockaddr_t *src, uint32_t timeout_ms) { (void)sock; (void)buf; (void)len; (void)received; (void)src; (void)timeout_ms; return OVE_ERR_NOT_SUPPORTED; }
333static inline int ove_dns_resolve(const char *hostname, ove_sockaddr_t *addr, uint32_t timeout_ms) { (void)hostname; (void)addr; (void)timeout_ms; return OVE_ERR_NOT_SUPPORTED; }
334static inline void ove_sockaddr_ipv4(ove_sockaddr_t *addr, uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t port) { (void)addr; (void)a; (void)b; (void)c; (void)d; (void)port; }
337#endif /* CONFIG_OVE_NET */
338
339#ifdef __cplusplus
340}
341#endif
342
345#endif /* OVE_NET_H */
ove_sock_type_t
Socket type.
Definition net.h:46
ove_af_t
Address family.
Definition net.h:52
@ OVE_SOCK_DGRAM
Definition net.h:48
@ OVE_SOCK_STREAM
Definition net.h:47
@ OVE_AF_INET
Definition net.h:53
@ OVE_AF_INET6
Definition net.h:54
#define OVE_ERR_NOT_SUPPORTED
The requested feature is not supported by the active backend.
Definition types.h:38
struct ove_netif * ove_netif_t
Opaque handle for a network interface.
Definition types.h:124
struct ove_socket * ove_socket_t
Opaque handle for a network socket.
Definition types.h:121
Network interface configuration.
Definition net.h:70
ove_sockaddr_t static_ip
Definition net.h:72
ove_sockaddr_t gateway
Definition net.h:73
ove_sockaddr_t dns
Definition net.h:75
int use_dhcp
Definition net.h:71
ove_sockaddr_t netmask
Definition net.h:74
Generic socket address (large enough for IPv4 or IPv6).
Definition net.h:61
ove_af_t family
Definition net.h:62
uint16_t port
Definition net.h:63