GCC Code Coverage Report


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