F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
IpSocket.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title IpSocket.hpp
3 // \author mstarch
4 // \brief hpp file for IpSocket core implementation classes
5 //
6 // \copyright
7 // Copyright 2009-2020, by the California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 #ifndef DRV_IP_IPHELPER_HPP_
13 #define DRV_IP_IPHELPER_HPP_
14 
15 #include <Fw/FPrimeBasicTypes.hpp>
16 #include <Os/Mutex.hpp>
17 #include <config/IpCfg.hpp>
18 
19 namespace Drv {
20 
21 struct SocketDescriptor final {
22  int fd = -1;
23  int serverFd = -1;
24 };
25 
49 };
50 
57 class IpSocket {
58  public:
59  IpSocket();
60  virtual ~IpSocket() {};
87  virtual SocketIpStatus configure(const char* const ipv4_address,
88  const U16 port,
89  const U32 send_timeout_seconds,
90  const U32 send_timeout_microseconds);
91 
110  SocketIpStatus open(SocketDescriptor& socketDescriptor);
127  virtual SocketIpStatus send(const SocketDescriptor& socketDescriptor, const U8* const data, const FwSizeType size);
144  SocketIpStatus recv(const SocketDescriptor& fd, U8* const data, FwSizeType& size);
145 
154  void close(const SocketDescriptor& socketDescriptor);
155 
167  void shutdown(const SocketDescriptor& socketDescriptor);
168 
169  protected:
180  virtual bool isValidPort(U16 port);
181 
187  SocketIpStatus setupTimeouts(int socketFd);
188 
201  static SocketIpStatus addressToIp4(const char* const ipv4_address, void* const out);
215  virtual FwSignedSizeType sendProtocol(const SocketDescriptor& socketDescriptor,
216  const U8* const data,
217  const FwSizeType size) = 0;
218 
226  virtual FwSignedSizeType recvProtocol(const SocketDescriptor& socketDescriptor,
227  U8* const data,
228  const FwSizeType size) = 0;
229 
240 
246  SocketIpStatus setupSocketOptions(int socketFd);
247 
250  U16 m_port;
252 };
253 } // namespace Drv
254 
255 #endif /* DRV_SOCKETIPDRIVER_SOCKETHELPER_HPP_ */
Failed to send after configured retries.
Definition: IpSocket.hpp:42
virtual bool isValidPort(U16 port)
Check if the given port is valid for the socket.
Definition: IpSocket.cpp:71
Failed to read socket with disconnect.
Definition: IpSocket.hpp:38
Socket open failed.
Definition: IpSocket.hpp:31
Interrupted status for retries.
Definition: IpSocket.hpp:36
Failed to connect socket.
Definition: IpSocket.hpp:34
Failed to configure socket.
Definition: IpSocket.hpp:35
PlatformSizeType FwSizeType
SocketIpStatus recv(const SocketDescriptor &fd, U8 *const data, FwSizeType &size)
receive data from the IP socket from the given buffer
Definition: IpSocket.cpp:179
Host IP lookup failed.
Definition: IpSocket.hpp:32
SocketIpStatus setupTimeouts(int socketFd)
setup the socket timeout properties of the opened outgoing socket
Definition: IpSocket.cpp:75
virtual SocketIpStatus configure(const char *const ipv4_address, const U16 port, const U32 send_timeout_seconds, const U32 send_timeout_microseconds)
configure the ip socket with an IPv4 address and transmission timeouts
Definition: IpSocket.cpp:51
static SocketIpStatus addressToIp4(const char *const ipv4_address, void *const out)
converts a given IPv4 address in dotted-quad form "x.x.x.x" to a network-order in_addr structure...
Definition: IpSocket.cpp:92
virtual ~IpSocket()
Definition: IpSocket.hpp:60
Automatic connections are disabled.
Definition: IpSocket.hpp:47
int fd
Used for all sockets to track the communication file descriptor.
Definition: IpSocket.hpp:22
PlatformSignedSizeType FwSignedSizeType
virtual SocketIpStatus send(const SocketDescriptor &socketDescriptor, const U8 *const data, const FwSizeType size)
send data out the IP socket from the given buffer
Definition: IpSocket.cpp:144
Bad IP address supplied.
Definition: IpSocket.hpp:33
Another thread is opening.
Definition: IpSocket.hpp:46
virtual SocketIpStatus openProtocol(SocketDescriptor &fd)=0
Protocol specific open implementation, called from open.
void shutdown(const SocketDescriptor &socketDescriptor)
shutdown the socket
Definition: IpSocket.cpp:123
Failed to listen on socket.
Definition: IpSocket.hpp:40
virtual FwSignedSizeType sendProtocol(const SocketDescriptor &socketDescriptor, const U8 *const data, const FwSizeType size)=0
Protocol specific implementation of send. Called directly with retry from send.
U16 m_port
IP address port used.
Definition: IpSocket.hpp:250
Failed to accept connection.
Definition: IpSocket.hpp:41
Socket operation successful.
Definition: IpSocket.hpp:30
Failed to read socket.
Definition: IpSocket.hpp:37
SocketIpStatus setupSocketOptions(int socketFd)
setup the socket options of the input socket as defined in IpCfg.hpp
Definition: IpSocket.cpp:229
Failed to read back port from connection.
Definition: IpSocket.hpp:44
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:53
Operation is invalid.
Definition: IpSocket.hpp:48
virtual SocketIpStatus handleZeroReturn()
Handle zero return from recvProtocol.
Definition: IpSocket.cpp:223
char m_ipv4_address[SOCKET_MAX_IPV4_ADDRESS_SIZE]
IPv4 address (dotted-quad "x.x.x.x")
Definition: IpSocket.hpp:251
void close(const SocketDescriptor &socketDescriptor)
closes the socket
Definition: IpSocket.cpp:119
virtual FwSignedSizeType recvProtocol(const SocketDescriptor &socketDescriptor, U8 *const data, const FwSizeType size)=0
Protocol specific implementation of recv. Called directly with error handling from recv...
SocketIpStatus open(SocketDescriptor &socketDescriptor)
open the IP socket for communications
Definition: IpSocket.cpp:132
SocketIpStatus
Status enumeration for socket return values.
Definition: IpSocket.hpp:29
Socket has not been started.
Definition: IpSocket.hpp:43
U32 m_timeoutMicroseconds
Definition: IpSocket.hpp:249
Helper base-class for setting up Berkeley sockets.
Definition: IpSocket.hpp:57
No data available or read operation would block.
Definition: IpSocket.hpp:45
Failed to bind to socket.
Definition: IpSocket.hpp:39
int serverFd
Used for server sockets to track the listening file descriptor.
Definition: IpSocket.hpp:23
U32 m_timeoutSeconds
Definition: IpSocket.hpp:248