ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
lwip_raw_udp_impl.h
Go to the documentation of this file.
1#pragma once
3
4#ifdef USE_SOCKET_IMPL_LWIP_TCP
5
6#include <array>
7#include <cstdint>
8
9#include "headers.h"
10#include "lwip/ip.h"
11#include "lwip/udp.h"
12
13namespace esphome::socket {
14
19 public:
21 LWIPRawUDPSendImpl(sa_family_t family, struct udp_pcb *pcb) : pcb_(pcb), family_(family) {}
25
26 int bind(const struct sockaddr *name, socklen_t addrlen);
27 int close();
28
30 ssize_t sendto(const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen);
31
32 int setsockopt(int level, int optname, const void *optval, socklen_t optlen);
33 int getsockopt(int level, int optname, void *optval, socklen_t *optlen);
34
35 int setblocking(bool blocking);
36
37 bool ready() const { return false; }
38 int get_fd() const { return -1; }
39
40 protected:
42 int ip2sockaddr_(const ip_addr_t *ip, uint16_t port, struct sockaddr *name, socklen_t *addrlen);
43
46
47 struct udp_pcb *pcb_;
49};
50
56 public:
58 LWIPRawUDPImpl(sa_family_t family, struct udp_pcb *pcb);
60
67
69 int close();
70
74 ssize_t read(void *buf, size_t len);
78 ssize_t recvfrom(void *buf, size_t len, struct sockaddr *src_addr, socklen_t *addrlen);
79
82 bool ready() const { return this->rx_count_ > 0; }
83
85 uint16_t get_rx_dropped() const { return this->rx_dropped_; }
86
87 protected:
88 static void s_recv_fn(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port);
89 void recv_fn_(struct pbuf *p, const ip_addr_t *addr, u16_t port);
90
96 static constexpr uint8_t UDP_RX_QUEUE_SIZE = 4;
97 static constexpr uint8_t UDP_RX_MASK = UDP_RX_QUEUE_SIZE - 1;
98 static_assert((UDP_RX_QUEUE_SIZE & UDP_RX_MASK) == 0, "UDP_RX_QUEUE_SIZE must be power of 2");
99 // Fields are written by recv_fn_ before rx_count_ makes a slot visible
100 struct UDPRxPacket {
102 struct pbuf *pb;
103 uint16_t src_port;
104 };
105 std::array<UDPRxPacket, UDP_RX_QUEUE_SIZE> rx_queue_{};
106 uint16_t rx_dropped_{0};
107 uint8_t rx_read_idx_{0};
108 uint8_t rx_count_{0};
109};
110
111} // namespace esphome::socket
112
113#endif // USE_SOCKET_IMPL_LWIP_TCP
UDP socket with receive support for LWIP raw API.
static constexpr uint8_t UDP_RX_MASK
LWIPRawUDPImpl(sa_family_t family, struct udp_pcb *pcb)
Caller (the factory) must hold the lwip lock; registers the recv callback.
ssize_t recvfrom(void *buf, size_t len, struct sockaddr *src_addr, socklen_t *addrlen)
Read the next queued packet and return the source address.
int close()
Close the socket, flushing any queued rx packets first.
ssize_t read(void *buf, size_t len)
Read the next queued packet, discarding source address info.
bool ready() const
Returns true if there are packets available to read.
void recv_fn_(struct pbuf *p, const ip_addr_t *addr, u16_t port)
static void s_recv_fn(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
static constexpr uint8_t UDP_RX_QUEUE_SIZE
Ring buffer for received UDP packets.
std::array< UDPRxPacket, UDP_RX_QUEUE_SIZE > rx_queue_
uint16_t get_rx_dropped() const
Number of packets dropped because the rx queue was full (saturating).
Send-only UDP socket implementation for LWIP raw API.
LWIPRawUDPSendImpl(sa_family_t family, struct udp_pcb *pcb)
The pcb is allocated by the factory (like the TCP impl); never null here.
int getsockopt(int level, int optname, void *optval, socklen_t *optlen)
LWIPRawUDPSendImpl & operator=(const LWIPRawUDPSendImpl &)=delete
LWIPRawUDPSendImpl(const LWIPRawUDPSendImpl &)=delete
int bind(const struct sockaddr *name, socklen_t addrlen)
int setsockopt(int level, int optname, const void *optval, socklen_t optlen)
int ip2sockaddr_(const ip_addr_t *ip, uint16_t port, struct sockaddr *name, socklen_t *addrlen)
Convert lwip ip_addr_t and port to sockaddr.
int close_internal_locked_()
Shared close logic — removes the udp pcb. Caller must hold LWIP_LOCK.
ssize_t sendto(const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen)
Send a UDP packet to the specified destination.
uint16_t flags
uint32_t socklen_t
Definition headers.h:109
uint8_t sa_family_t
Definition headers.h:69
__int64 ssize_t
Definition httplib.h:178
const void size_t len
Definition hal.h:68