BlueToe
an alternative GATT/BLE implementation
Loading...
Searching...
No Matches
client_characteristic_configuration.hpp
1#ifndef BLUETOE_CLIENT_CHARACTERISTIC_CONFIGURATION_HPP
2#define BLUETOE_CLIENT_CHARACTERISTIC_CONFIGURATION_HPP
3
4#include <cassert>
5#include <cstdint>
6#include <algorithm>
7
8namespace bluetoe {
9namespace details {
10
16 class client_characteristic_configuration
17 {
18 public:
19 constexpr client_characteristic_configuration()
20 : data_( nullptr )
21 {
22 }
23
24 constexpr explicit client_characteristic_configuration( std::uint8_t* data, std::size_t )
25 : data_( data )
26 {
27 }
28
29 std::uint16_t flags( std::size_t index ) const
30 {
31 assert( data_ );
32
33 return ( data_[ index / 4 ] >> shift( index ) ) & 0x3;
34 }
35
36 void flags( std::size_t index, std::uint16_t new_flags )
37 {
38 assert( data_ );
39
40 // do not assert on new_flags as they might come from a client
41 data_[ index / 4 ] = ( data_[ index / 4 ] & ~mask( index ) ) | ( ( new_flags & 0x03 ) << shift( index ) );
42 }
43
44 static constexpr std::size_t bits_per_config = 2;
45
46 private:
47 static constexpr std::size_t shift( std::size_t index )
48 {
49 return ( index % 4 ) * bits_per_config;
50 }
51
52 static constexpr std::uint8_t mask( std::size_t index )
53 {
54 return 0x03 << shift( index );
55 }
56
57 std::uint8_t* data_;
58 };
59
65 template < std::size_t Size >
66 class client_characteristic_configurations
67 {
68 public:
73 static constexpr std::size_t number_of_characteristics_with_configuration = Size;
74
75 client_characteristic_configurations()
76 {
77 std::fill( std::begin( configs_ ), std::end( configs_ ), 0 );
78 }
79
80 client_characteristic_configuration client_configurations()
81 {
82 return client_characteristic_configuration( &configs_[ 0 ], Size );
83 };
84
90 const std::uint8_t* serialized_cccds_begin() const
91 {
92 return std::begin( configs_ );
93 }
94
100 const std::uint8_t* serialized_cccds_end() const
101 {
102 return std::end( configs_ );
103 }
104
110 std::uint8_t* serialized_cccds_begin()
111 {
112 return std::begin( configs_ );
113 }
114
120 std::uint8_t* serialized_cccds_end()
121 {
122 return std::end( configs_ );
123 }
124
125 private:
126 std::uint8_t configs_[ ( Size * client_characteristic_configuration::bits_per_config + 7 ) / 8 ];
127 };
128
129 template <>
130 class client_characteristic_configurations< 0 >
131 {
132 public:
133 client_characteristic_configuration client_configurations()
134 {
135 return client_characteristic_configuration();
136 }
137 };
138
139}
140}
141
142#endif