F´ Flight Software - C/C++ Documentation
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 FwIndexType portNum, Fw::ComBuffer& data, U32 context) {
51  FW_ASSERT(data.getBuffLength() < std::numeric_limits<U32>::max(), static_cast<FwAssertArgType>(data.getBuffLength()));
52  this->handle_framing(data.getBuffAddr(), static_cast<U32>(data.getBuffLength()), Fw::ComPacket::FW_PACKET_UNKNOWN);
53 }
54 
55 void Framer ::bufferIn_handler(const FwIndexType portNum, Fw::Buffer& fwBuffer) {
56  this->handle_framing(fwBuffer.getData(), fwBuffer.getSize(), Fw::ComPacket::FW_PACKET_FILE);
57  // Deallocate the buffer after it was processed by the framing protocol
58  this->bufferDeallocate_out(0, fwBuffer);
59 }
60 
61 void Framer ::comStatusIn_handler(const FwIndexType portNum, Fw::Success& condition) {
62  if (this->isConnected_comStatusOut_OutputPort(portNum)) {
63  this->comStatusOut_out(portNum, condition);
64  }
65 }
66 
67 // ----------------------------------------------------------------------
68 // Framing protocol implementations
69 // ----------------------------------------------------------------------
70 
71 void Framer ::send(Fw::Buffer& outgoing) {
72  FW_ASSERT(!this->m_frame_sent); // Prevent multiple sends per-packet
73  const Drv::SendStatus sendStatus = this->framedOut_out(0, outgoing);
74  if (sendStatus.e != Drv::SendStatus::SEND_OK) {
75  // Note: if there is a data sending problem, an EVR likely wouldn't
76  // make it down. Log the issue in hopes that
77  // someone will see it.
78  Fw::Logger::log("[ERROR] Failed to send framed data: %d\n", sendStatus.e);
79  }
80  this->m_frame_sent = true; // A frame was sent
81 }
82 
83 Fw::Buffer Framer ::allocate(const U32 size) {
84  return this->framedAllocate_out(0, size);
85 }
86 
87 } // end namespace Svc
abstract class representing a framing protocol
PlatformAssertArgType FwAssertArgType
Definition: FpConfig.h:39
void comStatusOut_out(FwIndexType portNum, Fw::Success &condition)
Invoke output port comStatusOut.
Representing success.
static void log(const char *format,...)
log a formated string with supplied arguments
Definition: Logger.cpp:21
U8 * getData() const
Definition: Buffer.cpp:68
PlatformIndexType FwIndexType
Definition: FpConfig.h:25
Drv::SendStatus framedOut_out(FwIndexType portNum, Fw::Buffer &sendBuffer)
Invoke output port framedOut.
Send worked as expected.
Serializable::SizeType getBuffLength() const
returns current buffer size
Auto-generated base for Framer component.
void setup(FramingProtocolInterface &interface)
setup function called to supply the interface used for allocation and sending
void bufferDeallocate_out(FwIndexType portNum, Fw::Buffer &fwBuffer)
Invoke output port bufferDeallocate.
U8 * getBuffAddr()
gets buffer address for data filling
Definition: ComBuffer.cpp:40
Fw::Buffer framedAllocate_out(FwIndexType portNum, U32 size)
Invoke output port framedAllocate.
C++-compatible configuration header for fprime configuration.
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:32
Status returned by the send call.
Framer(const char *const compName)
Definition: Framer.cpp:24
bool isConnected_comStatusOut_OutputPort(FwIndexType portNum)
T e
The raw enum value.
void setup(FramingProtocol &protocol)
Setup this component with a supplied framing protocol.
Definition: Framer.cpp:29
SizeType getSize() const
Definition: Buffer.cpp:72
interface supplied to the framing protocol
#define FW_ASSERT(...)
Definition: Assert.hpp:14
Success/Failure.
virtual void frame(const U8 *const data, const U32 size, Fw::ComPacket::ComPacketType packet_type)=0
frame a given set of bytes