F´ Flight Software - C/C++ Documentation  devel
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 <FpConfig.hpp>
16 #include <IpCfg.hpp>
17 #include <Os/Mutex.hpp>
18 
19 namespace Drv {
20 
21 struct SocketDescriptor final {
24 };
25 
47 };
48 
55 class IpSocket {
56  public:
57  IpSocket();
58  virtual ~IpSocket(){};
77  SocketIpStatus configure(const char* hostname, const U16 port, const U32 send_timeout_seconds,
78  const U32 send_timeout_microseconds);
79 
98  SocketIpStatus open(SocketDescriptor& socketDescriptor);
115  SocketIpStatus send(const SocketDescriptor& socketDescriptor, const U8* const data, const U32 size);
132  SocketIpStatus recv(const SocketDescriptor& fd, U8* const data, U32& size);
133 
142  void close(const SocketDescriptor& socketDescriptor);
143 
155  void shutdown(const SocketDescriptor& socketDescriptor);
156 
157  PROTECTED:
168  virtual bool isValidPort(U16 port);
169 
176 
183  static SocketIpStatus addressToIp4(const char* address, void* ip4);
197  virtual I32 sendProtocol(const SocketDescriptor& socketDescriptor, const U8* const data, const U32 size) = 0;
198 
206  virtual I32 recvProtocol(const SocketDescriptor& socketDescriptor, U8* const data, const U32 size) = 0;
207 
210  U16 m_port;
212 };
213 } // namespace Drv
214 
215 #endif /* DRV_SOCKETIPDRIVER_SOCKETHELPER_HPP_ */
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:30
int PlatformIntType
DefaultTypes.hpp provides fallback defaults for the platform types.
C++-compatible configuration header for fprime configuration.
@ SOCKET_MAX_HOSTNAME_SIZE
Definition: IpCfg.hpp:22
Helper base-class for setting up Berkeley sockets.
Definition: IpSocket.hpp:55
U16 m_port
IP address port used.
Definition: IpSocket.hpp:210
virtual SocketIpStatus openProtocol(SocketDescriptor &fd)=0
Protocol specific open implementation, called from open.
virtual ~IpSocket()
Definition: IpSocket.hpp:58
char m_hostname[SOCKET_MAX_HOSTNAME_SIZE]
Hostname to supply.
Definition: IpSocket.hpp:211
SocketIpStatus setupTimeouts(PlatformIntType socketFd)
setup the socket timeout properties of the opened outgoing socket
Definition: IpSocket.cpp:68
SocketIpStatus open(SocketDescriptor &socketDescriptor)
open the IP socket for communications
Definition: IpSocket.cpp:119
U32 m_timeoutSeconds
Definition: IpSocket.hpp:208
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
Definition: IpSocket.cpp:53
virtual I32 sendProtocol(const SocketDescriptor &socketDescriptor, const U8 *const data, const U32 size)=0
Protocol specific implementation of send. Called directly with retry from send.
U32 m_timeoutMicroseconds
Definition: IpSocket.hpp:209
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.
Definition: IpSocket.cpp:85
virtual bool isValidPort(U16 port)
Check if the given port is valid for the socket.
Definition: IpSocket.cpp:64
virtual I32 recvProtocol(const SocketDescriptor &socketDescriptor, U8 *const data, const U32 size)=0
Protocol specific implementation of recv. Called directly with error handling from recv.
SocketIpStatus send(const SocketDescriptor &socketDescriptor, const U8 *const data, const U32 size)
send data out the IP socket from the given buffer
Definition: IpSocket.cpp:131
void shutdown(const SocketDescriptor &socketDescriptor)
shutdown the socket
Definition: IpSocket.cpp:110
SocketIpStatus recv(const SocketDescriptor &fd, U8 *const data, U32 &size)
receive data from the IP socket from the given buffer
Definition: IpSocket.cpp:163
void close(const SocketDescriptor &socketDescriptor)
closes the socket
Definition: IpSocket.cpp:106
SocketIpStatus
Status enumeration for socket return values.
Definition: IpSocket.hpp:29
@ SOCK_INVALID_IP_ADDRESS
Bad IP address supplied.
Definition: IpSocket.hpp:33
@ SOCK_FAILED_TO_GET_HOST_IP
Host IP lookup failed.
Definition: IpSocket.hpp:32
@ SOCK_FAILED_TO_ACCEPT
Failed to accept connection.
Definition: IpSocket.hpp:41
@ SOCK_SUCCESS
Socket operation successful.
Definition: IpSocket.hpp:30
@ SOCK_FAILED_TO_BIND
Failed to bind to socket.
Definition: IpSocket.hpp:39
@ SOCK_ANOTHER_THREAD_OPENING
Another thread is opening.
Definition: IpSocket.hpp:46
@ SOCK_DISCONNECTED
Failed to read socket with disconnect.
Definition: IpSocket.hpp:38
@ SOCK_READ_ERROR
Failed to read socket.
Definition: IpSocket.hpp:37
@ SOCK_FAILED_TO_SET_SOCKET_OPTIONS
Failed to configure socket.
Definition: IpSocket.hpp:35
@ SOCK_FAILED_TO_GET_SOCKET
Socket open failed.
Definition: IpSocket.hpp:31
@ SOCK_FAILED_TO_LISTEN
Failed to listen on socket.
Definition: IpSocket.hpp:40
@ SOCK_FAILED_TO_READ_BACK_PORT
Failed to read back port from connection.
Definition: IpSocket.hpp:44
@ SOCK_INTERRUPTED_TRY_AGAIN
Interrupted status for retries.
Definition: IpSocket.hpp:36
@ SOCK_FAILED_TO_CONNECT
Failed to connect socket.
Definition: IpSocket.hpp:34
@ SOCK_SEND_ERROR
Failed to send after configured retries.
Definition: IpSocket.hpp:42
@ SOCK_NO_DATA_AVAILABLE
No data available or read operation would block.
Definition: IpSocket.hpp:45
@ SOCK_NOT_STARTED
Socket has not been started.
Definition: IpSocket.hpp:43
PlatformIntType serverFd
Used for server sockets to track the listening file descriptor.
Definition: IpSocket.hpp:23
PlatformIntType fd
Used for all sockets to track the communication file descriptor.
Definition: IpSocket.hpp:22