use core::future::Future;
use silizium::zigbee::security::man;
use crate::ember::key::{Struct, Type};
use crate::ember::{Eui64, NodeId, security};
use crate::error::Error;
use crate::frame::parameters::security::{
check_key_context, clear_key_table, clear_transient_link_keys, erase_key_table_entry,
export_key, export_link_key_by_eui, export_link_key_by_index, export_transient_key,
find_key_table_entry, get_aps_key_info, get_current_security_state, get_network_key_info,
import_key, import_link_key, import_transient_key, request_link_key,
send_trust_center_link_key, set_initial_security_state, update_tc_link_key,
};
use crate::parameters::security::get_key;
use crate::transport::Transport;
pub trait Security {
fn check_key_context(
&mut self,
context: man::Context,
) -> impl Future<Output = Result<(), Error>> + Send;
fn clear_key_table(&mut self) -> impl Future<Output = Result<(), Error>> + Send;
fn clear_transient_link_keys(&mut self) -> impl Future<Output = Result<(), Error>> + Send;
fn erase_key_table_entry(
&mut self,
index: u8,
) -> impl Future<Output = Result<(), Error>> + Send;
fn export_key(
&mut self,
man_context: man::Context,
) -> impl Future<Output = Result<man::Key, Error>> + Send;
fn export_link_key_by_eui(
&mut self,
eui: Eui64,
) -> impl Future<Output = Result<export_link_key_by_eui::Payload, Error>> + Send;
fn export_link_key_by_index(
&mut self,
index: u8,
) -> impl Future<Output = Result<export_link_key_by_index::Payload, Error>> + Send;
fn export_transient_key_by_eui(
&mut self,
eui: Eui64,
) -> impl Future<Output = Result<export_transient_key::TransientKey, Error>> + Send;
fn export_transient_key_by_index(
&mut self,
index: u8,
) -> impl Future<Output = Result<export_transient_key::TransientKey, Error>> + Send;
fn find_key_table_entry(
&mut self,
address: Eui64,
link_key: bool,
) -> impl Future<Output = Result<u8, Error>> + Send;
fn get_aps_key_info(
&mut self,
context_in: man::Context,
) -> impl Future<Output = Result<get_aps_key_info::KeyInfo, Error>> + Send;
fn get_current_security_state(
&mut self,
) -> impl Future<Output = Result<security::current::State, Error>> + Send;
#[deprecated]
fn get_key(&mut self, key: Type) -> impl Future<Output = Result<Struct, Error>> + Send;
fn get_network_key_info(
&mut self,
) -> impl Future<Output = Result<man::NetworkKeyInfo, Error>> + Send;
fn import_key(
&mut self,
context: man::Context,
key: man::Key,
) -> impl Future<Output = Result<(), Error>> + Send;
fn import_link_key(
&mut self,
index: u8,
address: Eui64,
plaintext_key: man::Key,
) -> impl Future<Output = Result<(), Error>> + Send;
fn import_transient_key(
&mut self,
context: man::Context,
eui64: Eui64,
plaintext_key: man::Key,
flags: man::Flags,
) -> impl Future<Output = Result<(), Error>> + Send;
fn request_link_key(
&mut self,
partner: Eui64,
) -> impl Future<Output = Result<(), Error>> + Send;
fn send_trust_center_link_key(
&mut self,
destination_node_id: NodeId,
destination_eui64: Eui64,
) -> impl Future<Output = Result<(), Error>> + Send;
fn set_initial_security_state(
&mut self,
state: security::initial::State,
) -> impl Future<Output = Result<(), Error>> + Send;
fn update_tc_link_key(
&mut self,
max_attempts: u8,
) -> impl Future<Output = Result<(), Error>> + Send;
}
impl<T> Security for T
where
T: Transport,
{
async fn check_key_context(&mut self, context: man::Context) -> Result<(), Error> {
self.communicate(check_key_context::Command::new(context))
.await?
.try_into()
}
async fn clear_key_table(&mut self) -> Result<(), Error> {
self.communicate(clear_key_table::Command).await?.try_into()
}
async fn clear_transient_link_keys(&mut self) -> Result<(), Error> {
self.communicate(clear_transient_link_keys::Command)
.await
.map(drop)
}
async fn erase_key_table_entry(&mut self, index: u8) -> Result<(), Error> {
self.communicate(erase_key_table_entry::Command::new(index))
.await
.map(drop)
}
async fn export_key(&mut self, man_context: man::Context) -> Result<man::Key, Error> {
self.communicate(export_key::Command::new(man_context))
.await?
.try_into()
}
async fn export_link_key_by_eui(
&mut self,
eui: Eui64,
) -> Result<export_link_key_by_eui::Payload, Error> {
self.communicate(export_link_key_by_eui::Command::new(eui))
.await?
.try_into()
}
async fn export_link_key_by_index(
&mut self,
index: u8,
) -> Result<export_link_key_by_index::Payload, Error> {
self.communicate(export_link_key_by_index::Command::new(index))
.await?
.try_into()
}
async fn export_transient_key_by_eui(
&mut self,
eui: Eui64,
) -> Result<export_transient_key::TransientKey, Error> {
self.communicate(export_transient_key::by_eui::Command::new(eui))
.await?
.try_into()
}
async fn export_transient_key_by_index(
&mut self,
index: u8,
) -> Result<export_transient_key::TransientKey, Error> {
self.communicate(export_transient_key::by_index::Command::new(index))
.await?
.try_into()
}
async fn find_key_table_entry(&mut self, address: Eui64, link_key: bool) -> Result<u8, Error> {
self.communicate(find_key_table_entry::Command::new(address, link_key))
.await
.map(Into::into)
}
async fn get_aps_key_info(
&mut self,
context_in: man::Context,
) -> Result<get_aps_key_info::KeyInfo, Error> {
self.communicate(get_aps_key_info::Command::new(context_in))
.await?
.try_into()
}
async fn get_current_security_state(&mut self) -> Result<security::current::State, Error> {
self.communicate(get_current_security_state::Command)
.await?
.try_into()
}
async fn get_key(&mut self, key: Type) -> Result<Struct, Error> {
self.communicate(get_key::Command::new(key))
.await?
.try_into()
}
async fn get_network_key_info(&mut self) -> Result<man::NetworkKeyInfo, Error> {
self.communicate(get_network_key_info::Command)
.await?
.try_into()
}
async fn import_key(&mut self, context: man::Context, key: man::Key) -> Result<(), Error> {
self.communicate(import_key::Command::new(context, key))
.await?
.try_into()
}
async fn import_link_key(
&mut self,
index: u8,
address: Eui64,
plaintext_key: man::Key,
) -> Result<(), Error> {
self.communicate(import_link_key::Command::new(index, address, plaintext_key))
.await?
.try_into()
}
async fn import_transient_key(
&mut self,
context: man::Context,
eui64: Eui64,
plaintext_key: man::Key,
flags: man::Flags,
) -> Result<(), Error> {
self.communicate(import_transient_key::Command::new(
context,
eui64,
plaintext_key,
flags,
))
.await?
.try_into()
}
async fn request_link_key(&mut self, partner: Eui64) -> Result<(), Error> {
self.communicate(request_link_key::Command::new(partner))
.await?
.try_into()
}
async fn send_trust_center_link_key(
&mut self,
destination_node_id: NodeId,
destination_eui64: Eui64,
) -> Result<(), Error> {
self.communicate(send_trust_center_link_key::Command::new(
destination_node_id,
destination_eui64,
))
.await?
.try_into()
}
async fn set_initial_security_state(
&mut self,
state: security::initial::State,
) -> Result<(), Error> {
self.communicate(set_initial_security_state::Command::new(state))
.await?
.try_into()
}
async fn update_tc_link_key(&mut self, max_attempts: u8) -> Result<(), Error> {
self.communicate(update_tc_link_key::Command::new(max_attempts))
.await?
.try_into()
}
}