power_manager_integration_test_lib/client_connectors/
system_power_mode_client.rs

1// Copyright 2022 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 crate::TestEnv;
6use {fidl_fuchsia_power_clientlevel as fpowerclient, fidl_fuchsia_power_systemmode as fpowermode};
7
8/// Convenience type for interacting with the Power Manager's client power level service.
9pub struct PowerLevelClient {
10    watcher_proxy: fpowerclient::WatcherProxy,
11}
12
13impl PowerLevelClient {
14    pub fn new(test_env: &TestEnv, client_type: &str) -> Self {
15        let connector = test_env.connect_to_protocol::<fpowerclient::ConnectorMarker>();
16        let (watcher_proxy, watcher_remote) =
17            fidl::endpoints::create_proxy::<fpowerclient::WatcherMarker>();
18        connector
19            .connect(client_type_from_str(client_type), watcher_remote)
20            .expect("Failed to connect power client");
21        Self { watcher_proxy }
22    }
23
24    pub async fn get_power_level(&self) -> u64 {
25        self.watcher_proxy.watch().await.expect("Failed to get client power level")
26    }
27}
28
29/// Convenience type for interacting with the Power Manager's system power mode requester and
30/// configurator services.
31pub struct SystemModeClient {
32    _requester_proxy: fpowermode::RequesterProxy,
33    configurator_proxy: fpowermode::ClientConfiguratorProxy,
34}
35
36impl SystemModeClient {
37    pub fn new(test_env: &TestEnv) -> Self {
38        let _requester_proxy = test_env.connect_to_protocol::<fpowermode::RequesterMarker>();
39        let configurator_proxy =
40            test_env.connect_to_protocol::<fpowermode::ClientConfiguratorMarker>();
41        Self { _requester_proxy, configurator_proxy }
42    }
43
44    pub async fn set_client_default_power_level(&self, client_type: &str, level: u64) {
45        let client_type = client_type_from_str(client_type);
46        let mut config = self
47            .configurator_proxy
48            .get(client_type)
49            .await
50            .expect("Failed to get client configuration")
51            .expect("Missing client configuration");
52        config.default_level = level;
53        self.configurator_proxy
54            .set(client_type, &config)
55            .await
56            .expect("Failed to set client configuration");
57    }
58}
59
60fn client_type_from_str(client_type: &str) -> fpowerclient::ClientType {
61    match client_type {
62        "wlan" => fpowerclient::ClientType::Wlan,
63        e => panic!("Invalid client type: {}", e),
64    }
65}