F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
DpContainer.cpp
Go to the documentation of this file.
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 
20  : 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  this->initUserDataField();
23  // Set the packet buffer
24  // This action also updates the data buffer
25  this->setBuffer(buffer);
26 }
27 
29  : 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  this->initUserDataField();
32 }
33 
34 // ----------------------------------------------------------------------
35 // Public member functions
36 // ----------------------------------------------------------------------
37 
39  FW_ASSERT(this->m_buffer.isValid());
40  auto deserializer = this->m_buffer.getDeserializer();
41 
42  // Reset deserialization
44 
45  // Deserialize the packet type
46  if (status == Fw::FW_SERIALIZE_OK) {
47  FwPacketDescriptorType packetDescriptor;
48  status = deserializer.deserializeTo(packetDescriptor);
49  if (packetDescriptor != ComPacketType::FW_PACKET_DP) {
51  }
52  }
53  // Deserialize the container id
54  if (status == Fw::FW_SERIALIZE_OK) {
55  status = deserializer.deserializeTo(this->m_id);
56  }
57  // Deserialize the priority
58  if (status == Fw::FW_SERIALIZE_OK) {
59  status = deserializer.deserializeTo(this->m_priority);
60  }
61  // Deserialize the time tag
62  if (status == Fw::FW_SERIALIZE_OK) {
63  status = deserializer.deserializeTo(this->m_timeTag);
64  }
65  // Deserialize the processing types
66  if (status == Fw::FW_SERIALIZE_OK) {
67  status = deserializer.deserializeTo(this->m_procTypes);
68  }
69  // Deserialize the user data
70  if (status == Fw::FW_SERIALIZE_OK) {
71  const FwSizeType requestedSize = sizeof this->m_userData;
72  FwSizeType receivedSize = requestedSize;
73  status = deserializer.deserializeTo(this->m_userData, receivedSize, Fw::Serialization::OMIT_LENGTH);
74  if (receivedSize != requestedSize) {
76  }
77  }
78  // Deserialize the data product state
79  if (status == Fw::FW_SERIALIZE_OK) {
80  status = deserializer.deserializeTo(this->m_dpState);
81  }
82  // Deserialize the data size
83  if (status == Fw::FW_SERIALIZE_OK) {
84  status = deserializer.deserializeSize(this->m_dataSize);
85  }
86  return status;
87 }
88 
90  FW_ASSERT(this->m_buffer.isValid());
91  auto serializer = this->m_buffer.getSerializer();
92  // Serialize the packet type
93  Fw::SerializeStatus status =
94  serializer.serializeFrom(static_cast<FwPacketDescriptorType>(Fw::ComPacketType::FW_PACKET_DP));
95  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
96  // Serialize the container id
97  status = serializer.serializeFrom(this->m_id);
98  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
99  // Serialize the priority
100  status = serializer.serializeFrom(this->m_priority);
101  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
102  // Serialize the time tag
103  status = serializer.serializeFrom(this->m_timeTag);
104  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
105  // Serialize the processing types
106  status = serializer.serializeFrom(this->m_procTypes);
107  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
108  // Serialize the user data
109  status = serializer.serializeFrom(this->m_userData, static_cast<FwSizeType>(sizeof this->m_userData),
111  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
112  // Serialize the data product state
113  status = serializer.serializeFrom(this->m_dpState);
114  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
115  // Serialize the data size
116  status = serializer.serializeSize(this->m_dataSize);
117  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
118  // Update the header hash
119  this->updateHeaderHash();
120 }
121 
122 void DpContainer::setBuffer(const Buffer& buffer) {
123  // Set the buffer
124  this->m_buffer = buffer;
125  // Check that the buffer is large enough to hold a data product packet with
126  // zero-size data
127  const FwSizeType bufferSize = buffer.getSize();
128  FW_ASSERT(bufferSize >= MIN_PACKET_SIZE, static_cast<FwAssertArgType>(bufferSize),
129  static_cast<FwAssertArgType>(MIN_PACKET_SIZE));
130  // Initialize the data buffer
131  U8* const buffAddr = buffer.getData();
132  const FwSizeType dataCapacity = buffer.getSize() - MIN_PACKET_SIZE;
133  // Check that data buffer is in bounds for packet buffer
134  const FwSizeType minBufferSize = DATA_OFFSET + dataCapacity;
135  FW_ASSERT(bufferSize >= minBufferSize, static_cast<FwAssertArgType>(bufferSize),
136  static_cast<FwAssertArgType>(minBufferSize));
137  U8* const dataAddr = &buffAddr[DATA_OFFSET];
138  // Set the buffer
139  // This action also clears the serialization state in the buffer
140  this->m_dataBuffer.setExtBuffer(dataAddr, static_cast<Fw::Serializable::SizeType>(dataCapacity));
141  // Reset the data size
142  this->m_dataSize = 0;
143 }
144 
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 
162  const FwSizeType bufferSize = this->m_buffer.getSize();
163  const FwSizeType minBufferSize = HEADER_HASH_OFFSET + HASH_DIGEST_LENGTH;
164  FW_ASSERT(bufferSize >= minBufferSize, static_cast<FwAssertArgType>(bufferSize),
165  static_cast<FwAssertArgType>(minBufferSize));
166  const U8* const buffAddr = this->m_buffer.getData();
168 }
169 
171  const FwSizeType bufferSize = this->m_buffer.getSize();
172  FW_ASSERT(bufferSize >= Header::SIZE, static_cast<FwAssertArgType>(bufferSize),
173  static_cast<FwAssertArgType>(Header::SIZE));
174  U8* const buffAddr = this->m_buffer.getData();
175  Utils::HashBuffer computedHash;
176  Utils::Hash::hash(buffAddr, Header::SIZE, computedHash);
177  return computedHash;
178 }
179 
181  const FwSizeType bufferSize = this->m_buffer.getSize();
182  const FwSizeType minBufferSize = HEADER_HASH_OFFSET + HASH_DIGEST_LENGTH;
183  FW_ASSERT(bufferSize >= minBufferSize, static_cast<FwAssertArgType>(bufferSize),
184  static_cast<FwAssertArgType>(minBufferSize));
185  U8* const buffAddr = this->m_buffer.getData();
186  (void)::memcpy(&buffAddr[HEADER_HASH_OFFSET], hash.getBuffAddr(), HASH_DIGEST_LENGTH);
187 }
188 
190  this->setHeaderHash(this->computeHeaderHash());
191 }
192 
194  storedHash = this->getHeaderHash();
195  computedHash = this->computeHeaderHash();
196  return (storedHash == computedHash) ? Success::SUCCESS : Success::FAILURE;
197 }
198 
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 
211  U8* const buffAddr = this->m_buffer.getData();
212  const U8* const dataAddr = &buffAddr[DATA_OFFSET];
213  const FwSizeType dataSize = this->getDataSize();
214  const FwSizeType bufferSize = this->m_buffer.getSize();
215  FW_ASSERT(DATA_OFFSET + dataSize <= bufferSize, static_cast<FwAssertArgType>(DATA_OFFSET + dataSize),
216  static_cast<FwAssertArgType>(bufferSize));
217  Utils::HashBuffer computedHash;
218  Utils::Hash::hash(dataAddr, dataSize, computedHash);
219  return computedHash;
220 }
221 
223  U8* const buffAddr = this->m_buffer.getData();
224  const FwSizeType bufferSize = this->m_buffer.getSize();
225  const FwSizeType dataHashOffset = this->getDataHashOffset();
226  U8* const dataHashAddr = &buffAddr[dataHashOffset];
227  FW_ASSERT(dataHashOffset + HASH_DIGEST_LENGTH <= bufferSize,
228  static_cast<FwAssertArgType>(dataHashOffset + HASH_DIGEST_LENGTH),
229  static_cast<FwAssertArgType>(bufferSize));
230  ExternalSerializeBuffer serialBuffer(dataHashAddr, HASH_DIGEST_LENGTH);
231  hash.resetDeser();
232  const Fw::SerializeStatus status = hash.copyRaw(serialBuffer, HASH_DIGEST_LENGTH);
233  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
234 }
235 
237  this->setDataHash(this->computeDataHash());
238 }
239 
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 void DpContainer::initUserDataField() {
251  (void)::memset(this->m_userData, 0, sizeof this->m_userData);
252 }
253 
254 } // namespace Fw
Serialization/Deserialization operation was successful.
U16 FwPacketDescriptorType
The width of packet descriptors when they are serialized by the framework.
#define HASH_DIGEST_LENGTH
Definition: CRC32.hpp:18
U8 * getBuffAddr() override
Header::UserData m_userData
The user data.
SerializeStatus serializeFrom(U8 val, Endianness mode=Endianness::BIG) override
Serialize an 8-bit unsigned integer value.
Representing success.
PlatformSizeType FwSizeType
void setHeaderHash(const Utils::HashBuffer &hash)
Set the header hash.
void setSize(FwSizeType size)
Definition: Buffer.cpp:75
static constexpr FwSizeType MIN_PACKET_SIZE
Definition: DpContainer.hpp:65
Time m_timeTag
The time tag.
U8 * getData() const
Definition: Buffer.cpp:56
void setDataHash(Utils::HashBuffer hash)
Set the data hash.
static constexpr FwSizeType PACKET_DESCRIPTOR_OFFSET
The offset for the packet descriptor field.
Definition: DpContainer.hpp:39
Fw::ExternalSerializeBufferWithMemberCopy m_dataBuffer
Utils::HashBuffer getHeaderHash() const
SerializeStatus copyRaw(SerialBufferBase &dest, Serializable::SizeType size) override
Copy raw bytes from this buffer to destination and advance source offset.
static constexpr FwSizeType HEADER_HASH_OFFSET
The header hash offset.
Definition: DpContainer.hpp:59
void setBuffer(const Buffer &buffer)
Set the packet buffer.
SerializeStatus
forward declaration for string
ExternalSerializeBufferWithMemberCopy getDeserializer()
Definition: Buffer.cpp:105
FwSizeType m_dataSize
The data size.
FwDpPriorityType m_priority
The priority.
Data was the wrong format (e.g. wrong packet type)
Omit length from serialization.
void updateHeaderHash()
Compute and set the header hash.
bool isValid() const
Definition: Buffer.cpp:52
Data was left in the buffer, but not enough to deserialize.
External serialize buffer with no copy semantics.
Utils::HashBuffer getDataHash() const
T
The raw enum type.
Utils::HashBuffer computeDataHash() const
void shrinkBufferSize()
void resetDeser() override
Reset deserialization pointer to beginning of buffer.
DpState m_dpState
The data product state.
FwSizeType getDataHashOffset() const
Get the data hash offset.
FwSizeType getPacketSize() const
Get the packet size corresponding to the data size.
Representing failure.
Success::T checkDataHash(Utils::HashBuffer &storedHash, Utils::HashBuffer &computedHash) const
Check the data hash.
static constexpr FwSizeType DATA_OFFSET
The data offset.
Definition: DpContainer.hpp:61
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:53
Utils::HashBuffer computeHeaderHash() const
FwSizeType getSize() const
Definition: Buffer.cpp:60
static void hash(const void *data, const FwSizeType len, HashBuffer &buffer)
Definition: CRC32.cpp:25
FwIdType FwDpIdType
The type of a data product identifier.
void updateDataHash()
Update the data hash.
void setExtBuffer(U8 *buffPtr, Serializable::SizeType size)
Set the external buffer.
DpCfg::ProcType::SerialType m_procTypes
The processing types.
FwDpIdType m_id
void serializeHeader()
Definition: DpContainer.cpp:89
SerializeStatus moveDeserToOffset(FwSizeType offset) override
Move deserialization pointer to specified offset.
static constexpr FwSizeType SIZE
The header size.
Definition: DpContainer.hpp:55
A container class for holding a hash buffer.
Definition: HashBuffer.hpp:26
Success::T checkHeaderHash(Utils::HashBuffer &storedHash, Utils::HashBuffer &computedHash) const
Check the header hash.
Fw::SerializeStatus deserializeHeader()
Definition: DpContainer.cpp:38
FwSizeType getDataSize() const
Implementation of malloc based allocator.
Buffer m_buffer
The packet buffer.
ExternalSerializeBufferWithMemberCopy getSerializer()
Definition: Buffer.cpp:95
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformAssertArgType FwAssertArgType
The type of arguments to assert functions.
DpContainer()
Constructor for container with default initialization.
Definition: DpContainer.cpp:28