BlueToe
an alternative GATT/BLE implementation
Loading...
Searching...
No Matches
address.hpp
1#ifndef BLUETOE_LINK_LAYER_ADDRESS_HPP
2#define BLUETOE_LINK_LAYER_ADDRESS_HPP
3
4#include <cstdint>
5#include <initializer_list>
6#include <iosfwd>
7
8namespace bluetoe {
9namespace link_layer {
10
11 class random_device_address;
12
16 class address
17 {
18 public:
23
27 explicit address( const std::initializer_list< std::uint8_t >& initial_values );
28
32 explicit address( const std::uint8_t* initial_values );
33
41
45 template < typename HashFunc >
46 static random_device_address generate_resolvable_private_address( const std::uint8_t* irk, std::uint32_t random, HashFunc hash_func );
47
51 std::ostream& print( std::ostream& ) const;
52
56 std::uint8_t msb() const;
57
61 bool operator==( const address& rhs ) const;
62
66 bool operator!=( const address& rhs ) const;
67
71 typedef std::uint8_t const * const_iterator;
72
77
82 private:
83 static std::uint32_t create_prand( std::uint32_t rand );
84
85 static constexpr std::size_t address_size_in_bytes = 6;
86 std::uint8_t value_[ address_size_in_bytes ];
87
88 // this type is not intended for polymorphic use; not implemented
89 void operator delete ( void* ptr );
90 void operator delete[]( void* ptr );
91 };
92
96 std::ostream& operator<<( std::ostream& out, const address& a );
97
106 class device_address : public address
107 {
108 public:
110
114 bool is_random() const
115 {
116 return is_random_;
117 }
118
122 bool is_public() const
123 {
124 return !is_random_;
125 }
126
132 bool is_static() const
133 {
134 return ( *( end() - 1 ) & 0xC0 ) == 0xC0;
135 }
136
142 bool is_private() const
143 {
144 const auto v = *( end() - 1 ) & 0xC0;
145 return v == 0x40 || v == 0x00;
146 }
147
153 bool is_resolvable() const
154 {
155 return ( *( end() - 1 ) & 0xC0 ) == 0x40;
156 }
157
162 {
163 return is_random() && is_resolvable();
164 }
165
166 using address::operator==;
167 using address::operator!=;
168
172 bool operator==( const device_address& rhs ) const;
173
177 bool operator!=( const device_address& rhs ) const;
178
183 device_address( const std::initializer_list< std::uint8_t >& initial_values, bool is_random )
184 : address( initial_values )
185 , is_random_( is_random )
186 {}
187
192 device_address( const std::uint8_t* initial_values, bool is_random )
193 : address( initial_values )
194 , is_random_( is_random )
195 {}
196
197 protected:
201 explicit device_address( bool is_random )
202 : address()
203 , is_random_( is_random )
204 {}
205
206 private:
207 bool is_random_;
208 };
209
210 std::ostream& operator<<( std::ostream& out, const device_address& a );
211
218 {
219 public:
224
228 explicit public_device_address( const std::initializer_list< std::uint8_t >& initial_values )
229 : device_address( initial_values, false ) {}
230
234 explicit public_device_address( const std::uint8_t* initial_values )
235 : device_address( initial_values, false ) {}
236 };
237
244 {
245 public:
250
254 explicit random_device_address( const std::initializer_list< std::uint8_t >& initial_values )
255 : device_address( initial_values, true ) {}
256
260 explicit random_device_address( const std::uint8_t* initial_values )
261 : device_address( initial_values, true ) {}
262 };
263
264 template < typename HashFunc >
265 random_device_address address::generate_resolvable_private_address( const std::uint8_t* irk, std::uint32_t random, HashFunc hash_func )
266 {
267 const std::uint32_t prand = create_prand( random );
268 const std::uint32_t hash = hash_func( irk, prand );
269
270 std::uint8_t initial_values[ address_size_in_bytes ] = {
271 static_cast<uint8_t>( prand >> 16 ),
272 static_cast<uint8_t>( prand >> 8 ),
273 static_cast<uint8_t>( prand ),
274 static_cast<uint8_t>( hash >> 16 ),
275 static_cast<uint8_t>( hash >> 8 ),
276 static_cast<uint8_t>( hash ) };
277
278 return random_device_address( initial_values );
279 }
280
281}
282
283}
284
285#endif