F´ Flight Software - C/C++ Documentation  devel
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) {
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 =
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
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:55
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:30
#define HASH_DIGEST_LENGTH
Definition: CRC32.hpp:18
PlatformAssertArgType FwAssertArgType
Definition: FpConfig.h:39
U32 FwDpIdType
Definition: FpConfig.h:115
U32 FwPacketDescriptorType
Definition: FpConfig.h:87
PlatformSizeType FwSizeType
Definition: FpConfig.h:35
U8 * getData() const
Definition: Buffer.cpp:68
bool isValid() const
Definition: Buffer.cpp:64
U32 getSize() const
Definition: Buffer.cpp:72
SerializeBufferBase & getSerializeRepr()
Definition: Buffer.cpp:107
@ FW_PACKET_DP
Data product packet.
Definition: ComPacket.hpp:27
DpCfg::ProcType::SerialType m_procTypes
The processing types.
static constexpr FwSizeType DATA_OFFSET
The data offset.
Definition: DpContainer.hpp:54
DpContainer()
Constructor for container with default initialization.
Definition: DpContainer.cpp:28
Utils::HashBuffer getDataHash() const
FwDpPriorityType m_priority
The priority.
Header::UserData m_userData
The user data.
static constexpr FwSizeType MIN_PACKET_SIZE
Definition: DpContainer.hpp:58
void updateHeaderHash()
Compute and set the header hash.
Utils::HashBuffer computeDataHash() const
Utils::HashBuffer computeHeaderHash() const
Fw::SerializeStatus deserializeHeader()
Definition: DpContainer.cpp:38
Utils::HashBuffer getHeaderHash() const
Fw::ExternalSerializeBufferWithMemberCopy m_dataBuffer
Time m_timeTag
The time tag.
FwDpIdType m_id
Success::T checkHeaderHash(Utils::HashBuffer &storedHash, Utils::HashBuffer &computedHash) const
Check the header hash.
void serializeHeader()
Definition: DpContainer.cpp:91
void setDataHash(Utils::HashBuffer hash)
Set the data hash.
Buffer m_buffer
The packet buffer.
FwSizeType m_dataSize
The data size.
FwSizeType getDataSize() const
Definition: DpContainer.hpp:95
void setBuffer(const Buffer &buffer)
Set the packet buffer.
void setHeaderHash(const Utils::HashBuffer &hash)
Set the header hash.
FwSizeType getDataHashOffset() const
Get the data hash offset.
static constexpr FwSizeType HEADER_HASH_OFFSET
The header hash offset.
Definition: DpContainer.hpp:52
Success::T checkDataHash(Utils::HashBuffer &storedHash, Utils::HashBuffer &computedHash) const
Check the data hash.
void updateDataHash()
Update the data hash.
DpState m_dpState
The data product state.
External serialize buffer with no copy semantics.
void setExtBuffer(U8 *buffPtr, Serializable::SizeType size)
NATIVE_UINT_TYPE SizeType
@ OMIT_LENGTH
Omit length from serialization.
SerializeStatus deserializeSize(FwSizeType &size)
deserialize a size value
SerializeStatus setBuffLen(Serializable::SizeType length)
sets buffer length manually after filling with data
SerializeStatus moveDeserToOffset(FwSizeType offset)
Moves deserialization to the specified offset.
void resetSer()
reset to beginning of buffer to reuse for serialization
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
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
SerializeStatus serializeSize(const FwSizeType size)
serialize a size value
T
The raw enum type.
@ FAILURE
Representing failure.
@ SUCCESS
Representing success.
A container class for holding a hash buffer.
Definition: HashBuffer.hpp:26
static void hash(const void *data, const NATIVE_INT_TYPE len, HashBuffer &buffer)
Definition: CRC32.cpp:29
SerializeStatus
forward declaration for string
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.
@ FW_SERIALIZE_FORMAT_ERROR
Data was the wrong format (e.g. wrong packet type)
@ FW_DESERIALIZE_SIZE_MISMATCH
Data was left in the buffer, but not enough to deserialize.
static constexpr FwSizeType PACKET_DESCRIPTOR_OFFSET
The offset for the packet descriptor field.
Definition: DpContainer.hpp:32
static constexpr FwSizeType SIZE
The header size.
Definition: DpContainer.hpp:48