F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
UdpComponentImpl.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title UdpComponentImpl.cpp
3 // \author mstarch
4 // \brief cpp file for UdpComponentImpl 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 
13 #include <limits>
15 #include <IpCfg.hpp>
16 #include <FpConfig.hpp>
17 #include "Fw/Types/Assert.hpp"
18 
19 
20 namespace Drv {
21 
22 // ----------------------------------------------------------------------
23 // Construction, initialization, and destruction
24 // ----------------------------------------------------------------------
25 
26 UdpComponentImpl::UdpComponentImpl(const char* const compName)
27  : UdpComponentBase(compName) {}
28 
30  const U16 port,
31  const U32 send_timeout_seconds,
32  const U32 send_timeout_microseconds) {
33  return m_socket.configureSend(hostname, port, send_timeout_seconds, send_timeout_microseconds);
34 }
35 
36 SocketIpStatus UdpComponentImpl::configureRecv(const char* hostname, const U16 port, FwSizeType buffer_size) {
37  FW_ASSERT(buffer_size <= std::numeric_limits<U32>::max(), static_cast<FwAssertArgType>(buffer_size));
38  m_allocation_size = buffer_size; // Store the buffer size
39 
40  return m_socket.configureRecv(hostname, port);
41 }
42 
44 
46  return this->m_socket.getRecvPort();
47 }
48 
49 // ----------------------------------------------------------------------
50 // Implementations for socket read task virtual methods
51 // ----------------------------------------------------------------------
52 
54  return m_socket;
55 }
56 
58  return allocate_out(0, static_cast<U32>(m_allocation_size));
59 }
60 
63  if (status == SOCK_SUCCESS) {
64  recvStatus = RecvStatus::RECV_OK;
65  }
66  else if (status == SOCK_NO_DATA_AVAILABLE) {
67  recvStatus = RecvStatus::RECV_NO_DATA;
68  }
69  else {
70  recvStatus = RecvStatus::RECV_ERROR;
71  }
72  this->recv_out(0, buffer, recvStatus);
73 }
74 
77  this->ready_out(0);
78  }
79 }
80 
81 // ----------------------------------------------------------------------
82 // Handler implementations for user-defined typed input ports
83 // ----------------------------------------------------------------------
84 
85 Drv::SendStatus UdpComponentImpl::send_handler(const NATIVE_INT_TYPE portNum, Fw::Buffer& fwBuffer) {
86  Drv::SocketIpStatus status = send(fwBuffer.getData(), fwBuffer.getSize());
87  // Always return the buffer
88  deallocate_out(0, fwBuffer);
89  if ((status == SOCK_DISCONNECTED) || (status == SOCK_INTERRUPTED_TRY_AGAIN)) {
91  } else if (status != SOCK_SUCCESS) {
93  }
94  return SendStatus::SEND_OK;
95 }
96 
97 } // end namespace Drv
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:55
PlatformAssertArgType FwAssertArgType
Definition: FpConfig.h:39
PlatformSizeType FwSizeType
Definition: FpConfig.h:35
C++-compatible configuration header for fprime configuration.
Helper base-class for setting up Berkeley sockets.
Definition: IpSocket.hpp:55
Status associated with the received data.
@ RECV_OK
Receive worked as expected.
@ RECV_ERROR
Receive error occurred retrying may succeed.
@ RECV_NO_DATA
Receive worked, but there was no data.
Status returned by the send call.
@ SEND_ERROR
Send error occurred retrying may succeed.
@ SEND_RETRY
Data send should be retried.
@ SEND_OK
Send worked as expected.
SocketIpStatus send(const U8 *const data, const U32 size)
send data to the IP socket from the given buffer
Auto-generated base for Udp component.
void deallocate_out(FwIndexType portNum, Fw::Buffer &fwBuffer)
Invoke output port deallocate.
void recv_out(FwIndexType portNum, Fw::Buffer &recvBuffer, const Drv::RecvStatus &recvStatus)
Invoke output port recv.
void ready_out(FwIndexType portNum)
Invoke output port ready.
Fw::Buffer allocate_out(FwIndexType portNum, U32 size)
Invoke output port allocate.
bool isConnected_ready_OutputPort(FwIndexType portNum)
void sendBuffer(Fw::Buffer buffer, SocketIpStatus status)
sends a buffer to be filled with data
Fw::Buffer getBuffer()
returns a buffer to fill with data
IpSocket & getSocketHandler()
returns a reference to the socket handler
SocketIpStatus configureRecv(const char *hostname, const U16 port, FwSizeType buffer_size=1024)
Configures the Udp receive settings but does not open the connection.
void connected()
called when the IPv4 system has been connected
U16 getRecvPort()
get the port being received on
~UdpComponentImpl()
Destroy the component.
UdpComponentImpl(const char *const compName)
construct the TcpClient component.
SocketIpStatus configureSend(const char *hostname, const U16 port, const U32 send_timeout_seconds=SOCKET_SEND_TIMEOUT_SECONDS, const U32 send_timeout_microseconds=SOCKET_SEND_TIMEOUT_MICROSECONDS)
Configures the Udp send settings but does not open the connection.
U16 getRecvPort()
get the port being received on
Definition: UdpSocket.cpp:77
SocketIpStatus configureRecv(const char *hostname, const U16 port)
configure the udp socket for incoming transmissions
Definition: UdpSocket.cpp:69
SocketIpStatus configureSend(const char *hostname, const U16 port, const U32 send_timeout_seconds, const U32 send_timeout_microseconds)
configure the udp socket for outgoing transmissions
Definition: UdpSocket.cpp:62
U8 * getData() const
Definition: Buffer.cpp:68
U32 getSize() const
Definition: Buffer.cpp:72
SocketIpStatus
Status enumeration for socket return values.
Definition: IpSocket.hpp:29
@ SOCK_SUCCESS
Socket operation successful.
Definition: IpSocket.hpp:30
@ SOCK_DISCONNECTED
Failed to read socket with disconnect.
Definition: IpSocket.hpp:38
@ SOCK_INTERRUPTED_TRY_AGAIN
Interrupted status for retries.
Definition: IpSocket.hpp:36
@ SOCK_NO_DATA_AVAILABLE
No data available or read operation would block.
Definition: IpSocket.hpp:45