GCC Code Coverage Report


Directory: ./
File: Fw/Dp/DpContainer.cpp
Date: 2026-09-03 21:13:48
Exec Total Coverage
Lines: 140 143 97.9%
Functions: 17 17 100.0%
Branches: 70 74 94.6%

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