F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
TcpClientSocket.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title TcpClientSocket.cpp
3 // \author mstarch
4 // \brief cpp file for TcpClientSocket 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 
14 #include <Fw/Logger/Logger.hpp>
15 #include <Fw/Types/Assert.hpp>
16 #include <FpConfig.hpp>
17 
18 #ifdef TGT_OS_TYPE_VXWORKS
19  #include <socket.h>
20  #include <inetLib.h>
21  #include <fioLib.h>
22  #include <hostLib.h>
23  #include <ioLib.h>
24  #include <vxWorks.h>
25  #include <sockLib.h>
26  #include <taskLib.h>
27  #include <sysLib.h>
28  #include <errnoLib.h>
29  #include <cstring>
30 #elif defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN
31  #include <sys/socket.h>
32  #include <unistd.h>
33  #include <arpa/inet.h>
34 #else
35  #error OS not supported for IP Socket Communications
36 #endif
37 
38 #include <cstdio>
39 #include <cstring>
40 
41 namespace Drv {
42 
44 
46  return port != 0;
47 }
48 
49 
51  NATIVE_INT_TYPE socketFd = -1;
52  struct sockaddr_in address;
53 
54  // Acquire a socket, or return error
55  if ((socketFd = ::socket(AF_INET, SOCK_STREAM, 0)) == -1) {
57  }
58  // Set up the address port and name
59  address.sin_family = AF_INET;
60  this->m_lock.lock();
61  address.sin_port = htons(this->m_port);
62  this->m_lock.unlock();
63 
64  // OS specific settings
65 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN
66  address.sin_len = static_cast<U8>(sizeof(struct sockaddr_in));
67 #endif
68 
69  // First IP address to socket sin_addr
70  if (IpSocket::addressToIp4(m_hostname, &(address.sin_addr)) != SOCK_SUCCESS) {
71  ::close(socketFd);
73  };
74 
75  // Now apply timeouts
76  if (IpSocket::setupTimeouts(socketFd) != SOCK_SUCCESS) {
77  ::close(socketFd);
79  }
80 
81  // TCP requires connect to the socket to allow for communication
82  if (::connect(socketFd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)) < 0) {
83  ::close(socketFd);
85  }
86  fd = socketFd;
87  Fw::Logger::logMsg("Connected to %s:%hu as a tcp client\n", reinterpret_cast<POINTER_CAST>(m_hostname), m_port);
88  return SOCK_SUCCESS;
89 }
90 
91 I32 TcpClientSocket::sendProtocol(const U8* const data, const U32 size) {
92  return static_cast<I32>(::send(this->m_fd, data, size, SOCKET_IP_SEND_FLAGS));
93 }
94 
95 I32 TcpClientSocket::recvProtocol(U8* const data, const U32 size) {
96  return static_cast<I32>(::recv(this->m_fd, data, size, SOCKET_IP_RECV_FLAGS));
97 }
98 
99 } // namespace Drv
PlatformPointerCastType POINTER_CAST
Definition: BasicTypes.h:53
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:51
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:26
C++-compatible configuration header for fprime configuration.
@ SOCKET_IP_RECV_FLAGS
Definition: IpCfg.hpp:20
@ SOCKET_IP_SEND_FLAGS
Definition: IpCfg.hpp:19
Helper base-class for setting up Berkeley sockets.
Definition: IpSocket.hpp:47
void close()
closes the socket
Definition: IpSocket.cpp:123
U16 m_port
IP address port used.
Definition: IpSocket.hpp:219
char m_hostname[SOCKET_MAX_HOSTNAME_SIZE]
Hostname to supply.
Definition: IpSocket.hpp:222
SocketIpStatus send(const U8 *const data, const U32 size)
send data out the IP socket from the given buffer
Definition: IpSocket.cpp:168
Os::Mutex m_lock
Definition: IpSocket.hpp:215
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:86
NATIVE_INT_TYPE m_fd
Definition: IpSocket.hpp:216
SocketIpStatus recv(U8 *const data, U32 &size)
receive data from the IP socket from the given buffer
Definition: IpSocket.cpp:207
SocketIpStatus setupTimeouts(NATIVE_INT_TYPE socketFd)
setup the socket timeout properties of the opened outgoing socket
Definition: IpSocket.cpp:69
I32 sendProtocol(const U8 *const data, const U32 size) override
Protocol specific implementation of send. Called directly with retry from send.
I32 recvProtocol(U8 *const data, const U32 size) override
Protocol specific implementation of recv. Called directly with error handling from recv.
TcpClientSocket()
Constructor for client socket tcp implementation.
SocketIpStatus openProtocol(NATIVE_INT_TYPE &fd) override
Tcp specific implementation for opening a client socket.
bool isValidPort(U16 port) override
Check if the given port is valid for the socket.
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)
Definition: Logger.cpp:18
void unlock()
alias for unLock to meet BasicLockable requirements
Definition: Mutex.hpp:16
void lock()
lock the mutex
Definition: Mutex.cpp:12
SocketIpStatus
Status enumeration for socket return values.
Definition: IpSocket.hpp:23
@ SOCK_INVALID_IP_ADDRESS
Bad IP address supplied.
Definition: IpSocket.hpp:27
@ SOCK_SUCCESS
Socket operation successful.
Definition: IpSocket.hpp:24
@ SOCK_FAILED_TO_SET_SOCKET_OPTIONS
Failed to configure socket.
Definition: IpSocket.hpp:29
@ SOCK_FAILED_TO_GET_SOCKET
Socket open failed.
Definition: IpSocket.hpp:25
@ SOCK_FAILED_TO_CONNECT
Failed to connect socket.
Definition: IpSocket.hpp:28