1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use fidl_fuchsia_lowpan::MacAddress;
use fidl_fuchsia_lowpan_device::ConnectivityState as lowpan_ConnectivityState;
use fidl_fuchsia_lowpan_device::DeviceState;
use fidl_fuchsia_lowpan_device::Role as lowpan_RoleState;
use fidl_fuchsia_lowpan_test::{
    MacAddressFilterItem, MacAddressFilterMode, MacAddressFilterSettings, NeighborInfo,
};
use serde::{Deserialize, Serialize};

/// Supported Wpan commands.
pub enum WpanMethod {
    GetIsCommissioned,
    GetMacAddressFilterSettings,
    GetNcpChannel,
    GetNcpMacAddress,
    GetNcpRssi,
    GetNcpDeviceState,
    GetNcpState,
    GetNetworkName,
    GetNeighborTable,
    GetPanId,
    GetPartitionId,
    GetThreadRloc16,
    GetThreadRouterId,
    GetWeaveNodeId,
    InitializeProxies,
    ReplaceMacAddressFilterSettings,
}

impl std::str::FromStr for WpanMethod {
    type Err = anyhow::Error;

    fn from_str(method: &str) -> Result<Self, Self::Err> {
        match method {
            "GetIsCommissioned" => Ok(WpanMethod::GetIsCommissioned),
            "GetMacAddressFilterSettings" => Ok(WpanMethod::GetMacAddressFilterSettings),
            "GetNcpChannel" => Ok(WpanMethod::GetNcpChannel),
            "GetNcpMacAddress" => Ok(WpanMethod::GetNcpMacAddress),
            "GetNcpRssi" => Ok(WpanMethod::GetNcpRssi),
            "GetNcpDeviceState" => Ok(WpanMethod::GetNcpDeviceState),
            "GetNcpState" => Ok(WpanMethod::GetNcpState),
            "GetNeighborTable" => Ok(WpanMethod::GetNeighborTable),
            "GetNetworkName" => Ok(WpanMethod::GetNetworkName),
            "GetPanId" => Ok(WpanMethod::GetPanId),
            "GetPartitionId" => Ok(WpanMethod::GetPartitionId),
            "GetThreadRloc16" => Ok(WpanMethod::GetThreadRloc16),
            "GetThreadRouterId" => Ok(WpanMethod::GetThreadRouterId),
            "GetWeaveNodeId" => Ok(WpanMethod::GetWeaveNodeId),
            "InitializeProxies" => Ok(WpanMethod::InitializeProxies),
            "ReplaceMacAddressFilterSettings" => Ok(WpanMethod::ReplaceMacAddressFilterSettings),
            _ => return Err(format_err!("invalid Wpan FIDL method: {}", method)),
        }
    }
}

#[derive(Serialize)]
pub enum ConnectivityState {
    Inactive,
    Ready,
    Offline,
    Attaching,
    Attached,
    Isolated,
    Commissioning,
    Unknown,
}

#[derive(Serialize)]
pub enum RoleState {
    Detached,
    Child,
    Router,
    Leader,
    Unknown,
}

#[derive(Serialize)]
pub struct DeviceStateDto {
    pub connectivity_state: Option<ConnectivityState>,
    pub role: Option<RoleState>,
}

impl From<DeviceState> for DeviceStateDto {
    fn from(d: DeviceState) -> Self {
        DeviceStateDto {
            connectivity_state: d.connectivity_state.map(|state| state.into()),
            role: d.role.map(|role| role.into()),
        }
    }
}

impl From<lowpan_RoleState> for RoleState {
    fn from(value: lowpan_RoleState) -> Self {
        match value {
            lowpan_RoleState::Detached => RoleState::Detached,
            lowpan_RoleState::EndDevice => RoleState::Child,
            lowpan_RoleState::Router => RoleState::Router,
            lowpan_RoleState::SleepyEndDevice => RoleState::Child,
            lowpan_RoleState::SleepyRouter => RoleState::Router,
            lowpan_RoleState::Leader => RoleState::Leader,
            lowpan_RoleState::Coordinator => RoleState::Leader,
            _ => RoleState::Unknown,
        }
    }
}

impl From<lowpan_ConnectivityState> for ConnectivityState {
    fn from(value: lowpan_ConnectivityState) -> Self {
        match value {
            lowpan_ConnectivityState::Inactive => ConnectivityState::Inactive,
            lowpan_ConnectivityState::Ready => ConnectivityState::Ready,
            lowpan_ConnectivityState::Offline => ConnectivityState::Offline,
            lowpan_ConnectivityState::Attaching => ConnectivityState::Attaching,
            lowpan_ConnectivityState::Attached => ConnectivityState::Attached,
            lowpan_ConnectivityState::Isolated => ConnectivityState::Isolated,
            lowpan_ConnectivityState::Commissioning => ConnectivityState::Commissioning,
            _ => ConnectivityState::Unknown,
        }
    }
}

#[derive(Serialize, Deserialize)]
pub struct MacAddressFilterItemDto {
    pub mac_address: Option<[u8; 8]>,
    pub rssi: Option<i8>,
}

#[derive(Serialize, Deserialize)]
pub struct MacAddressFilterSettingsDto {
    pub items: Option<Vec<MacAddressFilterItemDto>>,
    pub mode: Option<MacAddressFilterModeDto>,
}

#[derive(Serialize, Deserialize)]
pub enum MacAddressFilterModeDto {
    Disabled = 0,
    Allow = 1,
    Deny = 2,
}

#[derive(Serialize, Deserialize)]
pub struct NeighborInfoDto {
    pub mac_address: Option<[u8; 8]>,
    pub short_address: Option<u16>,
    pub age: Option<i64>,
    pub is_child: Option<bool>,
    pub link_frame_count: Option<u32>,
    pub mgmt_frame_count: Option<u32>,
    pub last_rssi_in: Option<i32>,
    pub avg_rssi_in: Option<i8>,
    pub lqi_in: Option<u8>,
    pub thread_mode: Option<u8>,
}

impl Into<MacAddressFilterItemDto> for MacAddressFilterItem {
    fn into(self) -> MacAddressFilterItemDto {
        let mac_address = self.mac_address.map(|addr| addr.octets);
        MacAddressFilterItemDto { mac_address, rssi: self.rssi }
    }
}

impl Into<MacAddressFilterItem> for MacAddressFilterItemDto {
    fn into(self) -> MacAddressFilterItem {
        let mac_address = self.mac_address.map(|octets| MacAddress { octets });
        MacAddressFilterItem { mac_address, rssi: self.rssi, ..Default::default() }
    }
}

impl Into<MacAddressFilterSettings> for MacAddressFilterSettingsDto {
    fn into(self) -> MacAddressFilterSettings {
        MacAddressFilterSettings {
            mode: match self.mode {
                Some(mode) => Some(mode.into()),
                None => None,
            },
            items: match self.items {
                Some(items) => Some(items.into_iter().map(|x| x.into()).collect()),
                None => None,
            },
            ..Default::default()
        }
    }
}

impl Into<MacAddressFilterSettingsDto> for MacAddressFilterSettings {
    fn into(self) -> MacAddressFilterSettingsDto {
        MacAddressFilterSettingsDto {
            mode: match self.mode {
                Some(mode) => Some(mode.into()),
                None => None,
            },
            items: match self.items {
                Some(items) => Some(items.into_iter().map(|x| x.into()).collect()),
                None => None,
            },
        }
    }
}

impl Into<MacAddressFilterModeDto> for MacAddressFilterMode {
    fn into(self) -> MacAddressFilterModeDto {
        match self {
            MacAddressFilterMode::Disabled => MacAddressFilterModeDto::Disabled,
            MacAddressFilterMode::Allow => MacAddressFilterModeDto::Allow,
            MacAddressFilterMode::Deny => MacAddressFilterModeDto::Deny,
        }
    }
}

impl Into<MacAddressFilterMode> for MacAddressFilterModeDto {
    fn into(self) -> MacAddressFilterMode {
        match self {
            MacAddressFilterModeDto::Disabled => MacAddressFilterMode::Disabled,
            MacAddressFilterModeDto::Allow => MacAddressFilterMode::Allow,
            MacAddressFilterModeDto::Deny => MacAddressFilterMode::Deny,
        }
    }
}

impl Into<NeighborInfoDto> for NeighborInfo {
    fn into(self) -> NeighborInfoDto {
        let mac_address = self.mac_address.map(|addr| addr.octets);
        NeighborInfoDto {
            mac_address,
            short_address: self.short_address,
            age: self.age,
            is_child: self.is_child,
            link_frame_count: self.link_frame_count,
            mgmt_frame_count: self.mgmt_frame_count,
            last_rssi_in: self.last_rssi_in,
            avg_rssi_in: self.avg_rssi_in,
            lqi_in: self.lqi_in,
            thread_mode: self.thread_mode,
        }
    }
}

impl Into<NeighborInfo> for NeighborInfoDto {
    fn into(self) -> NeighborInfo {
        let mac_address = self.mac_address.map(|octets| MacAddress { octets });
        NeighborInfo {
            mac_address,
            short_address: self.short_address,
            age: self.age,
            is_child: self.is_child,
            link_frame_count: self.link_frame_count,
            mgmt_frame_count: self.mgmt_frame_count,
            last_rssi_in: self.last_rssi_in,
            avg_rssi_in: self.avg_rssi_in,
            lqi_in: self.lqi_in,
            thread_mode: self.thread_mode,
            ..Default::default()
        }
    }
}