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
// 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 serde::{Deserialize, Serialize};

/// Supported Weave commands.
pub enum WeaveMethod {
    GetPairingCode,
    GetPairingState,
    GetQrCode,
    ResetConfig,
}

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

    fn from_str(method: &str) -> Result<Self, Self::Err> {
        match method {
            "GetPairingCode" => Ok(WeaveMethod::GetPairingCode),
            "GetPairingState" => Ok(WeaveMethod::GetPairingState),
            "GetQrCode" => Ok(WeaveMethod::GetQrCode),
            "ResetConfig" => Ok(WeaveMethod::ResetConfig),
            _ => return Err(format_err!("invalid Weave FIDL method: {}", method)),
        }
    }
}

#[derive(Serialize, PartialEq, Copy, Clone, Debug)]
pub struct PairingState {
    /// Has Weave been fully provisioned? This implies that all provisioning
    /// has been completed as expected as specified in the configuration.
    pub is_weave_fully_provisioned: Option<bool>,
    /// Has WiFi been provisioned? Defaults to false.
    pub is_wlan_provisioned: Option<bool>,
    /// Has Thread been provisioned? Defaults to false.
    pub is_thread_provisioned: Option<bool>,
    /// Has the fabric been provisioned? Defaults to false.
    pub is_fabric_provisioned: Option<bool>,
    /// Has the service been provisioned? Defaults to false.
    pub is_service_provisioned: Option<bool>,
}

#[derive(Deserialize, PartialEq, Copy, Clone, Debug)]
pub struct ResetConfig {
    /// Reset network configuration information.
    pub network_config: Option<bool>,
    /// Reset fabric configuration information.
    pub fabric_config: Option<bool>,
    /// Reset service configuration information.
    pub service_config: Option<bool>,
    /// Reset device operational credentials.
    pub operational_credentials: Option<bool>,
}

impl From<PairingState> for fidl_fuchsia_weave::PairingState {
    fn from(item: PairingState) -> Self {
        fidl_fuchsia_weave::PairingState {
            is_weave_fully_provisioned: item.is_weave_fully_provisioned,
            is_wlan_provisioned: item.is_wlan_provisioned,
            is_thread_provisioned: item.is_thread_provisioned,
            is_fabric_provisioned: item.is_fabric_provisioned,
            is_service_provisioned: item.is_service_provisioned,
            ..Default::default()
        }
    }
}

impl From<fidl_fuchsia_weave::PairingState> for PairingState {
    fn from(item: fidl_fuchsia_weave::PairingState) -> Self {
        PairingState {
            is_weave_fully_provisioned: item.is_weave_fully_provisioned,
            is_wlan_provisioned: item.is_wlan_provisioned,
            is_thread_provisioned: item.is_thread_provisioned,
            is_fabric_provisioned: item.is_fabric_provisioned,
            is_service_provisioned: item.is_service_provisioned,
        }
    }
}

impl From<ResetConfig> for fidl_fuchsia_weave::ResetConfigFlags {
    fn from(item: ResetConfig) -> Self {
        let mut flags: fidl_fuchsia_weave::ResetConfigFlags =
            fidl_fuchsia_weave::ResetConfigFlags::empty();
        flags.set(
            fidl_fuchsia_weave::ResetConfigFlags::NETWORK_CONFIG,
            item.network_config.unwrap_or(false),
        );
        flags.set(
            fidl_fuchsia_weave::ResetConfigFlags::FABRIC_CONFIG,
            item.fabric_config.unwrap_or(false),
        );
        flags.set(
            fidl_fuchsia_weave::ResetConfigFlags::SERVICE_CONFIG,
            item.service_config.unwrap_or(false),
        );
        flags.set(
            fidl_fuchsia_weave::ResetConfigFlags::OPERATIONAL_CREDENTIALS,
            item.operational_credentials.unwrap_or(false),
        );
        flags
    }
}