F´ Flight Software - C/C++ Documentation devel
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
10#include "Fw/Dp/DpContainer.hpp"
11#include "Fw/Types/Assert.hpp"
12
13namespace 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 bool omitLength = true;
74 const FwSizeType requestedSize = sizeof this->m_userData;
75 FwSizeType receivedSize = requestedSize;
76 status = serializeRepr.deserialize(this->m_userData, receivedSize, omitLength);
77 if (receivedSize != requestedSize) {
79 }
80 }
81 // Deserialize the data product state
82 if (status == Fw::FW_SERIALIZE_OK) {
83 status = serializeRepr.deserialize(this->m_dpState);
84 }
85 // Deserialize the data size
86 if (status == Fw::FW_SERIALIZE_OK) {
87 status = serializeRepr.deserialize(this->m_dataSize);
88 }
89 return status;
90}
91
93 FW_ASSERT(this->m_buffer.isValid());
94 Fw::SerializeBufferBase& serializeRepr = this->m_buffer.getSerializeRepr();
95 // Reset serialization
96 serializeRepr.resetSer();
97 // Serialize the packet type
98 Fw::SerializeStatus status =
100 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
101 // Serialize the container id
102 status = serializeRepr.serialize(this->m_id);
103 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
104 // Serialize the priority
105 status = serializeRepr.serialize(this->m_priority);
106 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
107 // Serialize the time tag
108 status = serializeRepr.serialize(this->m_timeTag);
109 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
110 // Serialize the processing types
111 status = serializeRepr.serialize(this->m_procTypes);
112 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
113 // Serialize the user data
114 const bool omitLength = true;
115 status = serializeRepr.serialize(this->m_userData, sizeof this->m_userData, omitLength);
116 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
117 // Serialize the data product state
118 status = serializeRepr.serialize(this->m_dpState);
119 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
120 // Serialize the data size
121 status = serializeRepr.serialize(this->m_dataSize);
122 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
123 // Update the header hash
124 this->updateHeaderHash();
125}
126
127void DpContainer::setBuffer(const Buffer& buffer) {
128 // Set the buffer
129 this->m_buffer = buffer;
130 // Check that the buffer is large enough to hold a data product packet
132 // Initialize the data buffer
133 U8* const buffAddr = buffer.getData();
134 const FwSizeType dataCapacity = buffer.getSize() - MIN_PACKET_SIZE;
135 // Check that data buffer is in bounds for packet buffer
136 FW_ASSERT(DATA_OFFSET + dataCapacity <= buffer.getSize());
137 U8* const dataAddr = &buffAddr[DATA_OFFSET];
138 this->m_dataBuffer.setExtBuffer(dataAddr, dataCapacity);
139}
140
142 Utils::HashBuffer hashBuffer;
143 U8* const buffAddr = this->m_buffer.getData();
144 Utils::Hash::hash(buffAddr, Header::SIZE, hashBuffer);
146 const Fw::SerializeStatus status = hashBuffer.copyRaw(serialBuffer, HASH_DIGEST_LENGTH);
147 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
148}
149
151 Utils::HashBuffer hashBuffer;
152 U8* const buffAddrBase = this->m_buffer.getData();
153 const U8* const dataAddr = &buffAddrBase[DATA_OFFSET];
154 const FwSizeType dataSize = this->getDataSize();
155 const FwSizeType bufferSize = this->m_buffer.getSize();
156 FW_ASSERT(DATA_OFFSET + dataSize <= bufferSize, DATA_OFFSET + dataSize, bufferSize);
157 Utils::Hash::hash(dataAddr, dataSize, hashBuffer);
158 const FwSizeType dataHashOffset = this->getDataHashOffset();
159 U8* const dataHashAddr = &buffAddrBase[dataHashOffset];
160 FW_ASSERT(dataHashOffset + HASH_DIGEST_LENGTH <= bufferSize, dataHashOffset + HASH_DIGEST_LENGTH, bufferSize);
161 ExternalSerializeBuffer serialBuffer(dataHashAddr, HASH_DIGEST_LENGTH);
162 const Fw::SerializeStatus status = hashBuffer.copyRaw(serialBuffer, HASH_DIGEST_LENGTH);
163 FW_ASSERT(status == Fw::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(status));
164}
165
166// ----------------------------------------------------------------------
167// Private member functions
168// ----------------------------------------------------------------------
169
170void DpContainer::initUserDataField() {
171 (void)::memset(this->m_userData, 0, sizeof this->m_userData);
172}
173
174} // namespace Fw
#define FW_ASSERT(...)
Definition Assert.hpp:14
uint8_t U8
8-bit unsigned integer
Definition BasicTypes.h:26
#define HASH_DIGEST_LENGTH
Definition CRC32.hpp:18
PlatformAssertArgType FwAssertArgType
Definition FpConfig.h:21
U32 FwDpIdType
Definition FpConfig.h:71
U32 FwPacketDescriptorType
Definition FpConfig.h:53
PlatformSizeType FwSizeType
Definition FpConfig.h:18
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.
DpContainer()
Constructor for container with default initialization.
FwDpPriorityType m_priority
The priority.
Fw::ExternalSerializeBuffer m_dataBuffer
The data buffer.
Header::UserData m_userData
The user data.
static constexpr FwSizeType MIN_PACKET_SIZE
void updateHeaderHash()
Update the header hash.
Fw::SerializeStatus deserializeHeader()
Time m_timeTag
The time tag.
Buffer m_buffer
The packet buffer.
FwSizeType m_dataSize
The data size.
FwSizeType getDataSize() const
void setBuffer(const Buffer &buffer)
Set the packet buffer.
FwSizeType getDataHashOffset() const
Get the data hash offset.
static constexpr FwSizeType HEADER_HASH_OFFSET
The header hash offset.
void updateDataHash()
Update the data hash.
DpState m_dpState
The data product state.
void setExtBuffer(U8 *buffPtr, NATIVE_UINT_TYPE size)
Set the external buffer.
SerializeStatus moveDeserToOffset(FwSizeType offset)
Moves deserialization to the specified offset.
SerializeStatus setBuffLen(NATIVE_UINT_TYPE length)
sets buffer length manually after filling with data
void resetSer()
reset to beginning of buffer to reuse for serialization
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
SerializeStatus copyRaw(SerializeBufferBase &dest, NATIVE_UINT_TYPE size)
directly copies buffer without looking for a size in the stream.
A container class for holding a hash buffer.
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.
static constexpr FwSizeType SIZE
The header size.