F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
UdpSenderComponentImpl.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title UdpSenderImpl.cpp
3 // \author tcanham
4 // \brief cpp file for UdpSender component implementation class
5 //
6 // \copyright
7 // Copyright 2009-2015, by the California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 
13 
15 #include <FpConfig.hpp>
16 #include <sys/types.h>
17 #include <cstring>
18 #include <cerrno>
19 #include <cstdlib>
20 #include <unistd.h>
21 
22 //#define DEBUG_PRINT(...) printf(##__VA_ARGS__)
23 #define DEBUG_PRINT(...)
24 
25 namespace Svc {
26 
27  // ----------------------------------------------------------------------
28  // Construction, initialization, and destruction
29  // ----------------------------------------------------------------------
30 
33  const char *const compName
34  ) : UdpSenderComponentBase(compName),
35  m_fd(-1),
36  m_packetsSent(0),
37  m_bytesSent(0),
38  m_seq(0)
39  {
40 
41  }
42 
44  init(
45  const NATIVE_INT_TYPE queueDepth,
46  const NATIVE_INT_TYPE msgSize,
47  const NATIVE_INT_TYPE instance
48  )
49  {
50  UdpSenderComponentBase::init(queueDepth, msgSize, instance);
51  }
52 
55  {
56  if (this->m_fd != -1) {
57  close(this->m_fd);
58  }
59  }
60 
62  const char* addr,
63  const char* port
64  ) {
65 
66  FW_ASSERT(addr);
67  FW_ASSERT(port);
68  // open UDP connection
69  this->m_fd = socket(AF_INET, SOCK_DGRAM, 0);
70  if (-1 == this->m_fd) {
71  Fw::LogStringArg arg(strerror(errno));
72  this->log_WARNING_HI_US_SocketError(arg);
73  return;
74  }
75 
76  /* fill in the server's address and data */
77  memset(&m_servAddr, 0, sizeof(m_servAddr));
78  m_servAddr.sin_family = AF_INET;
79  m_servAddr.sin_port = htons(atoi(port));
80  inet_aton(addr , &m_servAddr.sin_addr);
81 
82  Fw::LogStringArg arg(addr);
83  this->log_ACTIVITY_HI_US_PortOpened(arg,atoi(port));
84 
85  }
86 
87 
88 
89  // ----------------------------------------------------------------------
90  // Handler implementations for user-defined typed input ports
91  // ----------------------------------------------------------------------
92 
93  void UdpSenderComponentImpl ::
94  Sched_handler(
95  const NATIVE_INT_TYPE portNum,
96  U32 context
97  )
98  {
99  this->tlmWrite_US_BytesSent(this->m_bytesSent);
100  this->tlmWrite_US_PacketsSent(this->m_packetsSent);
101  }
102 
103  // ----------------------------------------------------------------------
104  // Handler implementations for user-defined serial input ports
105  // ----------------------------------------------------------------------
106 
107  void UdpSenderComponentImpl ::
108  PortsIn_handler(
109  NATIVE_INT_TYPE portNum,
110  Fw::SerializeBufferBase &Buffer
111  )
112  {
113  // return if we never successfully created the socket
114  if (-1 == this->m_fd) {
115  return;
116  }
117 
118  DEBUG_PRINT("PortsIn_handler: %d\n",portNum);
119  Fw::SerializeStatus stat;
120  m_sendBuff.resetSer();
121 
122  // serialize sequence number
123  stat = m_sendBuff.serialize(this->m_seq++);
124  FW_ASSERT(Fw::FW_SERIALIZE_OK == stat,stat);
125  // serialize port call
126  stat = m_sendBuff.serialize(static_cast<U8>(portNum));
127  FW_ASSERT(Fw::FW_SERIALIZE_OK == stat,stat);
128  // serialize port arguments buffer
129  stat = m_sendBuff.serialize(Buffer);
130  FW_ASSERT(Fw::FW_SERIALIZE_OK == stat,stat);
131  // send on UDP socket
132  DEBUG_PRINT("Sending %d bytes\n",m_sendBuff.getBuffLength());
133  ssize_t sendStat = sendto(this->m_fd,
134  m_sendBuff.getBuffAddr(),
135  m_sendBuff.getBuffLength(),
136  0,
137  reinterpret_cast<struct sockaddr *>(&m_servAddr),
138  sizeof(m_servAddr));
139  if (-1 == sendStat) {
140  Fw::LogStringArg arg(strerror(errno));
141  this->log_WARNING_HI_US_SendError(arg);
142  } else {
143  FW_ASSERT((int)m_sendBuff.getBuffLength() == sendStat,(int)m_sendBuff.getBuffLength(),sendStat,portNum);
144  this->m_packetsSent++;
145  this->m_bytesSent += sendStat;
146  }
147  }
148 
149 #ifdef BUILD_UT
150  UdpSerialBuffer& UdpSenderComponentImpl::UdpSerialBuffer::operator=(const Svc::UdpSenderComponentImpl::UdpSerialBuffer& other) {
151  this->resetSer();
152  this->serialize(other.getBuffAddr(),other.getBuffLength(),true);
153  return *this;
154  }
155 
156  UdpSenderComponentImpl::UdpSerialBuffer::UdpSerialBuffer(
157  const Fw::SerializeBufferBase& other) : Fw::SerializeBufferBase() {
158  FW_ASSERT(sizeof(this->m_buff)>= other.getBuffLength(),sizeof(this->m_buff),other.getBuffLength());
159  memcpy(this->m_buff,other.getBuffAddr(),other.getBuffLength());
160  this->setBuffLen(other.getBuffLength());
161  }
162 
163  UdpSenderComponentImpl::UdpSerialBuffer::UdpSerialBuffer(
164  const UdpSenderComponentImpl::UdpSerialBuffer& other) : Fw::SerializeBufferBase() {
165  FW_ASSERT(sizeof(this->m_buff)>= other.getBuffLength(),sizeof(this->m_buff),other.getBuffLength());
166  memcpy(this->m_buff,other.m_buff,other.getBuffLength());
167  this->setBuffLen(other.getBuffLength());
168  }
169 
170  UdpSenderComponentImpl::UdpSerialBuffer::UdpSerialBuffer(): Fw::SerializeBufferBase() {
171 
172  }
173 
174 #endif
175 
176 
177 } // end namespace Svc
#define FW_ASSERT(...)
Definition: Assert.hpp:14
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.
#define DEBUG_PRINT(...)
virtual U8 * getBuffAddr()=0
gets buffer address for data filling
Serializable::SizeType getBuffLength() const
returns current buffer size
UdpSenderComponentImpl(const char *const compName)
void open(const char *addr, const char *port)
void init(const NATIVE_INT_TYPE queueDepth, const NATIVE_INT_TYPE msgSize, const NATIVE_INT_TYPE instance=0)
SerializeStatus
forward declaration for string
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.