BlueToe
an alternative GATT/BLE implementation
Loading...
Searching...
No Matches
bas.hpp
1#ifndef BLUTOE_SERVICES_BAS_HPP
2
3#include <bluetoe/service.hpp>
4#include <bluetoe/mixin.hpp>
5#include <type_traits>
6
7namespace bluetoe
8{
9 namespace bas
10 {
14 using service_uuid = service_uuid16< 0x180F >;
15
19 using level_uuid = characteristic_uuid16< 0x2A19 >;
20
21 namespace details {
23 struct handler_tag {};
24 struct no_battery_level_notifications_tag {};
25 struct valid_bas_service_option_tag {};
26
27 template < typename Handler >
28 struct handler_impl
29 {
30 struct read_handler : bluetoe::details::value_handler_base
31 {
32 template < class Server >
33 static std::uint8_t call_read_handler( std::size_t offset, std::size_t, std::uint8_t* out_buffer, std::size_t& out_size, void* server )
34 {
35 if ( offset != 0 )
37
38 Handler& handler = static_cast< Handler& >( *static_cast< Server* >( server ) );
39
40 const int value = handler.read_battery_level();
41 assert(value >= 0);
42 assert(value <= 100);
43
44 *out_buffer = static_cast< std::uint8_t >( value );
45 out_size = 1;
46
48 }
49
50 struct meta_type :
51 bluetoe::details::value_handler_base::meta_type,
52 bluetoe::details::characteristic_value_read_handler_meta_type {};
53 };
54
55 template < typename Server >
56 void notifiy_battery_level( Server& srv )
57 {
58 srv.template notify< level_uuid >();
59 }
60 };
61
62 template < typename, typename >
63 struct service_impl;
64
65 template < typename Handler, typename ... Options >
66 struct service_impl< Handler, std::tuple< Options... > >
67 {
68 using type =
70 service_uuid,
72 level_uuid,
73 typename handler_impl< Handler >::read_handler,
76 >,
78 Options...
79 >;
80 };
81
82
83 template < typename ... Options >
84 struct calculate_service {
85 using handler = typename bluetoe::details::find_by_meta_type< handler_tag, Options... >::type;
86
87 static_assert( !std::is_same< handler, bluetoe::details::no_such_type >::value, "bas::handler<> is required" );
88
89 using other_args = typename bluetoe::details::find_all_by_not_meta_type<
90 details::valid_bas_service_option_tag, Options... >::type;
91
92 using type = typename service_impl< typename handler::type, other_args >::type;
93 };
95 }
96
107 struct meta_type : details::no_battery_level_notifications_tag
108 , details::valid_bas_service_option_tag {};
110 };
111
123 template < typename Handler >
124 struct handler {
126 using type = Handler;
127
128 struct meta_type : details::handler_tag
129 , details::valid_bas_service_option_tag {};
131 };
132 }
133
149 template < typename ... Options >
150 using battery_level = typename bas::details::calculate_service< Options... >::type;
151}
152
153#endif
A characteristic is a typed value that is accessable by a GATT client hosted by a GATT server.
Definition: characteristic.hpp:160
a service with zero or more characteristics
Definition: service.hpp:150
@ success
Definition: codes.hpp:154
@ attribute_not_long
Definition: codes.hpp:209
pass a handler to the service
Definition: bas.hpp:124
indicates no support for battery level notifications
Definition: bas.hpp:105
class to be mixed into the server instance
Definition: mixin.hpp:78
queues a notification of a characteristic as soon, as it was configured for notification.
Definition: characteristic_value.hpp:127
adds the ability to notify this characteristic.
Definition: characteristic_value.hpp:89