sl4f_lib/wpan/
types.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use fidl_fuchsia_lowpan::MacAddress;
6use fidl_fuchsia_lowpan_device::{
7    ConnectivityState as lowpan_ConnectivityState, DeviceState, Role as lowpan_RoleState,
8};
9use fidl_fuchsia_lowpan_test::{
10    MacAddressFilterItem, MacAddressFilterMode, MacAddressFilterSettings, NeighborInfo,
11};
12use serde::{Deserialize, Serialize};
13
14/// Supported Wpan commands.
15pub enum WpanMethod {
16    GetIsCommissioned,
17    GetMacAddressFilterSettings,
18    GetNcpChannel,
19    GetNcpMacAddress,
20    GetNcpRssi,
21    GetNcpDeviceState,
22    GetNcpState,
23    GetNetworkName,
24    GetNeighborTable,
25    GetPanId,
26    GetPartitionId,
27    GetThreadRloc16,
28    GetThreadRouterId,
29    GetWeaveNodeId,
30    InitializeProxies,
31    ReplaceMacAddressFilterSettings,
32}
33
34impl std::str::FromStr for WpanMethod {
35    type Err = anyhow::Error;
36
37    fn from_str(method: &str) -> Result<Self, Self::Err> {
38        match method {
39            "GetIsCommissioned" => Ok(WpanMethod::GetIsCommissioned),
40            "GetMacAddressFilterSettings" => Ok(WpanMethod::GetMacAddressFilterSettings),
41            "GetNcpChannel" => Ok(WpanMethod::GetNcpChannel),
42            "GetNcpMacAddress" => Ok(WpanMethod::GetNcpMacAddress),
43            "GetNcpRssi" => Ok(WpanMethod::GetNcpRssi),
44            "GetNcpDeviceState" => Ok(WpanMethod::GetNcpDeviceState),
45            "GetNcpState" => Ok(WpanMethod::GetNcpState),
46            "GetNeighborTable" => Ok(WpanMethod::GetNeighborTable),
47            "GetNetworkName" => Ok(WpanMethod::GetNetworkName),
48            "GetPanId" => Ok(WpanMethod::GetPanId),
49            "GetPartitionId" => Ok(WpanMethod::GetPartitionId),
50            "GetThreadRloc16" => Ok(WpanMethod::GetThreadRloc16),
51            "GetThreadRouterId" => Ok(WpanMethod::GetThreadRouterId),
52            "GetWeaveNodeId" => Ok(WpanMethod::GetWeaveNodeId),
53            "InitializeProxies" => Ok(WpanMethod::InitializeProxies),
54            "ReplaceMacAddressFilterSettings" => Ok(WpanMethod::ReplaceMacAddressFilterSettings),
55            _ => return Err(format_err!("invalid Wpan FIDL method: {}", method)),
56        }
57    }
58}
59
60#[derive(Serialize)]
61pub enum ConnectivityState {
62    Inactive,
63    Ready,
64    Offline,
65    Attaching,
66    Attached,
67    Isolated,
68    Commissioning,
69    Unknown,
70}
71
72#[derive(Serialize)]
73pub enum RoleState {
74    Detached,
75    Child,
76    Router,
77    Leader,
78    Unknown,
79}
80
81#[derive(Serialize)]
82pub struct DeviceStateDto {
83    pub connectivity_state: Option<ConnectivityState>,
84    pub role: Option<RoleState>,
85}
86
87impl From<DeviceState> for DeviceStateDto {
88    fn from(d: DeviceState) -> Self {
89        DeviceStateDto {
90            connectivity_state: d.connectivity_state.map(|state| state.into()),
91            role: d.role.map(|role| role.into()),
92        }
93    }
94}
95
96impl From<lowpan_RoleState> for RoleState {
97    fn from(value: lowpan_RoleState) -> Self {
98        match value {
99            lowpan_RoleState::Detached => RoleState::Detached,
100            lowpan_RoleState::EndDevice => RoleState::Child,
101            lowpan_RoleState::Router => RoleState::Router,
102            lowpan_RoleState::SleepyEndDevice => RoleState::Child,
103            lowpan_RoleState::SleepyRouter => RoleState::Router,
104            lowpan_RoleState::Leader => RoleState::Leader,
105            lowpan_RoleState::Coordinator => RoleState::Leader,
106            _ => RoleState::Unknown,
107        }
108    }
109}
110
111impl From<lowpan_ConnectivityState> for ConnectivityState {
112    fn from(value: lowpan_ConnectivityState) -> Self {
113        match value {
114            lowpan_ConnectivityState::Inactive => ConnectivityState::Inactive,
115            lowpan_ConnectivityState::Ready => ConnectivityState::Ready,
116            lowpan_ConnectivityState::Offline => ConnectivityState::Offline,
117            lowpan_ConnectivityState::Attaching => ConnectivityState::Attaching,
118            lowpan_ConnectivityState::Attached => ConnectivityState::Attached,
119            lowpan_ConnectivityState::Isolated => ConnectivityState::Isolated,
120            lowpan_ConnectivityState::Commissioning => ConnectivityState::Commissioning,
121            _ => ConnectivityState::Unknown,
122        }
123    }
124}
125
126#[derive(Serialize, Deserialize)]
127pub struct MacAddressFilterItemDto {
128    pub mac_address: Option<[u8; 8]>,
129    pub rssi: Option<i8>,
130}
131
132#[derive(Serialize, Deserialize)]
133pub struct MacAddressFilterSettingsDto {
134    pub items: Option<Vec<MacAddressFilterItemDto>>,
135    pub mode: Option<MacAddressFilterModeDto>,
136}
137
138#[derive(Serialize, Deserialize)]
139pub enum MacAddressFilterModeDto {
140    Disabled = 0,
141    Allow = 1,
142    Deny = 2,
143}
144
145#[derive(Serialize, Deserialize)]
146pub struct NeighborInfoDto {
147    pub mac_address: Option<[u8; 8]>,
148    pub short_address: Option<u16>,
149    pub age: Option<i64>,
150    pub is_child: Option<bool>,
151    pub link_frame_count: Option<u32>,
152    pub mgmt_frame_count: Option<u32>,
153    pub last_rssi_in: Option<i32>,
154    pub avg_rssi_in: Option<i8>,
155    pub lqi_in: Option<u8>,
156    pub thread_mode: Option<u8>,
157}
158
159impl Into<MacAddressFilterItemDto> for MacAddressFilterItem {
160    fn into(self) -> MacAddressFilterItemDto {
161        let mac_address = self.mac_address.map(|addr| addr.octets);
162        MacAddressFilterItemDto { mac_address, rssi: self.rssi }
163    }
164}
165
166impl Into<MacAddressFilterItem> for MacAddressFilterItemDto {
167    fn into(self) -> MacAddressFilterItem {
168        let mac_address = self.mac_address.map(|octets| MacAddress { octets });
169        MacAddressFilterItem { mac_address, rssi: self.rssi, ..Default::default() }
170    }
171}
172
173impl Into<MacAddressFilterSettings> for MacAddressFilterSettingsDto {
174    fn into(self) -> MacAddressFilterSettings {
175        MacAddressFilterSettings {
176            mode: match self.mode {
177                Some(mode) => Some(mode.into()),
178                None => None,
179            },
180            items: match self.items {
181                Some(items) => Some(items.into_iter().map(|x| x.into()).collect()),
182                None => None,
183            },
184            ..Default::default()
185        }
186    }
187}
188
189impl Into<MacAddressFilterSettingsDto> for MacAddressFilterSettings {
190    fn into(self) -> MacAddressFilterSettingsDto {
191        MacAddressFilterSettingsDto {
192            mode: match self.mode {
193                Some(mode) => Some(mode.into()),
194                None => None,
195            },
196            items: match self.items {
197                Some(items) => Some(items.into_iter().map(|x| x.into()).collect()),
198                None => None,
199            },
200        }
201    }
202}
203
204impl Into<MacAddressFilterModeDto> for MacAddressFilterMode {
205    fn into(self) -> MacAddressFilterModeDto {
206        match self {
207            MacAddressFilterMode::Disabled => MacAddressFilterModeDto::Disabled,
208            MacAddressFilterMode::Allow => MacAddressFilterModeDto::Allow,
209            MacAddressFilterMode::Deny => MacAddressFilterModeDto::Deny,
210        }
211    }
212}
213
214impl Into<MacAddressFilterMode> for MacAddressFilterModeDto {
215    fn into(self) -> MacAddressFilterMode {
216        match self {
217            MacAddressFilterModeDto::Disabled => MacAddressFilterMode::Disabled,
218            MacAddressFilterModeDto::Allow => MacAddressFilterMode::Allow,
219            MacAddressFilterModeDto::Deny => MacAddressFilterMode::Deny,
220        }
221    }
222}
223
224impl Into<NeighborInfoDto> for NeighborInfo {
225    fn into(self) -> NeighborInfoDto {
226        let mac_address = self.mac_address.map(|addr| addr.octets);
227        NeighborInfoDto {
228            mac_address,
229            short_address: self.short_address,
230            age: self.age,
231            is_child: self.is_child,
232            link_frame_count: self.link_frame_count,
233            mgmt_frame_count: self.mgmt_frame_count,
234            last_rssi_in: self.last_rssi_in,
235            avg_rssi_in: self.avg_rssi_in,
236            lqi_in: self.lqi_in,
237            thread_mode: self.thread_mode,
238        }
239    }
240}
241
242impl Into<NeighborInfo> for NeighborInfoDto {
243    fn into(self) -> NeighborInfo {
244        let mac_address = self.mac_address.map(|octets| MacAddress { octets });
245        NeighborInfo {
246            mac_address,
247            short_address: self.short_address,
248            age: self.age,
249            is_child: self.is_child,
250            link_frame_count: self.link_frame_count,
251            mgmt_frame_count: self.mgmt_frame_count,
252            last_rssi_in: self.last_rssi_in,
253            avg_rssi_in: self.avg_rssi_in,
254            lqi_in: self.lqi_in,
255            thread_mode: self.thread_mode,
256            ..Default::default()
257        }
258    }
259}