GCC Code Coverage Report


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