use std::future::Future;
use zigbee_primitives::{ExtPanId, Ieee, Key, NodeId, PanId};
use crate::backup::Backup;
use crate::error::HostError;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NetworkSettings {
pub channel: u8,
pub pan_id: PanId,
pub extended_pan_id: ExtPanId,
pub network_key: Key,
pub tx_power: Option<i8>,
}
#[derive(Debug, Clone)]
pub enum StartMode {
Normal {
settings: NetworkSettings,
replace_other: bool,
},
Restore(Box<Backup>),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StartOutcome {
Resumed,
Formed,
Restored,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NetworkInfo {
pub ieee: Ieee,
pub nwk: NodeId,
pub channel: u8,
pub pan_id: PanId,
pub extended_pan_id: ExtPanId,
pub nwk_update_id: u8,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AdapterInfo {
pub kind: &'static str,
pub firmware: String,
pub protocol: u8,
pub reports_key_exchange: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JoinPolicy {
pub install_code_only: bool,
pub allow_legacy: bool,
}
impl Default for JoinPolicy {
fn default() -> Self {
JoinPolicy {
install_code_only: false,
allow_legacy: true,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Aps {
pub profile: u16,
pub cluster: u16,
pub source_endpoint: u8,
pub destination_endpoint: u8,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Beacon {
pub channel: u8,
pub pan_id: PanId,
pub extended_pan_id: ExtPanId,
pub permit_joining: bool,
pub nwk_update_id: u8,
pub lqi: u8,
pub rssi: Option<i8>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AdapterEvent {
Message {
source: NodeId,
source_ieee: Option<Ieee>,
aps: Aps,
broadcast: bool,
group: Option<u16>,
lqi: u8,
rssi: Option<i8>,
payload: Vec<u8>,
},
Joined {
nwk: NodeId,
ieee: Ieee,
parent: NodeId,
rejoin: bool,
},
Left {
nwk: NodeId,
ieee: Ieee,
},
KeyExchange {
ieee: Ieee,
success: bool,
},
RouteRecord {
source: NodeId,
ieee: Option<Ieee>,
relays: Vec<NodeId>,
lqi: Option<u8>,
rssi: Option<i8>,
},
Delivery {
tag: u16,
delivered: bool,
},
NetworkDown,
NetworkUp,
Failed(String),
}
pub trait Adapter: Send + Sync + 'static {
fn info(&self) -> AdapterInfo;
fn start(
&self,
mode: StartMode,
) -> impl Future<Output = Result<(StartOutcome, NetworkInfo), HostError>> + Send;
fn permit_join(
&self,
seconds: u8,
policy: &JoinPolicy,
) -> impl Future<Output = Result<(), HostError>> + Send;
fn add_join_key(
&self,
ieee: Ieee,
key: Key,
) -> impl Future<Output = Result<(), HostError>> + Send;
fn send_unicast(
&self,
nwk: NodeId,
aps: Aps,
payload: Vec<u8>,
) -> impl Future<Output = Result<u16, HostError>> + Send;
fn send_broadcast(
&self,
destination: NodeId,
aps: Aps,
payload: Vec<u8>,
) -> impl Future<Output = Result<(), HostError>> + Send;
fn network_info(&self) -> impl Future<Output = Result<NetworkInfo, HostError>> + Send;
fn backup(&self) -> impl Future<Output = Result<Backup, HostError>> + Send;
fn active_scan(
&self,
channel_mask: u32,
) -> impl Future<Output = Result<Vec<Beacon>, HostError>> + Send;
fn leave(&self) -> impl Future<Output = Result<(), HostError>> + Send;
fn energy_scan(
&self,
channel_mask: u32,
) -> impl Future<Output = Result<Vec<(u8, i8)>, HostError>> + Send;
}