GCC Code Coverage Report


Directory: ./
File: Fw/Dp/DpContainer.cpp
Date: 2026-09-03 22:12:29
Exec Total Coverage
Lines: 121 142 85.2%
Functions: 14 17 82.4%
Branches: 54 72 75.0%

Line Branch Exec Source
1 // ======================================================================
2 // \title DpContainer.cpp
3 // \author bocchino
4 // \brief cpp file for DpContainer
5 // ======================================================================
6
7 #include <cstring>
8
9 #include "Fw/Com/ComPacket.hpp"
10 #include "Fw/Dp/DpContainer.hpp"
11 #include "Fw/Types/Assert.hpp"
12
13 namespace Fw {
14
15 // ----------------------------------------------------------------------
16 // Constructor
17 // ----------------------------------------------------------------------
18
19 2 DpContainer::DpContainer(FwDpIdType id, const Fw::Buffer& buffer)
20
3/3
✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
✓ Branch 8 taken 2 times.
2 : m_id(id), m_priority(0), m_timeTag(), m_procTypes(0), m_dpState(), m_dataSize(0), m_buffer(), m_dataBuffer() {
21 // Initialize the user data field
22 2 this->initUserDataField();
23 // Set the packet buffer
24 // This action also updates the data buffer
25
1/1
✓ Branch 1 taken 2 times.
2 this->setBuffer(buffer);
26 2 }
27
28 8 DpContainer::DpContainer()
29
3/3
✓ Branch 2 taken 8 times.
✓ Branch 5 taken 8 times.
✓ Branch 8 taken 8 times.
8 : m_id(0), m_priority(0), m_timeTag(), m_procTypes(0), m_dataSize(0), m_buffer(), m_dataBuffer() {
30 // Initialize the user data field
31 8 this->initUserDataField();
32 8 }
33
34 // ----------------------------------------------------------------------
35 // Public member functions
36 // ----------------------------------------------------------------------
37
38 2 Fw::SerializeStatus DpContainer::deserializeHeader() {
39 2 FW_ASSERT(this->m_buffer.isValid());
40
1/1
✓ Branch 1 taken 2 times.
2 auto deserializer = this->m_buffer.getDeserializer();
41
42 // Reset deserialization
43
1/1
✓ Branch 1 taken 2 times.
2 Fw::SerializeStatus status = deserializer.moveDeserToOffset(Header::PACKET_DESCRIPTOR_OFFSET);
44
45 // Deserialize the packet type
46
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (status == Fw::FW_SERIALIZE_OK) {
47 FwPacketDescriptorType packetDescriptor;
48
1/1
✓ Branch 1 taken 2 times.
2 status = deserializer.deserializeTo(packetDescriptor);
49
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if ((status == Fw::FW_SERIALIZE_OK) && (packetDescriptor != ComPacketType::FW_PACKET_DP)) {
50 status = Fw::FW_SERIALIZE_FORMAT_ERROR;
51 }
52 }
53 // Deserialize the container id
54
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (status == Fw::FW_SERIALIZE_OK) {
55
1/1
✓ Branch 1 taken 2 times.
2 status = deserializer.deserializeTo(this->m_id);
56 }
57 // Deserialize the priority
58
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (status == Fw::FW_SERIALIZE_OK) {
59
1/1
✓ Branch 1 taken 2 times.
2 status = deserializer.deserializeTo(this->m_priority);
60 }
61 // Deserialize the time tag
62
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (status == Fw::FW_SERIALIZE_OK) {
63
1/1
✓ Branch 1 taken 2 times.
2 status = deserializer.deserializeTo(this->m_timeTag);
64 }
65 // Deserialize the processing types
66
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (status == Fw::FW_SERIALIZE_OK) {
67
1/1
✓ Branch 1 taken 2 times.
2 status = deserializer.deserializeTo(this->m_procTypes);
68 }
69 // Deserialize the user data
70
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (status == Fw::FW_SERIALIZE_OK) {
71 2 const FwSizeType requestedSize = sizeof this->m_userData;
72 2 FwSizeType receivedSize = requestedSize;
73
1/1
✓ Branch 1 taken 2 times.
2 status = deserializer.deserializeTo(this->m_userData, receivedSize, Fw::Serialization::OMIT_LENGTH);
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (receivedSize != requestedSize) {
75 status = Fw::FW_DESERIALIZE_SIZE_MISMATCH;
76 }
77 }
78 // Deserialize the data product state
79
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (status == Fw::FW_SERIALIZE_OK) {
80
1/1
✓ Branch 1 taken 2 times.
2 status = deserializer.deserializeTo(this->m_dpState);
81 }
82 // Deserialize the data size
83
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (status == Fw::FW_SERIALIZE_OK) {
84
1/1
✓ Branch 1 taken 2 times.
2 status = deserializer.deserializeSize(this->m_dataSize);
85 }
86 2 return status;
87 2 }
88
89 2 void DpContainer::serializeHeader() {
90 2 FW_ASSERT(this->m_buffer.isValid());
91
1/1
✓ Branch 1 taken 2 times.
2 auto serializer = this->m_buffer.getSerializer();
92 // Serialize the packet type
93 Fw::SerializeStatus status =
94
1/1
✓ Branch 1 taken 2 times.
2 serializer.serializeFrom(static_cast<FwPacketDescriptorType>(Fw::ComPacketType::FW_PACKET_DP));
95 2 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
96 // Serialize the container id
97
1/1
✓ Branch 1 taken 2 times.
2 status = serializer.serializeFrom(this->m_id);
98 2 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
99 // Serialize the priority
100
1/1
✓ Branch 1 taken 2 times.
2 status = serializer.serializeFrom(this->m_priority);
101 2 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
102 // Serialize the time tag
103
1/1
✓ Branch 1 taken 2 times.
2 status = serializer.serializeFrom(this->m_timeTag);
104 2 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
105 // Serialize the processing types
106
1/1
✓ Branch 1 taken 2 times.
2 status = serializer.serializeFrom(this->m_procTypes);
107 2 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
108 // Serialize the user data
109
1/1
✓ Branch 1 taken 2 times.
2 status = serializer.serializeFrom(this->m_userData, static_cast<FwSizeType>(sizeof this->m_userData),
110 Fw::Serialization::OMIT_LENGTH);
111 2 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
112 // Serialize the data product state
113
1/1
✓ Branch 1 taken 2 times.
2 status = serializer.serializeFrom(this->m_dpState);
114 2 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
115 // Serialize the data size
116
1/1
✓ Branch 1 taken 2 times.
2 status = serializer.serializeSize(this->m_dataSize);
117 2 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
118 // Update the header hash
119
1/1
✓ Branch 1 taken 2 times.
2 this->updateHeaderHash();
120 2 }
121
122 6 void DpContainer::setBuffer(const Buffer& buffer) {
123 // Set the buffer
124 6 this->m_buffer = buffer;
125 // Check that the buffer is large enough to hold a data product packet with
126 // zero-size data
127 6 const FwSizeType bufferSize = buffer.getSize();
128 6 FW_ASSERT(bufferSize >= MIN_PACKET_SIZE, static_cast<FwAssertArgType>(bufferSize),
129 static_cast<FwAssertArgType>(MIN_PACKET_SIZE));
130 // Initialize the data buffer
131 6 U8* const buffAddr = buffer.getData();
132 6 const FwSizeType dataCapacity = buffer.getSize() - MIN_PACKET_SIZE;
133 // Check that data buffer is in bounds for packet buffer
134 6 const FwSizeType minBufferSize = DATA_OFFSET + dataCapacity;
135 6 FW_ASSERT(bufferSize >= minBufferSize, static_cast<FwAssertArgType>(bufferSize),
136 static_cast<FwAssertArgType>(minBufferSize));
137 6 U8* const dataAddr = &buffAddr[DATA_OFFSET];
138 // Set the buffer
139 // This action also clears the serialization state in the buffer
140 6 this->m_dataBuffer.setExtBuffer(dataAddr, static_cast<Fw::Serializable::SizeType>(dataCapacity));
141 // Reset the data size
142 6 this->m_dataSize = 0;
143 6 }
144
145 void DpContainer::shrinkBufferSize() {
146 // Calculate the assumed size for the Fw::Buffer
147 const FwSizeType newSize = this->getPacketSize();
148
149 // Check that the buffer can still store a data product
150 // AND
151 // That the update is a shrink operation. Growing an
152 // Fw::Buffer is not safe
153 FW_ASSERT(newSize >= MIN_PACKET_SIZE, static_cast<FwAssertArgType>(newSize));
154 FW_ASSERT(newSize <= this->m_buffer.getSize(), static_cast<FwAssertArgType>(newSize),
155 static_cast<FwAssertArgType>(this->m_buffer.getSize()));
156
157 // Shrink the Fw::Buffer
158 this->m_buffer.setSize(newSize);
159 }
160
161 2 Utils::HashBuffer DpContainer::getHeaderHash() const {
162 2 const FwSizeType bufferSize = this->m_buffer.getSize();
163 2 const FwSizeType minBufferSize = HEADER_HASH_OFFSET + HASH_DIGEST_LENGTH;
164 2 FW_ASSERT(bufferSize >= minBufferSize, static_cast<FwAssertArgType>(bufferSize),
165 static_cast<FwAssertArgType>(minBufferSize));
166 2 const U8* const buffAddr = this->m_buffer.getData();
167 2 return Utils::HashBuffer(&buffAddr[HEADER_HASH_OFFSET], HASH_DIGEST_LENGTH);
168 }
169
170 4 Utils::HashBuffer DpContainer::computeHeaderHash() const {
171 4 const FwSizeType bufferSize = this->m_buffer.getSize();
172 4 FW_ASSERT(bufferSize >= Header::SIZE, static_cast<FwAssertArgType>(bufferSize),
173 static_cast<FwAssertArgType>(Header::SIZE));
174 4 U8* const buffAddr = this->m_buffer.getData();
175 4 Utils::HashBuffer computedHash;
176
1/1
✓ Branch 1 taken 4 times.
4 Utils::Hash::hash(buffAddr, Header::SIZE, computedHash);
177 4 return computedHash;
178 }
179
180 2 void DpContainer::setHeaderHash(const Utils::HashBuffer& hash) {
181 2 const FwSizeType bufferSize = this->m_buffer.getSize();
182 2 const FwSizeType minBufferSize = HEADER_HASH_OFFSET + HASH_DIGEST_LENGTH;
183 2 FW_ASSERT(bufferSize >= minBufferSize, static_cast<FwAssertArgType>(bufferSize),
184 static_cast<FwAssertArgType>(minBufferSize));
185 2 U8* const buffAddr = this->m_buffer.getData();
186 2 (void)::memcpy(&buffAddr[HEADER_HASH_OFFSET], hash.getBuffAddr(), HASH_DIGEST_LENGTH);
187 2 }
188
189 2 void DpContainer::updateHeaderHash() {
190
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 this->setHeaderHash(this->computeHeaderHash());
191 2 }
192
193 2 Success::T DpContainer::checkHeaderHash(Utils::HashBuffer& storedHash, Utils::HashBuffer& computedHash) const {
194
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 storedHash = this->getHeaderHash();
195
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 computedHash = this->computeHeaderHash();
196
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 return (storedHash == computedHash) ? Success::SUCCESS : Success::FAILURE;
197 }
198
199 Utils::HashBuffer DpContainer::getDataHash() const {
200 const U8* const buffAddr = this->m_buffer.getData();
201 const FwSizeType dataHashOffset = this->getDataHashOffset();
202 const FwSizeType bufferSize = this->m_buffer.getSize();
203 FW_ASSERT(dataHashOffset + HASH_DIGEST_LENGTH <= bufferSize,
204 static_cast<FwAssertArgType>(dataHashOffset + HASH_DIGEST_LENGTH),
205 static_cast<FwAssertArgType>(bufferSize));
206 const U8* const dataHashAddr = &buffAddr[dataHashOffset];
207 return Utils::HashBuffer(dataHashAddr, HASH_DIGEST_LENGTH);
208 }
209
210 2 Utils::HashBuffer DpContainer::computeDataHash() const {
211 2 U8* const buffAddr = this->m_buffer.getData();
212 2 const U8* const dataAddr = &buffAddr[DATA_OFFSET];
213 2 const FwSizeType dataSize = this->getDataSize();
214 2 const FwSizeType bufferSize = this->m_buffer.getSize();
215 2 FW_ASSERT(DATA_OFFSET + dataSize <= bufferSize, static_cast<FwAssertArgType>(DATA_OFFSET + dataSize),
216 static_cast<FwAssertArgType>(bufferSize));
217 2 Utils::HashBuffer computedHash;
218
1/1
✓ Branch 1 taken 2 times.
2 Utils::Hash::hash(dataAddr, dataSize, computedHash);
219 2 return computedHash;
220 }
221
222 2 void DpContainer::setDataHash(Utils::HashBuffer hash) {
223
1/1
✓ Branch 1 taken 2 times.
2 U8* const buffAddr = this->m_buffer.getData();
224
1/1
✓ Branch 1 taken 2 times.
2 const FwSizeType bufferSize = this->m_buffer.getSize();
225 2 const FwSizeType dataHashOffset = this->getDataHashOffset();
226 2 U8* const dataHashAddr = &buffAddr[dataHashOffset];
227 2 FW_ASSERT(dataHashOffset + HASH_DIGEST_LENGTH <= bufferSize,
228 static_cast<FwAssertArgType>(dataHashOffset + HASH_DIGEST_LENGTH),
229 static_cast<FwAssertArgType>(bufferSize));
230
1/1
✓ Branch 1 taken 2 times.
2 ExternalSerializeBuffer serialBuffer(dataHashAddr, HASH_DIGEST_LENGTH);
231
1/1
✓ Branch 1 taken 2 times.
2 hash.resetDeser();
232
1/1
✓ Branch 1 taken 2 times.
2 const Fw::SerializeStatus status = hash.copyRaw(serialBuffer, HASH_DIGEST_LENGTH);
233 2 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
234 2 }
235
236 2 void DpContainer::updateDataHash() {
237
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 this->setDataHash(this->computeDataHash());
238 2 }
239
240 Success::T DpContainer::checkDataHash(Utils::HashBuffer& storedHash, Utils::HashBuffer& computedHash) const {
241 storedHash = this->getDataHash();
242 computedHash = this->computeDataHash();
243 return (computedHash == storedHash) ? Success::SUCCESS : Success::FAILURE;
244 }
245
246 // ----------------------------------------------------------------------
247 // Private member functions
248 // ----------------------------------------------------------------------
249
250 10 void DpContainer::initUserDataField() {
251 10 (void)::memset(this->m_userData, 0, sizeof this->m_userData);
252 10 }
253
254 } // namespace Fw
255