F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
SocketComponentHelper.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title SocketComponentHelper.hpp
3 // \author mstarch
4 // \brief hpp file for SocketComponentHelper 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 #ifndef DRV_SocketComponentHelper_HPP
13 #define DRV_SocketComponentHelper_HPP
14 
15 #include <Drv/Ip/IpSocket.hpp>
16 #include <Fw/Buffer/Buffer.hpp>
17 #include <Os/Condition.hpp>
18 #include <Os/Mutex.hpp>
19 #include <Os/Task.hpp>
20 
21 namespace Drv {
30  public:
37 
41  virtual ~SocketComponentHelper();
42 
64  void start(const Fw::StringBase& name,
67  const Os::Task::ParamType cpuAffinity = Os::Task::TASK_DEFAULT,
68  const FwTaskPriorityType priorityReconnect = Os::Task::TASK_PRIORITY_DEFAULT,
69  const Os::Task::ParamType stackReconnect = Os::Task::TASK_DEFAULT,
70  const Os::Task::ParamType cpuAffinityReconnect = Os::Task::TASK_DEFAULT);
71 
83 
93  bool isOpened();
94 
104  void setAutomaticOpen(bool auto_open);
105 
111  bool getAutomaticOpen();
112 
121  SocketIpStatus send(const U8* const data, const FwSizeType size);
122 
131  SocketIpStatus recv(U8* data, FwSizeType& size);
132 
140  void close();
141 
150  void shutdown();
151 
155  bool running();
156  bool runningReconnect();
157 
164  void stop();
165 
166  void stopReconnect();
167 
177 
179 
180  protected:
184  virtual void readLoop();
185 
189  virtual void reconnectLoop();
190 
201  virtual IpSocket& getSocketHandler() = 0;
202 
213  virtual Fw::Buffer getBuffer() = 0;
214 
225  virtual void sendBuffer(Fw::Buffer buffer, SocketIpStatus status) = 0;
226 
230  virtual void connected() = 0;
231 
237  static void readTask(void* pointer);
238 
244  static void reconnectTask(void* pointer);
245 
250  void requestReconnect();
251 
265 
266  private:
276  SocketIpStatus reopen();
277 
278  protected:
279  bool m_reopen = true;
281 
282  // Read/recv
285  bool m_stop = true;
286  OpenState m_open = OpenState::NOT_OPEN;
287 
288  // Reconnect
291  bool m_reconnectStop = true;
292  ReconnectState m_reconnectState = ReconnectState::NOT_RECONNECTING;
294  Fw::TimeInterval(0, 50000); // 50 ms, Interval at which reconnect task loop checks for requests
296  Fw::TimeInterval(0, 10000); // 10 ms, Interval at which reconnect requesters wait for response
297 };
298 } // namespace Drv
299 #endif // DRV_SocketComponentHelper_HPP
SocketComponentHelper()
constructs the socket read task
bool m_reopen
Force reopen on disconnect.
static constexpr FwSizeType TASK_DEFAULT
Definition: Task.hpp:37
bool m_stop
Stops the task when set to true.
void shutdown()
shutdown the socket communications
PlatformSizeType FwSizeType
bool isOpened()
check if IP socket has previously been opened
SocketIpStatus send(const U8 *const data, const FwSizeType size)
send data to the IP socket from the given buffer
void setAutomaticOpen(bool auto_open)
set socket to automatically open connections when true, or not when false
SocketIpStatus waitForReconnect(Fw::TimeInterval timeout=Fw::TimeInterval(1, 0))
wait method for a task to wait for a reconnect request to complete
SocketIpStatus recv(U8 *data, FwSizeType &size)
receive data from the IP socket from the given buffer
static void reconnectTask(void *pointer)
a task designed for socket reconnection
virtual Fw::Buffer getBuffer()=0
returns a buffer to fill with data
void requestReconnect()
signal to reconnect task that a reconnect is needed
static constexpr FwTaskPriorityType TASK_PRIORITY_DEFAULT
Definition: Task.hpp:38
supports a task to read a given socket adaptation
OpenState m_open
Have we successfully opened.
void start(const Fw::StringBase &name, const FwTaskPriorityType priority=Os::Task::TASK_PRIORITY_DEFAULT, const Os::Task::ParamType stack=Os::Task::TASK_DEFAULT, const Os::Task::ParamType cpuAffinity=Os::Task::TASK_DEFAULT, const FwTaskPriorityType priorityReconnect=Os::Task::TASK_PRIORITY_DEFAULT, const Os::Task::ParamType stackReconnect=Os::Task::TASK_DEFAULT, const Os::Task::ParamType cpuAffinityReconnect=Os::Task::TASK_DEFAULT)
start the socket read task to start producing data
bool getAutomaticOpen()
get socket automatically open connections status
void close()
close the socket communications
virtual void connected()=0
called when the IPv4 system has been connected
PlatformTaskPriorityType FwTaskPriorityType
The type of task priorities used.
virtual IpSocket & getSocketHandler()=0
returns a reference to the socket handler
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:53
FwSizeType ParamType
backwards-compatible parameter type
Definition: Task.hpp:222
virtual ~SocketComponentHelper()
destructor of the socket read task
static void readTask(void *pointer)
a task designed to read from the socket and output incoming data
void stop()
stop the socket read task and close the associated socket.
virtual void reconnectLoop()
reconnect TCP socket
virtual void readLoop()
receive off the TCP socket
Os::Task::Status join()
joins to the stopping read task to wait for it to close
SocketIpStatus
Status enumeration for socket return values.
Definition: IpSocket.hpp:29
SocketIpStatus open()
open the socket for communications
Helper base-class for setting up Berkeley sockets.
Definition: IpSocket.hpp:57
virtual void sendBuffer(Fw::Buffer buffer, SocketIpStatus status)=0
sends a buffer to be filled with data
bool running()
is the read loop running