iceoryx_posh  2.0.2
chunk_header.hpp
1 // Copyright (c) 2019 - 2020 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2020 - 2022 by Apex.AI Inc. All rights reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // SPDX-License-Identifier: Apache-2.0
17 #ifndef IOX_POSH_MEPOO_CHUNK_HEADER_HPP
18 #define IOX_POSH_MEPOO_CHUNK_HEADER_HPP
19 
20 #include "iceoryx_posh/iceoryx_posh_types.hpp"
21 #include "iceoryx_posh/internal/popo/building_blocks/unique_port_id.hpp"
22 #include "iceoryx_posh/mepoo/chunk_settings.hpp"
23 
24 #include <cstdint>
25 
26 namespace iox
27 {
28 namespace popo
29 {
30 template <typename T>
32 }
33 
34 namespace mepoo
35 {
38 {
39 };
40 
42 {
43  using UserPayloadOffset_t = uint32_t;
44 
48  ChunkHeader(const uint32_t chunkSize, const ChunkSettings& chunkSettings) noexcept;
49 
50  // copy/move ctors/assignment operators are deleted since the calculations for the user-header and user-payload
51  // alignment are dependent on the address of the this pointer
52  ChunkHeader(const ChunkHeader&) = delete;
53  ChunkHeader(ChunkHeader&&) = delete;
54 
55  ChunkHeader& operator=(const ChunkHeader&) = delete;
56  ChunkHeader& operator=(ChunkHeader&&) = delete;
57 
62  static constexpr uint8_t CHUNK_HEADER_VERSION{1U};
63 
65  static constexpr uint16_t NO_USER_HEADER{0x0000};
67  static constexpr uint16_t UNKNOWN_USER_HEADER{0xFFFF};
68 
71  uint8_t chunkHeaderVersion() const noexcept;
72 
75  uint16_t userHeaderId() const noexcept;
76 
79  void* userHeader() noexcept;
80 
83  const void* userHeader() const noexcept;
84 
87  void* userPayload() noexcept;
88 
91  const void* userPayload() const noexcept;
92 
96  static ChunkHeader* fromUserPayload(void* const userPayload) noexcept;
97 
101  static const ChunkHeader* fromUserPayload(const void* const userPayload) noexcept;
102 
106  static ChunkHeader* fromUserHeader(void* const userHeader) noexcept;
107 
111  static const ChunkHeader* fromUserHeader(const void* const userHeader) noexcept;
112 
115  uint32_t usedSizeOfChunk() const noexcept;
116 
119  uint32_t chunkSize() const noexcept;
120 
123  uint32_t userHeaderSize() const noexcept;
124 
127  uint32_t userPayloadSize() const noexcept;
128 
131  uint32_t userPayloadAlignment() const noexcept;
132 
135  popo::UniquePortId originId() const noexcept;
136 
139  uint64_t sequenceNumber() const noexcept;
140 
141  private:
142  template <typename T>
143  friend class popo::ChunkSender;
144 
145  void setOriginId(const popo::UniquePortId originId) noexcept;
146 
147  void setSequenceNumber(const uint64_t sequenceNumber) noexcept;
148 
149  uint64_t overflowSafeUsedSizeOfChunk() const noexcept;
150 
151  private:
152  // the order of these members must be changed carefully and if this happens, the m_chunkHeaderVersion
153  // needs to be adapted in order to be able to detect incompatibilities between publisher/subscriber
154  // or record&replay, m_chunkSize and m_chunkHeaderVersion should therefore neither changed the type,
155  // nor the position
156 
157  // size of the whole chunk, including the header
158  uint32_t m_chunkSize{0U};
159  uint8_t m_chunkHeaderVersion{CHUNK_HEADER_VERSION};
160  // reserved for future functionality and used to indicate the padding bytes; currently not used and set to `0`
161  uint8_t m_reserved{0};
162  // currently just a placeholder
163  uint16_t m_userHeaderId{NO_USER_HEADER};
164  popo::UniquePortId m_originId{popo::InvalidPortId};
165  uint64_t m_sequenceNumber{0U};
166  uint32_t m_userHeaderSize{0U};
167  uint32_t m_userPayloadSize{0U};
168  uint32_t m_userPayloadAlignment{1U};
169  UserPayloadOffset_t m_userPayloadOffset{sizeof(ChunkHeader)};
170 };
171 
172 } // namespace mepoo
173 } // namespace iox
174 
175 #endif // IOX_POSH_MEPOO_CHUNK_HEADER_HPP
Definition: chunk_settings.hpp:30
Definition: chunk_header.hpp:31
Definition: chunk_header.hpp:42
uint32_t usedSizeOfChunk() const noexcept
Calculates the used size of the chunk with the ChunkHeader, user-heander and user-payload.
uint16_t userHeaderId() const noexcept
The id of the user-header used by the chunk; if no user-header is used, this is set to NO_USER_HEADER...
uint32_t chunkSize() const noexcept
The size of the whole chunk, including the header.
static ChunkHeader * fromUserHeader(void *const userHeader) noexcept
Get a pointer to the ChunkHeader associated to the user-header of the chunk.
uint32_t userHeaderSize() const noexcept
The size of the chunk occupied by the user-header.
ChunkHeader(const uint32_t chunkSize, const ChunkSettings &chunkSettings) noexcept
constructs and initializes a ChunkHeader
static constexpr uint8_t CHUNK_HEADER_VERSION
From the 1.0 release onward, this must be incremented for each incompatible change,...
Definition: chunk_header.hpp:62
popo::UniquePortId originId() const noexcept
The unique identifier of the publisher the chunk was sent from.
void * userPayload() noexcept
Get a pointer to the user-payload carried by the chunk.
uint64_t sequenceNumber() const noexcept
A serial number for the sent chunks.
uint32_t userPayloadAlignment() const noexcept
The alignment of the chunk occupied by the user-payload.
void * userHeader() noexcept
Get the pointer to the user-header.
uint32_t userPayloadSize() const noexcept
The size of the chunk occupied by the user-payload.
static ChunkHeader * fromUserPayload(void *const userPayload) noexcept
Get a pointer to the ChunkHeader associated to the user-payload of the chunk.
uint8_t chunkHeaderVersion() const noexcept
The ChunkHeader version is used to detect incompatibilities for record&replay functionality.
static constexpr uint16_t UNKNOWN_USER_HEADER
User-Header id for an unknown user-header.
Definition: chunk_header.hpp:67
static constexpr uint16_t NO_USER_HEADER
User-Header id for no user-header.
Definition: chunk_header.hpp:65
Helper struct to use as default template parameter when no user-header is used.
Definition: chunk_header.hpp:38