PROFINET IO Controller Stack 1.0.0
Modern C++ implementation of a PROFINET IO Controller stack
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
1
7
8#pragma once
9
10#include <array>
11#include <chrono>
12#include <cstddef>
13#include <cstdint>
14#include <filesystem>
15#include <span>
16#include <string>
17#include <vector>
18
19namespace profinet
20{
21
22// =============================================================================
23// Constants
24// =============================================================================
25
26// NOLINTBEGIN(readability-identifier-naming)
28inline constexpr std::size_t MAX_ETHERNET_FRAME = 1522;
30inline constexpr std::uint16_t PROFINET_ETHERTYPE = 0x8892;
32inline constexpr std::uint16_t IP_ETHERTYPE = 0x8000;
34inline constexpr std::uint16_t VLAN_ETHERTYPE = 0x8100;
35
37inline constexpr int macAddressLength = 6;
38
40inline constexpr int uuidLenght = 16;
41
43inline constexpr std::size_t RECEIVE_BUFFER_LENGTH = 4096;
44
45// NOLINTEND(readability-identifier-naming)
46
48using MacAddress = std::array<std::uint8_t, macAddressLength>;
49
50// =============================================================================
51// Byte Conversion Utilities
52// =============================================================================
53
57std::string Hex4(std::uint16_t value);
58
62std::string Hex2(std::uint8_t value);
63
67std::vector<std::uint8_t> ToVec(const std::string& input);
68
74std::string HexFallback(const char* widthPrefix, std::uint32_t value, int digitWidth);
75// =============================================================================
76// Address Conversion Utilities
77// =============================================================================
78
90std::size_t SkipVlanTags(std::span<const std::uint8_t> frame) noexcept;
91
96std::string ToHex(const std::uint8_t* data, std::size_t len);
97
101std::string ToHex(const std::vector<std::uint8_t>& data);
102
107MacAddress String2Mac(const std::string& macStr);
108
112std::string Mac2String(const MacAddress& mac);
113
118std::string Mac2String(const std::uint8_t* macBytes, std::size_t len);
119
124std::string String2Ip(std::span<const std::uint8_t> ipBytes);
125
131std::string String2Ip(const std::uint8_t* ipBytes, std::size_t len);
132
137std::array<std::uint8_t, 4> Ip2String(const std::string& ipStr);
138
143std::string DecodeBytes(const std::uint8_t* data, std::size_t len);
144
148std::string DecodeBytes(const std::vector<std::uint8_t>& data);
149
150// =============================================================================
151// Platform Network Utilities
152// =============================================================================
155{
156 public:
157 virtual ~IMacAddressProvider() = default;
158
164 virtual MacAddress GetMac(const std::string& interface) = 0;
165};
166
169{
170 public:
176 MacAddress GetMac(const std::string& interface) override;
177};
178
190MacAddress GetInterfaceMac(const std::string& interface);
191
199std::filesystem::path GetExecutableDirectory();
200
217template <typename T>
218[[nodiscard]] constexpr T ByteSwap(T value)
219{
220 static_assert(std::is_integral_v<T>, "ByteSwap requires an integral type");
221 static_assert(!std::is_same_v<T, bool>, "ByteSwap does not support bool");
222
223 using UnsignedT = std::make_unsigned_t<T>;
224
225 auto input = static_cast<UnsignedT>(value);
226 UnsignedT result = 0;
227
228 for (std::size_t i = 0U; i < sizeof(T); ++i)
229 {
230 result = static_cast<UnsignedT>((result << 8U) | (input & 0xFFU));
231 input >>= 8U;
232 }
233
234 return static_cast<T>(result);
235}
236
243template <typename T>
244[[nodiscard]] constexpr std::uint8_t ByteAt(T value, std::size_t index)
245{
246 static_assert(std::is_integral_v<T>, "ByteAt requires an integral type");
247 static_assert(!std::is_same_v<T, bool>, "ByteAt does not support bool");
248
249 using UnsignedT = std::make_unsigned_t<T>;
250 const auto unsignedValue = static_cast<UnsignedT>(value);
251
252 return static_cast<std::uint8_t>(
253 (unsignedValue >> (index * 8U)) & 0xFFU);
254}
255
256// =============================================================================
257// Timeout helper
258// =============================================================================
259
267{
268 public:
271 explicit MaxTimeout(std::chrono::duration<double> seconds)
272 : dieAfter(std::chrono::steady_clock::now() +
273 std::chrono::duration_cast<std::chrono::steady_clock::duration>(seconds))
274 {
275 }
276
279 [[nodiscard]] bool TimedOut() const
280 {
281 return std::chrono::steady_clock::now() > dieAfter;
282 }
283
286 [[nodiscard]] std::chrono::duration<double> Remaining() const
287 {
288 auto rem = dieAfter - std::chrono::steady_clock::now();
289 auto secs = std::chrono::duration<double>(rem);
290 return secs.count() > 0 ? secs : std::chrono::duration<double>(0);
291 }
292
293 private:
295 std::chrono::steady_clock::time_point dieAfter;
296};
297
298} // namespace profinet
Provides the MAC address of a network interface.
Definition util.h:155
virtual MacAddress GetMac(const std::string &interface)=0
Get the MAC address of a network interface.
virtual ~IMacAddressProvider()=default
Production implementation of IMacAddressProvider.
Definition util.h:169
MacAddress GetMac(const std::string &interface) override
Get the MAC address of a network interface.
Definition util.cpp:266
Time-limited-operation helper.
Definition util.h:267
MaxTimeout(std::chrono::duration< double > seconds)
Start a countdown of the given duration.
Definition util.h:271
std::chrono::steady_clock::time_point dieAfter
Time point at which TimedOut() will start returning true.
Definition util.h:295
bool TimedOut() const
Whether the countdown has elapsed.
Definition util.h:279
std::chrono::duration< double > Remaining() const
Time left before the countdown elapses.
Definition util.h:286
std::string Hex2(std::uint8_t value)
Format an 8-bit unsigned integer as a 2-digit hexadecimal string.
Definition util.cpp:49
constexpr std::uint16_t VLAN_ETHERTYPE
EtherType value identifying an 802.1Q VLAN tag (0x8100).
Definition util.h:34
constexpr int uuidLenght
Constant lenght of a UUID.
Definition util.h:40
MacAddress GetInterfaceMac(const std::string &interface)
Get the MAC address assigned to a network interface.
std::vector< std::uint8_t > ToVec(const std::string &input)
Convert a string into a vector of raw bytes.
Definition util.cpp:54
std::string String2Ip(std::span< const std::uint8_t > ipBytes)
Format raw IPv4 address bytes as a dotted string.
Definition util.cpp:206
std::array< std::uint8_t, macAddressLength > MacAddress
A 6-byte Ethernet MAC address.
Definition util.h:48
std::array< std::uint8_t, 4 > Ip2String(const std::string &ipStr)
Parse a dotted-decimal IPv4 address string.
Definition util.cpp:222
std::filesystem::path GetExecutableDirectory()
Retrieves the absolute directory path of the currently running executable.
constexpr std::uint16_t IP_ETHERTYPE
EtherType value identifying IP frames (0x8000).
Definition util.h:32
constexpr T ByteSwap(T value)
Definition util.h:218
MacAddress String2Mac(const std::string &macStr)
Parse a colon-separated MAC address string.
Definition util.cpp:122
constexpr std::size_t MAX_ETHERNET_FRAME
Maximum PROFINET Ethernet frame size in bytes (including VLAN tag headroom).
Definition util.h:28
std::size_t SkipVlanTags(std::span< const std::uint8_t > frame) noexcept
Return the byte offset of the real EtherType in a raw Ethernet frame.
Definition util.cpp:70
constexpr int macAddressLength
Constant lenght of a mac address.
Definition util.h:37
std::string ToHex(const std::uint8_t *data, std::size_t len)
Hex-encode a byte buffer.
Definition util.cpp:104
constexpr std::uint16_t PROFINET_ETHERTYPE
EtherType value identifying PROFINET frames (0x8892).
Definition util.h:30
std::string Hex4(std::uint16_t value)
Format a 16-bit unsigned integer as a 4-digit hexadecimal string.
Definition util.cpp:44
std::string HexFallback(const char *widthPrefix, std::uint32_t value, int digitWidth)
Format a hexadecimal string with a custom prefix and dynamic width.
Definition util.cpp:59
constexpr std::size_t RECEIVE_BUFFER_LENGTH
Receive buffer length.
Definition util.h:43
std::string Mac2String(const MacAddress &mac)
Format a MAC address as a colon-separated string.
Definition util.cpp:188
std::string DecodeBytes(const std::uint8_t *data, std::size_t len)
Decode bytes to a UTF-8 string, stripping trailing NUL bytes.
Definition util.cpp:246
constexpr std::uint8_t ByteAt(T value, std::size_t index)
Return one byte from an integral value.
Definition util.h:244
Bytes frame