F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
Drv::IpSocket Class Referenceabstract

Helper base-class for setting up Berkeley sockets. More...

#include <Drv/Ip/IpSocket.hpp>

Inheritance diagram for Drv::IpSocket:
Drv::TcpClientSocket Drv::TcpServerSocket Drv::UdpSocket

Public Member Functions

 IpSocket ()
 
virtual ~IpSocket ()
 
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 More...
 
bool isStarted ()
 Returns true when the socket is started. More...
 
bool isOpened ()
 check if IP socket has previously been opened More...
 
virtual SocketIpStatus startup ()
 startup the socket, a no-op on unless this is server More...
 
SocketIpStatus open ()
 open the IP socket for communications More...
 
SocketIpStatus send (const U8 *const data, const U32 size)
 send data out the IP socket from the given buffer More...
 
SocketIpStatus recv (U8 *const data, U32 &size)
 receive data from the IP socket from the given buffer More...
 
void close ()
 closes the socket More...
 
virtual void shutdown ()
 shutdown the socket More...
 

Protected Member Functions

virtual bool isValidPort (U16 port)
 Check if the given port is valid for the socket. More...
 
SocketIpStatus setupTimeouts (NATIVE_INT_TYPE socketFd)
 setup the socket timeout properties of the opened outgoing socket More...
 
virtual SocketIpStatus openProtocol (NATIVE_INT_TYPE &fd)=0
 Protocol specific open implementation, called from open. More...
 
virtual I32 sendProtocol (const U8 *const data, const U32 size)=0
 Protocol specific implementation of send. Called directly with retry from send. More...
 
virtual I32 recvProtocol (U8 *const data, const U32 size)=0
 Protocol specific implementation of recv. Called directly with error handling from recv. More...
 

Static Protected Member Functions

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. More...
 

Protected Attributes

Os::Mutex m_lock
 
NATIVE_INT_TYPE m_fd
 
U32 m_timeoutSeconds
 
U32 m_timeoutMicroseconds
 
U16 m_port
 IP address port used. More...
 
bool m_open
 Have we successfully opened. More...
 
bool m_started
 Have we successfully started the socket. More...
 
char m_hostname [SOCKET_MAX_HOSTNAME_SIZE]
 Hostname to supply. More...
 

Detailed Description

Helper base-class for setting up Berkeley sockets.

Certain IP headers have conflicting definitions with the m_data member of various types in fprime. TcpHelper separates the ip setup from the incoming Fw::Buffer in the primary component class preventing this collision.

Definition at line 47 of file IpSocket.hpp.

Constructor & Destructor Documentation

◆ IpSocket()

Drv::IpSocket::IpSocket ( )

Definition at line 49 of file IpSocket.cpp.

◆ ~IpSocket()

virtual Drv::IpSocket::~IpSocket ( )
inlinevirtual

Definition at line 50 of file IpSocket.hpp.

Member Function Documentation

◆ addressToIp4()

SocketIpStatus Drv::IpSocket::addressToIp4 ( const char *  address,
void *  ip4 
)
staticprotected

converts a given address in dot form x.x.x.x to an ip address. ONLY works for IPv4.

Parameters
addressaddress to convert
ip4IPv4 representation structure to fill
Returns
: status of conversion

Definition at line 86 of file IpSocket.cpp.

◆ close()

void Drv::IpSocket::close ( )

closes the socket

Closes the socket opened by the open call. In this case of the TcpServer, this does NOT close server's listening port (call shutdown) but will close the active client connection.

Definition at line 123 of file IpSocket.cpp.

◆ configure()

SocketIpStatus Drv::IpSocket::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

Configures the IP handler (Tcp, Tcp server, and Udp) to use the given hostname and port. When multiple ports are used for send/receive these settings affect the send direction (as is the case for udp). Hostname DNS translation is left up to the caller and thus hostname must be an IP address in dot-notation of the form "x.x.x.x". Port cannot be set to 0 as dynamic port assignment is not supported.

Note: for UDP sockets this is equivalent to configureSend and only sets up the transmission direction of the socket. A separate call to configureRecv is required to receive on the socket and should be made before the open call has been made.

Parameters
hostnamesocket uses for outgoing transmissions (and incoming when tcp). Must be of form x.x.x.x
portport socket uses for outgoing transmissions (and incoming when tcp). Must NOT be 0.
send_timeout_secondssend timeout seconds portion
send_timeout_microsecondssend timeout microseconds portion. Must be less than 1000000
Returns
status of configure

Definition at line 53 of file IpSocket.cpp.

◆ isOpened()

bool Drv::IpSocket::isOpened ( )

check if IP socket has previously been opened

Check if this IpSocket has previously been opened. In the case of Udp this will check for outgoing transmissions and (if configured) incoming transmissions as well. This does not guarantee errors will not occur when using this socket as the remote component may have disconnected.

Returns
true if socket is open, false otherwise

Definition at line 115 of file IpSocket.cpp.

◆ isStarted()

bool Drv::IpSocket::isStarted ( )

Returns true when the socket is started.

Returns true when the socket is started up sufficiently to be actively listening to clients. Returns false otherwise. This means startup() was called and returned success.

Definition at line 107 of file IpSocket.cpp.

◆ isValidPort()

bool Drv::IpSocket::isValidPort ( U16  port)
protectedvirtual

Check if the given port is valid for the socket.

Some ports should be allowed for sockets and disabled on others (e.g. port 0 is a valid tcp server port but not a client. This will check the port and return "true" if the port is valid, or "false" otherwise. In the default implementation, all ports are considered valid.

Parameters
portport to check
Returns
true if valid, false otherwise

Reimplemented in Drv::TcpClientSocket.

Definition at line 65 of file IpSocket.cpp.

◆ open()

SocketIpStatus Drv::IpSocket::open ( )

open the IP socket for communications

This will open the IP socket for communication. This method error checks and validates properties set using the configure method. Tcp sockets will open bidirectional communication assuming the configure function was previously called. Udp sockets allow configureRecv and configure/configureSend calls to configure for each direction separately and may be operated in a single-direction or bidirectional mode. This call returns a status of SOCK_SEND means the port is ready for transmissions and any other status should be treated as an error with the socket not capable of sending nor receiving. This method will properly close resources on any unsuccessful status.

In the case of server components (TcpServer) this function will block until a client has connected.

Note: delegates to openProtocol for protocol specific implementation

Returns
status of open

Definition at line 148 of file IpSocket.cpp.

◆ openProtocol()

virtual SocketIpStatus Drv::IpSocket::openProtocol ( NATIVE_INT_TYPE fd)
protectedpure virtual

Protocol specific open implementation, called from open.

Parameters
fd(output) file descriptor opened. Only valid on SOCK_SUCCESS. Otherwise will be invalid
Returns
status of open

Implemented in Drv::UdpSocket, Drv::TcpServerSocket, and Drv::TcpClientSocket.

◆ recv()

SocketIpStatus Drv::IpSocket::recv ( U8 *const  data,
U32 &  size 
)

receive data from the IP socket from the given buffer

Receives data from the IpSocket. Should the socket be unavailable, SOCK_DISCONNECTED will be returned and the socket should be reopened using the open call. This can happen even when the socket has already been opened should a transmission error/closure be detected. Since this blocks until data is available, it will retry as long as EINTR is set and less than a max number of iterations has passed. This function will block to receive data and will retry (up to a configured set of retries) as long as EINTR is returned.

Note: delegates to recvProtocol to send the data

Parameters
datapointer to data to fill with received data
sizemaximum size of data buffer to fill
Returns
status of the send, SOCK_DISCONNECTED to reopen, SOCK_SUCCESS on success, something else on error

Definition at line 207 of file IpSocket.cpp.

◆ recvProtocol()

virtual I32 Drv::IpSocket::recvProtocol ( U8 *const  data,
const U32  size 
)
protectedpure virtual

Protocol specific implementation of recv. Called directly with error handling from recv.

Parameters
datadata pointer to fill
sizesize of data buffer
Returns
: size of data received, or -1 on error.

Implemented in Drv::UdpSocket, Drv::TcpServerSocket, and Drv::TcpClientSocket.

◆ send()

SocketIpStatus Drv::IpSocket::send ( const U8 *const  data,
const U32  size 
)

send data out the IP socket from the given buffer

Sends data out of the IpSocket. This outgoing transmission will be retried several times if the transmission fails to send all the data. Retries are globally configured in the IpCfg.hpp header. Should the socket be unavailable, SOCK_DISCONNECTED is returned and the socket should be reopened using the open call. This can happen even when the socket has already been opened should a transmission error/closure be detected. Unless an error is received, all data will have been transmitted.

Note: delegates to sendProtocol to send the data

Parameters
datapointer to data to send
sizesize of data to send
Returns
status of the send, SOCK_DISCONNECTED to reopen, SOCK_SUCCESS on success, something else on error

Definition at line 168 of file IpSocket.cpp.

◆ sendProtocol()

virtual I32 Drv::IpSocket::sendProtocol ( const U8 *const  data,
const U32  size 
)
protectedpure virtual

Protocol specific implementation of send. Called directly with retry from send.

Parameters
datadata to send
sizesize of data to send
Returns
: size of data sent, or -1 on error.

Implemented in Drv::UdpSocket, Drv::TcpServerSocket, and Drv::TcpClientSocket.

◆ setupTimeouts()

SocketIpStatus Drv::IpSocket::setupTimeouts ( NATIVE_INT_TYPE  socketFd)
protected

setup the socket timeout properties of the opened outgoing socket

Parameters
socketFdfile descriptor to setup
Returns
status of timeout setup

Definition at line 69 of file IpSocket.cpp.

◆ shutdown()

void Drv::IpSocket::shutdown ( )
virtual

shutdown the socket

Closes the socket opened by the open call. In this case of the TcpServer, this does close server's listening port. This will shutdown all clients.

Reimplemented in Drv::TcpServerSocket.

Definition at line 134 of file IpSocket.cpp.

◆ startup()

SocketIpStatus Drv::IpSocket::startup ( )
virtual

startup the socket, a no-op on unless this is server

This will start-up the socket. In the case of most sockets, this is a no-op. On server sockets this binds to the server address and progresses through the listen step such that on open new clients may be accepted.

Returns
status of startup

Reimplemented in Drv::TcpServerSocket.

Definition at line 141 of file IpSocket.cpp.

Member Data Documentation

◆ m_fd

NATIVE_INT_TYPE Drv::IpSocket::m_fd
protected

Definition at line 216 of file IpSocket.hpp.

◆ m_hostname

char Drv::IpSocket::m_hostname[SOCKET_MAX_HOSTNAME_SIZE]
protected

Hostname to supply.

Definition at line 222 of file IpSocket.hpp.

◆ m_lock

Os::Mutex Drv::IpSocket::m_lock
protected

Definition at line 215 of file IpSocket.hpp.

◆ m_open

bool Drv::IpSocket::m_open
protected

Have we successfully opened.

Definition at line 220 of file IpSocket.hpp.

◆ m_port

U16 Drv::IpSocket::m_port
protected

IP address port used.

Definition at line 219 of file IpSocket.hpp.

◆ m_started

bool Drv::IpSocket::m_started
protected

Have we successfully started the socket.

Definition at line 221 of file IpSocket.hpp.

◆ m_timeoutMicroseconds

U32 Drv::IpSocket::m_timeoutMicroseconds
protected

Definition at line 218 of file IpSocket.hpp.

◆ m_timeoutSeconds

U32 Drv::IpSocket::m_timeoutSeconds
protected

Definition at line 217 of file IpSocket.hpp.


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