1#ifndef BLUETOE_BITS_HPP
2#define BLUETOE_BITS_HPP
10 constexpr std::uint16_t read_handle(
const std::uint8_t* h )
12 return *h + ( *( h + 1 ) << 8 );
15 constexpr std::uint16_t read_16bit_uuid(
const std::uint8_t* h )
17 return read_handle( h );
20 constexpr std::uint16_t read_16bit(
const std::uint8_t* h )
22 return read_handle( h );
25 constexpr std::uint32_t read_24bit(
const std::uint8_t* h )
27 return static_cast< std::uint32_t
>( read_16bit( h ) ) | (
static_cast< std::uint32_t
>( *( h + 2 ) ) << 16 );
30 constexpr std::uint32_t read_32bit(
const std::uint8_t* p )
32 return static_cast< std::uint32_t
>( read_16bit( p ) )
33 | (
static_cast< std::uint32_t
>( read_16bit( p + 2 ) ) << 16 );
36 constexpr std::uint64_t read_64bit(
const std::uint8_t* p )
38 return static_cast< std::uint64_t
>( read_32bit( p ) )
39 | (
static_cast< std::uint64_t
>( read_32bit( p + 4 ) ) << 32 );
42 inline std::uint8_t* write_handle( std::uint8_t* out, std::uint16_t handle )
44 out[ 0 ] = handle & 0xff;
45 out[ 1 ] = handle >> 8;
50 inline std::uint8_t* write_16bit_uuid( std::uint8_t* out, std::uint16_t uuid )
52 return write_handle( out, uuid );
55 inline std::uint8_t* write_16bit( std::uint8_t* out, std::uint16_t bits16 )
57 return write_handle( out, bits16 );
60 inline std::uint8_t* write_32bit( std::uint8_t* out, std::uint32_t bits32 )
62 return write_16bit( write_16bit( out, bits32 & 0xffff ), bits32 >> 16 );
65 inline std::uint8_t* write_64bit( std::uint8_t* out, std::uint64_t bits64 )
67 return write_32bit( write_32bit( out, bits64 & 0xffffffff ), bits64 >> 32 );
70 inline std::uint8_t* write_byte( std::uint8_t* out, std::uint8_t
byte )
84 template <
typename I >
85 typename std::make_signed< I >::type distance( I start, I end )
87 static_assert( std::is_unsigned< I >::value,
"I has to be an unsigned type" );
90 return -distance( end, start );
92 const I positive = end - start;
93 const I negative = start + ~I(0) - end + 1;
95 return positive < negative
100 template < std::
size_t N,
typename I >
101 typename std::make_signed< I >::type distance_n( I start, I end )
103 static_assert( std::is_unsigned< I >::value,
"I has to be an unsigned type" );
106 return -distance_n< N >( end, start );
108 const I positive = end - start;
109 const I negative = start + I(1 << N) - end;
111 return positive < negative