|
LibJuno 1.0.4
LibJuno is a lightweight C11 library designed specifically for embedded systems.
|
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"
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. | |
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.
|
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().
| ptRoot | Module root instance; must be non-NULL. |
JUNO_STATUS_SUCCESS always (after a successful guard check). | 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.
| ptUdp | Caller-owned module union storage; must be non-NULL. |
| ptCfg | Socket configuration (address, port, role); must be non-NULL. |
| pfcnFailureHandler | Diagnostic callback invoked before any error return; may be NULL. |
| pvFailureUserData | Opaque user data pointer passed to the failure handler; may be NULL. |
JUNO_STATUS_SUCCESS on success; non-zero on failure.
|
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.
| ptLinux | Linux derivation instance; must be non-NULL and pre-initialised. |
| ptCfg | Socket configuration; must be non-NULL. |
JUNO_STATUS_SUCCESS on success; JUNO_STATUS_ERR on POSIX failure.
|
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.
| ptRoot | Module root instance; socket must be open. |
| ptMsg | Output buffer; must be non-NULL. |
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.
|
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.
| ptRoot | Module root instance; socket must be open. |
| ptMsg | Message to send; must be non-NULL. |
JUNO_STATUS_SUCCESS on success; JUNO_STATUS_ERR on failure.
|
static |
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.