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 78 this->m_recv_port = port;
84 U16 port = this->m_recv_port;
90 struct sockaddr_in address;
94 address.sin_family = AF_INET;
95 address.sin_port = htons(this->m_recv_port);
97 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN 98 address.sin_len =
static_cast<U8>(
sizeof(
struct sockaddr_in));
106 if (::
bind(fd, reinterpret_cast<struct sockaddr*>(&address),
sizeof(address)) < 0) {
110 socklen_t size =
sizeof(address);
111 if (::getsockname(fd, reinterpret_cast<struct sockaddr *>(&address), &size) == -1) {
115 FW_ASSERT(
sizeof(this->m_state->
m_addr_recv) ==
sizeof(address),
sizeof(this->m_state->m_addr_recv),
sizeof(address));
116 memcpy(&this->m_state->
m_addr_recv, &address,
sizeof(this->m_state->m_addr_recv));
124 struct sockaddr_in address;
129 if ((socketFd = ::socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
136 address.sin_family = AF_INET;
137 address.sin_port = htons(this->
m_port);
140 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN 141 address.sin_len =
static_cast<U8>(
sizeof(
struct sockaddr_in));
155 FW_ASSERT(
sizeof(this->m_state->
m_addr_send) ==
sizeof(address),
sizeof(this->m_state->m_addr_send),
157 memcpy(&this->m_state->
m_addr_send, &address,
sizeof(this->m_state->m_addr_send));
161 U16 recv_port = this->m_recv_port;
162 if (recv_port != 0) {
163 status = this->
bind(socketFd);
166 (void) ::
close(socketFd);
172 if ((port == 0) && (recv_port > 0)) {
173 Fw::Logger::log(
"Setup to only receive udp at %s:%hu\n", m_recv_hostname,
175 }
else if ((port > 0) && (recv_port == 0)) {
178 }
else if ((port > 0) && (recv_port > 0)) {
187 FW_ASSERT(port > 0 || recv_port > 0, port, recv_port);
190 socketDescriptor.
fd = socketFd;
197 reinterpret_cast<struct sockaddr *>(&this->m_state->m_addr_send),
sizeof(this->m_state->m_addr_send)));
202 return static_cast<I32
>(::recvfrom(socketDescriptor.
fd, data, size,
SOCKET_IP_RECV_FLAGS,
nullptr,
nullptr));
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.
U16 getRecvPort()
get the port being received on
virtual bool isValidPort(U16 port)
Check if the given port is valid for the socket.
PlatformIntType NATIVE_INT_TYPE
static void log(const char *format,...)
log a formated string with supplied arguments
SocketIpStatus configureRecv(const char *hostname, const U16 port)
configure the udp socket for incoming transmissions
struct sockaddr_in m_addr_recv
UDP server address, maybe unused.
virtual ~UdpSocket()
to cleanup state created at instantiation
SocketIpStatus bind(const PlatformIntType fd)
bind the UDP to a port such that it can receive packets at the previously configured port ...
int PlatformIntType
DefaultTypes.hpp provides fallback defaults for the platform types.
struct sockaddr_in m_addr_send
UDP server address, maybe unused.
U16 m_port
IP address port used.
I32 sendProtocol(const SocketDescriptor &socketDescriptor, const U8 *const data, const U32 size) override
Protocol specific implementation of send. Called directly with retry from send.
I32 recvProtocol(const SocketDescriptor &socketDescriptor, U8 *const data, const U32 size) override
Protocol specific implementation of recv. Called directly with error handling from recv...
Socket operation successful.
UdpSocket()
Constructor for client socket udp implementation.
char * string_copy(char *destination, const char *source, FwSizeType num)
copy string with null-termination guaranteed
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
Failed to read back port from connection.
SocketIpStatus setupTimeouts(PlatformIntType socketFd)
setup the socket timeout properties of the opened outgoing socket
C++-compatible configuration header for fprime configuration.
uint8_t U8
8-bit unsigned integer
virtual 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
PlatformIntType fd
Used for all sockets to track the communication file descriptor.
void close(const SocketDescriptor &socketDescriptor)
closes the socket
char m_hostname[SOCKET_MAX_HOSTNAME_SIZE]
Hostname to supply.
SocketIpStatus
Status enumeration for socket return values.
Helper base-class for setting up Berkeley sockets.
Failed to bind to socket.
SocketIpStatus configure(const char *hostname, const U16 port, const U32 send_timeout_seconds, const U32 send_timeout_microseconds) override
configure is disabled
SocketIpStatus openProtocol(SocketDescriptor &socketDescriptor) override
udp specific implementation for opening a socket.