settings/light/
types.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright 2020 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::light::light_hardware_configuration::DisableConditions;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(PartialEq, Default, Debug, Clone, Serialize, Deserialize)]
pub struct LightInfo {
    pub light_groups: HashMap<String, LightGroup>,
}

/// Internal representation of a light group.
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct LightGroup {
    pub name: String,
    pub enabled: bool,
    pub light_type: LightType,
    pub lights: Vec<LightState>,

    /// Each light in the underlying fuchsia.hardware.light API has a unique, fixed index. We need
    /// to remember the index of the lights in this light group in order to write values back.
    pub hardware_index: Vec<u32>,

    /// A list of conditions under which the "enabled" field of the light group should be false,
    /// which signals to clients the light's state is being overridden by external conditions, such
    /// as an LED dedicated to showing that a device's mic is muted that is off when the mic is not
    /// muted.
    ///
    /// Lights that are disabled can still have their value set, but the changes may not be
    /// noticeable to the user until the condition disabling/overriding ends.
    pub disable_conditions: Vec<DisableConditions>,
}

impl From<LightGroup> for fidl_fuchsia_settings::LightGroup {
    fn from(src: LightGroup) -> Self {
        fidl_fuchsia_settings::LightGroup {
            name: Some(src.name),
            enabled: Some(src.enabled),
            type_: Some(src.light_type.into()),
            lights: Some(src.lights.into_iter().map(LightState::into).collect()),
            ..Default::default()
        }
    }
}

impl From<fidl_fuchsia_settings::LightGroup> for LightGroup {
    fn from(src: fidl_fuchsia_settings::LightGroup) -> Self {
        LightGroup {
            name: src.name.unwrap(),
            enabled: src.enabled.unwrap(),
            light_type: src.type_.unwrap().into(),
            lights: src.lights.unwrap().into_iter().map(LightState::from).collect(),
            // These are not used in storage, but will be filled out by the controller.
            hardware_index: vec![],
            disable_conditions: vec![],
        }
    }
}

#[derive(PartialEq, Eq, Debug, Copy, Clone, Serialize, Deserialize)]
pub enum LightType {
    Brightness,
    Rgb,
    Simple,
}

impl From<fidl_fuchsia_settings::LightType> for LightType {
    fn from(src: fidl_fuchsia_settings::LightType) -> Self {
        match src {
            fidl_fuchsia_settings::LightType::Brightness => LightType::Brightness,
            fidl_fuchsia_settings::LightType::Rgb => LightType::Rgb,
            fidl_fuchsia_settings::LightType::Simple => LightType::Simple,
        }
    }
}

impl From<LightType> for fidl_fuchsia_settings::LightType {
    fn from(src: LightType) -> Self {
        match src {
            LightType::Brightness => fidl_fuchsia_settings::LightType::Brightness,
            LightType::Rgb => fidl_fuchsia_settings::LightType::Rgb,
            LightType::Simple => fidl_fuchsia_settings::LightType::Simple,
        }
    }
}

/// Converts between a Capability and a LightType for convenience for tests.
impl From<fidl_fuchsia_hardware_light::Capability> for LightType {
    fn from(src: fidl_fuchsia_hardware_light::Capability) -> Self {
        match src {
            fidl_fuchsia_hardware_light::Capability::Brightness => LightType::Brightness,
            fidl_fuchsia_hardware_light::Capability::Rgb => LightType::Rgb,
            fidl_fuchsia_hardware_light::Capability::Simple => LightType::Simple,
        }
    }
}

/// Converts between a LightType and a Capability for convenience for tests.
impl From<LightType> for fidl_fuchsia_hardware_light::Capability {
    fn from(src: LightType) -> Self {
        match src {
            LightType::Brightness => fidl_fuchsia_hardware_light::Capability::Brightness,
            LightType::Rgb => fidl_fuchsia_hardware_light::Capability::Rgb,
            LightType::Simple => fidl_fuchsia_hardware_light::Capability::Simple,
        }
    }
}

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct LightState {
    pub value: Option<LightValue>,
}

impl LightState {
    pub(crate) fn is_finite(&self) -> bool {
        (self.value).as_ref().map_or(true, |val| val.is_finite())
    }
}

impl From<fidl_fuchsia_settings::LightState> for LightState {
    fn from(src: fidl_fuchsia_settings::LightState) -> Self {
        LightState { value: src.value.map(LightValue::from) }
    }
}

impl From<LightState> for fidl_fuchsia_settings::LightState {
    fn from(src: LightState) -> Self {
        fidl_fuchsia_settings::LightState {
            value: src.value.map(fidl_fuchsia_settings::LightValue::from),
            ..Default::default()
        }
    }
}

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub enum LightValue {
    Brightness(f64),
    Rgb(ColorRgb),
    Simple(bool),
}

impl LightValue {
    pub(crate) fn is_finite(&self) -> bool {
        match self {
            LightValue::Brightness(brightness) => brightness.is_finite(),
            LightValue::Rgb(color_rgb) => color_rgb.is_finite(),
            LightValue::Simple(_) => true,
        }
    }
}

impl From<fidl_fuchsia_settings::LightValue> for LightValue {
    fn from(src: fidl_fuchsia_settings::LightValue) -> Self {
        match src {
            fidl_fuchsia_settings::LightValue::On(on) => LightValue::Simple(on),
            fidl_fuchsia_settings::LightValue::Brightness(brightness) => {
                LightValue::Brightness(brightness)
            }
            fidl_fuchsia_settings::LightValue::Color(color) => LightValue::Rgb(color.into()),
        }
    }
}

impl From<fidl_fuchsia_hardware_light::Rgb> for LightValue {
    fn from(src: fidl_fuchsia_hardware_light::Rgb) -> Self {
        LightValue::Rgb(ColorRgb {
            red: src.red as f32,
            green: src.green as f32,
            blue: src.blue as f32,
        })
    }
}

impl From<LightValue> for fidl_fuchsia_settings::LightValue {
    fn from(src: LightValue) -> Self {
        match src {
            LightValue::Simple(on) => fidl_fuchsia_settings::LightValue::On(on),
            LightValue::Brightness(brightness) => {
                fidl_fuchsia_settings::LightValue::Brightness(brightness)
            }
            LightValue::Rgb(color) => fidl_fuchsia_settings::LightValue::Color(color.into()),
        }
    }
}

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct ColorRgb {
    pub red: f32,
    pub green: f32,
    pub blue: f32,
}

impl ColorRgb {
    pub(crate) fn is_finite(&self) -> bool {
        self.red.is_finite() && self.green.is_finite() && self.blue.is_finite()
    }
}

impl From<fidl_fuchsia_ui_types::ColorRgb> for ColorRgb {
    fn from(src: fidl_fuchsia_ui_types::ColorRgb) -> Self {
        ColorRgb { red: src.red, green: src.green, blue: src.blue }
    }
}

impl From<ColorRgb> for fidl_fuchsia_ui_types::ColorRgb {
    fn from(src: ColorRgb) -> Self {
        fidl_fuchsia_ui_types::ColorRgb { red: src.red, green: src.green, blue: src.blue }
    }
}

/// Converts between internal RGB representation and underlying fuchsia.hardware.light
/// representation.
impl TryFrom<ColorRgb> for fidl_fuchsia_hardware_light::Rgb {
    type Error = &'static str;
    fn try_from(src: ColorRgb) -> Result<Self, Self::Error> {
        if src.red > 1.0
            || src.green > 1.0
            || src.blue > 1.0
            || src.red < 0.0
            || src.green < 0.0
            || src.blue < 0.0
        {
            return Err("values must be between 0.0 and 1.0 inclusive");
        }

        Ok(fidl_fuchsia_hardware_light::Rgb {
            red: src.red as f64,
            green: src.green as f64,
            blue: src.blue as f64,
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::light::types::ColorRgb;

    #[fuchsia::test]
    fn test_try_from_rgb() {
        assert!(fidl_fuchsia_hardware_light::Rgb::try_from(ColorRgb {
            red: -0.0,
            green: 0.1,
            blue: 1.0
        })
        .is_ok());

        assert!(fidl_fuchsia_hardware_light::Rgb::try_from(ColorRgb {
            red: 0.0 - f32::EPSILON,
            green: 0.1,
            blue: 0.2
        })
        .is_err());

        assert!(fidl_fuchsia_hardware_light::Rgb::try_from(ColorRgb {
            red: 0.3,
            green: 1.0 + f32::EPSILON,
            blue: 0.2
        })
        .is_err());

        assert_eq!(
            fidl_fuchsia_hardware_light::Rgb::try_from(ColorRgb {
                red: 0.0,
                green: 1.0,
                blue: 0.5
            }),
            Ok(fidl_fuchsia_hardware_light::Rgb { red: 0.0, green: 1.0, blue: 0.5 })
        );
    }
}