49 static Address ipv4(uint8_t a, uint8_t b, uint8_t c, uint8_t d,
52 ove_sockaddr_ipv4(&addr.raw, a, b, c, d, port);
60 uint16_t port()
const {
return raw.port; }
66 void set_port(uint16_t p) { raw.port = p; }
86 NetIfConfig() : raw{} {}
95 NetIfConfig &static_ip(
const Address &ip,
const Address &mask,
98 raw.static_ip = ip.raw;
99 raw.netmask = mask.raw;
100 raw.gateway = gw.raw;
108 NetIfConfig &dhcp() {
118 NetIfConfig &dns(
const Address &d) {
124 ove_netif_config_t raw;
147 int err = ove_netif_init(&handle_, &storage_);
148 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
155 if (!handle_)
return;
156 ove_netif_deinit(handle_);
159 NetIf(
const NetIf &) =
delete;
160 NetIf &operator=(
const NetIf &) =
delete;
162#ifdef CONFIG_OVE_ZERO_HEAP
163 NetIf(NetIf &&) =
delete;
164 NetIf &operator=(NetIf &&) =
delete;
170 NetIf(NetIf &&other) noexcept : handle_(other.handle_) {
171 other.handle_ =
nullptr;
179 NetIf &operator=(NetIf &&other)
noexcept {
180 if (
this != &other) {
181 if (handle_) ove_netif_deinit(handle_);
182 handle_ = other.handle_;
183 other.handle_ =
nullptr;
194 [[nodiscard]]
int up(
const NetIfConfig &cfg) {
195 return ove_netif_up(handle_, &cfg.raw);
202 ove_netif_down(handle_);
212 [[nodiscard]]
int get_addr(Address *ip =
nullptr,
213 Address *gateway =
nullptr,
214 Address *netmask =
nullptr) {
215 return ove_netif_get_addr(handle_,
216 ip ? &ip->raw : nullptr,
217 gateway ? &gateway->raw : nullptr,
218 netmask ? &netmask->raw : nullptr);
225 bool valid()
const {
return handle_ !=
nullptr; }
231 ove_netif_t handle()
const {
return handle_; }
234 ove_netif_t handle_{};
235 ove_netif_storage_t storage_{};
262 int err = ove_socket_open(&handle_, &storage_,
263 OVE_AF_INET, OVE_SOCK_STREAM);
264 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
272 if (open_) ove_socket_close(handle_);
275 TcpSocket(
const TcpSocket &) =
delete;
276 TcpSocket &operator=(
const TcpSocket &) =
delete;
278#ifdef CONFIG_OVE_ZERO_HEAP
279 TcpSocket(TcpSocket &&) =
delete;
280 TcpSocket &operator=(TcpSocket &&) =
delete;
286 TcpSocket(TcpSocket &&other) noexcept
287 : handle_(other.handle_)
288 , storage_(other.storage_)
289 , open_(other.open_) {
290 other.handle_ =
nullptr;
299 TcpSocket &operator=(TcpSocket &&other)
noexcept {
300 if (
this != &other) {
301 if (open_) ove_socket_close(handle_);
302 handle_ = other.handle_;
303 storage_ = other.storage_;
305 other.handle_ =
nullptr;
318 [[nodiscard]]
int connect(
const Address &addr,
319 uint32_t timeout_ms = OVE_WAIT_FOREVER) {
320 return ove_socket_connect(handle_, &addr.raw, timeout_ms);
330 [[nodiscard]]
int send(
const void *data,
size_t len,
331 size_t *sent =
nullptr) {
332 return ove_socket_send(handle_, data, len, sent);
343 [[nodiscard]]
int recv(
void *buf,
size_t len,
344 size_t *received =
nullptr,
345 uint32_t timeout_ms = OVE_WAIT_FOREVER) {
346 return ove_socket_recv(handle_, buf, len, received, timeout_ms);
356 ove_socket_close(handle_);
365 bool is_open()
const {
return open_; }
371 ove_socket_t handle()
const {
return handle_; }
374 friend class TcpListener;
381 TcpSocket(ove_socket_t h, ove_socket_storage_t s)
382 : handle_(h), storage_(s), open_(true) {}
384 ove_socket_t handle_{};
385 ove_socket_storage_t storage_{};
407 int err = ove_socket_open(&handle_, &storage_,
408 OVE_AF_INET, OVE_SOCK_DGRAM);
409 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
417 if (open_) ove_socket_close(handle_);
420 UdpSocket(
const UdpSocket &) =
delete;
421 UdpSocket &operator=(
const UdpSocket &) =
delete;
423#ifdef CONFIG_OVE_ZERO_HEAP
424 UdpSocket(UdpSocket &&) =
delete;
425 UdpSocket &operator=(UdpSocket &&) =
delete;
431 UdpSocket(UdpSocket &&other) noexcept
432 : handle_(other.handle_)
433 , storage_(other.storage_)
434 , open_(other.open_) {
435 other.handle_ =
nullptr;
444 UdpSocket &operator=(UdpSocket &&other)
noexcept {
445 if (
this != &other) {
446 if (open_) ove_socket_close(handle_);
447 handle_ = other.handle_;
448 storage_ = other.storage_;
450 other.handle_ =
nullptr;
462 [[nodiscard]]
int bind(
const Address &addr) {
463 return ove_socket_bind(handle_, &addr.raw);
474 [[nodiscard]]
int send_to(
const void *data,
size_t len,
476 size_t *sent =
nullptr) {
477 return ove_socket_sendto(handle_, data, len, sent, &dest.raw);
489 [[nodiscard]]
int recv_from(
void *buf,
size_t len,
490 Address *src =
nullptr,
491 size_t *received =
nullptr,
492 uint32_t timeout_ms = OVE_WAIT_FOREVER) {
493 return ove_socket_recvfrom(handle_, buf, len, received,
494 src ? &src->raw : nullptr,
505 ove_socket_close(handle_);
514 bool is_open()
const {
return open_; }
520 ove_socket_t handle()
const {
return handle_; }
523 ove_socket_t handle_{};
524 ove_socket_storage_t storage_{};
548 int err = ove_socket_open(&handle_, &storage_,
549 OVE_AF_INET, OVE_SOCK_STREAM);
550 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
558 if (open_) ove_socket_close(handle_);
561 TcpListener(
const TcpListener &) =
delete;
562 TcpListener &operator=(
const TcpListener &) =
delete;
564#ifdef CONFIG_OVE_ZERO_HEAP
565 TcpListener(TcpListener &&) =
delete;
566 TcpListener &operator=(TcpListener &&) =
delete;
572 TcpListener(TcpListener &&other) noexcept
573 : handle_(other.handle_)
574 , storage_(other.storage_)
575 , open_(other.open_) {
576 other.handle_ =
nullptr;
585 TcpListener &operator=(TcpListener &&other)
noexcept {
586 if (
this != &other) {
587 if (open_) ove_socket_close(handle_);
588 handle_ = other.handle_;
589 storage_ = other.storage_;
591 other.handle_ =
nullptr;
603 [[nodiscard]]
int bind(
const Address &addr) {
604 return ove_socket_bind(handle_, &addr.raw);
612 [[nodiscard]]
int listen(
int backlog = 4) {
613 return ove_socket_listen(handle_, backlog);
627 [[nodiscard]]
int accept(TcpSocket &client,
628 uint32_t timeout_ms = OVE_WAIT_FOREVER) {
629 ove_socket_t cli_handle{};
630 ove_socket_storage_t cli_storage{};
631 int err = ove_socket_accept(handle_, &cli_handle,
632 &cli_storage, timeout_ms);
635 client.handle_ = cli_handle;
636 client.storage_ = cli_storage;
649 ove_socket_close(handle_);
658 bool is_open()
const {
return open_; }
664 ove_socket_t handle()
const {
return handle_; }
667 ove_socket_t handle_{};
668 ove_socket_storage_t storage_{};
689[[nodiscard]]
inline int resolve(
const char *hostname, Address &addr,
690 uint32_t timeout_ms = 5000) {
691 return ove_dns_resolve(hostname, &addr.raw, timeout_ms);
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:19
Common type definitions and concepts for the C++ wrapper layer.