1#ifndef BLUETOE_SM_INCLUDE_IO_CAPABILTIES_HPP
2#define BLUETOE_SM_INCLUDE_IO_CAPABILTIES_HPP
5#include <bluetoe/ll_meta_types.hpp>
6#include <bluetoe/bits.hpp>
11 struct pairing_input_capabilty_meta_type {};
12 struct pairing_output_capabilty_meta_type {};
13 struct pairing_just_works_meta_type {};
15 enum class io_capabilities : std::uint8_t {
17 display_yes_no = 0x01,
19 no_input_no_output = 0x03,
20 keyboard_display = 0x04,
21 last = keyboard_display
24 enum class legacy_pairing_algorithm : std::uint8_t {
27 passkey_entry_display,
31 enum class lesc_pairing_algorithm : std::uint8_t {
34 passkey_entry_display,
52 static std::array< std::uint8_t, 16 > sm_pairing_passkey()
58 static bool sm_pairing_request_yes_no(
const S& )
64 details::pairing_input_capabilty_meta_type,
65 link_layer::details::valid_link_layer_option_meta_type {};
79 virtual void yes_no_response(
bool ) = 0;
101 template <
typename T, T& Obj >
105 static std::array< std::uint8_t, 16 > sm_pairing_passkey()
111 static void sm_pairing_request_yes_no( S& state )
113 state.wait_for_user_response();
114 Obj.sm_pairing_yes_no( state );
118 details::pairing_input_capabilty_meta_type,
119 link_layer::details::valid_link_layer_option_meta_type {};
138 template <
typename T, T& Obj >
142 static std::array< std::uint8_t, 16 > sm_pairing_passkey()
144 std::array< std::uint8_t, 16 > result = {{ 0 }};
145 details::write_32bit( result.data(), Obj.sm_pairing_passkey() );
151 details::pairing_input_capabilty_meta_type,
152 link_layer::details::valid_link_layer_option_meta_type {};
162 static details::io_capabilities get_io_capabilities(
const pairing_no_input& )
164 return details::io_capabilities::no_input_no_output;
167 template <
typename T, T& Obj >
170 return details::io_capabilities::no_input_no_output;
173 template <
typename T, T& Obj >
176 return details::io_capabilities::keyboard_only;
179 static details::legacy_pairing_algorithm select_legacy_pairing_algorithm(
const pairing_no_input&, details::io_capabilities )
181 return details::legacy_pairing_algorithm::just_works;
184 template <
typename T, T& Obj >
185 static details::legacy_pairing_algorithm select_legacy_pairing_algorithm(
const pairing_yes_no< T, Obj >&, details::io_capabilities )
187 return details::legacy_pairing_algorithm::just_works;
190 template <
typename T, T& Obj >
191 static details::legacy_pairing_algorithm select_legacy_pairing_algorithm(
const pairing_keyboard< T, Obj >&, details::io_capabilities io_capability )
193 if ( io_capability == details::io_capabilities::no_input_no_output )
194 return details::legacy_pairing_algorithm::just_works;
196 return details::legacy_pairing_algorithm::passkey_entry_input;
199 static details::lesc_pairing_algorithm select_lesc_pairing_algorithm(
const pairing_no_input&, details::io_capabilities )
201 return details::lesc_pairing_algorithm::just_works;
204 template <
typename T, T& Obj >
205 static details::lesc_pairing_algorithm select_lesc_pairing_algorithm(
const pairing_yes_no< T, Obj >&, details::io_capabilities )
207 return details::lesc_pairing_algorithm::just_works;
210 template <
typename T, T& Obj >
211 static details::lesc_pairing_algorithm select_lesc_pairing_algorithm(
const pairing_keyboard< T, Obj >&, details::io_capabilities io_capability )
213 if ( io_capability == details::io_capabilities::no_input_no_output )
214 return details::lesc_pairing_algorithm::just_works;
216 return details::lesc_pairing_algorithm::passkey_entry_input;
219 static void sm_pairing_numeric_output(
const std::array< std::uint8_t, 16 >& )
223 template <
class S,
class F >
224 static void sm_pairing_numeric_compare_output(
const S&, F& )
229 details::pairing_output_capabilty_meta_type,
230 link_layer::details::valid_link_layer_option_meta_type {};
245 template <
typename T, T& Obj >
249 static details::io_capabilities get_io_capabilities(
const pairing_no_input& )
251 return details::io_capabilities::display_only;
254 template <
typename O, O& Other >
257 return details::io_capabilities::display_yes_no;
260 template <
typename O, O& Other >
263 return details::io_capabilities::keyboard_display;
266 static details::legacy_pairing_algorithm select_legacy_pairing_algorithm(
const pairing_no_input&, details::io_capabilities io_capability )
268 if ( io_capability == details::io_capabilities::keyboard_only || io_capability == details::io_capabilities::keyboard_display )
269 return details::legacy_pairing_algorithm::passkey_entry_display;
271 return details::legacy_pairing_algorithm::just_works;
274 template <
typename O, O& Other >
275 static details::legacy_pairing_algorithm select_legacy_pairing_algorithm(
const pairing_yes_no< O, Other >&, details::io_capabilities io_capability )
277 if ( io_capability == details::io_capabilities::keyboard_only || io_capability == details::io_capabilities::keyboard_display )
278 return details::legacy_pairing_algorithm::passkey_entry_display;
280 return details::legacy_pairing_algorithm::just_works;
283 template <
typename O, O& Other >
284 static details::legacy_pairing_algorithm select_legacy_pairing_algorithm(
const pairing_keyboard< O, Other >&, details::io_capabilities io_capability )
286 if ( io_capability == details::io_capabilities::no_input_no_output )
287 return details::legacy_pairing_algorithm::just_works;
289 if ( io_capability == details::io_capabilities::keyboard_only )
290 return details::legacy_pairing_algorithm::passkey_entry_display;
292 return details::legacy_pairing_algorithm::passkey_entry_input;
295 static details::lesc_pairing_algorithm select_lesc_pairing_algorithm(
const pairing_no_input&, details::io_capabilities io_capability )
297 if ( io_capability == details::io_capabilities::keyboard_only || io_capability == details::io_capabilities::keyboard_display )
298 return details::lesc_pairing_algorithm::passkey_entry_display;
300 return details::lesc_pairing_algorithm::just_works;
303 template <
typename O, O& Other >
304 static details::lesc_pairing_algorithm select_lesc_pairing_algorithm(
const pairing_yes_no< O, Other >&, details::io_capabilities io_capability )
306 if ( io_capability == details::io_capabilities::display_yes_no || io_capability == details::io_capabilities::keyboard_display )
307 return details::lesc_pairing_algorithm::numeric_comparison;
309 if ( io_capability == details::io_capabilities::keyboard_only )
310 return details::lesc_pairing_algorithm::passkey_entry_display;
312 return details::lesc_pairing_algorithm::just_works;
315 template <
typename O, O& Other >
316 static details::lesc_pairing_algorithm select_lesc_pairing_algorithm(
const pairing_keyboard< O, Other >&, details::io_capabilities io_capability )
318 if ( io_capability == details::io_capabilities::display_yes_no || io_capability == details::io_capabilities::keyboard_display )
319 return details::lesc_pairing_algorithm::numeric_comparison;
321 if ( io_capability == details::io_capabilities::display_only )
322 return details::lesc_pairing_algorithm::passkey_entry_input;
324 if ( io_capability == details::io_capabilities::keyboard_only )
325 return details::lesc_pairing_algorithm::passkey_entry_display;
327 return details::lesc_pairing_algorithm::just_works;
330 static void sm_pairing_numeric_output(
const std::array< std::uint8_t, 16 >& key )
332 Obj.sm_pairing_numeric_output(
static_cast< int >( details::read_32bit( key.data() ) ) );
335 template <
class S,
class F >
336 static void sm_pairing_numeric_compare_output(
const S& state, F& funcs )
338 const auto& Na = state.remote_nonce();
339 const auto& Nb = state.local_nonce();
340 const std::uint8_t* Pkax = state.remote_public_key_x();
341 const std::uint8_t* PKbx = state.local_public_key_x();
343 const auto pass_key = funcs.g2( Pkax, PKbx, Na, Nb );
344 Obj.sm_pairing_numeric_output(
static_cast< int >( pass_key ) );
348 details::pairing_output_capabilty_meta_type,
349 link_layer::details::valid_link_layer_option_meta_type {};
366 details::pairing_just_works_meta_type,
367 link_layer::details::valid_link_layer_option_meta_type {};
381 details::pairing_just_works_meta_type,
382 link_layer::details::valid_link_layer_option_meta_type {};
388 template <
typename ... Options >
389 class io_capabilities_matrix
392 using input_capabilities =
typename find_by_meta_type< pairing_input_capabilty_meta_type, Options...,
pairing_no_input >::type;
393 using output_capabilities =
typename find_by_meta_type< pairing_output_capabilty_meta_type, Options...,
pairing_no_output >::type;
395 static io_capabilities get_io_capabilities()
397 return output_capabilities::get_io_capabilities(input_capabilities());
400 static legacy_pairing_algorithm select_legacy_pairing_algorithm( std::uint8_t io_capability )
402 return select_legacy_pairing_algorithm(
static_cast< io_capabilities
>( io_capability ) );
405 static legacy_pairing_algorithm select_legacy_pairing_algorithm( io_capabilities io_capability )
407 return output_capabilities::select_legacy_pairing_algorithm( input_capabilities(), io_capability );
410 static lesc_pairing_algorithm select_lesc_pairing_algorithm( std::uint8_t io_capability )
412 return select_lesc_pairing_algorithm(
static_cast< io_capabilities
>( io_capability ) );
415 static lesc_pairing_algorithm select_lesc_pairing_algorithm( io_capabilities io_capability )
417 return output_capabilities::select_lesc_pairing_algorithm( input_capabilities(), io_capability );
420 static void sm_pairing_numeric_output(
const std::array< std::uint8_t, 16 >& temp_key )
422 output_capabilities::sm_pairing_numeric_output( temp_key );
425 template <
class S,
class F >
426 static void sm_pairing_numeric_compare_output(
const S& state, F& func )
428 output_capabilities::sm_pairing_numeric_compare_output( state, func );
431 static std::array< std::uint8_t, 16 > sm_pairing_passkey()
433 return input_capabilities::sm_pairing_passkey();
437 static void sm_pairing_request_yes_no( S& state )
439 input_capabilities::sm_pairing_request_yes_no( state );
interface that can be stored to provide response to a yes_no question asynchronously.
Definition: io_capabilities.hpp:76
mark that "just works" pairing is accepted, even when the device have IO capabilities to exchange a p...
Definition: io_capabilities.hpp:378
Device has a numeric keyboard that can input the numbers '0' to '9' and a confirmation.
Definition: io_capabilities.hpp:140
mark that "just works" pairing is not acceptable
Definition: io_capabilities.hpp:363
defines that the device has no means to output a numeric value
Definition: io_capabilities.hpp:160
the device has a mean to output the 6 digit pass key used during the pairing process.
Definition: io_capabilities.hpp:247
Device has at least two buttons that can be easily mapped to 'yes' and 'no' or the device has a mecha...
Definition: io_capabilities.hpp:103