F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
SocketReadTask.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title SocketReadTask.cpp
3 // \author mstarch
4 // \brief cpp file for SocketReadTask 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/Logger/Logger.hpp>
15 #include <Fw/Types/Assert.hpp>
16 #include <cerrno>
17 
18 #define MAXIMUM_SIZE 0x7FFFFFFF
19 
20 namespace Drv {
21 
22 SocketReadTask::SocketReadTask() : m_reconnect(false), m_stop(false) {}
23 
25 
27  const bool reconnect,
28  const Os::Task::ParamType priority,
29  const Os::Task::ParamType stack,
30  const Os::Task::ParamType cpuAffinity) {
31  FW_ASSERT(m_task.getState() == Os::Task::State::NOT_STARTED); // It is a coding error to start this task multiple times
32  FW_ASSERT(not this->m_stop); // It is a coding error to stop the thread before it is started
33  m_reconnect = reconnect;
34  // Note: the first step is for the IP socket to open the port
35  Os::Task::Arguments arguments(name, SocketReadTask::readTask, this, priority, stack, cpuAffinity);
36  Os::Task::Status stat = m_task.start(arguments);
37  FW_ASSERT(Os::Task::OP_OK == stat, static_cast<NATIVE_INT_TYPE>(stat));
38 }
39 
41  return this->getSocketHandler().startup();
42 }
43 
45  SocketIpStatus status = this->getSocketHandler().open();
46  // Call connected any time the open is successful
47  if (Drv::SOCK_SUCCESS == status) {
48  this->connected();
49  }
50  return status;
51 }
52 
54  this->getSocketHandler().shutdown();
55 }
56 
58  this->getSocketHandler().close();
59 }
60 
62  return m_task.join();
63 }
64 
66  this->m_stop = true;
67  this->getSocketHandler().shutdown(); // Break out of any receives and fully shutdown
68 }
69 
70 void SocketReadTask::readTask(void* pointer) {
71  FW_ASSERT(pointer);
73  SocketReadTask* self = reinterpret_cast<SocketReadTask*>(pointer);
74  do {
75  // Open a network connection if it has not already been open
76  if ((not self->getSocketHandler().isStarted()) and (not self->m_stop) and
77  ((status = self->startup()) != SOCK_SUCCESS)) {
79  "[WARNING] Failed to open port with status %d and errno %d\n",
80  static_cast<POINTER_CAST>(status),
81  static_cast<POINTER_CAST>(errno));
83  continue;
84  }
85 
86  // Open a network connection if it has not already been open
87  if ((not self->getSocketHandler().isOpened()) and (not self->m_stop) and
88  ((status = self->open()) != SOCK_SUCCESS)) {
90  "[WARNING] Failed to open port with status %d and errno %d\n",
91  static_cast<POINTER_CAST>(status),
92  static_cast<POINTER_CAST>(errno));
94  continue;
95  }
96 
97  // If the network connection is open, read from it
98  if (self->getSocketHandler().isStarted() and self->getSocketHandler().isOpened() and (not self->m_stop)) {
99  Fw::Buffer buffer = self->getBuffer();
100  U8* data = buffer.getData();
101  FW_ASSERT(data);
102  U32 size = buffer.getSize();
103  status = self->getSocketHandler().recv(data, size);
104  if ((status != SOCK_SUCCESS) && (status != SOCK_INTERRUPTED_TRY_AGAIN)) {
105  Fw::Logger::logMsg("[WARNING] Failed to recv from port with status %d and errno %d\n",
106  static_cast<POINTER_CAST>(status),
107  static_cast<POINTER_CAST>(errno));
108  self->getSocketHandler().close();
109  buffer.setSize(0);
110  } else {
111  // Send out received data
112  buffer.setSize(size);
113  }
114  self->sendBuffer(buffer, status);
115  }
116  }
117  // As long as not told to stop, and we are successful interrupted or ordered to retry, keep receiving
118  while (not self->m_stop &&
119  (status == SOCK_SUCCESS || status == SOCK_INTERRUPTED_TRY_AGAIN || self->m_reconnect));
120  self->getSocketHandler().shutdown(); // Shutdown the port entirely
121 }
122 } // namespace Drv
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformPointerCastType POINTER_CAST
Definition: BasicTypes.h:53
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:51
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:26
static const Fw::Time SOCKET_RETRY_INTERVAL
Definition: IpCfg.hpp:24
void close()
closes the socket
Definition: IpSocket.cpp:123
virtual void shutdown()
shutdown the socket
Definition: IpSocket.cpp:134
SocketIpStatus open()
open the IP socket for communications
Definition: IpSocket.cpp:148
virtual SocketIpStatus startup()
startup the socket, a no-op on unless this is server
Definition: IpSocket.cpp:141
supports a task to read a given socket adaptation
virtual void connected()=0
called when the IPv4 system has been connected
bool m_stop
Stops the task when set to true.
void close()
close the socket communications
static void readTask(void *pointer)
a task designed to read from the socket and output incoming data
virtual IpSocket & getSocketHandler()=0
returns a reference to the socket handler
void shutdown()
shutdown the socket communications
virtual ~SocketReadTask()
destructor of the socket read task
void stop()
stop the socket read task and close the associated socket.
bool m_reconnect
Force reconnection.
Os::Task::Status join()
joins to the stopping read task to wait for it to close
SocketIpStatus open()
open the socket for communications
void start(const Fw::StringBase &name, const bool reconnect=true, const Os::Task::ParamType priority=Os::Task::TASK_DEFAULT, const Os::Task::ParamType stack=Os::Task::TASK_DEFAULT, const Os::Task::ParamType cpuAffinity=Os::Task::TASK_DEFAULT)
start the socket read task to start producing data
SocketIpStatus startup()
startup the socket for communications
SocketReadTask()
constructs the socket read task
U8 * getData() const
Definition: Buffer.cpp:68
U32 getSize() const
Definition: Buffer.cpp:72
void setSize(U32 size)
Definition: Buffer.cpp:87
static void logMsg(const char *fmt, POINTER_CAST a0=0, POINTER_CAST a1=0, POINTER_CAST a2=0, POINTER_CAST a3=0, POINTER_CAST a4=0, POINTER_CAST a5=0, POINTER_CAST a6=0, POINTER_CAST a7=0, POINTER_CAST a8=0, POINTER_CAST a9=0)
Definition: Logger.cpp:18
State getState()
get the task's state
Definition: Task.cpp:74
FwSizeType ParamType
backwards-compatible parameter type
Definition: Task.hpp:218
Status start(const Arguments &arguments) override
start the task
Definition: Task.cpp:82
Status join() override
block until the task has ended
Definition: Task.cpp:130
@ OP_OK
message sent/received okay
Definition: Task.hpp:30
static Status delay(Fw::Time interval)
delay the current task
Definition: DefaultTask.cpp:11
SocketIpStatus
Status enumeration for socket return values.
Definition: IpSocket.hpp:23
@ SOCK_SUCCESS
Socket operation successful.
Definition: IpSocket.hpp:24
@ SOCK_INTERRUPTED_TRY_AGAIN
Interrupted status for retries.
Definition: IpSocket.hpp:30