F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
TcpClientComponentImpl.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title TcpClientComponentImpl.cpp
3 // \author mstarch
4 // \brief cpp file for TcpClientComponentImpl component implementation class
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/FPrimeBasicTypes.hpp>
15 #include <limits>
16 #include "Fw/Types/Assert.hpp"
17 
18 namespace Drv {
19 
20 // ----------------------------------------------------------------------
21 // Construction, initialization, and destruction
22 // ----------------------------------------------------------------------
23 
25 
27  const U16 port,
28  const U32 send_timeout_seconds,
29  const U32 send_timeout_microseconds,
30  FwSizeType buffer_size) {
31  // Check that ensures the configured buffer size fits within the limits fixed-width type, U32
32  FW_ASSERT(buffer_size <= std::numeric_limits<U32>::max(), static_cast<FwAssertArgType>(buffer_size));
33  m_allocation_size = buffer_size; // Store the buffer size
34  return m_socket.configure(hostname, port, send_timeout_seconds, send_timeout_microseconds);
35 }
36 
38 
39 // ----------------------------------------------------------------------
40 // Implementations for socket read task virtual methods
41 // ----------------------------------------------------------------------
42 
44  return m_socket;
45 }
46 
48  return allocate_out(0, static_cast<U32>(m_allocation_size));
49 }
50 
53  if (status == SOCK_SUCCESS) {
54  recvStatus = ByteStreamStatus::OP_OK;
55  } else if (status == SOCK_NO_DATA_AVAILABLE) {
56  recvStatus = ByteStreamStatus::RECV_NO_DATA;
57  } else {
58  recvStatus = ByteStreamStatus::OTHER_ERROR;
59  }
60  this->recv_out(0, buffer, recvStatus);
61 }
62 
65  this->ready_out(0);
66  }
67 }
68 
69 // ----------------------------------------------------------------------
70 // Handler implementations for user-defined typed input ports
71 // ----------------------------------------------------------------------
72 
73 Drv::ByteStreamStatus TcpClientComponentImpl::send_handler(const FwIndexType portNum, Fw::Buffer& fwBuffer) {
74  FW_ASSERT_NO_OVERFLOW(fwBuffer.getSize(), U32);
75  Drv::SocketIpStatus status = send(fwBuffer.getData(), static_cast<U32>(fwBuffer.getSize()));
76  Drv::ByteStreamStatus returnStatus;
77  switch (status) {
79  returnStatus = ByteStreamStatus::SEND_RETRY;
80  break;
81  case SOCK_SUCCESS:
82  returnStatus = ByteStreamStatus::OP_OK;
83  break;
84  default:
85  returnStatus = ByteStreamStatus::OTHER_ERROR;
86  break;
87  }
88  return returnStatus;
89 }
90 
91 void TcpClientComponentImpl::recvReturnIn_handler(FwIndexType portNum, Fw::Buffer& fwBuffer) {
92  this->deallocate_out(0, fwBuffer);
93 }
94 
95 } // end namespace Drv
Fw::Buffer getBuffer() override
returns a buffer to fill with data
Interrupted status for retries.
Definition: IpSocket.hpp:36
PlatformSizeType FwSizeType
~TcpClientComponentImpl()
Destroy the component.
SocketIpStatus send(const U8 *const data, const U32 size)
send data to the IP socket from the given buffer
Auto-generated base for TcpClient component.
SocketIpStatus configure(const char *hostname, const U16 port, const U32 send_timeout_seconds=SOCKET_SEND_TIMEOUT_SECONDS, const U32 send_timeout_microseconds=SOCKET_SEND_TIMEOUT_MICROSECONDS, FwSizeType buffer_size=1024)
Configures the TcpClient settings but does not open the connection.
TcpClientComponentImpl(const char *const compName)
construct the TcpClient component.
U8 * getData() const
Definition: Buffer.cpp:56
void recv_out(FwIndexType portNum, Fw::Buffer &buffer, const Drv::ByteStreamStatus &status)
Invoke output port recv.
Error occurred, retrying may succeed.
Receive worked, but there was no data.
IpSocket & getSocketHandler() override
returns a reference to the socket handler
Status returned by the send call.
Socket operation successful.
Definition: IpSocket.hpp:30
Fw::Buffer allocate_out(FwIndexType portNum, FwSizeType size)
Invoke output port allocate.
Operation worked as expected.
void connected() override
called when the IPv4 system has been connected
void deallocate_out(FwIndexType portNum, Fw::Buffer &fwBuffer)
Invoke output port deallocate.
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
Definition: IpSocket.cpp:51
FwSizeType getSize() const
Definition: Buffer.cpp:60
PlatformIndexType FwIndexType
#define FW_ASSERT_NO_OVERFLOW(value, T)
Definition: Assert.hpp:49
SocketIpStatus
Status enumeration for socket return values.
Definition: IpSocket.hpp:29
Helper base-class for setting up Berkeley sockets.
Definition: IpSocket.hpp:57
No data available or read operation would block.
Definition: IpSocket.hpp:45
void sendBuffer(Fw::Buffer buffer, SocketIpStatus status) override
sends a buffer to be filled with data
#define FW_ASSERT(...)
Definition: Assert.hpp:14
bool isConnected_ready_OutputPort(FwIndexType portNum)
void ready_out(FwIndexType portNum)
Invoke output port ready.