fprime/Svc/FprimeDeframer/FprimeDeframer.cpp
Thomas Boyer-Chammard d0246f148b
Add Framer FPP interface, implement FprimeFramer and adapt ComQueue (#3486)
* Initial FprimeFramer and FprimePacketizer

* Code clarity + set up UTs

* Rework ComQueue and ComStub to use DataWithContext

* Add packets to RefPackets.fppi

* Fix ComQueue tests

* Add hotfix to FileDownlink instead of ComQueue

* Fix cancelPacket as well

* Fix ComQueue UTs by removing hotfix

* Refactor DataWithContext to use an FPP object for context instead of Fw.Buffer

* Touch up testing

* Add docs

* more docs

* More docs

* Rework buffer deallocation pattern to pass-through ComQueue

* Update ComStub UTs

* Restore original FileDownlink.cpp

* Formatting tweak

* Update deprecated getSerializeRepr() calls

* deserialization methods

* Fix spelling

* add cast for safety

* CMakefile change

* Bump ComQueue depth

* Update RPI deployment with new Downlink stack

* Rename comQueueIn port to comPktQueueIn

* Fix comQueueIn to comPktQueueIn change

* Remove legacy Svc.Framer

* Fix CMake UTs

* Fix RPI topology config

* Fix FprimeProtocol.fpp module

* Fix namespacing

* Use const reference for FrameContext port

* Review comments EXCEPT port passback refactor

* Rework ComStub with new ByteStream

* New ByteStream - ComInterface model

* Rework TcpClient / TcpServer with new bytestream

* Adapt UDP component for new ByteStream

* Adapt FrameAccumulator for new ByteStream

* Adapt FprimeFramer for new ByteStream

* Update Ref topology with new ByteStream model

* Remove all legacy deallocates from Drivers; reintroduce DEPRECATED model types

* Fix spelling and include error

* More spelling....

* RPI and RpiDemo fixes

* Fix conversion warning on RPI

* static_cast for short int on RPI

* Standardize port names

* Remove legacy Drv types and merge RECV/SEND enum type, delete StreamCrossover

* Update SDDs

* Update SDDs

* Fix ComInterface <-> Framer interfaction, clarify comments and fix annotations

* Switch ComStub from ASSERT to log failure and return buffer

* Add history size check + clarify test handler overrides

* Fix RPI topology to wire comStub on Uplink

* Rename comm to comDriver in RPI topology

* Update communication adapter interface docs
2025-04-29 16:40:36 -07:00

95 lines
4.4 KiB
C++

// ======================================================================
// \title FprimeDeframer.cpp
// \author thomas-bc
// \brief cpp file for FprimeDeframer component implementation class
// ======================================================================
#include "Svc/FprimeDeframer/FprimeDeframer.hpp"
#include "Fw/FPrimeBasicTypes.hpp"
#include "Fw/Types/Assert.hpp"
#include "Svc/FprimeProtocol/FrameHeaderSerializableAc.hpp"
#include "Svc/FprimeProtocol/FrameTrailerSerializableAc.hpp"
namespace Svc {
// ----------------------------------------------------------------------
// Component construction and destruction
// ----------------------------------------------------------------------
FprimeDeframer ::FprimeDeframer(const char* const compName) : FprimeDeframerComponentBase(compName) {}
FprimeDeframer ::~FprimeDeframer() {}
// ----------------------------------------------------------------------
// Handler implementations for user-defined typed input ports
// ----------------------------------------------------------------------
void FprimeDeframer ::framedIn_handler(FwIndexType portNum, Fw::Buffer& data, const ComCfg::FrameContext& context) {
if (data.getSize() < FprimeProtocol::FrameHeader::SERIALIZED_SIZE + FprimeProtocol::FrameTrailer::SERIALIZED_SIZE) {
// Incoming buffer is not long enough to contain a valid frame (header+trailer)
this->log_WARNING_HI_InvalidBufferReceived();
this->bufferDeallocate_out(0, data); // drop the frame
return;
}
// Header and Trailer objects to hold the deserialized data (types are autocoded by FPP)
FprimeProtocol::FrameHeader header;
FprimeProtocol::FrameTrailer trailer;
// ---------------- Validate Frame Header ----------------
// Deserialize transmitted header into the header object
auto deserializer = data.getDeserializer();
Fw::SerializeStatus status = header.deserialize(deserializer);
FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK, status);
// Check that deserialized start_word token matches expected value (default start_word value in the FPP object)
const FprimeProtocol::FrameHeader defaultValue;
if (header.getstartWord() != defaultValue.getstartWord()) {
this->log_WARNING_HI_InvalidStartWord();
this->bufferDeallocate_out(0, data);
return;
}
// We expect the frame size to be size of header + body (of size specified in header) + trailer
const FwSizeType expectedFrameSize = FprimeProtocol::FrameHeader::SERIALIZED_SIZE + header.getlengthField() +
FprimeProtocol::FrameTrailer::SERIALIZED_SIZE;
if (data.getSize() < expectedFrameSize) {
this->log_WARNING_HI_InvalidLengthReceived();
this->bufferDeallocate_out(0, data);
return;
}
// ---------------- Validate Frame Trailer ----------------
// Deserialize transmitted trailer: trailer is at offset = len(header) + len(body)
status = deserializer.moveDeserToOffset(FprimeProtocol::FrameHeader::SERIALIZED_SIZE + header.getlengthField());
FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK, status);
status = trailer.deserialize(deserializer);
FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK, status);
// Compute CRC over the transmitted data (header + body)
Utils::Hash hash;
Utils::HashBuffer computedCrc;
FwSizeType fieldToHashSize = header.getlengthField() + FprimeProtocol::FrameHeader::SERIALIZED_SIZE;
hash.init();
// Add byte by byte to the hash
for (FwSizeType i = 0; i < fieldToHashSize; i++) {
hash.update(data.getData() + i, 1);
}
hash.final(computedCrc);
// Check that the CRC in the trailer of the frame matches the computed CRC
if (trailer.getcrcField() != computedCrc.asBigEndianU32()) {
this->log_WARNING_HI_InvalidChecksum();
this->bufferDeallocate_out(0, data);
return;
}
// ---------------- Extract payload from frame ----------------
// Shift data pointer to effectively remove the header
data.setData(data.getData() + FprimeProtocol::FrameHeader::SERIALIZED_SIZE);
// Shrink size to effectively remove the trailer (also removes the header)
data.setSize(data.getSize() - FprimeProtocol::FrameHeader::SERIALIZED_SIZE -
FprimeProtocol::FrameTrailer::SERIALIZED_SIZE);
// Emit the deframed data
this->deframedOut_out(0, data, context);
}
} // namespace Svc