input_pipeline/light_sensor/
led_watcher.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// 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 async_utils::hanging_get::client::HangingGetStream;
use fidl_fuchsia_settings::{LightGroup as LightGroupFidl, LightProxy, LightValue};
use fidl_fuchsia_ui_brightness::ControlProxy;
use fuchsia_async as fasync;
use fuchsia_inspect::Property;
#[cfg(test)]
use futures::channel::mpsc;
use futures::channel::oneshot;
use futures::StreamExt;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

#[derive(Debug, Clone)]
pub struct LightGroup {
    /// Name of the light group.
    name: String,
    /// None brightness implies the light is disabled.
    brightness: Option<f32>,
}

impl LightGroup {
    #[cfg(test)]
    pub(crate) fn new(name: String, brightness: Option<f32>) -> Self {
        Self { name, brightness }
    }

    pub(crate) fn name(&self) -> &String {
        &self.name
    }

    pub(crate) fn brightness(&self) -> Option<f32> {
        self.brightness
    }

    #[cfg(test)]
    pub(crate) fn set_brightness_for_test(&mut self, brightness: Option<f32>) {
        self.brightness = brightness;
    }

    fn map_from_light_groups(light_groups: Vec<LightGroupFidl>) -> HashMap<String, LightGroup> {
        light_groups
            .into_iter()
            .filter_map(|light_group| {
                let enabled = light_group.enabled;
                let lights = light_group.lights;
                light_group.name.and_then(|name| {
                    if enabled.unwrap_or(false) {
                        lights
                            .as_ref()
                            .and_then(|lights| lights.get(0))
                            .and_then(|light| light.value.as_ref())
                            .map(|value| match value {
                                LightValue::On(true) => Some(1.0),
                                LightValue::On(false) => Some(0.0),
                                LightValue::Brightness(b) => Some(*b as f32),
                                LightValue::Color(_) => None,
                            })
                            .map(|brightness| (name.clone(), LightGroup { name, brightness }))
                    } else {
                        Some((name.clone(), LightGroup { name, brightness: None }))
                    }
                })
            })
            .collect()
    }
}

pub struct LedWatcher {
    backlight_brightness: Rc<RefCell<f32>>,
    light_groups: Rc<RefCell<HashMap<String, LightGroup>>>,
    #[cfg(test)]
    update: Option<RefCell<mpsc::Sender<Update>>>,
}

#[cfg(test)]
enum Update {
    Brightness,
    LightGroups,
}

impl LedWatcher {
    /// Create a new `LedWatcher` for the `light_groups` with the supplied calibration. Only the
    /// [LightGroup]s that have a corresponding calibration entry in [Calibration].`leds` will be
    /// tracked.
    pub(crate) fn new(light_groups: Vec<LightGroupFidl>) -> Self {
        Self {
            backlight_brightness: Rc::new(RefCell::new(0.0)),
            light_groups: Rc::new(RefCell::new(LightGroup::map_from_light_groups(light_groups))),
            #[cfg(test)]
            update: None,
        }
    }

    #[cfg(test)]
    fn new_with_sender(light_groups: Vec<LightGroupFidl>, update: mpsc::Sender<Update>) -> Self {
        Self {
            backlight_brightness: Rc::new(RefCell::new(0.0)),
            light_groups: Rc::new(RefCell::new(LightGroup::map_from_light_groups(light_groups))),
            update: Some(RefCell::new(update)),
        }
    }

    /// Spawn a local task to continuously watch for light group changes. Returns a tuple with a
    /// [LedWatcherHandle], which can be used to get the current led states, and a task, which can
    /// be used to track completion of the spawned task.
    pub(crate) fn handle_light_groups_and_brightness_watch(
        self,
        light_proxy: LightProxy,
        brightness_proxy: ControlProxy,
        mut cancelation_rx: oneshot::Receiver<()>,
        light_proxy_receives_initial_response: fuchsia_inspect::BoolProperty,
        brightness_proxy_receives_initial_response: fuchsia_inspect::BoolProperty,
    ) -> (LedWatcherHandle, fasync::Task<()>) {
        let light_groups = Rc::clone(&self.light_groups);
        let backlight_brightness = Rc::clone(&self.backlight_brightness);
        let task = fasync::Task::local(async move {
            let mut light_groups_stream =
                HangingGetStream::new(light_proxy, LightProxy::watch_light_groups);
            let mut brightness_stream =
                HangingGetStream::new(brightness_proxy, ControlProxy::watch_current_brightness);
            let mut light_group_fut = light_groups_stream.select_next_some();
            let mut brightness_fut = brightness_stream.select_next_some();
            loop {
                futures::select! {
                    watch_result = light_group_fut => {
                        light_proxy_receives_initial_response.set(true);
                        match watch_result {
                            Ok(light_groups) => self.update_light_groups(light_groups),
                            Err(e) => tracing::warn!("Failed to get light group update: {:?}", e),
                        }
                        light_group_fut = light_groups_stream.select_next_some()
                    }
                    watch_result = brightness_fut => {
                        brightness_proxy_receives_initial_response.set(true);
                        match watch_result {
                            Ok(brightness) => self.update_brightness(brightness),
                            Err(e) => tracing::warn!("Failed to get brightness update: {:?}", e),
                        }
                        brightness_fut = brightness_stream.select_next_some();
                    }
                    _ = cancelation_rx => break,
                    complete => break,
                }
            }
        });

        (LedWatcherHandle { light_groups, backlight_brightness }, task)
    }

    fn update_light_groups(&self, light_groups: Vec<LightGroupFidl>) {
        *self.light_groups.borrow_mut() = LightGroup::map_from_light_groups(light_groups);
        #[cfg(test)]
        if let Some(sender) = &self.update {
            sender.borrow_mut().try_send(Update::LightGroups).expect("Failed to send update");
        }
    }

    fn update_brightness(&self, brightness: f32) {
        *self.backlight_brightness.borrow_mut() = brightness;
        #[cfg(test)]
        if let Some(sender) = &self.update {
            sender.borrow_mut().try_send(Update::Brightness).expect("Failed to send update");
        }
    }
}

#[derive(Clone, Debug)]
pub struct LedWatcherHandle {
    light_groups: Rc<RefCell<HashMap<String, LightGroup>>>,
    backlight_brightness: Rc<RefCell<f32>>,
}

impl LedState for LedWatcherHandle {
    fn light_groups(&self) -> HashMap<String, LightGroup> {
        Clone::clone(&*self.light_groups.borrow())
    }

    fn backlight_brightness(&self) -> f32 {
        *self.backlight_brightness.borrow()
    }
}

pub trait LedState {
    fn light_groups(&self) -> HashMap<String, LightGroup>;
    fn backlight_brightness(&self) -> f32;
}

pub struct CancelableTask {
    inner: fasync::Task<()>,
    cancelation_tx: oneshot::Sender<()>,
}

impl CancelableTask {
    pub(crate) fn new(cancelation_tx: oneshot::Sender<()>, task: fasync::Task<()>) -> Self {
        Self { cancelation_tx, inner: task }
    }

    /// Detach this task so it continues running independently in the background. The task will
    /// no longer be cancelable.
    pub fn detach(self) {
        self.inner.detach();
    }

    /// Submit a cancelation request and wait for the task to end.
    pub async fn cancel(self) {
        // If the send fails, the watcher has already ended so there's no need to worry about the
        // result.
        let _ = self.cancelation_tx.send(());
        self.inner.await
    }
}

#[cfg(test)]
mod led_watcher_tests;