GCC Code Coverage Report


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