Direct-BT
2.3.1
Direct-BT - Direct Bluetooth Programming.
|
Go to the documentation of this file.
26 #ifndef JAU_BYTE_UTIL_HPP_
27 #define JAU_BYTE_UTIL_HPP_
34 #include <type_traits>
44 #if defined __has_builtin
45 #if __has_builtin(__builtin_bswap16)
46 #define __has_builtin_bswap16 1
48 #elif defined(__GNUC__) && __GNUC_PREREQ (4, 8)
49 #define __has_builtin_bswap16 1
51 #if defined __has_builtin
52 #if __has_builtin(__builtin_bswap32)
53 #define __has_builtin_bswap32 1
55 #elif defined(__GNUC__) && __GNUC_PREREQ (4, 8)
56 #define __has_builtin_bswap32 1
58 #if defined __has_builtin
59 #if __has_builtin(__builtin_bswap64)
60 #define __has_builtin_bswap64 1
62 #elif defined(__GNUC__) && __GNUC_PREREQ (4, 8)
63 #define __has_builtin_bswap64 1
66 constexpr uint16_t
bswap(uint16_t
const source) noexcept {
67 #if defined __has_builtin_bswap16
68 return __builtin_bswap16(source);
70 return (uint16_t) ( ( ( (source) >> 8 ) & 0xff ) |
71 ( ( (source) & 0xff) << 8 ) );
75 constexpr uint32_t
bswap(uint32_t
const source) noexcept {
76 #if defined __has_builtin_bswap32
77 return __builtin_bswap32(source);
79 return ( ( source & 0xff000000U ) >> 24 ) |
80 ( ( source & 0x00ff0000U ) >> 8 ) |
81 ( ( source & 0x0000ff00U ) << 8 ) |
82 ( ( source & 0x000000ffU ) << 24 );
86 constexpr uint64_t
bswap(uint64_t
const & source) noexcept {
87 #if defined __has_builtin_bswap64
88 return __builtin_bswap64(source);
90 return ( ( source & 0xff00000000000000ULL ) >> 56 ) |
91 ( ( source & 0x00ff000000000000ULL ) >> 40 ) |
92 ( ( source & 0x0000ff0000000000ULL ) >> 24 ) |
93 ( ( source & 0x000000ff00000000ULL ) >> 8 ) |
94 ( ( source & 0x00000000ff000000ULL ) << 8 ) |
95 ( ( source & 0x0000000000ff0000ULL ) << 24 ) |
96 ( ( source & 0x000000000000ff00ULL ) << 40 ) |
97 ( ( source & 0x00000000000000ffULL ) << 56 );
103 uint8_t
const *
const s = source.
data;
104 uint8_t *
const d = dest.
data;
113 uint8_t
const *
const s = source.
data;
114 uint8_t *
const d = dest.
data;
123 uint8_t
const *
const s = source.
data;
124 uint8_t *
const d = dest.
data;
147 constexpr uint8_t b[4] { 0x44, 0x43, 0x42, 0x41 };
148 return jau::bit_cast<uint32_t, uint8_t[4]>( b );
260 template <
typename Dummy_type>
struct has_endian_little : std::integral_constant<bool, endian::little == endian::native> {};
280 template <
typename Dummy_type>
struct has_endian_big : std::integral_constant<bool, endian::big == endian::native> {};
306 constexpr uint16_t
be_to_cpu(uint16_t
const n) noexcept {
316 constexpr uint16_t
cpu_to_be(uint16_t
const h) noexcept {
325 constexpr uint16_t
le_to_cpu(uint16_t
const l) noexcept {
334 constexpr uint16_t
cpu_to_le(uint16_t
const h) noexcept {
344 constexpr uint32_t
be_to_cpu(uint32_t
const n) noexcept {
353 constexpr uint32_t
cpu_to_be(uint32_t
const h) noexcept {
362 constexpr uint32_t
le_to_cpu(uint32_t
const l) noexcept {
371 constexpr uint32_t
cpu_to_le(uint32_t
const h) noexcept {
381 constexpr uint64_t
be_to_cpu(uint64_t
const & n) noexcept {
390 constexpr uint64_t
cpu_to_be(uint64_t
const & h) noexcept {
399 constexpr uint64_t
le_to_cpu(uint64_t
const & l) noexcept {
408 constexpr uint64_t
cpu_to_le(uint64_t
const & h) noexcept {
536 constexpr
void put_uint8(uint8_t * buffer,
nsize_t const byte_offset,
const uint8_t v) noexcept
538 *pointer_cast<uint8_t *>( buffer + byte_offset ) = v;
542 return *pointer_cast<uint8_t const *>( buffer + byte_offset );
546 return *pointer_cast<int8_t const *>( buffer + byte_offset );
556 template<
typename T> __pack (
struct packed_t {
558 constexpr T
get(
const bool littleEndian)
const noexcept {
return littleEndian ?
le_to_cpu(store) :
be_to_cpu(store); }
561 constexpr
void put_uint16(uint8_t * buffer,
nsize_t const byte_offset,
const uint16_t v) noexcept
571 pointer_cast<packed_t<uint16_t>*>( buffer + byte_offset )->store = v;
573 constexpr
void put_uint16(uint8_t * buffer,
nsize_t const byte_offset,
const uint16_t v,
const bool littleEndian) noexcept
584 pointer_cast<packed_t<uint16_t>*>( buffer + byte_offset )->store = littleEndian ?
cpu_to_le(v) :
cpu_to_be(v);
598 return pointer_cast<const packed_t<uint16_t>*>( buffer + byte_offset )->store;
600 constexpr uint16_t
get_uint16(uint8_t
const * buffer,
nsize_t const byte_offset,
const bool littleEndian) noexcept
612 return pointer_cast<const packed_t<uint16_t>*>( buffer + byte_offset )->
get(littleEndian);
615 constexpr
void put_uint32(uint8_t * buffer,
nsize_t const byte_offset,
const uint32_t v) noexcept
617 pointer_cast<packed_t<uint32_t>*>( buffer + byte_offset )->store = v;
619 constexpr
void put_uint32(uint8_t * buffer,
nsize_t const byte_offset,
const uint32_t v,
const bool littleEndian) noexcept
621 pointer_cast<packed_t<uint32_t>*>( buffer + byte_offset )->store = littleEndian ?
cpu_to_le(v) :
cpu_to_be(v);
625 return pointer_cast<const packed_t<uint32_t>*>( buffer + byte_offset )->store;
627 constexpr uint32_t
get_uint32(uint8_t
const * buffer,
nsize_t const byte_offset,
const bool littleEndian) noexcept
629 return pointer_cast<const packed_t<uint32_t>*>( buffer + byte_offset )->
get(littleEndian);
632 constexpr
void put_uint64(uint8_t * buffer,
nsize_t const byte_offset,
const uint64_t & v) noexcept
634 pointer_cast<packed_t<uint64_t>*>( buffer + byte_offset )->store = v;
636 constexpr
void put_uint64(uint8_t * buffer,
nsize_t const byte_offset,
const uint64_t & v,
const bool littleEndian) noexcept
638 pointer_cast<packed_t<uint64_t>*>( buffer + byte_offset )->store = littleEndian ?
cpu_to_le(v) :
cpu_to_be(v);
642 return pointer_cast<const packed_t<uint64_t>*>( buffer + byte_offset )->store;
644 constexpr uint64_t
get_uint64(uint8_t
const * buffer,
nsize_t const byte_offset,
const bool littleEndian) noexcept
646 return pointer_cast<const packed_t<uint64_t>*>( buffer + byte_offset )->
get(littleEndian);
651 pointer_cast<packed_t<uint128_t>*>( buffer + byte_offset )->store = v;
655 pointer_cast<packed_t<uint128_t>*>( buffer + byte_offset )->store = littleEndian ?
cpu_to_le(v) :
cpu_to_be(v);
659 return pointer_cast<const packed_t<uint128_t>*>( buffer + byte_offset )->store;
663 return pointer_cast<const packed_t<uint128_t>*>( buffer + byte_offset )->
get(littleEndian);
668 pointer_cast<packed_t<uint192_t>*>( buffer + byte_offset )->store = v;
672 pointer_cast<packed_t<uint192_t>*>( buffer + byte_offset )->store = littleEndian ?
cpu_to_le(v) :
cpu_to_be(v);
676 return pointer_cast<const packed_t<uint192_t>*>( buffer + byte_offset )->store;
680 return pointer_cast<const packed_t<uint192_t>*>( buffer + byte_offset )->
get(littleEndian);
685 pointer_cast<packed_t<uint256_t>*>( buffer + byte_offset )->store = v;
689 pointer_cast<packed_t<uint256_t>*>( buffer + byte_offset )->store = littleEndian ?
cpu_to_le(v) :
cpu_to_be(v);
693 return pointer_cast<const packed_t<uint256_t>*>( buffer + byte_offset )->store;
697 return pointer_cast<const packed_t<uint256_t>*>( buffer + byte_offset )->
get(littleEndian);
708 typename std::enable_if_t<
709 std::is_standard_layout_v<T>,
714 pointer_cast<packed_t<T>*>( buffer + byte_offset )->store = v;
719 typename std::enable_if_t<
720 std::is_standard_layout_v<T>,
722 put_value(uint8_t * buffer,
nsize_t const byte_offset,
const T& v,
const bool littleEndian) noexcept
724 pointer_cast<packed_t<T>*>( buffer + byte_offset )->store = littleEndian ?
cpu_to_le(v) :
cpu_to_be(v);
729 typename std::enable_if_t<
730 std::is_standard_layout_v<T>,
734 return pointer_cast<const packed_t<T>*>( buffer + byte_offset )->store;
739 typename std::enable_if_t<
740 std::is_standard_layout_v<T>,
742 get_value(uint8_t
const * buffer,
nsize_t const byte_offset,
const bool littleEndian) noexcept
744 return pointer_cast<const packed_t<T>*>( buffer + byte_offset )->
get(littleEndian);
#define constexpr_cxx20
constexpr qualifier replacement for C++20 constexpr.
constexpr bool is_builtin_bit_cast_available() noexcept
Query whether __builtin_bit_cast(Dest_type, arg) is available, using jau::has_builtin_bit_cast.
@ undefined
Undetermined endian.
@ pdp
Identifier for DEC PDP-11, aka ENDIAN_LITTLE_WORD.
A little-endian type trait for convenience .
@ honeywell
Identifier for Honeywell 316, aka ENDIAN_BIG_WORD.
constexpr void put_uint192(uint8_t *buffer, nsize_t const byte_offset, const uint192_t &v) noexcept
#define PRAGMA_DISABLE_WARNING_MULTICHAR
@ little
Identifier for little endian.
#define PRAGMA_DISABLE_WARNING_PUSH
constexpr void put_uint32(uint8_t *buffer, nsize_t const byte_offset, const uint32_t v) noexcept
constexpr void put_uint128(uint8_t *buffer, nsize_t const byte_offset, const uint128_t &v) noexcept
PRAGMA_DISABLE_WARNING_POP constexpr_cxx20 std::string to_string(const endian &v) noexcept
Return std::string representation of the given jau::endian.
constexpr int8_t get_int8(uint8_t const *buffer, nsize_t const byte_offset) noexcept
constexpr void put_uint64(uint8_t *buffer, nsize_t const byte_offset, const uint64_t &v) noexcept
constexpr uint256_t get_uint256(uint8_t const *buffer, nsize_t const byte_offset) noexcept
A big-endian type trait for convenience .
constexpr bool has_endian_big_v
Value access of big-endian type trait for convenience .
constexpr bool isLittleEndian() noexcept
Evaluates true if platform is running in little endian mode, i.e.
@ big
Identifier for big endian.
constexpr void put_uint256(uint8_t *buffer, nsize_t const byte_offset, const uint256_t &v) noexcept
constexpr uint192_t get_uint192(uint8_t const *buffer, nsize_t const byte_offset) noexcept
constexpr std::enable_if_t< std::is_standard_layout_v< T >, void > put_value(uint8_t *buffer, nsize_t const byte_offset, const T &v) noexcept
constexpr bool has_endian_little_v
Value access of little-endian type trait for convenience .
constexpr uint16_t cpu_to_be(uint16_t const h) noexcept
constexpr uint32_t get_host_order() noexcept
@ native
Identifier for native platform type, one of the above.
constexpr uint8_t get_uint8(uint8_t const *buffer, nsize_t const byte_offset) noexcept
Entry * get(const EUI48 &addr, const std::string &name, AddressNameEntryMatchFunc m) noexcept
Returns a matching BTSecurityRegistry::Entry with the given.
constexpr uint32_t get_uint32(uint8_t const *buffer, nsize_t const byte_offset) noexcept
#define PRAGMA_DISABLE_WARNING_POP
constexpr bool isLittleOrBigEndian() noexcept
Evaluates true if platform is running in little or big endian mode, i.e.
constexpr uint16_t cpu_to_le(uint16_t const h) noexcept
constexpr uint128_t get_uint128(uint8_t const *buffer, nsize_t const byte_offset) noexcept
constexpr uint16_t bswap(uint16_t const source) noexcept
constexpr bool isDefinedEndian(const endian &v) noexcept
Evaluates true if the given endian is defined, i.e.
constexpr uint64_t get_uint64(uint8_t const *buffer, nsize_t const byte_offset) noexcept
constexpr std::enable_if_t< std::is_standard_layout_v< T >, T > get_value(uint8_t const *buffer, nsize_t const byte_offset) noexcept
endian
Endian identifier, indicating endianess of all scaler types.
constexpr uint16_t le_to_cpu(uint16_t const l) noexcept
constexpr uint16_t be_to_cpu(uint16_t const n) noexcept
On the i386 the host byte order is Least Significant Byte first (LSB) or Little-Endian,...
uint_fast32_t nsize_t
Natural 'size_t' alternative using uint_fast32_t as its natural sized type.
constexpr uint16_t get_uint16(uint8_t const *buffer, nsize_t const byte_offset) noexcept
constexpr void put_uint16(uint8_t *buffer, nsize_t const byte_offset, const uint16_t v) noexcept
constexpr bool isBigEndian() noexcept
Evaluates true if platform is running in big endian mode, i.e.
constexpr void put_uint8(uint8_t *buffer, nsize_t const byte_offset, const uint8_t v) noexcept