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  Fw::SerializeBufferBase& serializeRepr = this->m_buffer.getSerializeRepr();
41  // Set buffer length
42  Fw::SerializeStatus status = serializeRepr.setBuffLen(this->m_buffer.getSize());
43  // Reset deserialization
44  if (status == Fw::FW_SERIALIZE_OK) {
45  status = serializeRepr.moveDeserToOffset(Header::PACKET_DESCRIPTOR_OFFSET);
46  }
47  // Deserialize the packet type
48  if (status == Fw::FW_SERIALIZE_OK) {
49  FwPacketDescriptorType packetDescriptor;
50  status = serializeRepr.deserialize(packetDescriptor);
51  if (packetDescriptor != Fw::ComPacket::FW_PACKET_DP) {
53  }
54  }
55  // Deserialize the container id
56  if (status == Fw::FW_SERIALIZE_OK) {
57  status = serializeRepr.deserialize(this->m_id);
58  }
59  // Deserialize the priority
60  if (status == Fw::FW_SERIALIZE_OK) {
61  status = serializeRepr.deserialize(this->m_priority);
62  }
63  // Deserialize the time tag
64  if (status == Fw::FW_SERIALIZE_OK) {
65  status = serializeRepr.deserialize(this->m_timeTag);
66  }
67  // Deserialize the processing types
68  if (status == Fw::FW_SERIALIZE_OK) {
69  status = serializeRepr.deserialize(this->m_procTypes);
70  }
71  // Deserialize the user data
72  if (status == Fw::FW_SERIALIZE_OK) {
73  const FwSizeType requestedSize = sizeof this->m_userData;
74  FwSizeType receivedSize = requestedSize;
75  status = serializeRepr.deserialize(this->m_userData, receivedSize, Fw::Serialization::OMIT_LENGTH);
76  if (receivedSize != requestedSize) {
78  }
79  }
80  // Deserialize the data product state
81  if (status == Fw::FW_SERIALIZE_OK) {
82  status = serializeRepr.deserialize(this->m_dpState);
83  }
84  // Deserialize the data size
85  if (status == Fw::FW_SERIALIZE_OK) {
86  status = serializeRepr.deserializeSize(this->m_dataSize);
87  }
88  return status;
89 }
90 
92  FW_ASSERT(this->m_buffer.isValid());
93  Fw::SerializeBufferBase& serializeRepr = this->m_buffer.getSerializeRepr();
94  // Reset serialization
95  serializeRepr.resetSer();
96  // Serialize the packet type
97  Fw::SerializeStatus status =
98  serializeRepr.serialize(static_cast<FwPacketDescriptorType>(Fw::ComPacket::FW_PACKET_DP));
99  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
100  // Serialize the container id
101  status = serializeRepr.serialize(this->m_id);
102  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
103  // Serialize the priority
104  status = serializeRepr.serialize(this->m_priority);
105  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
106  // Serialize the time tag
107  status = serializeRepr.serialize(this->m_timeTag);
108  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
109  // Serialize the processing types
110  status = serializeRepr.serialize(this->m_procTypes);
111  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
112  // Serialize the user data
113  status = serializeRepr.serialize(this->m_userData, static_cast<FwSizeType>(sizeof this->m_userData),
115  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
116  // Serialize the data product state
117  status = serializeRepr.serialize(this->m_dpState);
118  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
119  // Serialize the data size
120  status = serializeRepr.serializeSize(this->m_dataSize);
121  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
122  // Update the header hash
123  this->updateHeaderHash();
124 }
125 
126 void DpContainer::setBuffer(const Buffer& buffer) {
127  // Set the buffer
128  this->m_buffer = buffer;
129  // Check that the buffer is large enough to hold a data product packet with
130  // zero-size data
131  const FwSizeType bufferSize = buffer.getSize();
132  FW_ASSERT(bufferSize >= MIN_PACKET_SIZE, static_cast<FwAssertArgType>(bufferSize),
133  static_cast<FwAssertArgType>(MIN_PACKET_SIZE));
134  // Initialize the data buffer
135  U8* const buffAddr = buffer.getData();
136  const FwSizeType dataCapacity = buffer.getSize() - MIN_PACKET_SIZE;
137  // Check that data buffer is in bounds for packet buffer
138  const FwSizeType minBufferSize = DATA_OFFSET + dataCapacity;
139  FW_ASSERT(bufferSize >= minBufferSize, static_cast<FwAssertArgType>(bufferSize),
140  static_cast<FwAssertArgType>(minBufferSize));
141  U8* const dataAddr = &buffAddr[DATA_OFFSET];
142  // Set the buffer
143  // This action also clears the serialization state in the buffer
144  this->m_dataBuffer.setExtBuffer(dataAddr, static_cast<Fw::Serializable::SizeType>(dataCapacity));
145  // Reset the data size
146  this->m_dataSize = 0;
147 }
148 
150  const FwSizeType bufferSize = this->m_buffer.getSize();
151  const FwSizeType minBufferSize = HEADER_HASH_OFFSET + HASH_DIGEST_LENGTH;
152  FW_ASSERT(bufferSize >= minBufferSize, static_cast<FwAssertArgType>(bufferSize),
153  static_cast<FwAssertArgType>(minBufferSize));
154  const U8* const buffAddr = this->m_buffer.getData();
156 }
157 
159  const FwSizeType bufferSize = this->m_buffer.getSize();
160  FW_ASSERT(bufferSize >= Header::SIZE, static_cast<FwAssertArgType>(bufferSize),
161  static_cast<FwAssertArgType>(Header::SIZE));
162  U8* const buffAddr = this->m_buffer.getData();
163  Utils::HashBuffer computedHash;
164  Utils::Hash::hash(buffAddr, Header::SIZE, computedHash);
165  return computedHash;
166 }
167 
169  const FwSizeType bufferSize = this->m_buffer.getSize();
170  const FwSizeType minBufferSize = HEADER_HASH_OFFSET + HASH_DIGEST_LENGTH;
171  FW_ASSERT(bufferSize >= minBufferSize, static_cast<FwAssertArgType>(bufferSize),
172  static_cast<FwAssertArgType>(minBufferSize));
173  U8* const buffAddr = this->m_buffer.getData();
174  (void)::memcpy(&buffAddr[HEADER_HASH_OFFSET], hash.getBuffAddr(), HASH_DIGEST_LENGTH);
175 }
176 
178  this->setHeaderHash(this->computeHeaderHash());
179 }
180 
182  storedHash = this->getHeaderHash();
183  computedHash = this->computeHeaderHash();
184  return (storedHash == computedHash) ? Success::SUCCESS : Success::FAILURE;
185 }
186 
188  const U8* const buffAddr = this->m_buffer.getData();
189  const FwSizeType dataHashOffset = this->getDataHashOffset();
190  const FwSizeType bufferSize = this->m_buffer.getSize();
191  FW_ASSERT(dataHashOffset + HASH_DIGEST_LENGTH <= bufferSize,
192  static_cast<FwAssertArgType>(dataHashOffset + HASH_DIGEST_LENGTH),
193  static_cast<FwAssertArgType>(bufferSize));
194  const U8* const dataHashAddr = &buffAddr[dataHashOffset];
195  return Utils::HashBuffer(dataHashAddr, HASH_DIGEST_LENGTH);
196 }
197 
199  U8* const buffAddr = this->m_buffer.getData();
200  const U8* const dataAddr = &buffAddr[DATA_OFFSET];
201  const FwSizeType dataSize = this->getDataSize();
202  const FwSizeType bufferSize = this->m_buffer.getSize();
203  FW_ASSERT(DATA_OFFSET + dataSize <= bufferSize, static_cast<FwAssertArgType>(DATA_OFFSET + dataSize),
204  static_cast<FwAssertArgType>(bufferSize));
205  Utils::HashBuffer computedHash;
206  Utils::Hash::hash(dataAddr, static_cast<NATIVE_INT_TYPE>(dataSize), computedHash);
207  return computedHash;
208 }
209 
211  U8* const buffAddr = this->m_buffer.getData();
212  const FwSizeType bufferSize = this->m_buffer.getSize();
213  const FwSizeType dataHashOffset = this->getDataHashOffset();
214  U8* const dataHashAddr = &buffAddr[dataHashOffset];
215  FW_ASSERT(dataHashOffset + HASH_DIGEST_LENGTH <= bufferSize,
216  static_cast<FwAssertArgType>(dataHashOffset + HASH_DIGEST_LENGTH),
217  static_cast<FwAssertArgType>(bufferSize));
218  ExternalSerializeBuffer serialBuffer(dataHashAddr, HASH_DIGEST_LENGTH);
219  hash.resetSer();
220  const Fw::SerializeStatus status = hash.copyRaw(serialBuffer, HASH_DIGEST_LENGTH);
221  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
222 }
223 
225  this->setDataHash(this->computeDataHash());
226 }
227 
229  storedHash = this->getDataHash();
230  computedHash = this->computeDataHash();
231  return (computedHash == storedHash) ? Success::SUCCESS : Success::FAILURE;
232 }
233 
234 // ----------------------------------------------------------------------
235 // Private member functions
236 // ----------------------------------------------------------------------
237 
238 void DpContainer::initUserDataField() {
239  (void)::memset(this->m_userData, 0, sizeof this->m_userData);
240 }
241 
242 } // namespace Fw
Serialization/Deserialization operation was successful.
SerializeBufferBase & getSerializeRepr()
Definition: Buffer.cpp:107
#define HASH_DIGEST_LENGTH
Definition: CRC32.hpp:18
Data product packet.
Definition: ComPacket.hpp:27
Header::UserData m_userData
The user data.
Representing success.
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.
static constexpr FwSizeType MIN_PACKET_SIZE
Definition: DpContainer.hpp:58
Time m_timeTag
The time tag.
PlatformSizeType FwSizeType
Definition: FpConfig.h:35
U8 * getData() const
Definition: Buffer.cpp:68
U32 FwDpIdType
Definition: FpConfig.h:115
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 getSize() const
Definition: Buffer.cpp:72
void setBuffer(const Buffer &buffer)
Set the packet buffer.
SerializeStatus
forward declaration for string
U32 FwPacketDescriptorType
Definition: FpConfig.h:87
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.
static void hash(const void *data, const NATIVE_INT_TYPE len, HashBuffer &buffer)
Definition: CRC32.cpp:29
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.
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:30
Utils::HashBuffer computeHeaderHash() const
void updateDataHash()
Update the data hash.
void setExtBuffer(U8 *buffPtr, Serializable::SizeType size)
Omit length from serialization.
DpCfg::ProcType::SerialType m_procTypes
The processing types.
FwDpIdType m_id
void serializeHeader()
Definition: DpContainer.cpp:91
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
Buffer m_buffer
The packet buffer.
#define FW_ASSERT(...)
Definition: Assert.hpp:14
SerializeStatus setBuffLen(Serializable::SizeType length)
sets buffer length manually after filling with data
DpContainer()
Constructor for container with default initialization.
Definition: DpContainer.cpp:28