18#ifdef TGT_OS_TYPE_VXWORKS
30#elif defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN
31 #include <sys/socket.h>
33 #include <arpa/inet.h>
35 #error OS not supported for IP Socket Communications
68 this->m_recv_port = port;
75 struct sockaddr_in address;
80 address.sin_family = AF_INET;
81 address.sin_port = htons(m_recv_port);
83#if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN
84 address.sin_len =
static_cast<U8>(
sizeof(
struct sockaddr_in));
92 if (
::bind(fd,
reinterpret_cast<struct sockaddr*
>(&address),
sizeof(address)) < 0) {
95 FW_ASSERT(
sizeof(this->m_state->
m_addr_recv) ==
sizeof(address),
sizeof(this->m_state->m_addr_recv),
sizeof(address));
96 memcpy(&this->m_state->
m_addr_recv, &address,
sizeof(this->m_state->m_addr_recv));
103 struct sockaddr_in address;
106 if (
m_port == 0 && m_recv_port == 0) {
111 if ((socketFd = ::socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
118 address.sin_family = AF_INET;
119 address.sin_port = htons(this->
m_port);
122#if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN
123 address.sin_len =
static_cast<U8>(
sizeof(
struct sockaddr_in));
138 FW_ASSERT(
sizeof(this->m_state->
m_addr_send) ==
sizeof(address),
sizeof(this->m_state->m_addr_send),
140 memcpy(&this->m_state->
m_addr_send, &address,
sizeof(this->m_state->m_addr_send));
144 if ((m_recv_port != 0) && ((status = this->
bind(socketFd)) !=
SOCK_SUCCESS)) {
148 const char* actions = (m_recv_port != 0 &&
m_port != 0) ?
"send and receive" : ((
m_port != 0) ?
"send":
"receive");
159 reinterpret_cast<struct sockaddr *
>(&this->m_state->
m_addr_send),
sizeof(this->m_state->m_addr_send));
PlatformPointerCastType POINTER_CAST
PlatformIntType NATIVE_INT_TYPE
uint8_t U8
8-bit unsigned integer
C++-compatible configuration header for fprime configuration.
@ SOCKET_MAX_HOSTNAME_SIZE
Helper base-class for setting up Berkley sockets.
void close()
closes the socket
U16 m_port
IP address port used.
char m_hostname[SOCKET_MAX_HOSTNAME_SIZE]
Hostname to supply.
SocketIpStatus configure(const char *hostname, const U16 port, const U32 send_timeout_seconds, const U32 send_timeout_microseconds)
configure the ip socket with host and transmission timeouts
static SocketIpStatus addressToIp4(const char *address, void *ip4)
converts a given address in dot form x.x.x.x to an ip address. ONLY works for IPv4.
SocketIpStatus setupTimeouts(NATIVE_INT_TYPE socketFd)
setup the socket timeout properties of the opened outgoing socket
SocketIpStatus bind(NATIVE_INT_TYPE fd)
bind the UDP to a port such that it can receive packets at the previously configured port
I32 sendProtocol(const U8 *const data, const U32 size)
Protocol specific implementation of send. Called directly with retry from send.
UdpSocket()
Constructor for client socket udp implementation.
I32 recvProtocol(U8 *const data, const U32 size)
Protocol specific implementation of recv. Called directly with error handling from recv.
SocketIpStatus openProtocol(NATIVE_INT_TYPE &fd)
udp specific implementation for opening a socket.
SocketIpStatus configureRecv(const char *hostname, const U16 port)
configure the udp socket for incoming transmissions
SocketIpStatus configureSend(const char *hostname, const U16 port, const U32 send_timeout_seconds, const U32 send_timeout_microseconds)
configure the udp socket for outgoing transmissions
virtual ~UdpSocket()
to cleanup state created at instantiation
static void logMsg(const char *fmt, POINTER_CAST a0=0, POINTER_CAST a1=0, POINTER_CAST a2=0, POINTER_CAST a3=0, POINTER_CAST a4=0, POINTER_CAST a5=0, POINTER_CAST a6=0, POINTER_CAST a7=0, POINTER_CAST a8=0, POINTER_CAST a9=0)
SocketIpStatus
Status enumeration for socket return values.
@ SOCK_INVALID_IP_ADDRESS
Bad IP address supplied.
@ SOCK_SUCCESS
Socket operation successful.
@ SOCK_FAILED_TO_BIND
Failed to bind to socket.
@ SOCK_FAILED_TO_GET_SOCKET
Socket open failed.
char * string_copy(char *destination, const char *source, U32 num)
copy string with null-termination guaranteed
struct sockaddr_in m_addr_send
UDP server address, maybe unused.
struct sockaddr_in m_addr_recv
UDP server address, maybe unused.