GCC Code Coverage Report


Directory: /home/runner/work/fprime/fprime/Svc/FprimeDeframer/
File: FprimeDeframer.cpp
Date: 2026-09-03 22:13:43
Exec Total Coverage
Lines: 0 54 0.0%
Functions: 0 4 0.0%
Branches: 0 49 0.0%

Line Branch Exec Source
1 // ======================================================================
2 // \title FprimeDeframer.cpp
3 // \author thomas-bc
4 // \brief cpp file for FprimeDeframer component implementation class
5 // ======================================================================
6
7 #include "Svc/FprimeDeframer/FprimeDeframer.hpp"
8 #include "Fw/FPrimeBasicTypes.hpp"
9 #include "Fw/Types/Assert.hpp"
10
11 #include "Svc/FprimeProtocol/FrameHeaderSerializableAc.hpp"
12 #include "Svc/FprimeProtocol/FrameTrailerSerializableAc.hpp"
13
14 namespace Svc {
15
16 // ----------------------------------------------------------------------
17 // Component construction and destruction
18 // ----------------------------------------------------------------------
19
20 FprimeDeframer ::FprimeDeframer(const char* const compName) : FprimeDeframerComponentBase(compName) {}
21
22 FprimeDeframer ::~FprimeDeframer() {}
23
24 // ----------------------------------------------------------------------
25 // Handler implementations for user-defined typed input ports
26 // ----------------------------------------------------------------------
27
28 void FprimeDeframer ::dataIn_handler(FwIndexType portNum, Fw::Buffer& data, const ComCfg::FrameContext& context) {
29 if (data.getSize() < FprimeProtocol::FrameHeader::SERIALIZED_SIZE + FprimeProtocol::FrameTrailer::SERIALIZED_SIZE) {
30 // Incoming buffer is not long enough to contain a valid frame (header+trailer)
31 this->log_WARNING_HI_InvalidBufferReceived();
32 this->dataReturnOut_out(0, data, context); // drop the frame
33 return;
34 }
35
36 // Header and Trailer objects to hold the deserialized data (types are autocoded by FPP)
37 FprimeProtocol::FrameHeader header;
38 FprimeProtocol::FrameTrailer trailer;
39
40 // ---------------- Validate Frame Header ----------------
41 // Deserialize transmitted header into the header object
42 auto deserializer = data.getDeserializer();
43 Fw::SerializeStatus status = header.deserializeFrom(deserializer);
44 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK, status);
45 // Check that deserialized start_word token matches expected value (default start_word value in the FPP object)
46 const FprimeProtocol::FrameHeader defaultValue;
47 if (header.get_startWord() != defaultValue.get_startWord()) {
48 this->log_WARNING_HI_InvalidStartWord();
49 this->dataReturnOut_out(0, data, context); // drop the frame
50 return;
51 }
52 // We expect the frame size to be size of header + body (of size specified in header) + trailer
53 const FwSizeType expectedFrameSize = FprimeProtocol::FrameHeader::SERIALIZED_SIZE + header.get_lengthField() +
54 FprimeProtocol::FrameTrailer::SERIALIZED_SIZE;
55 // Reject packets whose data does not match the header
56 if (data.getSize() != expectedFrameSize) {
57 this->log_WARNING_HI_InvalidLengthReceived();
58 this->dataReturnOut_out(0, data, context); // drop the frame
59 return;
60 }
61 // -------- Attempt to extract APID from Payload --------
62 ComCfg::FrameContext contextCopy = context;
63 if (deserializer.getDeserializeSizeLeft() <
64 FprimeProtocol::FrameTrailer::SERIALIZED_SIZE + sizeof(FwPacketDescriptorType)) {
65 // Not enough data to read a valid FwPacketDescriptor, emit event and skip attempting to read an APID
66 this->log_WARNING_LO_PayloadTooShort();
67 } else {
68 // If PacketDescriptor translates to an invalid APID, let it default to FW_PACKET_UNKNOWN
69 // and let downstream components (e.g. custom router) handle it
70 FwPacketDescriptorType packetDescriptor = 0;
71 status = deserializer.deserializeTo(packetDescriptor);
72 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK, status);
73 // If a valid descriptor is deserialized, set it in the context
74 if (ComCfg::Apid::isValid(packetDescriptor)) {
75 contextCopy.set_apid(static_cast<ComCfg::Apid::T>(packetDescriptor));
76 } else {
77 contextCopy.set_apid(ComCfg::Apid::INVALID_UNINITIALIZED);
78 }
79 }
80
81 // ---------------- Validate Frame Trailer ----------------
82 // Deserialize transmitted trailer: trailer is at offset = len(header) + len(body)
83 status = deserializer.moveDeserToOffset(FprimeProtocol::FrameHeader::SERIALIZED_SIZE + header.get_lengthField());
84 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK, status);
85 status = trailer.deserializeFrom(deserializer);
86 FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK, status);
87 // Compute CRC over the transmitted data (header + body)
88 Utils::Hash hash;
89 Utils::HashBuffer computedCrc;
90 FwSizeType fieldToHashSize = header.get_lengthField() + FprimeProtocol::FrameHeader::SERIALIZED_SIZE;
91 hash.init();
92 // Add byte by byte to the hash
93 for (FwSizeType i = 0; i < fieldToHashSize; i++) {
94 hash.update(data.getData() + i, 1);
95 }
96 hash.finalize(computedCrc);
97 // Check that the CRC in the trailer of the frame matches the computed CRC
98 if (trailer.get_crcField() != computedCrc.asBigEndianU32()) {
99 this->log_WARNING_HI_InvalidChecksum();
100 this->dataReturnOut_out(0, data, context); // drop the frame
101 return;
102 }
103
104 // ---------------- Extract payload from frame ----------------
105 // Advance past the header (adjusts size accordingly)
106 data.advance(FprimeProtocol::FrameHeader::SERIALIZED_SIZE);
107 // Shrink size to effectively remove the trailer
108 data.setSize(data.getSize() - FprimeProtocol::FrameTrailer::SERIALIZED_SIZE);
109 // Emit the deframed data
110 this->dataOut_out(0, data, contextCopy);
111 }
112
113 void FprimeDeframer ::dataReturnIn_handler(FwIndexType portNum,
114 Fw::Buffer& fwBuffer,
115 const ComCfg::FrameContext& context) {
116 this->dataReturnOut_out(0, fwBuffer, context);
117 }
118
119 } // namespace Svc
120