F´ Flight Software - C/C++ Documentation  NASA-v1.5.0
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GroundInterface.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title GroundInterface.cpp
3 // \author lestarch
4 // \brief cpp file for GroundInterface component implementation class
5 // ======================================================================
6 
7 #include <Fw/Com/ComPacket.hpp>
10 #include <string.h>
11 
12 namespace Svc {
13 
15  const TOKEN_TYPE GroundInterfaceComponentImpl::START_WORD = static_cast<TOKEN_TYPE>(0xdeadbeef);
16  const U32 GroundInterfaceComponentImpl::END_WORD = static_cast<U32>(0xcafecafe);
17 
18  // ----------------------------------------------------------------------
19  // Construction, initialization, and destruction
20  // ----------------------------------------------------------------------
21 
24  const char *const compName
25  ) : GroundInterfaceComponentBase(compName),
26  m_ext_buffer(0xfeedfeed, 0xdeeddeed, reinterpret_cast<POINTER_CAST>(m_buffer), GND_BUFFER_SIZE),
27  m_data_size(0),
28  m_in_ring(m_in_buffer, GND_BUFFER_SIZE)
29  {
30 
31  }
32 
34  init(
35  const NATIVE_INT_TYPE instance
36  )
37  {
39  }
40 
43  {
44 
45  }
46 
47  // ----------------------------------------------------------------------
48  // Handler implementations for user-defined typed input ports
49  // ----------------------------------------------------------------------
50 
51  void GroundInterfaceComponentImpl ::
52  downlinkPort_handler(
53  const NATIVE_INT_TYPE portNum,
54  Fw::ComBuffer &data,
55  U32 context
56  )
57  {
60  }
61 
64  const NATIVE_INT_TYPE portNum,
65  Fw::Buffer &fwBuffer
66  )
67  {
71  }
72 
75  const NATIVE_INT_TYPE portNum,
76  Fw::Buffer &buffer
77  )
78  {
79  processBuffer(buffer);
80  }
81 
84  const NATIVE_INT_TYPE portNum,
85  NATIVE_UINT_TYPE context
86  )
87  {
88  // TODO: replace with a call to a buffer manager
89  Fw::Buffer buffer = m_ext_buffer;
90  // Call read poll if it is hooked up
92  readPoll_out(0, buffer);
93  processBuffer(buffer);
94  }
95  }
96 
98  // TODO: replace with a call to a buffer manager
99  Fw::Buffer buffer = m_ext_buffer;
100  Fw::ExternalSerializeBuffer buffer_wrapper(reinterpret_cast<U8*>(m_ext_buffer.getdata()),
102  // True size is supplied size plus sizeof(TOKEN_TYPE) if a packet_type other than "UNKNOWN" was supplied.
103  // This is because if not UNKOWN, the packet_type is serialized too. Otherwise it is assumed the PACKET_TYPE is
104  // already the first token in the UNKNOWN typed buffer.
105  U32 true_size = (packet_type != Fw::ComPacket::FW_PACKET_UNKNOWN) ? size + sizeof(TOKEN_TYPE) : size;
106  U32 total_size = sizeof(TOKEN_TYPE) + sizeof(TOKEN_TYPE) + true_size + sizeof(U32);
107  // Serialize data
108  FW_ASSERT(GND_BUFFER_SIZE >= total_size, GND_BUFFER_SIZE, total_size);
109  buffer_wrapper.serialize(START_WORD);
110  buffer_wrapper.serialize(static_cast<TOKEN_TYPE>(true_size));
111  // Explicitly set the packet type, if it didn't come with the data already
112  if (packet_type != Fw::ComPacket::FW_PACKET_UNKNOWN) {
113  buffer_wrapper.serialize(packet_type);
114  }
115  buffer_wrapper.serialize(data, size, true);
116  buffer_wrapper.serialize(static_cast<TOKEN_TYPE>(END_WORD));
117 
118  buffer.setsize(buffer_wrapper.getBuffLength());
119  buffer.setdata(reinterpret_cast<POINTER_CAST>(buffer_wrapper.getBuffAddr()));
120  FW_ASSERT(buffer.getsize() == total_size, buffer.getsize(), total_size);
121  write_out(0, buffer);
122  }
123 
126  {
127  // Read the packet type from the data buffer
128  U32 packet_type = Fw::ComPacket::FW_PACKET_UNKNOWN;
129  m_in_ring.peek(packet_type, HEADER_SIZE);
130 
131  // Process variable type
132  switch (packet_type) {
134  Fw::ComBuffer com;
136  // Reset com buffer for sending out data
137  com.setBuffLen(m_data_size);
138  uplinkPort_out(0, com, 0);
139  break;
140  }
142  // If file uplink is possible, handle files. Otherwise ignore.
146  m_in_ring.peek(reinterpret_cast<U8*>(buffer.getdata()), m_data_size - sizeof(packet_type), HEADER_SIZE + sizeof(packet_type));
147  buffer.setsize(m_data_size - sizeof(packet_type));
148  fileUplinkBufferSendOut_out(0, buffer);
149  }
150  break;
151  }
152  default:
153  return;
154  }
155  }
156 
158  processRing()
159  {
160  // Header items for the packet
161  TOKEN_TYPE start;
162  U32 checksum; //TODO: make this run a CRC32
163  // Inner-loop, process ring buffer looking for at least the header
165  m_data_size = 0;
166  // Peek into the header and read out values
167  Fw::SerializeStatus status = m_in_ring.peek(start, 0);
168  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status);
169  status = m_in_ring.peek(m_data_size, sizeof(TOKEN_TYPE));
170  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status);
171  // Check the header for correctness
172  if (start != START_WORD || m_data_size >= MAX_DATA_SIZE) {
173  m_in_ring.rotate(1);
174  continue;
175  }
176  // Check for enough data to deserialize everything otherwise break and wait for more.
177  else if (m_in_ring.get_remaining_size() < (HEADER_SIZE + m_data_size + sizeof(END_WORD))) {
178  break;
179  }
180  // Continue with the data portion and checksum
181  m_in_ring.peek(checksum, HEADER_SIZE + m_data_size);
182  // Check checksum
183  if (checksum == END_WORD) {
184  routeComData();
185  m_in_ring.rotate(HEADER_SIZE + m_data_size + sizeof(U32));
186  }
187  // Failed checksum, keep looking for valid message
188  else {
189  m_in_ring.rotate(1);
190  }
191  }
192  }
193 
195  processBuffer(Fw::Buffer& buffer)
196  {
197  NATIVE_UINT_TYPE buffer_offset = 0;
198  while (buffer_offset < buffer.getsize()) {
199  NATIVE_UINT_TYPE ser_size = (buffer.getsize() >= m_in_ring.get_remaining_size(true)) ?
200  m_in_ring.get_remaining_size(true) : static_cast<NATIVE_UINT_TYPE>(buffer.getsize());
201  m_in_ring.serialize(reinterpret_cast<U8*>(buffer.getdata()) + buffer_offset, ser_size);
202  buffer_offset = buffer_offset + ser_size;
203  processRing();
204  }
205  }
206 } // end namespace Svc
Svc::GroundInterfaceComponentImpl::fileDownlinkBufferSendIn_handler
void fileDownlinkBufferSendIn_handler(const NATIVE_INT_TYPE portNum, Fw::Buffer &fwBuffer)
Definition: GroundInterface.cpp:63
Svc::GroundInterfaceComponentImpl::frame_send
void frame_send(U8 *data, TOKEN_TYPE size, TOKEN_TYPE packet_type=Fw::ComPacket::FW_PACKET_UNKNOWN)
Definition: GroundInterface.cpp:97
Svc::GroundInterfaceComponentImpl::~GroundInterfaceComponentImpl
~GroundInterfaceComponentImpl(void)
Definition: GroundInterface.cpp:42
Fw::Buffer::setsize
void setsize(U32 val)
set member size
Definition: BufferSerializableAc.cpp:74
Types::CircularBuffer::serialize
Fw::SerializeStatus serialize(const U8 *const buffer, const NATIVE_UINT_TYPE size)
Definition: CircularBuffer.cpp:54
Fw::Buffer::getsize
U32 getsize(void)
get member size
Definition: BufferSerializableAc.cpp:61
Fw::Buffer::setdata
void setdata(U64 val)
set member data
Definition: BufferSerializableAc.cpp:71
Fw::SerializeStatus
SerializeStatus
forward declaration for string
Definition: Serializable.hpp:14
Fw::ComPacket::FW_PACKET_UNKNOWN
@ FW_PACKET_UNKNOWN
Definition: ComPacket.hpp:28
Fw::ComBuffer::getBuffAddr
U8 * getBuffAddr(void)
gets buffer address for data filling
Definition: ComBuffer.cpp:36
Fw::SerializeBufferBase::serialize
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
Definition: Serializable.cpp:67
Svc::GroundInterfaceComponentBase::uplinkPort_out
void uplinkPort_out(NATIVE_INT_TYPE portNum, Fw::ComBuffer &data, U32 context)
Definition: GroundInterfaceComponentAc.cpp:613
U8
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.hpp:76
Fw::ComPacket::FW_PACKET_FILE
@ FW_PACKET_FILE
Definition: ComPacket.hpp:25
Svc::GroundInterfaceComponentImpl::GroundInterfaceComponentImpl
GroundInterfaceComponentImpl(const char *const compName)
Definition: GroundInterface.cpp:23
Svc::GroundInterfaceComponentImpl::data
PRIVATE Fw::ComBuffer & data
Definition: GroundInterface.hpp:55
Svc::GroundInterfaceComponentImpl::processRing
void processRing()
Process all the data in the ring.
Definition: GroundInterface.cpp:158
Svc::GroundInterfaceComponentImpl::routeComData
void routeComData()
Processes the out-going data into coms order.
Definition: GroundInterface.cpp:125
Fw::ComPacket::FW_PACKET_COMMAND
@ FW_PACKET_COMMAND
Definition: ComPacket.hpp:22
Fw::Buffer
Definition: BufferSerializableAc.hpp:24
Svc::GroundInterfaceComponentBase::fileDownlinkBufferSendOut_out
void fileDownlinkBufferSendOut_out(NATIVE_INT_TYPE portNum, Fw::Buffer &fwBuffer)
Definition: GroundInterfaceComponentAc.cpp:623
Svc::GroundInterfaceComponentImpl::MAX_DATA_SIZE
static const U32 MAX_DATA_SIZE
Definition: GroundInterface.hpp:22
Fw::FW_SERIALIZE_OK
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.
Definition: Serializable.hpp:15
Fw::Buffer::getdata
U64 getdata(void)
get member data
Definition: BufferSerializableAc.cpp:57
Fw::ObjBase::init
void init(void)
Object initializer.
Definition: ObjBase.cpp:26
Types::CircularBuffer::get_remaining_size
NATIVE_UINT_TYPE get_remaining_size(bool serialization=false)
Definition: CircularBuffer.cpp:33
Fw::SerializeBufferBase::setBuffLen
SerializeStatus setBuffLen(NATIVE_UINT_TYPE length)
sets buffer length manually after filling with data
Definition: Serializable.cpp:609
NATIVE_UINT_TYPE
unsigned int NATIVE_UINT_TYPE
native unsigned integer type declaration
Definition: BasicTypes.hpp:30
TOKEN_TYPE
#define TOKEN_TYPE
Definition: GroundInterface.hpp:13
Svc::GroundInterfaceComponentImpl::schedIn_handler
void schedIn_handler(const NATIVE_INT_TYPE portNum, NATIVE_UINT_TYPE context)
Definition: GroundInterface.cpp:83
Svc::GroundInterfaceComponentBase::readPoll_out
void readPoll_out(NATIVE_INT_TYPE portNum, Fw::Buffer &fwBuffer)
Definition: GroundInterfaceComponentAc.cpp:653
Types::CircularBuffer::rotate
Fw::SerializeStatus rotate(NATIVE_UINT_TYPE amount)
Definition: CircularBuffer.cpp:136
FW_ASSERT
#define FW_ASSERT(...)
Definition: Assert.hpp:9
Fw::SerializeBufferBase::getBuffLength
NATIVE_UINT_TYPE getBuffLength() const
returns current buffer size
Definition: Serializable.cpp:592
Svc::GroundInterfaceComponentImpl::START_WORD
static const TOKEN_TYPE START_WORD
Definition: GroundInterface.hpp:23
Fw::ExternalSerializeBuffer
Definition: Serializable.hpp:156
Types::CircularBuffer::peek
Fw::SerializeStatus peek(char &value, NATIVE_UINT_TYPE offset=0)
Definition: CircularBuffer.cpp:76
Svc::GroundInterfaceComponentBase::fileUplinkBufferGet_out
Fw::Buffer fileUplinkBufferGet_out(NATIVE_INT_TYPE portNum, U32 size)
Definition: GroundInterfaceComponentAc.cpp:633
Svc::GroundInterfaceComponentImpl::readCallback_handler
void readCallback_handler(const NATIVE_INT_TYPE portNum, Fw::Buffer &fwBuffer)
Definition: GroundInterface.cpp:74
HEADER_SIZE
#define HEADER_SIZE
Definition: GroundInterface.hpp:14
Svc::GroundInterfaceComponentImpl::m_in_ring
Types::CircularBuffer m_in_ring
Definition: GroundInterface.hpp:102
Svc::GroundInterfaceComponentImpl::m_data_size
TOKEN_TYPE m_data_size
Data size expected in incoming data.
Definition: GroundInterface.hpp:100
GroundInterface.hpp
Svc::GroundInterfaceComponentImpl::m_ext_buffer
Fw::Buffer m_ext_buffer
Definition: GroundInterface.hpp:97
Svc
Definition: ActiveLoggerComponentAc.cpp:22
Svc::GroundInterfaceComponentBase::isConnected_fileDownlinkBufferSendOut_OutputPort
bool isConnected_fileDownlinkBufferSendOut_OutputPort(NATIVE_INT_TYPE portNum)
Definition: GroundInterfaceComponentAc.cpp:797
Svc::GroundInterfaceComponentBase::fwBuffer
PROTECTED Fw::Buffer & fwBuffer
Definition: GroundInterfaceComponentAc.hpp:366
Svc::GroundInterfaceComponentBase::write_out
void write_out(NATIVE_INT_TYPE portNum, Fw::Buffer &fwBuffer)
Definition: GroundInterfaceComponentAc.cpp:643
Svc::GroundInterfaceComponentImpl::END_WORD
static const U32 END_WORD
Definition: GroundInterface.hpp:24
BasicTypes.hpp
Declares ISF basic types.
NATIVE_INT_TYPE
int NATIVE_INT_TYPE
native integer type declaration
Definition: BasicTypes.hpp:29
Fw::ComBuffer
Definition: ComBuffer.hpp:21
Svc::GroundInterfaceComponentBase::isConnected_readPoll_OutputPort
bool isConnected_readPoll_OutputPort(NATIVE_INT_TYPE portNum)
Definition: GroundInterfaceComponentAc.cpp:827
Svc::GroundInterfaceComponentBase
Auto-generated base for GroundInterface component.
Definition: GroundInterfaceComponentAc.hpp:44
ComPacket.hpp
Fw::ExternalSerializeBuffer::getBuffAddr
U8 * getBuffAddr(void)
gets buffer address for data filling
Definition: Serializable.cpp:725
Svc::GroundInterfaceComponentBase::isConnected_fileUplinkBufferGet_OutputPort
bool isConnected_fileUplinkBufferGet_OutputPort(NATIVE_INT_TYPE portNum)
Definition: GroundInterfaceComponentAc.cpp:807
Svc::GroundInterfaceComponentImpl::processBuffer
void processBuffer(Fw::Buffer &data)
Process a data buffer containing a read from the serial port.
Definition: GroundInterface.cpp:195
GND_BUFFER_SIZE
#define GND_BUFFER_SIZE
Definition: GroundInterface.hpp:12