ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
esphome::socket::LWIPRawUDPImpl Class Reference

UDP socket with receive support for LWIP raw API. More...

#include <lwip_raw_udp_impl.h>

Inheritance diagram for esphome::socket::LWIPRawUDPImpl:
esphome::socket::LWIPRawUDPSendImpl

Data Structures

struct  UDPRxPacket
 

Public Member Functions

 LWIPRawUDPImpl (sa_family_t family, struct udp_pcb *pcb)
 Caller (the factory) must hold the lwip lock; registers the recv callback.
 
 ~LWIPRawUDPImpl ()
 
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.
 
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.
 
bool ready () const
 Returns true if there are packets available to read.
 
uint16_t get_rx_dropped () const
 Number of packets dropped because the rx queue was full (saturating).
 
int bind (const struct sockaddr *name, socklen_t addrlen)
 
int get_fd () const
 
int getsockopt (int level, int optname, void *optval, socklen_t *optlen)
 
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.
 
int setblocking (bool blocking)
 
int setsockopt (int level, int optname, const void *optval, socklen_t optlen)
 

Protected Member Functions

void recv_fn_ (struct pbuf *p, const ip_addr_t *addr, u16_t port)
 

Static Protected Member Functions

static void s_recv_fn (void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
 

Protected Attributes

std::array< UDPRxPacket, UDP_RX_QUEUE_SIZE > rx_queue_ {}
 
uint16_t rx_dropped_ {0}
 
uint8_t rx_read_idx_ {0}
 
uint8_t rx_count_ {0}
 

Static Protected Attributes

static constexpr uint8_t UDP_RX_QUEUE_SIZE = 4
 Ring buffer for received UDP packets.
 
static constexpr uint8_t UDP_RX_MASK = UDP_RX_QUEUE_SIZE - 1
 

Detailed Description

UDP socket with receive support for LWIP raw API.

Extends LWIPRawUDPSendImpl with a fixed-size ring buffer for incoming packets. Inheritance is private (base dtor is non-virtual; converting to a base pointer would leak queued pbufs on destruction).

Definition at line 55 of file lwip_raw_udp_impl.h.

Constructor & Destructor Documentation

◆ LWIPRawUDPImpl()

esphome::socket::LWIPRawUDPImpl::LWIPRawUDPImpl ( sa_family_t family,
struct udp_pcb * pcb )

Caller (the factory) must hold the lwip lock; registers the recv callback.

Definition at line 205 of file lwip_raw_udp_impl.cpp.

◆ ~LWIPRawUDPImpl()

esphome::socket::LWIPRawUDPImpl::~LWIPRawUDPImpl ( )

Definition at line 210 of file lwip_raw_udp_impl.cpp.

Member Function Documentation

◆ bind()

int esphome::socket::LWIPRawUDPSendImpl::bind ( const struct sockaddr * name,
socklen_t addrlen )

Definition at line 26 of file lwip_raw_udp_impl.cpp.

◆ close()

int esphome::socket::LWIPRawUDPImpl::close ( )

Close the socket, flushing any queued rx packets first.

Definition at line 216 of file lwip_raw_udp_impl.cpp.

◆ get_fd()

int esphome::socket::LWIPRawUDPSendImpl::get_fd ( ) const
inline

Definition at line 38 of file lwip_raw_udp_impl.h.

◆ get_rx_dropped()

uint16_t esphome::socket::LWIPRawUDPImpl::get_rx_dropped ( ) const
inline

Number of packets dropped because the rx queue was full (saturating).

Definition at line 85 of file lwip_raw_udp_impl.h.

◆ getsockopt()

int esphome::socket::LWIPRawUDPSendImpl::getsockopt ( int level,
int optname,
void * optval,
socklen_t * optlen )

Definition at line 33 of file lwip_raw_udp_impl.cpp.

◆ read()

ssize_t esphome::socket::LWIPRawUDPImpl::read ( void * buf,
size_t len )

Read the next queued packet, discarding source address info.

If buf is smaller than the packet, data is silently truncated (returns bytes copied). Note: unlike POSIX MSG_TRUNC, this does not return the original packet length on truncation.

Definition at line 231 of file lwip_raw_udp_impl.cpp.

◆ ready()

bool esphome::socket::LWIPRawUDPImpl::ready ( ) const
inline

Returns true if there are packets available to read.

Intentionally unlocked — same rationale as LWIPRawImpl::ready().

Definition at line 82 of file lwip_raw_udp_impl.h.

◆ recv_fn_()

void esphome::socket::LWIPRawUDPImpl::recv_fn_ ( struct pbuf * p,
const ip_addr_t * addr,
u16_t port )
protected

Definition at line 271 of file lwip_raw_udp_impl.cpp.

◆ recvfrom()

ssize_t esphome::socket::LWIPRawUDPImpl::recvfrom ( void * buf,
size_t len,
struct sockaddr * src_addr,
socklen_t * addrlen )

Read the next queued packet and return the source address.

If buf is smaller than the packet, data is silently truncated (returns bytes copied). Note: unlike POSIX MSG_TRUNC, this does not return the original packet length on truncation.

Definition at line 233 of file lwip_raw_udp_impl.cpp.

◆ s_recv_fn()

void esphome::socket::LWIPRawUDPImpl::s_recv_fn ( void * arg,
struct udp_pcb * pcb,
struct pbuf * p,
const ip_addr_t * addr,
u16_t port )
staticprotected

Definition at line 263 of file lwip_raw_udp_impl.cpp.

◆ sendto()

ssize_t esphome::socket::LWIPRawUDPSendImpl::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.

Definition at line 30 of file lwip_raw_udp_impl.cpp.

◆ setblocking()

int esphome::socket::LWIPRawUDPSendImpl::setblocking ( bool blocking)

Definition at line 35 of file lwip_raw_udp_impl.cpp.

◆ setsockopt()

int esphome::socket::LWIPRawUDPSendImpl::setsockopt ( int level,
int optname,
const void * optval,
socklen_t optlen )

Definition at line 32 of file lwip_raw_udp_impl.cpp.

Field Documentation

◆ rx_count_

uint8_t esphome::socket::LWIPRawUDPImpl::rx_count_ {0}
protected

Definition at line 108 of file lwip_raw_udp_impl.h.

◆ rx_dropped_

uint16_t esphome::socket::LWIPRawUDPImpl::rx_dropped_ {0}
protected

Definition at line 106 of file lwip_raw_udp_impl.h.

◆ rx_queue_

std::array<UDPRxPacket, UDP_RX_QUEUE_SIZE> esphome::socket::LWIPRawUDPImpl::rx_queue_ {}
protected

Definition at line 105 of file lwip_raw_udp_impl.h.

◆ rx_read_idx_

uint8_t esphome::socket::LWIPRawUDPImpl::rx_read_idx_ {0}
protected

Definition at line 107 of file lwip_raw_udp_impl.h.

◆ UDP_RX_MASK

uint8_t esphome::socket::LWIPRawUDPImpl::UDP_RX_MASK = UDP_RX_QUEUE_SIZE - 1
staticconstexprprotected

Definition at line 97 of file lwip_raw_udp_impl.h.

◆ UDP_RX_QUEUE_SIZE

uint8_t esphome::socket::LWIPRawUDPImpl::UDP_RX_QUEUE_SIZE = 4
staticconstexprprotected

Ring buffer for received UDP packets.

Both producer (recv callback) and consumer (main loop) are serialized by the lwip lock — the callback runs under lwip core lock, and consumer methods hold LWIP_LOCK(). All 4 slots are usable (no wasted slot for full/empty distinction). No heap allocation in the recv callback — packets are dropped if the queue is full.

Definition at line 96 of file lwip_raw_udp_impl.h.


The documentation for this class was generated from the following files: