zigbee_host/
any_adapter.rs1use tokio::io::{AsyncRead, AsyncWrite};
5use tokio::sync::mpsc;
6use zigbee_primitives::{Ieee, Key, NodeId};
7
8use crate::adapter::{
9 Adapter, AdapterEvent, AdapterInfo, Aps, Beacon, JoinPolicy, NetworkInfo, StartMode,
10 StartOutcome,
11};
12use crate::backup::Backup;
13use crate::deconz_adapter::DeconzAdapter;
14use crate::error::HostError;
15use crate::ezsp_adapter::EzspAdapter;
16use crate::probe::NcpKind;
17use crate::znp_adapter::ZnpAdapter;
18
19#[derive(Debug)]
21#[non_exhaustive]
22pub enum AnyAdapter {
23 Ezsp(EzspAdapter),
25 Znp(ZnpAdapter),
27 Deconz(DeconzAdapter),
29}
30
31impl AnyAdapter {
32 pub async fn connect<T>(
34 io: T,
35 kind: NcpKind,
36 ) -> Result<(AnyAdapter, mpsc::UnboundedReceiver<AdapterEvent>), HostError>
37 where
38 T: AsyncRead + AsyncWrite + Send + 'static,
39 {
40 match kind {
41 NcpKind::Ezsp => {
42 let (a, rx) =
43 EzspAdapter::connect(io, zigbee_ezsp::client::ClientConfig::default()).await?;
44 Ok((AnyAdapter::Ezsp(a), rx))
45 }
46 NcpKind::Znp => {
47 let (a, rx) =
48 ZnpAdapter::connect(io, zigbee_znp::client::ClientConfig::default()).await?;
49 Ok((AnyAdapter::Znp(a), rx))
50 }
51 NcpKind::Deconz => {
52 let (a, rx) =
53 DeconzAdapter::connect(io, zigbee_deconz::client::ClientConfig::default())
54 .await?;
55 Ok((AnyAdapter::Deconz(a), rx))
56 }
57 }
58 }
59
60 pub fn kind(&self) -> NcpKind {
62 match self {
63 AnyAdapter::Ezsp(_) => NcpKind::Ezsp,
64 AnyAdapter::Znp(_) => NcpKind::Znp,
65 AnyAdapter::Deconz(_) => NcpKind::Deconz,
66 }
67 }
68}
69
70macro_rules! each {
71 ($self:ident, $a:ident => $e:expr) => {
72 match $self {
73 AnyAdapter::Ezsp($a) => $e,
74 AnyAdapter::Znp($a) => $e,
75 AnyAdapter::Deconz($a) => $e,
76 }
77 };
78}
79
80impl Adapter for AnyAdapter {
81 fn info(&self) -> AdapterInfo {
82 each!(self, a => a.info())
83 }
84
85 async fn start(&self, mode: StartMode) -> Result<(StartOutcome, NetworkInfo), HostError> {
86 each!(self, a => a.start(mode).await)
87 }
88
89 async fn permit_join(&self, seconds: u8, policy: &JoinPolicy) -> Result<(), HostError> {
90 each!(self, a => a.permit_join(seconds, policy).await)
91 }
92
93 async fn add_join_key(&self, ieee: Ieee, key: Key) -> Result<(), HostError> {
94 each!(self, a => a.add_join_key(ieee, key).await)
95 }
96
97 async fn send_unicast(
98 &self,
99 nwk: NodeId,
100 aps: Aps,
101 payload: Vec<u8>,
102 ) -> Result<u16, HostError> {
103 each!(self, a => a.send_unicast(nwk, aps, payload).await)
104 }
105
106 async fn send_broadcast(
107 &self,
108 destination: NodeId,
109 aps: Aps,
110 payload: Vec<u8>,
111 ) -> Result<(), HostError> {
112 each!(self, a => a.send_broadcast(destination, aps, payload).await)
113 }
114
115 async fn network_info(&self) -> Result<NetworkInfo, HostError> {
116 each!(self, a => a.network_info().await)
117 }
118
119 async fn backup(&self) -> Result<Backup, HostError> {
120 each!(self, a => a.backup().await)
121 }
122
123 async fn active_scan(&self, channel_mask: u32) -> Result<Vec<Beacon>, HostError> {
124 each!(self, a => a.active_scan(channel_mask).await)
125 }
126
127 async fn leave(&self) -> Result<(), HostError> {
128 each!(self, a => a.leave().await)
129 }
130
131 async fn energy_scan(&self, channel_mask: u32) -> Result<Vec<(u8, i8)>, HostError> {
132 each!(self, a => a.energy_scan(channel_mask).await)
133 }
134}