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.
45use crate::TestEnv;
6use futures::channel::mpsc;
7use futures::StreamExt;
8use log::*;
9use {fidl_fuchsia_power_profile as fprofile, fuchsia_async as fasync};
1011/// Convenience type for interacting with the Power Manager's power profile service.
12pub struct PowerProfileClient {
13 _watcher_task: fasync::Task<()>,
14 profile_receiver: mpsc::Receiver<fprofile::Profile>,
15}
1617impl PowerProfileClient {
18pub async fn new(test_env: &TestEnv) -> Self {
19let (mut profile_sender, profile_receiver) = mpsc::channel(1);
20let proxy = test_env.connect_to_protocol::<fprofile::WatcherMarker>();
2122let _watcher_task = fasync::Task::local(async move {
23while let Ok(profile) = proxy.watch().await {
24info!("Received power profile: {:?}", profile);
25 profile_sender.try_send(profile).expect("Failed to notify power profile change");
26 }
27 });
2829Self { _watcher_task, profile_receiver }
30 }
3132/// Returns the next power profile that the watcher has received, or hangs until one is
33 /// received.
34pub async fn get_power_profile(&mut self) -> fprofile::Profile {
35self.profile_receiver.next().await.expect("Failed to wait for power profile")
36 }
37}