| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \title FinPdu.cpp | ||
| 3 | // \author Brian Campuzano | ||
| 4 | // \brief cpp file for CFDP FIN (Finished) PDU | ||
| 5 | // ====================================================================== | ||
| 6 | |||
| 7 | #include <Fw/Types/Assert.hpp> | ||
| 8 | #include <Svc/Ccsds/CfdpManager/Types/FinPdu.hpp> | ||
| 9 | |||
| 10 | namespace Svc { | ||
| 11 | namespace Ccsds { | ||
| 12 | namespace Cfdp { | ||
| 13 | |||
| 14 | 43 | void FinPdu::initialize(PduDirection direction, | |
| 15 | Cfdp::Class::T txmMode, | ||
| 16 | EntityId sourceEid, | ||
| 17 | TransactionSeq transactionSeq, | ||
| 18 | EntityId destEid, | ||
| 19 | ConditionCode conditionCode, | ||
| 20 | FinDeliveryCode deliveryCode, | ||
| 21 | FinFileStatus fileStatus) { | ||
| 22 | // Initialize header with PduTypeEnum::FINISHED type | ||
| 23 | 43 | this->m_header.initialize(PduTypeEnum::FINISHED, direction, txmMode, sourceEid, transactionSeq, destEid); | |
| 24 | |||
| 25 | 43 | this->m_conditionCode = conditionCode; | |
| 26 | 43 | this->m_deliveryCode = deliveryCode; | |
| 27 | 43 | this->m_fileStatus = fileStatus; | |
| 28 | |||
| 29 | // Clear TLV list | ||
| 30 | 43 | this->m_tlvList.clear(); | |
| 31 | 43 | } | |
| 32 | |||
| 33 | 63 | U32 FinPdu::getBufferSize() const { | |
| 34 | 63 | U32 size = this->m_header.getBufferSize(); | |
| 35 | |||
| 36 | // Directive code: 1 byte | ||
| 37 | // Flags: 1 byte (condition code, delivery code, file status all packed) | ||
| 38 | 63 | size += static_cast<U32>(sizeof(U8) + sizeof(U8)); | |
| 39 | |||
| 40 | // Add TLV size | ||
| 41 | 63 | size += this->m_tlvList.getEncodedSize(); | |
| 42 | |||
| 43 | 63 | return size; | |
| 44 | } | ||
| 45 | |||
| 46 | 40 | Fw::SerializeStatus FinPdu::serializeTo(Fw::SerialBufferBase& buffer, Fw::Endianness mode) const { | |
| 47 | 40 | return this->toSerialBuffer(buffer); | |
| 48 | } | ||
| 49 | |||
| 50 | 32 | Fw::SerializeStatus FinPdu::deserializeFrom(Fw::SerialBufferBase& buffer, Fw::Endianness mode) { | |
| 51 | // Deserialize header first | ||
| 52 |
1/1✓ Branch 4 taken 32 times.
|
32 | Fw::SerializeStatus status = this->m_header.fromSerialBuffer(buffer); |
| 53 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 31 times.
|
32 | if (status != Fw::FW_SERIALIZE_OK) { |
| 54 | 1 | return status; | |
| 55 | } | ||
| 56 | |||
| 57 | // Validate this is a directive PDU (not file data) | ||
| 58 |
1/2✗ Branch 5 not taken.
✓ Branch 6 taken 31 times.
|
31 | if (this->m_header.m_pduType != PduType::PDU_TYPE_DIRECTIVE) { |
| 59 | ✗ | return Fw::FW_DESERIALIZE_TYPE_MISMATCH; | |
| 60 | } | ||
| 61 | |||
| 62 | // Validate directive code | ||
| 63 | 31 | U8 directiveCode; | |
| 64 |
1/1✓ Branch 7 taken 31 times.
|
31 | status = buffer.deserializeTo(directiveCode); |
| 65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 | if (status != Fw::FW_SERIALIZE_OK) { |
| 66 | ✗ | return status; | |
| 67 | } | ||
| 68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 | if (directiveCode != static_cast<U8>(FileDirective::FILE_DIRECTIVE_FIN)) { |
| 69 | ✗ | return Fw::FW_DESERIALIZE_TYPE_MISMATCH; | |
| 70 | } | ||
| 71 | |||
| 72 | // Now set the type to PduTypeEnum::FINISHED since we've validated it | ||
| 73 | 31 | this->m_header.m_type = PduTypeEnum::FINISHED; | |
| 74 | |||
| 75 | // Deserialize the FIN body | ||
| 76 |
1/1✓ Branch 4 taken 31 times.
|
31 | return this->fromSerialBuffer(buffer); |
| 77 | } | ||
| 78 | |||
| 79 | 40 | Fw::SerializeStatus FinPdu::toSerialBuffer(Fw::SerialBufferBase& serialBuffer) const { | |
| 80 | 40 | FW_ASSERT(this->m_header.m_type == PduTypeEnum::FINISHED); | |
| 81 | |||
| 82 | // Calculate PDU data length (everything after header) | ||
| 83 |
2/2✓ Branch 7 taken 40 times.
✓ Branch 15 taken 40 times.
|
40 | U32 dataLength = this->getBufferSize() - this->m_header.getBufferSize(); |
| 84 | |||
| 85 | // Update header with data length | ||
| 86 | 40 | PduHeader headerCopy = this->m_header; | |
| 87 | 40 | headerCopy.setPduDataLength(static_cast<U16>(dataLength)); | |
| 88 | |||
| 89 | // Serialize header | ||
| 90 |
1/1✓ Branch 1 taken 40 times.
|
40 | Fw::SerializeStatus status = headerCopy.toSerialBuffer(serialBuffer); |
| 91 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if (status != Fw::FW_SERIALIZE_OK) { |
| 92 | ✗ | return status; | |
| 93 | } | ||
| 94 | |||
| 95 | // Directive code (FIN = 5) | ||
| 96 | 40 | U8 directiveCode = static_cast<U8>(FileDirective::FILE_DIRECTIVE_FIN); | |
| 97 |
1/1✓ Branch 7 taken 40 times.
|
40 | status = serialBuffer.serializeFrom(directiveCode); |
| 98 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if (status != Fw::FW_SERIALIZE_OK) { |
| 99 | ✗ | return status; | |
| 100 | } | ||
| 101 | |||
| 102 | // Flags byte: condition code, delivery code, and file status packed together | ||
| 103 | // Bits 7-4: Condition code (4 bits) | ||
| 104 | // Bit 3: Spare (0) | ||
| 105 | // Bit 2: Delivery code (1 bit) | ||
| 106 | // Bits 1-0: File status (2 bits) | ||
| 107 | 40 | U8 flags = 0; | |
| 108 | 40 | flags = static_cast<U8>(flags | static_cast<U8>((static_cast<U8>(this->m_conditionCode) & 0x0FU) << 4)); | |
| 109 | 40 | flags = static_cast<U8>(flags | static_cast<U8>((static_cast<U8>(this->m_deliveryCode) & 0x01U) << 2)); | |
| 110 | 40 | flags = static_cast<U8>(flags | (static_cast<U8>(this->m_fileStatus) & 0x03U)); | |
| 111 | |||
| 112 |
1/1✓ Branch 7 taken 40 times.
|
40 | status = serialBuffer.serializeFrom(flags); |
| 113 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if (status != Fw::FW_SERIALIZE_OK) { |
| 114 | ✗ | return status; | |
| 115 | } | ||
| 116 | |||
| 117 | // Serialize TLVs (if any) | ||
| 118 |
1/1✓ Branch 6 taken 40 times.
|
40 | status = this->m_tlvList.toSerialBuffer(serialBuffer); |
| 119 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if (status != Fw::FW_SERIALIZE_OK) { |
| 120 | ✗ | return status; | |
| 121 | } | ||
| 122 | |||
| 123 | 40 | return Fw::FW_SERIALIZE_OK; | |
| 124 | } | ||
| 125 | |||
| 126 | 31 | Fw::SerializeStatus FinPdu::fromSerialBuffer(Fw::SerialBufferBase& serialBuffer) { | |
| 127 | 31 | FW_ASSERT(this->m_header.m_type == PduTypeEnum::FINISHED); | |
| 128 | |||
| 129 | // Directive code already read by fromBuffer() | ||
| 130 | |||
| 131 | // Flags byte contains: condition code, delivery code, and file status | ||
| 132 | 31 | U8 flags; | |
| 133 |
1/1✓ Branch 7 taken 31 times.
|
31 | Fw::SerializeStatus status = serialBuffer.deserializeTo(flags); |
| 134 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 | if (status != Fw::FW_SERIALIZE_OK) { |
| 135 | ✗ | return status; | |
| 136 | } | ||
| 137 | |||
| 138 | // Extract fields from flags byte: | ||
| 139 | // Bits 7-4: Condition code (4 bits) | ||
| 140 | // Bit 3: Spare | ||
| 141 | // Bit 2: Delivery code (1 bit) | ||
| 142 | // Bits 1-0: File status (2 bits) | ||
| 143 | 31 | U8 conditionCodeVal = (flags >> 4) & 0x0F; | |
| 144 | 31 | U8 deliveryCodeVal = (flags >> 2) & 0x01; | |
| 145 | 31 | U8 fileStatusVal = flags & 0x03; | |
| 146 | |||
| 147 | 31 | this->m_conditionCode = static_cast<ConditionCode>(conditionCodeVal); | |
| 148 | 31 | this->m_deliveryCode = static_cast<FinDeliveryCode>(deliveryCodeVal); | |
| 149 | 31 | this->m_fileStatus = static_cast<FinFileStatus>(fileStatusVal); | |
| 150 | |||
| 151 | // Deserialize TLVs (consumes rest of buffer) | ||
| 152 |
1/1✓ Branch 4 taken 31 times.
|
31 | status = this->m_tlvList.fromSerialBuffer(serialBuffer); |
| 153 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 | if (status != Fw::FW_SERIALIZE_OK) { |
| 154 | ✗ | return status; | |
| 155 | } | ||
| 156 | |||
| 157 | 31 | return Fw::FW_SERIALIZE_OK; | |
| 158 | } | ||
| 159 | |||
| 160 | } // namespace Cfdp | ||
| 161 | } // namespace Ccsds | ||
| 162 | } // namespace Svc | ||
| 163 |