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  this->m_dataBuffer.setExtBuffer(dataAddr, static_cast<Fw::Serializable::SizeType>(dataCapacity));
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, static_cast<NATIVE_INT_TYPE>(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
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:51
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:26
#define HASH_DIGEST_LENGTH
Definition: CRC32.hpp:18
PlatformAssertArgType FwAssertArgType
Definition: FpConfig.h:34
U32 FwDpIdType
Definition: FpConfig.h:102
U32 FwPacketDescriptorType
Definition: FpConfig.h:74
PlatformSizeType FwSizeType
Definition: FpConfig.h:30
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)
Set the external buffer.
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