BlueToe
an alternative GATT/BLE implementation
Loading...
Searching...
No Matches
connection_callbacks.hpp
1#ifndef BLUETOE_LINK_LAYER_CONNECTION_CALLBACKS_HPP
2#define BLUETOE_LINK_LAYER_CONNECTION_CALLBACKS_HPP
3
4#include <bluetoe/meta_types.hpp>
5
6namespace bluetoe {
7namespace link_layer {
8 class connection_details;
9
10 namespace details {
11 struct connection_callbacks_meta_type {};
12 }
13
34 template < typename T, T& Obj >
37 struct meta_type :
38 details::connection_callbacks_meta_type,
39 details::valid_link_layer_option_meta_type {};
40
41 // TODO No need for impl-indirection anymore
42 class impl {
43 public:
44 impl()
45 : event_type_( none )
46 , connection_( nullptr )
47 {
48 }
49
50 void connection_request( const connection_addresses& addresses )
51 {
52 addresses_ = addresses;
53 }
54
55 // this functions are called from the interrupt handlers of the scheduled radio and just store the informations that
56 // are provided.
57 template < class Connection, class Radio >
58 void connection_established(
59 const connection_details& details,
60 Connection& connection,
61 Radio& r )
62 {
63 event_type_ = established;
64 connection_ = &connection;
65 details_ = details;
66
67 r.wake_up();
68 }
69
70 template < class Connection, class Radio >
71 void connection_changed( const bluetoe::link_layer::connection_details& details, Connection& connection, Radio& r )
72 {
73 event_type_ = changed;
74 connection_ = &connection;
75 details_ = details;
76
77 r.wake_up();
78 }
79
80 template < class Connection, class Radio >
81 void connection_closed( Connection& connection, Radio& r )
82 {
83 event_type_ = closed;
84 connection_ = &connection;
85
86 r.wake_up();
87 }
88
89 template < class LinkLayer >
90 void handle_connection_events() {
91 if ( event_type_ == established )
92 {
93 call_ll_connection_established< T >( Obj, details_, addresses_, connection_data< LinkLayer >() );
94 }
95 else if ( event_type_ == changed )
96 {
97 call_ll_connection_changed< T >( Obj, details_, connection_data< LinkLayer >() );
98 }
99 else if ( event_type_ == closed )
100 {
101 call_ll_connection_closed< T >( Obj, connection_data< LinkLayer >() );
102 }
103
104 event_type_ = none;
105 connection_ = nullptr;
106 }
107
108 private:
109 enum {
110 none,
111 established,
112 changed,
113 closed
114 } event_type_;
115
116 void* connection_;
117 connection_details details_;
118 connection_addresses addresses_;
119
120 template < typename LinkLayer >
121 typename LinkLayer::connection_data_t& connection_data()
122 {
123 assert( connection_ );
124 return *static_cast< typename LinkLayer::connection_data_t* >( connection_ );
125 }
126
127 template < typename TT, typename Connection >
128 auto call_ll_connection_established(
129 TT& obj,
130 const connection_details& details,
131 const connection_addresses& addr,
132 Connection& connection )
133 -> decltype(&TT::template ll_connection_established< Connection >)
134 {
135 obj.ll_connection_established( details, addr, connection );
136
137 return 0;
138 }
139
140 template < typename TT, typename Connection >
141 auto call_ll_connection_changed( TT& obj, const bluetoe::link_layer::connection_details& details, Connection& connection )
142 -> decltype(&TT::template ll_connection_changed< Connection >)
143 {
144 obj.ll_connection_changed( details, connection );
145
146 return 0;
147 }
148
149 template < typename TT, typename Connection >
150 auto call_ll_connection_closed( TT& obj, Connection& connection )
151 -> decltype(&TT::template ll_connection_closed< Connection >)
152 {
153 obj.ll_connection_closed( connection );
154
155 return 0;
156 }
157
158 template < typename TT >
159 void call_ll_connection_established( ... )
160 {
161 }
162
163 template < typename TT >
164 void call_ll_connection_changed( ... )
165 {
166 }
167
168 template < typename TT >
169 void call_ll_connection_closed( ... )
170 {
171 }
172 };
173
175 };
176
177 namespace details {
178 struct no_connection_callbacks {
179 struct meta_type :
180 details::connection_callbacks_meta_type,
181 details::valid_link_layer_option_meta_type {};
182
183 struct impl {
184 void connection_request( const connection_addresses& ) {}
185
186 template < class Connection, class Radio >
187 void connection_established(
188 const connection_details&,
189 Connection&,
190 Radio& ) {}
191
192 template < class Connection, class Radio >
193 void connection_changed( const bluetoe::link_layer::connection_details&, Connection&, Radio& ) {}
194
195 template < class Connection, class Radio >
196 void connection_closed( Connection&, Radio& ) {}
197
198 template < class LinkLayer >
199 void handle_connection_events() {}
200 };
201 };
202 }
203
204}
205}
206
207#endif