| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \title DpContainer.hpp | ||
| 3 | // \author bocchino | ||
| 4 | // \brief hpp file for DpContainer | ||
| 5 | // ====================================================================== | ||
| 6 | |||
| 7 | #ifndef Fw_DpContainer_HPP | ||
| 8 | #define Fw_DpContainer_HPP | ||
| 9 | |||
| 10 | #include "Fw/Buffer/Buffer.hpp" | ||
| 11 | #include "Fw/Dp/DpStateEnumAc.hpp" | ||
| 12 | #include "Fw/Time/Time.hpp" | ||
| 13 | #include "Fw/Types/SuccessEnumAc.hpp" | ||
| 14 | #include "Utils/Hash/Hash.hpp" | ||
| 15 | #include "config/FppConstantsAc.hpp" | ||
| 16 | #include "config/ProcTypeEnumAc.hpp" | ||
| 17 | |||
| 18 | // Forward declare for UTs | ||
| 19 | namespace Fw { | ||
| 20 | class DpContainerTester; | ||
| 21 | } | ||
| 22 | |||
| 23 | namespace Fw { | ||
| 24 | |||
| 25 | //! A data product Container | ||
| 26 | class DpContainer { | ||
| 27 | friend class Fw::DpContainerTester; | ||
| 28 | |||
| 29 | public: | ||
| 30 | // ---------------------------------------------------------------------- | ||
| 31 | // Constants and Types | ||
| 32 | // ---------------------------------------------------------------------- | ||
| 33 | |||
| 34 | //! A DpContainer packet header | ||
| 35 | struct Header { | ||
| 36 | //! The type of user data | ||
| 37 | using UserData = U8[DpCfg::CONTAINER_USER_DATA_SIZE]; | ||
| 38 | //! The offset for the packet descriptor field | ||
| 39 | static constexpr FwSizeType PACKET_DESCRIPTOR_OFFSET = 0; | ||
| 40 | //! The offset for the id field | ||
| 41 | static constexpr FwSizeType ID_OFFSET = PACKET_DESCRIPTOR_OFFSET + sizeof(FwPacketDescriptorType); | ||
| 42 | //! The offset for the priority field | ||
| 43 | static constexpr FwSizeType PRIORITY_OFFSET = ID_OFFSET + sizeof(FwDpIdType); | ||
| 44 | //! The offset for the time tag field | ||
| 45 | static constexpr FwSizeType TIME_TAG_OFFSET = PRIORITY_OFFSET + sizeof(FwDpPriorityType); | ||
| 46 | //! The offset for the processing types field | ||
| 47 | static constexpr FwSizeType PROC_TYPES_OFFSET = TIME_TAG_OFFSET + Time::SERIALIZED_SIZE; | ||
| 48 | //! The offset for the user data field | ||
| 49 | static constexpr FwSizeType USER_DATA_OFFSET = PROC_TYPES_OFFSET + sizeof(DpCfg::ProcType::SerialType); | ||
| 50 | //! The offset of the data product state field | ||
| 51 | static constexpr FwSizeType DP_STATE_OFFSET = USER_DATA_OFFSET + DpCfg::CONTAINER_USER_DATA_SIZE; | ||
| 52 | //! The offset for the data size field | ||
| 53 | static constexpr FwSizeType DATA_SIZE_OFFSET = DP_STATE_OFFSET + DpState::SERIALIZED_SIZE; | ||
| 54 | //! The header size | ||
| 55 | static constexpr FwSizeType SIZE = DATA_SIZE_OFFSET + sizeof(FwSizeStoreType); | ||
| 56 | }; | ||
| 57 | |||
| 58 | //! The header hash offset | ||
| 59 | static constexpr FwSizeType HEADER_HASH_OFFSET = Header::SIZE; | ||
| 60 | //! The data offset | ||
| 61 | static constexpr FwSizeType DATA_OFFSET = HEADER_HASH_OFFSET + HASH_DIGEST_LENGTH; | ||
| 62 | //! The minimum packet size | ||
| 63 | //! Reserve space for the header, the header hash, and the data hash | ||
| 64 | //! This is also the number of non-data bytes in the packet | ||
| 65 | static constexpr FwSizeType MIN_PACKET_SIZE = Header::SIZE + 2 * HASH_DIGEST_LENGTH; | ||
| 66 | |||
| 67 | public: | ||
| 68 | // ---------------------------------------------------------------------- | ||
| 69 | // Constructors and destructors | ||
| 70 | // ---------------------------------------------------------------------- | ||
| 71 | |||
| 72 | //! Constructor for initialized container | ||
| 73 | DpContainer(FwDpIdType id, //!< The container id | ||
| 74 | const Fw::Buffer& buffer //!< The buffer | ||
| 75 | ); | ||
| 76 | |||
| 77 | //! Constructor for container with default initialization | ||
| 78 | DpContainer(); | ||
| 79 | |||
| 80 | //! Destructor | ||
| 81 | 134264 | virtual ~DpContainer() {} | |
| 82 | |||
| 83 | protected: | ||
| 84 | // ---------------------------------------------------------------------- | ||
| 85 | // Protected operators | ||
| 86 | // ---------------------------------------------------------------------- | ||
| 87 | |||
| 88 | //! Copy constructor | ||
| 89 | DpContainer(const DpContainer& src) = default; | ||
| 90 | |||
| 91 | //! Copy assignment operator | ||
| 92 |
2/2✓ Branch 8 taken 672 times.
✓ Branch 9 taken 21 times.
|
693 | DpContainer& operator=(const DpContainer& src) = default; |
| 93 | |||
| 94 | public: | ||
| 95 | // ---------------------------------------------------------------------- | ||
| 96 | // Public member functions | ||
| 97 | // ---------------------------------------------------------------------- | ||
| 98 | |||
| 99 | //! Get the container id | ||
| 100 | //! \return The id | ||
| 101 | 24855 | FwDpIdType getId() const { return this->m_id; } | |
| 102 | |||
| 103 | //! Get the data size | ||
| 104 | //! \return The data size | ||
| 105 | 64161 | FwSizeType getDataSize() const { return this->m_dataSize; } | |
| 106 | |||
| 107 | //! Get the packet buffer | ||
| 108 | //! \return The buffer | ||
| 109 | 2548 | Fw::Buffer getBuffer() const { return this->m_buffer; } | |
| 110 | |||
| 111 | //! Get the packet size corresponding to the data size | ||
| 112 | 7221 | FwSizeType getPacketSize() const { return getPacketSizeForDataSize(this->m_dataSize); } | |
| 113 | |||
| 114 | //! Get the priority | ||
| 115 | //! \return The priority | ||
| 116 | 7229 | FwDpPriorityType getPriority() const { return this->m_priority; } | |
| 117 | |||
| 118 | //! Get the time tag | ||
| 119 | //! \return The time tag | ||
| 120 | 27123 | Fw::Time getTimeTag() const { return this->m_timeTag; } | |
| 121 | |||
| 122 | //! Get the product state | ||
| 123 | 12298 | Fw::DpState getState() const { return this->m_dpState; } | |
| 124 | |||
| 125 | //! Get the processing types | ||
| 126 | //! \return The processing types | ||
| 127 | 3782 | DpCfg::ProcType::SerialType getProcTypes() const { return this->m_procTypes; } | |
| 128 | |||
| 129 | //! Get the data product state | ||
| 130 | DpState getDpState() const { return this->m_dpState; } | ||
| 131 | |||
| 132 | //! Deserialize the header from the packet buffer | ||
| 133 | //! Buffer must be valid, and its size must be at least MIN_PACKET_SIZE | ||
| 134 | //! Before calling this function, you should call checkHeaderHash() to | ||
| 135 | //! check the header hash | ||
| 136 | //! \return The serialize status | ||
| 137 | Fw::SerializeStatus deserializeHeader(); | ||
| 138 | |||
| 139 | //! Serialize the header into the packet buffer and update the header hash | ||
| 140 | //! Buffer must be valid, and its size must be at least MIN_PACKET_SIZE | ||
| 141 | void serializeHeader(); | ||
| 142 | |||
| 143 | //! Set the id | ||
| 144 | 1 | void setId(FwDpIdType id //!< The id | |
| 145 | ) { | ||
| 146 | 1 | this->m_id = id; | |
| 147 | 1 | } | |
| 148 | |||
| 149 | //! Set the priority | ||
| 150 | 10388 | void setPriority(FwDpPriorityType priority //!< The priority | |
| 151 | ) { | ||
| 152 | 10388 | this->m_priority = priority; | |
| 153 | 10388 | } | |
| 154 | |||
| 155 | //! Set the time tag | ||
| 156 | 10366 | void setTimeTag(Fw::Time timeTag //!< The time tag | |
| 157 | ) { | ||
| 158 | 10366 | this->m_timeTag = timeTag; | |
| 159 | 10366 | } | |
| 160 | |||
| 161 | //! Set the processing types bit mask | ||
| 162 | 4194 | void setProcTypes(DpCfg::ProcType::SerialType procTypes //!< The processing types | |
| 163 | ) { | ||
| 164 | 4194 | this->m_procTypes = procTypes; | |
| 165 | 4194 | } | |
| 166 | |||
| 167 | //! Set the data product state | ||
| 168 | 6154 | void setDpState(DpState dpState //!< The data product state | |
| 169 | ) { | ||
| 170 | 6154 | this->m_dpState = dpState; | |
| 171 | 6154 | } | |
| 172 | |||
| 173 | //! Set the data size | ||
| 174 | 31625 | void setDataSize(FwSizeType dataSize //!< The data size | |
| 175 | ) { | ||
| 176 | 31625 | this->m_dataSize = dataSize; | |
| 177 | 31625 | } | |
| 178 | |||
| 179 | //! Set the packet buffer | ||
| 180 | void setBuffer(const Buffer& buffer //!< The packet buffer | ||
| 181 | ); | ||
| 182 | |||
| 183 | //! Shrink the Fw::Buffer size to match the | ||
| 184 | //! DataSize in the container header | ||
| 185 | void shrinkBufferSize(); | ||
| 186 | |||
| 187 | //! Invalidate the packet buffer | ||
| 188 | 21 | void invalidateBuffer() { | |
| 189 |
2/2✓ Branch 2 taken 21 times.
✓ Branch 11 taken 21 times.
|
21 | this->m_buffer = Fw::Buffer(); |
| 190 | 21 | this->m_dataBuffer.clear(); | |
| 191 | 21 | this->m_dataSize = 0; | |
| 192 | 21 | } | |
| 193 | |||
| 194 | //! Get the stored header hash | ||
| 195 | //! \return The hash | ||
| 196 | Utils::HashBuffer getHeaderHash() const; | ||
| 197 | |||
| 198 | //! Compute the header hash from the header data | ||
| 199 | //! \return The hash | ||
| 200 | Utils::HashBuffer computeHeaderHash() const; | ||
| 201 | |||
| 202 | //! Set the header hash | ||
| 203 | void setHeaderHash(const Utils::HashBuffer& hash //!< The hash | ||
| 204 | ); | ||
| 205 | |||
| 206 | //! Compute and set the header hash | ||
| 207 | void updateHeaderHash(); | ||
| 208 | |||
| 209 | //! Check the header hash | ||
| 210 | Success::T checkHeaderHash(Utils::HashBuffer& storedHash, //!< The stored hash (output) | ||
| 211 | Utils::HashBuffer& computedHash //!< The computed hash (output) | ||
| 212 | ) const; | ||
| 213 | |||
| 214 | //! Get the data hash offset | ||
| 215 | 7946 | FwSizeType getDataHashOffset() const { | |
| 216 | // Data hash goes after the header, the header hash, and the data | ||
| 217 | 7946 | return Header::SIZE + HASH_DIGEST_LENGTH + this->m_dataSize; | |
| 218 | } | ||
| 219 | |||
| 220 | //! Get the stored data hash | ||
| 221 | //! \return The hash | ||
| 222 | Utils::HashBuffer getDataHash() const; | ||
| 223 | |||
| 224 | //! Compute the data hash from the data | ||
| 225 | //! \return The hash | ||
| 226 | Utils::HashBuffer computeDataHash() const; | ||
| 227 | |||
| 228 | //! Set the data hash | ||
| 229 | void setDataHash(Utils::HashBuffer hash //!< The hash | ||
| 230 | ); | ||
| 231 | |||
| 232 | //! Update the data hash | ||
| 233 | void updateDataHash(); | ||
| 234 | |||
| 235 | //! Check the data hash | ||
| 236 | Success::T checkDataHash(Utils::HashBuffer& storedHash, //!< The stored hash (output) | ||
| 237 | Utils::HashBuffer& computedHash //!< The computed hash (output) | ||
| 238 | ) const; | ||
| 239 | |||
| 240 | public: | ||
| 241 | // ---------------------------------------------------------------------- | ||
| 242 | // Public static functions | ||
| 243 | // ---------------------------------------------------------------------- | ||
| 244 | |||
| 245 | //! Get the packet size for a given data size | ||
| 246 | 17586 | static constexpr FwSizeType getPacketSizeForDataSize(FwSizeType dataSize //!< The data size | |
| 247 | ) { | ||
| 248 | 17586 | return Header::SIZE + dataSize + 2 * HASH_DIGEST_LENGTH; | |
| 249 | } | ||
| 250 | |||
| 251 | private: | ||
| 252 | // ---------------------------------------------------------------------- | ||
| 253 | // Private member functions | ||
| 254 | // ---------------------------------------------------------------------- | ||
| 255 | |||
| 256 | //! Initialize the user data field | ||
| 257 | void initUserDataField(); | ||
| 258 | |||
| 259 | public: | ||
| 260 | // ---------------------------------------------------------------------- | ||
| 261 | // Public member variables | ||
| 262 | // ---------------------------------------------------------------------- | ||
| 263 | |||
| 264 | //! The user data | ||
| 265 | Header::UserData m_userData; | ||
| 266 | |||
| 267 | protected: | ||
| 268 | // ---------------------------------------------------------------------- | ||
| 269 | // Protected member variables | ||
| 270 | // ---------------------------------------------------------------------- | ||
| 271 | |||
| 272 | //! The container id | ||
| 273 | //! This is a system-global id (component-local id + component base id) | ||
| 274 | FwDpIdType m_id; | ||
| 275 | |||
| 276 | //! The priority | ||
| 277 | FwDpPriorityType m_priority; | ||
| 278 | |||
| 279 | //! The time tag | ||
| 280 | Time m_timeTag; | ||
| 281 | |||
| 282 | //! The processing types | ||
| 283 | DpCfg::ProcType::SerialType m_procTypes; | ||
| 284 | |||
| 285 | //! The data product state | ||
| 286 | DpState m_dpState; | ||
| 287 | |||
| 288 | //! The data size | ||
| 289 | FwSizeType m_dataSize; | ||
| 290 | |||
| 291 | //! The packet buffer | ||
| 292 | Buffer m_buffer; | ||
| 293 | |||
| 294 | //! The data buffer | ||
| 295 | //! We use member copy semantics because m_dataBuffer points into m_buffer, | ||
| 296 | //! which is owned by this object | ||
| 297 | Fw::ExternalSerializeBufferWithMemberCopy m_dataBuffer; | ||
| 298 | }; | ||
| 299 | |||
| 300 | } // end namespace Fw | ||
| 301 | |||
| 302 | #endif | ||
| 303 |