LibJuno 1.0.4
LibJuno is a lightweight C11 library designed specifically for embedded systems.
Loading...
Searching...
No Matches
linux_udp_impl.cpp File Reference

Linux/POSIX implementation of the UDP module vtable. More...

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "udp_linux.h"
#include "juno/status.h"
#include "juno/macros.h"
Include dependency graph for linux_udp_impl.cpp:

Functions

static JUNO_STATUS_T OpenSocket (JUNO_UDP_LINUX_T *ptLinux, const JUNO_UDP_CFG_T *ptCfg)
 Open and configure a UDP socket into the Linux derivation.
 
static JUNO_STATUS_T Send (JUNO_UDP_ROOT_T *ptRoot, const UDP_THREAD_MSG_T *ptMsg)
 Send exactly one UDP_THREAD_MSG_T datagram.
 
static JUNO_STATUS_T Receive (JUNO_UDP_ROOT_T *ptRoot, UDP_THREAD_MSG_T *ptMsg)
 Receive exactly one UDP_THREAD_MSG_T datagram.
 
static JUNO_STATUS_T Free (JUNO_UDP_ROOT_T *ptRoot)
 Close the UDP socket and reset the descriptor to -1 (RAII cleanup).
 
JUNO_STATUS_T JunoUdp_LinuxInit (JUNO_UDP_T *ptUdp, const JUNO_UDP_CFG_T *ptCfg, JUNO_FAILURE_HANDLER_T pfcnFailureHandler, void *pvFailureUserData)
 Initialise a Linux UDP module instance and open the socket (RAII).
 

Variables

static const JUNO_UDP_API_T s_tJunoUdpLinuxApi
 Statically allocated Linux/POSIX UDP vtable.
 

Detailed Description

Linux/POSIX implementation of the UDP module vtable.

This is the only translation unit that includes POSIX socket headers. It provides the static s_tJunoUdpLinuxApi vtable that implements Send, Receive, and Free using BSD sockets, and exports JunoUdp_LinuxInit as the single RAII entry point that wires the vtable and opens the socket in one call.

Socket file descriptor and address state reside in the JUNO_UDP_LINUX_T derivation, not in the freestanding root. Vtable functions down-cast the root pointer to JUNO_UDP_LINUX_T * to access that state.

All memory is caller-owned; this module allocates nothing.

Function Documentation

◆ Free()

static JUNO_STATUS_T Free ( JUNO_UDP_ROOT_T ptRoot)
static

Close the UDP socket and reset the descriptor to -1 (RAII cleanup).

Down-casts ptRoot to JUNO_UDP_LINUX_T to access the socket fd. Idempotent: if _iSockFd is already -1 (never opened or already freed), returns JUNO_STATUS_SUCCESS without calling close().

Parameters
ptRootModule root instance; must be non-NULL.
Returns
JUNO_STATUS_SUCCESS always (after a successful guard check).

◆ JunoUdp_LinuxInit()

JUNO_STATUS_T JunoUdp_LinuxInit ( JUNO_UDP_T ptUdp,
const JUNO_UDP_CFG_T ptCfg,
JUNO_FAILURE_HANDLER_T  pfcnFailureHandler,
void *  pvFailureUserData 
)

Initialise a Linux UDP module instance and open the socket (RAII).

Wires s_tJunoUdpLinuxApi into the root via JunoUdp_Init, stores the failure handler, initialises _iSockFd to -1, then calls OpenSocket to create and bind/connect the POSIX socket.

On failure the socket is not opened and the module is left in a safe state with _iSockFd = -1.

Parameters
ptUdpCaller-owned module union storage; must be non-NULL.
ptCfgSocket configuration (address, port, role); must be non-NULL.
pfcnFailureHandlerDiagnostic callback invoked before any error return; may be NULL.
pvFailureUserDataOpaque user data pointer passed to the failure handler; may be NULL.
Returns
JUNO_STATUS_SUCCESS on success; non-zero on failure.

◆ OpenSocket()

static JUNO_STATUS_T OpenSocket ( JUNO_UDP_LINUX_T ptLinux,
const JUNO_UDP_CFG_T ptCfg 
)
static

Open and configure a UDP socket into the Linux derivation.

Creates a SOCK_DGRAM socket. For receivers, binds to INADDR_ANY on the configured port. For senders, connects to the configured remote address and port (defaulting to loopback if ptCfg->pcAddress is NULL).

Stores the resulting file descriptor and address in ptLinux->_iSockFd and ptLinux->_tAddr respectively.

Parameters
ptLinuxLinux derivation instance; must be non-NULL and pre-initialised.
ptCfgSocket configuration; must be non-NULL.
Returns
JUNO_STATUS_SUCCESS on success; JUNO_STATUS_ERR on POSIX failure.

◆ Receive()

static JUNO_STATUS_T Receive ( JUNO_UDP_ROOT_T ptRoot,
UDP_THREAD_MSG_T ptMsg 
)
static

Receive exactly one UDP_THREAD_MSG_T datagram.

Down-casts ptRoot to JUNO_UDP_LINUX_T to access the socket fd. Blocks until a datagram arrives. On a system error with EAGAIN or EWOULDBLOCK, returns JUNO_STATUS_TIMEOUT_ERROR without invoking the failure handler — timeout is a normal, expected condition for polled receivers. On a datagram of unexpected size, returns JUNO_STATUS_INVALID_DATA_ERROR.

Parameters
ptRootModule root instance; socket must be open.
ptMsgOutput buffer; must be non-NULL.
Returns
JUNO_STATUS_SUCCESS on success; JUNO_STATUS_TIMEOUT_ERROR on timeout (not a failure); JUNO_STATUS_INVALID_DATA_ERROR on wrong datagram size; JUNO_STATUS_ERR on other POSIX error.

◆ Send()

static JUNO_STATUS_T Send ( JUNO_UDP_ROOT_T ptRoot,
const UDP_THREAD_MSG_T ptMsg 
)
static

Send exactly one UDP_THREAD_MSG_T datagram.

Down-casts ptRoot to JUNO_UDP_LINUX_T to access the socket fd. Transmits the full message in a single send() call. Returns JUNO_STATUS_ERR if fewer than sizeof(UDP_THREAD_MSG_T) bytes are sent.

Parameters
ptRootModule root instance; socket must be open.
ptMsgMessage to send; must be non-NULL.
Returns
JUNO_STATUS_SUCCESS on success; JUNO_STATUS_ERR on failure.

Variable Documentation

◆ s_tJunoUdpLinuxApi

const JUNO_UDP_API_T s_tJunoUdpLinuxApi
static
Initial value:
=
{
}
static JUNO_STATUS_T Send(JUNO_UDP_ROOT_T *ptRoot, const UDP_THREAD_MSG_T *ptMsg)
Send exactly one UDP_THREAD_MSG_T datagram.
Definition linux_udp_impl.cpp:142
static JUNO_STATUS_T Receive(JUNO_UDP_ROOT_T *ptRoot, UDP_THREAD_MSG_T *ptMsg)
Receive exactly one UDP_THREAD_MSG_T datagram.
Definition linux_udp_impl.cpp:174
static JUNO_STATUS_T Free(JUNO_UDP_ROOT_T *ptRoot)
Close the UDP socket and reset the descriptor to -1 (RAII cleanup).
Definition linux_udp_impl.cpp:209

Statically allocated Linux/POSIX UDP vtable.

Wired into module roots by JunoUdp_LinuxInit. Callers do not reference this object directly; it is an implementation detail of this translation unit. This object has static storage duration and must remain valid for the lifetime of all roots initialised with it.