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