BlueToe
an alternative GATT/BLE implementation
Loading...
Searching...
No Matches
filter.hpp
1#ifndef BLUETOE_FILTER_HPP
2#define BLUETOE_FILTER_HPP
3
4#include <bluetoe/attribute.hpp>
5#include <bluetoe/bits.hpp>
6#include <bluetoe/uuid.hpp>
7#include <bluetoe/codes.hpp>
8#include <algorithm>
9#include <iterator>
10
11namespace bluetoe {
12namespace details {
13
17 class uuid_filter
18 {
19 public:
26 uuid_filter( const std::uint8_t* bytes, bool is_128bit )
27 : bytes_( bytes )
28 , is_128bit_( is_128bit )
29 {
30 if ( is_128bit && representable_as_16bit_uuid( bytes ) )
31 {
32 // lets treat this uuid as a 16 bit uuid
33 is_128bit_ = false;
34 bytes_ += 12;
35 }
36 }
37
38 bool operator()( std::size_t attribute_index, const attribute& attr ) const
39 {
40 if ( is_128bit_ )
41 {
42 if ( attr.uuid == bits( gatt_uuids::internal_128bit_uuid ) )
43 {
44 auto compare = attribute_access_arguments::compare_128bit_uuid( bytes_ );
45 return attr.access( compare, attribute_index ) == attribute_access_result::uuid_equal;
46 }
47 else
48 {
49 return false;
50 }
51 }
52 else
53 {
54 return read_16bit_uuid( bytes_ ) == attr.uuid;
55 }
56 }
57
58 private:
59 static bool representable_as_16bit_uuid( const std::uint8_t* bytes )
60 {
61 return std::equal( std::begin( bluetooth_base_uuid::bytes ), std::end( bluetooth_base_uuid::bytes ) - 4, bytes )
62 && bytes[ 14 ] == 0 && bytes[ 15 ] == 0;
63 }
64
65 const std::uint8_t* bytes_;
66 bool is_128bit_;
67 };
68
78 template < class UUID >
79 struct uuid16_filter;
80
81 template < std::uint64_t UUID, typename Check >
82 struct uuid16_filter< uuid16< UUID, Check > >
83 {
84 constexpr bool operator()( std::size_t, const attribute& attr ) const
85 {
86 return attr.uuid == UUID;
87 }
88 };
89
93 struct all_uuid_filter
94 {
95 constexpr bool operator()( std::size_t, const details::attribute& ) const
96 {
97 return true;
98 }
99 };
100
101}
102}
103
104#endif