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.deserialize(packetDescriptor);
49  if (packetDescriptor != Fw::ComPacket::FW_PACKET_DP) {
51  }
52  }
53  // Deserialize the container id
54  if (status == Fw::FW_SERIALIZE_OK) {
55  status = deserializer.deserialize(this->m_id);
56  }
57  // Deserialize the priority
58  if (status == Fw::FW_SERIALIZE_OK) {
59  status = deserializer.deserialize(this->m_priority);
60  }
61  // Deserialize the time tag
62  if (status == Fw::FW_SERIALIZE_OK) {
63  status = deserializer.deserialize(this->m_timeTag);
64  }
65  // Deserialize the processing types
66  if (status == Fw::FW_SERIALIZE_OK) {
67  status = deserializer.deserialize(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.deserialize(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.deserialize(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.serialize(static_cast<FwPacketDescriptorType>(Fw::ComPacket::FW_PACKET_DP));
95  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
96  // Serialize the container id
97  status = serializer.serialize(this->m_id);
98  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
99  // Serialize the priority
100  status = serializer.serialize(this->m_priority);
101  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
102  // Serialize the time tag
103  status = serializer.serialize(this->m_timeTag);
104  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
105  // Serialize the processing types
106  status = serializer.serialize(this->m_procTypes);
107  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
108  // Serialize the user data
109  status = serializer.serialize(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.serialize(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  const FwSizeType bufferSize = this->m_buffer.getSize();
147  const FwSizeType minBufferSize = HEADER_HASH_OFFSET + HASH_DIGEST_LENGTH;
148  FW_ASSERT(bufferSize >= minBufferSize, static_cast<FwAssertArgType>(bufferSize),
149  static_cast<FwAssertArgType>(minBufferSize));
150  const U8* const buffAddr = this->m_buffer.getData();
152 }
153 
155  const FwSizeType bufferSize = this->m_buffer.getSize();
156  FW_ASSERT(bufferSize >= Header::SIZE, static_cast<FwAssertArgType>(bufferSize),
157  static_cast<FwAssertArgType>(Header::SIZE));
158  U8* const buffAddr = this->m_buffer.getData();
159  Utils::HashBuffer computedHash;
160  Utils::Hash::hash(buffAddr, Header::SIZE, computedHash);
161  return computedHash;
162 }
163 
165  const FwSizeType bufferSize = this->m_buffer.getSize();
166  const FwSizeType minBufferSize = HEADER_HASH_OFFSET + HASH_DIGEST_LENGTH;
167  FW_ASSERT(bufferSize >= minBufferSize, static_cast<FwAssertArgType>(bufferSize),
168  static_cast<FwAssertArgType>(minBufferSize));
169  U8* const buffAddr = this->m_buffer.getData();
170  (void)::memcpy(&buffAddr[HEADER_HASH_OFFSET], hash.getBuffAddr(), HASH_DIGEST_LENGTH);
171 }
172 
174  this->setHeaderHash(this->computeHeaderHash());
175 }
176 
178  storedHash = this->getHeaderHash();
179  computedHash = this->computeHeaderHash();
180  return (storedHash == computedHash) ? Success::SUCCESS : Success::FAILURE;
181 }
182 
184  const U8* const buffAddr = this->m_buffer.getData();
185  const FwSizeType dataHashOffset = this->getDataHashOffset();
186  const FwSizeType bufferSize = this->m_buffer.getSize();
187  FW_ASSERT(dataHashOffset + HASH_DIGEST_LENGTH <= bufferSize,
188  static_cast<FwAssertArgType>(dataHashOffset + HASH_DIGEST_LENGTH),
189  static_cast<FwAssertArgType>(bufferSize));
190  const U8* const dataHashAddr = &buffAddr[dataHashOffset];
191  return Utils::HashBuffer(dataHashAddr, HASH_DIGEST_LENGTH);
192 }
193 
195  U8* const buffAddr = this->m_buffer.getData();
196  const U8* const dataAddr = &buffAddr[DATA_OFFSET];
197  const FwSizeType dataSize = this->getDataSize();
198  const FwSizeType bufferSize = this->m_buffer.getSize();
199  FW_ASSERT(DATA_OFFSET + dataSize <= bufferSize, static_cast<FwAssertArgType>(DATA_OFFSET + dataSize),
200  static_cast<FwAssertArgType>(bufferSize));
201  Utils::HashBuffer computedHash;
202  Utils::Hash::hash(dataAddr, dataSize, computedHash);
203  return computedHash;
204 }
205 
207  U8* const buffAddr = this->m_buffer.getData();
208  const FwSizeType bufferSize = this->m_buffer.getSize();
209  const FwSizeType dataHashOffset = this->getDataHashOffset();
210  U8* const dataHashAddr = &buffAddr[dataHashOffset];
211  FW_ASSERT(dataHashOffset + HASH_DIGEST_LENGTH <= bufferSize,
212  static_cast<FwAssertArgType>(dataHashOffset + HASH_DIGEST_LENGTH),
213  static_cast<FwAssertArgType>(bufferSize));
214  ExternalSerializeBuffer serialBuffer(dataHashAddr, HASH_DIGEST_LENGTH);
215  hash.resetSer();
216  const Fw::SerializeStatus status = hash.copyRaw(serialBuffer, HASH_DIGEST_LENGTH);
217  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
218 }
219 
221  this->setDataHash(this->computeDataHash());
222 }
223 
225  storedHash = this->getDataHash();
226  computedHash = this->computeDataHash();
227  return (computedHash == storedHash) ? Success::SUCCESS : Success::FAILURE;
228 }
229 
230 // ----------------------------------------------------------------------
231 // Private member functions
232 // ----------------------------------------------------------------------
233 
234 void DpContainer::initUserDataField() {
235  (void)::memset(this->m_userData, 0, sizeof this->m_userData);
236 }
237 
238 } // namespace Fw
Serialization/Deserialization operation was successful.
#define HASH_DIGEST_LENGTH
Definition: CRC32.hpp:18
Data product packet.
Definition: ComPacket.hpp:27
Header::UserData m_userData
The user data.
Representing success.
PlatformSizeType FwSizeType
void setHeaderHash(const Utils::HashBuffer &hash)
Set the header hash.
void resetSer()
reset to beginning of buffer to reuse for serialization
SerializeStatus copyRaw(SerializeBufferBase &dest, Serializable::SizeType size)
directly copies buffer without looking for a size in the stream.
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
static constexpr FwSizeType MIN_PACKET_SIZE
Definition: DpContainer.hpp:58
Time m_timeTag
The time tag.
U8 * getData() const
Definition: Buffer.cpp:68
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:32
Fw::ExternalSerializeBufferWithMemberCopy m_dataBuffer
Utils::HashBuffer getHeaderHash() const
static constexpr FwSizeType HEADER_HASH_OFFSET
The header hash offset.
Definition: DpContainer.hpp:52
U32 FwDpIdType
The type of a data product identifier.
void setBuffer(const Buffer &buffer)
Set the packet buffer.
SerializeStatus
forward declaration for string
ExternalSerializeBufferWithMemberCopy getDeserializer()
Definition: Buffer.cpp:117
FwSizeType m_dataSize
The data size.
FwDpPriorityType m_priority
The priority.
Data was the wrong format (e.g. wrong packet type)
void updateHeaderHash()
Compute and set the header hash.
bool isValid() const
Definition: Buffer.cpp:64
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
DpState m_dpState
The data product state.
FwSizeType getDataHashOffset() const
Get the data hash offset.
SerializeStatus moveDeserToOffset(FwSizeType offset)
Moves deserialization to the specified offset.
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:54
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:56
Utils::HashBuffer computeHeaderHash() const
static void hash(const void *data, const FwSizeType len, HashBuffer &buffer)
Definition: CRC32.cpp:32
void updateDataHash()
Update the data hash.
void setExtBuffer(U8 *buffPtr, Serializable::SizeType size)
Omit length from serialization.
U32 FwPacketDescriptorType
The type of a com packet descriptor.
DpCfg::ProcType::SerialType m_procTypes
The processing types.
FwDpIdType m_id
void serializeHeader()
Definition: DpContainer.cpp:89
static constexpr FwSizeType SIZE
The header size.
Definition: DpContainer.hpp:48
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
Definition: DpContainer.hpp:95
SizeType getSize() const
Definition: Buffer.cpp:72
Buffer m_buffer
The packet buffer.
ExternalSerializeBufferWithMemberCopy getSerializer()
Definition: Buffer.cpp:107
#define FW_ASSERT(...)
Definition: Assert.hpp:14
DpContainer()
Constructor for container with default initialization.
Definition: DpContainer.cpp:28