power_manager_integration_test_lib/client_connectors/
system_power_mode_client.rs

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
// Copyright 2022 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 crate::TestEnv;
use {fidl_fuchsia_power_clientlevel as fpowerclient, fidl_fuchsia_power_systemmode as fpowermode};

/// Convenience type for interacting with the Power Manager's client power level service.
pub struct PowerLevelClient {
    watcher_proxy: fpowerclient::WatcherProxy,
}

impl PowerLevelClient {
    pub fn new(test_env: &TestEnv, client_type: &str) -> Self {
        let connector = test_env.connect_to_protocol::<fpowerclient::ConnectorMarker>();
        let (watcher_proxy, watcher_remote) =
            fidl::endpoints::create_proxy::<fpowerclient::WatcherMarker>();
        connector
            .connect(client_type_from_str(client_type), watcher_remote)
            .expect("Failed to connect power client");
        Self { watcher_proxy }
    }

    pub async fn get_power_level(&self) -> u64 {
        self.watcher_proxy.watch().await.expect("Failed to get client power level")
    }
}

/// Convenience type for interacting with the Power Manager's system power mode requester and
/// configurator services.
pub struct SystemModeClient {
    _requester_proxy: fpowermode::RequesterProxy,
    configurator_proxy: fpowermode::ClientConfiguratorProxy,
}

impl SystemModeClient {
    pub fn new(test_env: &TestEnv) -> Self {
        let _requester_proxy = test_env.connect_to_protocol::<fpowermode::RequesterMarker>();
        let configurator_proxy =
            test_env.connect_to_protocol::<fpowermode::ClientConfiguratorMarker>();
        Self { _requester_proxy, configurator_proxy }
    }

    pub async fn set_client_default_power_level(&self, client_type: &str, level: u64) {
        let client_type = client_type_from_str(client_type);
        let mut config = self
            .configurator_proxy
            .get(client_type)
            .await
            .expect("Failed to get client configuration")
            .expect("Missing client configuration");
        config.default_level = level;
        self.configurator_proxy
            .set(client_type, &config)
            .await
            .expect("Failed to set client configuration");
    }
}

fn client_type_from_str(client_type: &str) -> fpowerclient::ClientType {
    match client_type {
        "wlan" => fpowerclient::ClientType::Wlan,
        e => panic!("Invalid client type: {}", e),
    }
}