use super::*;
use crate::AddressType;
pub async fn load_link_keys(
socket: &mut ManagementStream,
controller: Controller,
keys: Vec<LinkKey>,
debug: bool,
event_tx: Option<mpsc::Sender<Response>>,
) -> Result<()> {
let mut param = BytesMut::with_capacity(3 + keys.len() * 25);
param.put_u8(debug as u8);
param.put_u16_le(keys.len() as u16);
for key in keys {
param.put_slice(key.address.as_ref());
param.put_u8(key.address_type as u8);
param.put_u8(key.key_type as u8);
param.put_slice(&key.value[..]);
param.put_u8(key.pin_length);
}
let (_, _param) = exec_command(
socket,
Command::LoadLinkKeys,
controller,
Some(param.freeze()),
event_tx,
)
.await?;
Ok(())
}
pub async fn load_long_term_keys(
socket: &mut ManagementStream,
controller: Controller,
keys: Vec<LongTermKey>,
event_tx: Option<mpsc::Sender<Response>>,
) -> Result<()> {
let mut param = BytesMut::with_capacity(2 + keys.len() * 32);
param.put_u16_le(keys.len() as u16);
for key in keys {
param.put_slice(key.address.as_ref());
param.put_u8(key.address_type as u8);
param.put_u8(key.key_type as u8);
param.put_u8(key.master);
param.put_u8(key.encryption_size);
param.put_u16_le(key.encryption_diversifier);
param.put_u64_le(key.random_number);
param.put_slice(&key.value[..]);
}
let (_, _param) = exec_command(
socket,
Command::LoadLongTermKeys,
controller,
Some(param.freeze()),
event_tx,
)
.await?;
Ok(())
}
pub async fn load_identity_resolving_keys(
socket: &mut ManagementStream,
controller: Controller,
keys: Vec<IdentityResolvingKey>,
event_tx: Option<mpsc::Sender<Response>>,
) -> Result<()> {
let mut param = BytesMut::with_capacity(2 + keys.len() * 23);
param.put_u16_le(keys.len() as u16);
for key in keys {
param.put_slice(key.address.as_ref());
param.put_u8(key.address_type as u8);
param.put_slice(&key.value[..]);
}
let (_, _param) = exec_command(
socket,
Command::LoadIdentityResolvingKeys,
controller,
Some(param.freeze()),
event_tx,
)
.await?;
Ok(())
}
pub async fn load_connection_parameters(
socket: &mut ManagementStream,
controller: Controller,
connection_params: Vec<ConnectionParams>,
event_tx: Option<mpsc::Sender<Response>>,
) -> Result<()> {
let mut param = BytesMut::with_capacity(2 + connection_params.len() * 15);
param.put_u16_le(connection_params.len() as u16);
for cxn_param in connection_params {
param.put_slice(cxn_param.address.as_ref());
param.put_u8(cxn_param.address_type as u8);
param.put_u16_le(cxn_param.min_connection_interval);
param.put_u16_le(cxn_param.max_connection_interval);
param.put_u16_le(cxn_param.connection_latency);
param.put_u16_le(cxn_param.supervision_timeout);
}
let (_, _param) = exec_command(
socket,
Command::LoadConnectionParameters,
controller,
Some(param.freeze()),
event_tx,
)
.await?;
Ok(())
}
pub async fn load_blocked_keys(
socket: &mut ManagementStream,
controller: Controller,
keys: Vec<BlockedKey>,
event_tx: Option<mpsc::Sender<Response>>,
) -> Result<()> {
let mut param = BytesMut::with_capacity(2 + keys.len() * 17);
param.put_u16_le(keys.len() as u16);
for key in keys {
param.put_u8(key.key_type as u8);
param.put_slice(&key.value[..]);
}
let (_, _param) = exec_command(
socket,
Command::LoadBlockedKeys,
controller,
Some(param.freeze()),
event_tx,
)
.await?;
Ok(())
}
#[derive(Debug)]
pub struct LinkKey {
pub address: Address,
pub address_type: AddressType,
pub key_type: LinkKeyType,
pub value: [u8; 16],
pub pin_length: u8,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, FromPrimitive)]
#[repr(u8)]
pub enum LinkKeyType {
Combination = 0x00,
LocalUnit = 0x01,
RemoteUnit = 0x02,
DebugCombination = 0x03,
UnauthenticatedCombinationP192 = 0x04,
AuthenticatedCombinationP192 = 0x05,
ChangedCombination = 0x06,
UnauthenticatedCombinationP256 = 0x07,
AuthenticatedCombinationP256 = 0x08,
}
#[derive(Debug)]
pub struct LongTermKey {
pub address: Address,
pub address_type: AddressType,
pub key_type: LongTermKeyType,
pub master: u8,
pub encryption_size: u8,
pub encryption_diversifier: u16,
pub random_number: u64,
pub value: [u8; 16],
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, FromPrimitive)]
#[repr(u8)]
pub enum LongTermKeyType {
UnauthenticatedLegacy = 0x00,
AuthenticatedLegacy,
UnauthenticatedP256,
AuthenticatedP256,
DebugP256,
}
#[derive(Debug)]
pub struct IdentityResolvingKey {
pub address: Address,
pub address_type: AddressType,
pub value: [u8; 16],
}
pub struct BlockedKey {
pub key_type: BlockedKeyType,
pub value: [u8; 16],
}
#[repr(u8)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, FromPrimitive)]
pub enum BlockedKeyType {
LinkKey = 1 << 0,
LongTermKey = 1 << 1,
IdentityResolvingKey = 1 << 2,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, FromPrimitive)]
#[repr(u8)]
pub enum SignatureResolvingKeyType {
UnauthenticatedLocalCSRK = 0x00,
UnauthenticatedRemoteCSRK = 0x01,
AuthenticatedLocalCSRK = 0x02,
AuthenticatedRemoteCSRK,
}