use crate::enums;
use esp_idf_svc::sys as esp_idf_sys;
pub struct BLESecurity {
passkey: u32,
}
impl BLESecurity {
pub(crate) fn new() -> Self {
Self { passkey: 0 }
}
pub fn set_auth(&mut self, auth_req: enums::AuthReq) -> &mut Self {
unsafe {
esp_idf_sys::ble_hs_cfg.set_sm_bonding(auth_req.contains(enums::AuthReq::Bond) as _);
esp_idf_sys::ble_hs_cfg.set_sm_mitm(auth_req.contains(enums::AuthReq::Mitm) as _);
esp_idf_sys::ble_hs_cfg.set_sm_sc(auth_req.contains(enums::AuthReq::Sc) as _);
}
self
}
pub fn get_passkey(&self) -> u32 {
self.passkey
}
pub fn set_passkey(&mut self, passkey: u32) -> &mut Self {
debug_assert!(
passkey <= 999999,
"passkey must be between 000000..=999999 inclusive"
);
self.passkey = passkey;
self
}
pub fn set_io_cap(&mut self, iocap: enums::SecurityIOCap) -> &mut Self {
unsafe { esp_idf_sys::ble_hs_cfg.sm_io_cap = iocap as _ };
self
}
pub fn set_security_init_key(&mut self, init_key: enums::PairKeyDist) -> &mut Self {
unsafe { esp_idf_sys::ble_hs_cfg.sm_our_key_dist = init_key.bits() };
self
}
pub fn set_security_resp_key(&mut self, resp_key: enums::PairKeyDist) -> &mut Self {
unsafe { esp_idf_sys::ble_hs_cfg.sm_their_key_dist = resp_key.bits() };
self
}
pub fn resolve_rpa(&mut self) -> &mut Self {
self
.set_security_init_key(enums::PairKeyDist::ENC | enums::PairKeyDist::ID)
.set_security_resp_key(enums::PairKeyDist::ENC | enums::PairKeyDist::ID)
}
}