use crate::ble::replies::{OutOfBandReply, PasskeyReply};
use crate::ble::types::{EncryptionInfo, IdentityKey, MasterId, SecurityMode};
use crate::ble::Connection;
use crate::raw;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum IoCapabilities {
None,
DisplayYesNo,
DisplayOnly,
KeyboardOnly,
KeyboardDisplay,
}
impl IoCapabilities {
pub(crate) fn to_io_caps(self) -> u8 {
unwrap!(match self {
IoCapabilities::None => raw::BLE_GAP_IO_CAPS_NONE,
IoCapabilities::DisplayYesNo => raw::BLE_GAP_IO_CAPS_DISPLAY_YESNO,
IoCapabilities::DisplayOnly => raw::BLE_GAP_IO_CAPS_DISPLAY_ONLY,
IoCapabilities::KeyboardOnly => raw::BLE_GAP_IO_CAPS_KEYBOARD_ONLY,
IoCapabilities::KeyboardDisplay => raw::BLE_GAP_IO_CAPS_KEYBOARD_DISPLAY,
}
.try_into())
}
}
pub trait SecurityHandler {
fn io_capabilities(&self) -> IoCapabilities {
IoCapabilities::None
}
fn can_recv_out_of_band(&self, _conn: &Connection) -> bool {
false
}
fn can_bond(&self, _conn: &Connection) -> bool {
false
}
fn display_passkey(&self, _passkey: &[u8; 6]) {
panic!("SecurityHandler::display_passkey is not implemented");
}
fn enter_passkey(&self, _reply: PasskeyReply) {
panic!("SecurityHandler::enter_passkey is not implemented");
}
fn recv_out_of_band(&self, _reply: OutOfBandReply) {
panic!("SecurityHandler::recv_out_of_band is not implemented");
}
fn on_security_update(&self, _conn: &Connection, _security_mode: SecurityMode) {}
fn on_bonded(&self, _conn: &Connection, _master_id: MasterId, _key: EncryptionInfo, _peer_id: IdentityKey) {
panic!("SecurityHandler::on_bonded not implemented")
}
fn get_key(&self, _conn: &Connection, _master_id: MasterId) -> Option<EncryptionInfo> {
None
}
#[cfg(feature = "ble-gatt-server")]
fn save_sys_attrs(&self, _conn: &super::Connection) {}
#[cfg(feature = "ble-gatt-server")]
fn load_sys_attrs(&self, conn: &super::Connection) {
if let Err(err) = super::gatt_server::set_sys_attrs(conn, None) {
warn!("SecurityHandler failed to set sys attrs: {:?}", err);
}
}
}