Reverse the byte order of an integral value.
Reverse the byte order of an integral value.
The operation is performed on the corresponding unsigned representation, which avoids signed right-shift concerns. The original type is returned.
#pragma once
#include <array>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <span>
#include <string>
#include <vector>
{
using MacAddress = std::array<std::uint8_t, macAddressLength>;
std::string
Hex4(std::uint16_t value);
std::string
Hex2(std::uint8_t value);
std::vector<std::uint8_t>
ToVec(
const std::string& input);
std::string
HexFallback(
const char* widthPrefix, std::uint32_t value,
int digitWidth);
std::string
ToHex(
const std::uint8_t* data, std::size_t len);
std::string
ToHex(
const std::vector<std::uint8_t>& data);
std::string
Mac2String(
const std::uint8_t* macBytes, std::size_t len);
std::string
String2Ip(std::span<const std::uint8_t> ipBytes);
std::string
String2Ip(
const std::uint8_t* ipBytes, std::size_t len);
std::array<std::uint8_t, 4>
Ip2String(
const std::string& ipStr);
std::string
DecodeBytes(
const std::uint8_t* data, std::size_t len);
std::string
DecodeBytes(
const std::vector<std::uint8_t>& data);
class IMacAddressProvider
{
public:
};
class MacAddressProvider final : public IMacAddressProvider
{
public:
};
template <typename T>
[[nodiscard]]
constexpr T
ByteSwap(T value)
{
static_assert(std::is_integral_v<T>, "ByteSwap requires an integral type");
static_assert(!std::is_same_v<T, bool>, "ByteSwap does not support bool");
using UnsignedT = std::make_unsigned_t<T>;
auto input = static_cast<UnsignedT>(value);
UnsignedT result = 0;
for (std::size_t i = 0U; i < sizeof(T); ++i)
{
result = static_cast<UnsignedT>((result << 8U) | (input & 0xFFU));
input >>= 8U;
}
return static_cast<T>(result);
}
template <typename T>
[[nodiscard]]
constexpr std::uint8_t
ByteAt(T value, std::size_t index)
{
static_assert(std::is_integral_v<T>, "ByteAt requires an integral type");
static_assert(!std::is_same_v<T, bool>, "ByteAt does not support bool");
using UnsignedT = std::make_unsigned_t<T>;
const auto unsignedValue = static_cast<UnsignedT>(value);
return static_cast<std::uint8_t>(
(unsignedValue >> (index * 8U)) & 0xFFU);
}
class MaxTimeout
{
public:
explicit MaxTimeout(std::chrono::duration<double> seconds)
:
dieAfter(std::chrono::steady_clock::now() +
std::chrono::duration_cast<std::chrono::steady_clock::duration>(seconds))
{
}
{
return std::chrono::steady_clock::now() >
dieAfter;
}
[[nodiscard]] std::chrono::duration<double>
Remaining()
const
{
auto rem =
dieAfter - std::chrono::steady_clock::now();
auto secs = std::chrono::duration<double>(rem);
return secs.count() > 0 ? secs : std::chrono::duration<double>(0);
}
private:
std::chrono::steady_clock::time_point
dieAfter;
};
}
virtual MacAddress GetMac(const std::string &interface)=0
Get the MAC address of a network interface.
virtual ~IMacAddressProvider()=default
MacAddress GetMac(const std::string &interface) override
Get the MAC address of a network interface.
std::chrono::steady_clock::time_point dieAfter
Time point at which TimedOut() will start returning true.
bool TimedOut() const
Whether the countdown has elapsed.
std::chrono::duration< double > Remaining() const
Time left before the countdown elapses.
std::string Hex2(std::uint8_t value)
Format an 8-bit unsigned integer as a 2-digit hexadecimal string.
constexpr std::uint16_t VLAN_ETHERTYPE
EtherType value identifying an 802.1Q VLAN tag (0x8100).
constexpr int uuidLenght
Constant lenght of a UUID.
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.
std::string String2Ip(std::span< const std::uint8_t > ipBytes)
Format raw IPv4 address bytes as a dotted string.
std::array< std::uint8_t, macAddressLength > MacAddress
A 6-byte Ethernet MAC address.
std::array< std::uint8_t, 4 > Ip2String(const std::string &ipStr)
Parse a dotted-decimal IPv4 address string.
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).
constexpr T ByteSwap(T value)
MacAddress String2Mac(const std::string &macStr)
Parse a colon-separated MAC address string.
constexpr std::size_t MAX_ETHERNET_FRAME
Maximum PROFINET Ethernet frame size in bytes (including VLAN tag headroom).
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.
constexpr int macAddressLength
Constant lenght of a mac address.
std::string ToHex(const std::uint8_t *data, std::size_t len)
Hex-encode a byte buffer.
constexpr std::uint16_t PROFINET_ETHERTYPE
EtherType value identifying PROFINET frames (0x8892).
std::string Hex4(std::uint16_t value)
Format a 16-bit unsigned integer as a 4-digit hexadecimal string.
std::string HexFallback(const char *widthPrefix, std::uint32_t value, int digitWidth)
Format a hexadecimal string with a custom prefix and dynamic width.
constexpr std::size_t RECEIVE_BUFFER_LENGTH
Receive buffer length.
std::string Mac2String(const MacAddress &mac)
Format a MAC address as a colon-separated string.
std::string DecodeBytes(const std::uint8_t *data, std::size_t len)
Decode bytes to a UTF-8 string, stripping trailing NUL bytes.
constexpr std::uint8_t ByteAt(T value, std::size_t index)
Return one byte from an integral value.