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.
45use serde::{Deserialize, Serialize};
67pub const NODE_SEPARATOR: &'static str = "#/@";
8pub const POLICY_STASH_PREFIX: &str = "config";
9/// The StashNode abstraction requires that writing to a StashNode is done as a named field,
10/// so we will store the network config's data under the POLICY_DATA_KEY.
11pub const POLICY_DATA_KEY: &str = "data";
12pub const POLICY_STORAGE_ID: &str = "saved_networks";
1314pub type StashedSsid = Vec<u8>;
1516/// The data that will be stored between reboots of a device. Used to convert the data between JSON
17/// and network config.
18#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
19pub struct PersistentData {
20pub credential: Credential,
21pub has_ever_connected: bool,
22}
2324/// The network identifier is the SSID and security policy of the network, and it is used to
25/// distinguish networks. It mirrors the NetworkIdentifier in fidl_fuchsia_wlan_policy.
26#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
27pub struct NetworkIdentifier {
28pub ssid: StashedSsid,
29pub security_type: SecurityType,
30}
3132/// The security type of a network connection. It mirrors the fidl_fuchsia_wlan_policy SecurityType
33#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
34pub enum SecurityType {
35None,
36 Wep,
37 Wpa,
38 Wpa2,
39 Wpa3,
40}
4142/// The credential of a network connection. It mirrors the fidl_fuchsia_wlan_policy Credential
43#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
44pub enum Credential {
45None,
46 Password(Vec<u8>),
47 Psk(Vec<u8>),
48}
4950/// To deserialize file data into a JSON with a file version and data, a wrapper is needed since
51/// the values of the hashmap must be consistent.
52#[derive(Serialize, Deserialize)]
53#[serde(untagged)]
54pub enum FileContent {
55 Version(u8),
56 Networks(Vec<PersistentStorageData>),
57}
5859/// The data that will be stored between reboots of a device. Used to convert the data between JSON
60/// and network config.
61#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
62pub struct PersistentStorageData {
63pub ssid: StashedSsid,
64pub security_type: SecurityType,
65pub credential: Credential,
66#[serde(default = "has_ever_connected_default")]
67pub has_ever_connected: bool,
68}
6970/// Defines the default value of has_ever_connected in persisted data. This is used so that the
71/// config could be loaded even if this field is missing.
72fn has_ever_connected_default() -> bool {
73false
74}
7576impl PersistentStorageData {
77/// Used when migrating persisted networks from deprecated stash to the new local storage format.
78pub fn new_from_legacy_data(
79 id: NetworkIdentifier,
80 data: PersistentData,
81 ) -> PersistentStorageData {
82 PersistentStorageData {
83 ssid: id.ssid.clone(),
84 security_type: id.security_type,
85 credential: data.credential,
86 has_ever_connected: data.has_ever_connected,
87 }
88 }
89}