F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
Framer.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Framer.cpp
3 // \author mstarch
4 // \brief cpp file for Framer component implementation class
5 //
6 // \copyright
7 // Copyright 2009-2022, by the California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 
13 #include <FpConfig.hpp>
14 #include <Svc/Framer/Framer.hpp>
15 #include "Fw/Logger/Logger.hpp"
16 #include "Utils/Hash/Hash.hpp"
17 
18 namespace Svc {
19 
20 // ----------------------------------------------------------------------
21 // Construction, initialization, and destruction
22 // ----------------------------------------------------------------------
23 
24 Framer ::Framer(const char* const compName)
25  : FramerComponentBase(compName), FramingProtocolInterface(), m_protocol(nullptr), m_frame_sent(false) {}
26 
28 
30  FW_ASSERT(this->m_protocol == nullptr);
31  this->m_protocol = &protocol;
32  protocol.setup(*this);
33 }
34 
35 void Framer ::handle_framing(const U8* const data, const U32 size, Fw::ComPacket::ComPacketType packet_type) {
36  FW_ASSERT(this->m_protocol != nullptr);
37  this->m_frame_sent = false; // Clear the flag to detect if frame was sent
38  this->m_protocol->frame(data, size, packet_type);
39  // If no frame was sent, Framer has the obligation to report success
40  if (this->isConnected_comStatusOut_OutputPort(0) && (!this->m_frame_sent)) {
42  this->comStatusOut_out(0, status);
43  }
44 }
45 
46 // ----------------------------------------------------------------------
47 // Handler implementations for user-defined typed input ports
48 // ----------------------------------------------------------------------
49 
50 void Framer ::comIn_handler(const NATIVE_INT_TYPE portNum, Fw::ComBuffer& data, U32 context) {
51  this->handle_framing(data.getBuffAddr(), data.getBuffLength(), Fw::ComPacket::FW_PACKET_UNKNOWN);
52 }
53 
54 void Framer ::bufferIn_handler(const NATIVE_INT_TYPE portNum, Fw::Buffer& fwBuffer) {
55  this->handle_framing(fwBuffer.getData(), fwBuffer.getSize(), Fw::ComPacket::FW_PACKET_FILE);
56  // Deallocate the buffer after it was processed by the framing protocol
57  this->bufferDeallocate_out(0, fwBuffer);
58 }
59 
60 void Framer ::comStatusIn_handler(const NATIVE_INT_TYPE portNum, Fw::Success& condition) {
61  if (this->isConnected_comStatusOut_OutputPort(portNum)) {
62  this->comStatusOut_out(portNum, condition);
63  }
64 }
65 
66 // ----------------------------------------------------------------------
67 // Framing protocol implementations
68 // ----------------------------------------------------------------------
69 
70 void Framer ::send(Fw::Buffer& outgoing) {
71  FW_ASSERT(!this->m_frame_sent); // Prevent multiple sends per-packet
72  const Drv::SendStatus sendStatus = this->framedOut_out(0, outgoing);
73  if (sendStatus.e != Drv::SendStatus::SEND_OK) {
74  // Note: if there is a data sending problem, an EVR likely wouldn't
75  // make it down. Log the issue in hopes that
76  // someone will see it.
77  Fw::Logger::log("[ERROR] Failed to send framed data: %d\n", sendStatus.e);
78  }
79  this->m_frame_sent = true; // A frame was sent
80 }
81 
82 Fw::Buffer Framer ::allocate(const U32 size) {
83  return this->framedAllocate_out(0, size);
84 }
85 
86 } // end namespace Svc
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:55
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:30
C++-compatible configuration header for fprime configuration.
Status returned by the send call.
T e
The raw enum value.
@ SEND_OK
Send worked as expected.
U8 * getData() const
Definition: Buffer.cpp:68
U32 getSize() const
Definition: Buffer.cpp:72
U8 * getBuffAddr()
gets buffer address for data filling
Definition: ComBuffer.cpp:40
static void log(const char *format,...)
log a formated string with supplied arguments
Definition: Logger.cpp:21
Serializable::SizeType getBuffLength() const
returns current buffer size
Success/Failure.
@ SUCCESS
Representing success.
Auto-generated base for Framer component.
void bufferDeallocate_out(FwIndexType portNum, Fw::Buffer &fwBuffer)
Invoke output port bufferDeallocate.
void comStatusOut_out(FwIndexType portNum, Fw::Success &condition)
Invoke output port comStatusOut.
Drv::SendStatus framedOut_out(FwIndexType portNum, Fw::Buffer &sendBuffer)
Invoke output port framedOut.
Fw::Buffer framedAllocate_out(FwIndexType portNum, U32 size)
Invoke output port framedAllocate.
bool isConnected_comStatusOut_OutputPort(FwIndexType portNum)
Framer(const char *const compName)
Definition: Framer.cpp:24
void setup(FramingProtocol &protocol)
Setup this component with a supplied framing protocol.
Definition: Framer.cpp:29
abstract class representing a framing protocol
virtual void frame(const U8 *const data, const U32 size, Fw::ComPacket::ComPacketType packet_type)=0
frame a given set of bytes
void setup(FramingProtocolInterface &interface)
setup function called to supply the interface used for allocation and sending
interface supplied to the framing protocol