F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
45  {
46  if (this->m_fd != -1) {
47  close(this->m_fd);
48  }
49  }
50 
52  const char* addr,
53  const char* port
54  ) {
55 
56  FW_ASSERT(addr);
57  FW_ASSERT(port);
58  // open UDP connection
59  this->m_fd = socket(AF_INET, SOCK_DGRAM, 0);
60  if (-1 == this->m_fd) {
61  Fw::LogStringArg arg(strerror(errno));
62  this->log_WARNING_HI_US_SocketError(arg);
63  return;
64  }
65 
66  /* fill in the server's address and data */
67  memset(&m_servAddr, 0, sizeof(m_servAddr));
68  m_servAddr.sin_family = AF_INET;
69  m_servAddr.sin_port = htons(atoi(port));
70  inet_aton(addr , &m_servAddr.sin_addr);
71 
72  Fw::LogStringArg arg(addr);
73  this->log_ACTIVITY_HI_US_PortOpened(arg,atoi(port));
74 
75  }
76 
77 
78 
79  // ----------------------------------------------------------------------
80  // Handler implementations for user-defined typed input ports
81  // ----------------------------------------------------------------------
82 
83  void UdpSenderComponentImpl ::
84  Sched_handler(
85  const NATIVE_INT_TYPE portNum,
86  U32 context
87  )
88  {
89  this->tlmWrite_US_BytesSent(this->m_bytesSent);
90  this->tlmWrite_US_PacketsSent(this->m_packetsSent);
91  }
92 
93  // ----------------------------------------------------------------------
94  // Handler implementations for user-defined serial input ports
95  // ----------------------------------------------------------------------
96 
97  void UdpSenderComponentImpl ::
98  PortsIn_handler(
99  NATIVE_INT_TYPE portNum,
100  Fw::SerializeBufferBase &Buffer
101  )
102  {
103  // return if we never successfully created the socket
104  if (-1 == this->m_fd) {
105  return;
106  }
107 
108  DEBUG_PRINT("PortsIn_handler: %d\n",portNum);
109  Fw::SerializeStatus stat;
110  m_sendBuff.resetSer();
111 
112  // serialize sequence number
113  stat = m_sendBuff.serialize(this->m_seq++);
114  FW_ASSERT(Fw::FW_SERIALIZE_OK == stat,stat);
115  // serialize port call
116  stat = m_sendBuff.serialize(static_cast<U8>(portNum));
117  FW_ASSERT(Fw::FW_SERIALIZE_OK == stat,stat);
118  // serialize port arguments buffer
119  stat = m_sendBuff.serialize(Buffer);
120  FW_ASSERT(Fw::FW_SERIALIZE_OK == stat,stat);
121  // send on UDP socket
122  DEBUG_PRINT("Sending %d bytes\n",m_sendBuff.getBuffLength());
123  ssize_t sendStat = sendto(this->m_fd,
124  m_sendBuff.getBuffAddr(),
125  m_sendBuff.getBuffLength(),
126  0,
127  reinterpret_cast<struct sockaddr *>(&m_servAddr),
128  sizeof(m_servAddr));
129  if (-1 == sendStat) {
130  Fw::LogStringArg arg(strerror(errno));
131  this->log_WARNING_HI_US_SendError(arg);
132  } else {
133  FW_ASSERT((int)m_sendBuff.getBuffLength() == sendStat,(int)m_sendBuff.getBuffLength(),sendStat,portNum);
134  this->m_packetsSent++;
135  this->m_bytesSent += sendStat;
136  }
137  }
138 
139 #ifdef BUILD_UT
140  UdpSerialBuffer& UdpSenderComponentImpl::UdpSerialBuffer::operator=(const Svc::UdpSenderComponentImpl::UdpSerialBuffer& other) {
141  this->resetSer();
142  this->serialize(other.getBuffAddr(),other.getBuffLength(),true);
143  return *this;
144  }
145 
146  UdpSenderComponentImpl::UdpSerialBuffer::UdpSerialBuffer(
147  const Fw::SerializeBufferBase& other) : Fw::SerializeBufferBase() {
148  FW_ASSERT(sizeof(this->m_buff)>= other.getBuffLength(),sizeof(this->m_buff),other.getBuffLength());
149  memcpy(this->m_buff,other.getBuffAddr(),other.getBuffLength());
150  this->setBuffLen(other.getBuffLength());
151  }
152 
153  UdpSenderComponentImpl::UdpSerialBuffer::UdpSerialBuffer(
154  const UdpSenderComponentImpl::UdpSerialBuffer& other) : Fw::SerializeBufferBase() {
155  FW_ASSERT(sizeof(this->m_buff)>= other.getBuffLength(),sizeof(this->m_buff),other.getBuffLength());
156  memcpy(this->m_buff,other.m_buff,other.getBuffLength());
157  this->setBuffLen(other.getBuffLength());
158  }
159 
160  UdpSenderComponentImpl::UdpSerialBuffer::UdpSerialBuffer(): Fw::SerializeBufferBase() {
161 
162  }
163 
164 #endif
165 
166 
167 } // end namespace Svc
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:55
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:30
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)
SerializeStatus
forward declaration for string
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.