| 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 | 8 | FprimeDeframer ::FprimeDeframer(const char* const compName) : FprimeDeframerComponentBase(compName) {} | |
| 21 | |||
| 22 | 16 | FprimeDeframer ::~FprimeDeframer() {} | |
| 23 | |||
| 24 | // ---------------------------------------------------------------------- | ||
| 25 | // Handler implementations for user-defined typed input ports | ||
| 26 | // ---------------------------------------------------------------------- | ||
| 27 | |||
| 28 | 7 | void FprimeDeframer ::dataIn_handler(FwIndexType portNum, Fw::Buffer& data, const ComCfg::FrameContext& context) { | |
| 29 |
3/3✓ Branch 4 taken 7 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 5 times.
|
7 | 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 |
1/1✓ Branch 5 taken 2 times.
|
2 | this->log_WARNING_HI_InvalidBufferReceived(); |
| 32 |
1/1✓ Branch 5 taken 2 times.
|
2 | this->dataReturnOut_out(0, data, context); // drop the frame |
| 33 | 2 | return; | |
| 34 | } | ||
| 35 | |||
| 36 | // Header and Trailer objects to hold the deserialized data (types are autocoded by FPP) | ||
| 37 |
1/1✓ Branch 2 taken 5 times.
|
5 | FprimeProtocol::FrameHeader header; |
| 38 |
1/1✓ Branch 2 taken 5 times.
|
5 | FprimeProtocol::FrameTrailer trailer; |
| 39 | |||
| 40 | // ---------------- Validate Frame Header ---------------- | ||
| 41 | // Deserialize transmitted header into the header object | ||
| 42 |
1/1✓ Branch 2 taken 5 times.
|
5 | auto deserializer = data.getDeserializer(); |
| 43 |
1/1✓ Branch 2 taken 5 times.
|
5 | Fw::SerializeStatus status = header.deserializeFrom(deserializer); |
| 44 | 5 | 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 |
1/1✓ Branch 2 taken 5 times.
|
5 | const FprimeProtocol::FrameHeader defaultValue; |
| 47 |
2/2✓ Branch 4 taken 1 times.
✓ Branch 5 taken 4 times.
|
5 | if (header.get_startWord() != defaultValue.get_startWord()) { |
| 48 |
1/1✓ Branch 5 taken 1 times.
|
1 | this->log_WARNING_HI_InvalidStartWord(); |
| 49 |
1/1✓ Branch 5 taken 1 times.
|
1 | this->dataReturnOut_out(0, data, context); // drop the frame |
| 50 | 1 | return; | |
| 51 | } | ||
| 52 | // We expect the frame size to be size of header + body (of size specified in header) + trailer | ||
| 53 | 4 | const FwSizeType expectedFrameSize = FprimeProtocol::FrameHeader::SERIALIZED_SIZE + header.get_lengthField() + | |
| 54 | 4 | FprimeProtocol::FrameTrailer::SERIALIZED_SIZE; | |
| 55 | // Reject packets whose data does not match the header | ||
| 56 |
3/3✓ Branch 4 taken 4 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 3 times.
|
4 | if (data.getSize() != expectedFrameSize) { |
| 57 |
1/1✓ Branch 5 taken 1 times.
|
1 | this->log_WARNING_HI_InvalidLengthReceived(); |
| 58 |
1/1✓ Branch 5 taken 1 times.
|
1 | this->dataReturnOut_out(0, data, context); // drop the frame |
| 59 | 1 | return; | |
| 60 | } | ||
| 61 | // -------- Attempt to extract APID from Payload -------- | ||
| 62 |
1/1✓ Branch 2 taken 3 times.
|
3 | ComCfg::FrameContext contextCopy = context; |
| 63 |
3/3✓ Branch 2 taken 3 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 2 times.
|
3 | 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 |
1/1✓ Branch 5 taken 1 times.
|
1 | 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 | 2 | FwPacketDescriptorType packetDescriptor = 0; | |
| 71 |
1/1✓ Branch 2 taken 2 times.
|
2 | status = deserializer.deserializeTo(packetDescriptor); |
| 72 | 2 | FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK, status); | |
| 73 | // If a valid descriptor is deserialized, set it in the context | ||
| 74 |
3/3✓ Branch 1 taken 2 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
|
2 | if (ComCfg::Apid::isValid(packetDescriptor)) { |
| 75 |
1/1✓ Branch 2 taken 1 times.
|
1 | contextCopy.set_apid(static_cast<ComCfg::Apid::T>(packetDescriptor)); |
| 76 | } else { | ||
| 77 |
1/1✓ Branch 2 taken 1 times.
|
1 | 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 |
1/1✓ Branch 4 taken 3 times.
|
3 | status = deserializer.moveDeserToOffset(FprimeProtocol::FrameHeader::SERIALIZED_SIZE + header.get_lengthField()); |
| 84 | 3 | FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK, status); | |
| 85 |
1/1✓ Branch 2 taken 3 times.
|
3 | status = trailer.deserializeFrom(deserializer); |
| 86 | 3 | FW_ASSERT(status == Fw::SerializeStatus::FW_SERIALIZE_OK, status); | |
| 87 | // Compute CRC over the transmitted data (header + body) | ||
| 88 |
1/1✓ Branch 2 taken 3 times.
|
3 | Utils::Hash hash; |
| 89 |
1/1✓ Branch 2 taken 3 times.
|
3 | Utils::HashBuffer computedCrc; |
| 90 | 3 | FwSizeType fieldToHashSize = header.get_lengthField() + FprimeProtocol::FrameHeader::SERIALIZED_SIZE; | |
| 91 |
1/1✓ Branch 1 taken 3 times.
|
3 | hash.init(); |
| 92 | // Add byte by byte to the hash | ||
| 93 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 3 times.
|
32 | for (FwSizeType i = 0; i < fieldToHashSize; i++) { |
| 94 |
2/2✓ Branch 4 taken 29 times.
✓ Branch 8 taken 29 times.
|
29 | hash.update(data.getData() + i, 1); |
| 95 | } | ||
| 96 |
1/1✓ Branch 1 taken 3 times.
|
3 | hash.finalize(computedCrc); |
| 97 | // Check that the CRC in the trailer of the frame matches the computed CRC | ||
| 98 |
3/3✓ Branch 4 taken 3 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 2 times.
|
3 | if (trailer.get_crcField() != computedCrc.asBigEndianU32()) { |
| 99 |
1/1✓ Branch 5 taken 1 times.
|
1 | this->log_WARNING_HI_InvalidChecksum(); |
| 100 |
1/1✓ Branch 5 taken 1 times.
|
1 | this->dataReturnOut_out(0, data, context); // drop the frame |
| 101 | 1 | return; | |
| 102 | } | ||
| 103 | |||
| 104 | // ---------------- Extract payload from frame ---------------- | ||
| 105 | // Advance past the header (adjusts size accordingly) | ||
| 106 |
1/1✓ Branch 4 taken 2 times.
|
2 | data.advance(FprimeProtocol::FrameHeader::SERIALIZED_SIZE); |
| 107 | // Shrink size to effectively remove the trailer | ||
| 108 |
2/2✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
|
2 | data.setSize(data.getSize() - FprimeProtocol::FrameTrailer::SERIALIZED_SIZE); |
| 109 | // Emit the deframed data | ||
| 110 |
1/1✓ Branch 5 taken 2 times.
|
2 | this->dataOut_out(0, data, contextCopy); |
| 111 | 17 | } | |
| 112 | |||
| 113 | 1 | void FprimeDeframer ::dataReturnIn_handler(FwIndexType portNum, | |
| 114 | Fw::Buffer& fwBuffer, | ||
| 115 | const ComCfg::FrameContext& context) { | ||
| 116 | 1 | this->dataReturnOut_out(0, fwBuffer, context); | |
| 117 | 1 | } | |
| 118 | |||
| 119 | } // namespace Svc | ||
| 120 |