iceoryx_doc  1.0.1
chunk_header.hpp
1 // Copyright (c) 2019 - 2020 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2020 - 2021 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/typed_unique_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>
31 class ChunkSender;
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 
105  uint32_t usedSizeOfChunk() const noexcept;
106 
109  uint32_t chunkSize() const noexcept;
110 
113  uint32_t userHeaderSize() const noexcept;
114 
117  uint32_t userPayloadSize() const noexcept;
118 
121  uint32_t userPayloadAlignment() const noexcept;
122 
125  UniquePortId originId() const noexcept;
126 
129  uint64_t sequenceNumber() const noexcept;
130 
131  private:
132  template <typename T>
133  friend class popo::ChunkSender;
134 
135  void setOriginId(UniquePortId originId) noexcept;
136 
137  void setSequenceNumber(uint64_t sequenceNumber) noexcept;
138 
139  uint64_t overflowSafeUsedSizeOfChunk() const noexcept;
140 
141  private:
142  // the order of these members must be changed carefully and if this happens, the m_chunkHeaderVersion
143  // needs to be adapted in order to be able to detect incompatibilities between publisher/subscriber
144  // or record&replay, m_chunkSize and m_chunkHeaderVersion should therefore neither changed the type,
145  // nor the position
146 
147  // size of the whole chunk, including the header
148  uint32_t m_chunkSize{0U};
149  uint8_t m_chunkHeaderVersion{CHUNK_HEADER_VERSION};
150  // reserved for future functionality and used to indicate the padding bytes; currently not used and set to `0`
151  uint8_t m_reserved{0};
152  // currently just a placeholder
153  uint16_t m_userHeaderId{NO_USER_HEADER};
154  UniquePortId m_originId{popo::InvalidId};
155  uint64_t m_sequenceNumber{0U};
156  uint32_t m_userHeaderSize{0U};
157  uint32_t m_userPayloadSize{0U};
158  uint32_t m_userPayloadAlignment{1U};
159  UserPayloadOffset_t m_userPayloadOffset{sizeof(ChunkHeader)};
160 };
161 
162 } // namespace mepoo
163 } // namespace iox
164 
165 #endif // IOX_POSH_MEPOO_CHUNK_HEADER_HPP
Definition: chunk_settings.hpp:30
Definition: service_description.hpp:29
Definition: chunk_header.hpp:42
UniquePortId originId() const noexcept
The unique identifier of the publisher the chunk was sent from.
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.
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
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