F´ Flight Software - C/C++ Documentation  NASA-v2.0.1
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 <errno.h>
17 
18 #define MAXIMUM_SIZE 0x7FFFFFFF
19 
20 namespace Drv {
21 
22 SocketReadTask::SocketReadTask() : m_stop(false) {}
23 
25 
27  const NATIVE_INT_TYPE priority,
28  const NATIVE_INT_TYPE stack,
29  const bool reconnect,
30  const NATIVE_INT_TYPE cpuAffinity) {
31  FW_ASSERT(not m_task.isStarted()); // 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 the port
35  Os::Task::TaskStatus stat = m_task.start(name, 0, priority, stack, SocketReadTask::readTask, this, cpuAffinity);
36  FW_ASSERT(Os::Task::TASK_OK == stat, static_cast<NATIVE_INT_TYPE>(stat));
37 }
38 
40  return this->getSocketHandler().open();
41 }
42 
44  this->getSocketHandler().close();
45 }
46 
48  return m_task.join(value_ptr);
49 }
50 
52  this->m_stop = true;
53  this->getSocketHandler().close(); // Break out of any receives
54 }
55 
56 void SocketReadTask::readTask(void* pointer) {
57  FW_ASSERT(pointer);
59  SocketReadTask* self = reinterpret_cast<SocketReadTask*>(pointer);
60  do {
61  // Open a network connection if it has not already been open
62  if ((not self->getSocketHandler().isOpened()) and (not self->m_stop) and
63  ((status = self->getSocketHandler().open()) != SOCK_SUCCESS)) {
64  Fw::Logger::logMsg("[WARNING] Failed to open port with status %d and errno %d\n", status, errno);
66  }
67 
68  // If the network connection is open, read from it
69  if (self->getSocketHandler().isOpened() and (not self->m_stop)) {
70  Fw::Buffer buffer = self->getBuffer();
71  U8* data = buffer.getData();
72  FW_ASSERT(data);
73  I32 size = static_cast<I32>(buffer.getSize());
74  size = (size >= 0) ? size : MAXIMUM_SIZE; // Handle max U32 edge case
75  status = self->getSocketHandler().recv(data, size);
76  if ((status != SOCK_SUCCESS) && (status != SOCK_INTERRUPTED_TRY_AGAIN)) {
77  Fw::Logger::logMsg("[WARNING] Failed to recv from port with status %d and errno %d\n", status, errno);
78  self->getSocketHandler().close();
79  buffer.setSize(0);
80  } else {
81  // Send out received data
82  buffer.setSize(size);
83  }
84  self->sendBuffer(buffer, status);
85  }
86  }
87  // As long as not told to stop, and we are successful interrupted or ordered to retry, keep receiving
88  while (not self->m_stop &&
89  (status == SOCK_SUCCESS || status == SOCK_INTERRUPTED_TRY_AGAIN || self->m_reconnect));
90  self->getSocketHandler().close(); // Close the handler again, in case it reconnected
91 }
92 }; // namespace Drv
Drv::SocketReadTask::stopSocketTask
void stopSocketTask(void)
stop the socket read task and close the associated socket.
Definition: SocketReadTask.cpp:51
MAXIMUM_SIZE
#define MAXIMUM_SIZE
Definition: SocketReadTask.cpp:18
Drv::IpSocket::open
SocketIpStatus open(void)
open the IP socket for communications
Definition: IpSocket.cpp:117
Drv::SOCK_SUCCESS
@ SOCK_SUCCESS
Socket operation successful.
Definition: IpSocket.hpp:24
Drv::SOCK_INTERRUPTED_TRY_AGAIN
@ SOCK_INTERRUPTED_TRY_AGAIN
Interrupted status for retries.
Definition: IpSocket.hpp:30
SocketReadTask.hpp
Drv::SocketReadTask::SocketReadTask
SocketReadTask()
constructs the socket read task
Definition: SocketReadTask.cpp:22
Fw::Buffer::getData
U8 * getData() const
Definition: Buffer.cpp:56
Fw::StringBase
Definition: StringType.hpp:23
U8
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.hpp:76
Drv::SocketReadTask::m_reconnect
bool m_reconnect
Force reconnection.
Definition: SocketReadTask.hpp:144
Drv
Definition: BlockDriverImpl.cpp:5
Drv::SocketReadTask::open
SocketIpStatus open()
open the socket for communications
Definition: SocketReadTask.cpp:39
Drv::SocketReadTask::m_stop
bool m_stop
Stops the task when set to true.
Definition: SocketReadTask.hpp:145
Fw::Buffer
Definition: Buffer.hpp:43
Drv::SocketReadTask::close
void close()
close the socket communications
Definition: SocketReadTask.cpp:43
Drv::SocketReadTask::m_task
Os::Task m_task
Definition: SocketReadTask.hpp:143
Assert.hpp
Drv::SocketReadTask::getSocketHandler
virtual IpSocket & getSocketHandler()=0
returns a reference to the socket handler
Os::Task::TASK_OK
@ TASK_OK
message sent/received okay
Definition: Task.hpp:19
Fw::Buffer::getSize
U32 getSize() const
Definition: Buffer.cpp:60
Os::Task::TaskStatus
TaskStatus
Definition: Task.hpp:18
Drv::SocketReadTask
supports a task to read a given socket adaptation
Definition: SocketReadTask.hpp:27
Fw::Buffer::setSize
void setSize(U32 size)
Definition: Buffer.cpp:75
FW_ASSERT
#define FW_ASSERT(...)
Definition: Assert.hpp:9
Os::Task::isStarted
bool isStarted(void)
check to see if task is started
Definition: TaskCommon.cpp:19
Drv::SocketReadTask::startSocketTask
void startSocketTask(const Fw::StringBase &name, const NATIVE_INT_TYPE priority, const NATIVE_INT_TYPE stack, const bool reconnect=true, const NATIVE_INT_TYPE cpuAffinity=-1)
start the socket read task to start producing data
Definition: SocketReadTask.cpp:26
SOCKET_RETRY_INTERVAL_MS
@ SOCKET_RETRY_INTERVAL_MS
Definition: IpCfg.hpp:22
Fw::Logger::logMsg
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
Os::Task::delay
static TaskStatus delay(NATIVE_UINT_TYPE msecs)
delay the task
Definition: Task.cpp:41
Os::Task::start
TaskStatus start(const Fw::StringBase &name, NATIVE_INT_TYPE identifier, NATIVE_INT_TYPE priority, NATIVE_INT_TYPE stackSize, taskRoutine routine, void *arg, NATIVE_INT_TYPE cpuAffinity=-1)
start the task
Definition: Task.cpp:16
Drv::SocketIpStatus
SocketIpStatus
Status enumeration for socket return values.
Definition: IpSocket.hpp:23
Drv::SocketReadTask::~SocketReadTask
virtual ~SocketReadTask()
destructor of the socket read task
Definition: SocketReadTask.cpp:24
Drv::SocketReadTask::readTask
static void readTask(void *pointer)
a task designed to read from the socket and output incoming data
Definition: SocketReadTask.cpp:56
Drv::SocketReadTask::joinSocketTask
Os::Task::TaskStatus joinSocketTask(void **value_ptr)
joins to the stopping read task to wait for it to close
Definition: SocketReadTask.cpp:47
NATIVE_INT_TYPE
int NATIVE_INT_TYPE
native integer type declaration
Definition: BasicTypes.hpp:29
Drv::IpSocket::close
void close(void)
closes the socket
Definition: IpSocket.cpp:106
Logger.hpp
Os::Task::join
TaskStatus join(void **value_ptr)
Wait for task to finish.
Definition: Task.cpp:72