settings/light/
light_fidl_handler.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
273
274
275
276
277
278
279
280
281
// 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 std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

use fidl::prelude::*;
use fidl_fuchsia_settings::{
    LightError, LightGroup, LightRequest, LightSetLightGroupValuesResponder,
    LightSetLightGroupValuesResult, LightState, LightWatchLightGroupResponder,
    LightWatchLightGroupsResponder,
};
use zx::Status;

use crate::base::{SettingInfo, SettingType};
use crate::handler;
use crate::handler::base::{Request, Response};
use crate::ingress::{request, watch, Scoped};
use crate::job::source::Error as JobError;
use crate::job::Job;
use crate::light::light_controller::ARG_NAME;

impl watch::Responder<Vec<LightGroup>, zx::Status> for LightWatchLightGroupsResponder {
    fn respond(self, response: Result<Vec<LightGroup>, zx::Status>) {
        match response {
            Ok(light_groups) => {
                let _ = self.send(&light_groups);
            }
            Err(error) => {
                self.control_handle().shutdown_with_epitaph(error);
            }
        }
    }
}

impl watch::Responder<Vec<LightGroup>, zx::Status> for IndividualLightGroupResponder {
    fn respond(self, response: Result<Vec<LightGroup>, zx::Status>) {
        let light_group_name = self.light_group_name;
        match response {
            Ok(light_groups) => {
                match light_groups.into_iter().find(|group| {
                    group.name.as_ref().map(|n| *n == light_group_name).unwrap_or(false)
                }) {
                    Some(group) => {
                        let _ = self.responder.send(&group);
                    }
                    None => {
                        // Failed to find the given light group, close the connection.
                        self.responder.control_handle().shutdown_with_epitaph(Status::NOT_FOUND);
                    }
                }
            }
            Err(error) => {
                self.responder.control_handle().shutdown_with_epitaph(error);
            }
        }
    }
}

impl From<SettingInfo> for Vec<LightGroup> {
    fn from(info: SettingInfo) -> Self {
        if let SettingInfo::Light(light_info) = info {
            // Internally we store the data in a HashMap, need to flatten it out into a vector.
            light_info.light_groups.values().cloned().map(LightGroup::from).collect::<Vec<_>>()
        } else {
            panic!("incorrect value sent to light: {info:?}");
        }
    }
}

impl From<Response> for Scoped<LightSetLightGroupValuesResult> {
    fn from(response: Response) -> Self {
        Scoped(response.map(|_| ()).map_err(|e| match e {
            handler::base::Error::InvalidArgument(_, argument, _) => {
                if ARG_NAME == argument {
                    LightError::InvalidName
                } else {
                    LightError::InvalidValue
                }
            }
            _ => LightError::Failed,
        }))
    }
}

impl request::Responder<Scoped<LightSetLightGroupValuesResult>>
    for LightSetLightGroupValuesResponder
{
    fn respond(self, Scoped(response): Scoped<LightSetLightGroupValuesResult>) {
        let _ = self.send(response);
    }
}

impl TryFrom<LightRequest> for Job {
    type Error = JobError;

    fn try_from(item: LightRequest) -> Result<Self, Self::Error> {
        #[allow(unreachable_patterns)]
        match item {
            LightRequest::WatchLightGroups { responder } => {
                Ok(watch::Work::new_job(SettingType::Light, responder))
            }
            LightRequest::WatchLightGroup { name, responder } => {
                let mut hasher = DefaultHasher::new();
                name.hash(&mut hasher);
                let name_clone = name.clone();
                Ok(watch::Work::new_job_with_change_function(
                    SettingType::Light,
                    IndividualLightGroupResponder { responder, light_group_name: name },
                    watch::ChangeFunction::new(
                        hasher.finish(),
                        Box::new(move |old: &SettingInfo, new: &SettingInfo| match (old, new) {
                            (SettingInfo::Light(old_info), SettingInfo::Light(new_info)) => {
                                let old_light_group = old_info.light_groups.get(&name_clone);
                                let new_light_group = new_info.light_groups.get(&name_clone);
                                old_light_group != new_light_group
                            }
                            _ => false,
                        }),
                    ),
                ))
            }
            LightRequest::SetLightGroupValues { name, state, responder } => Ok(request::Work::new(
                SettingType::Light,
                Request::SetLightGroupValue(
                    name,
                    state.into_iter().map(LightState::into).collect::<Vec<_>>(),
                ),
                responder,
            )
            .into()),
            _ => {
                tracing::warn!("Received a call to an unsupported API: {:?}", item);
                Err(JobError::Unsupported)
            }
        }
    }
}
/// Responder that wraps LightWatchLightGroupResponder to filter the vector of light groups down to
/// the single light group the client is watching.
struct IndividualLightGroupResponder {
    responder: LightWatchLightGroupResponder,
    light_group_name: String,
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use fidl_fuchsia_settings::{LightMarker, LightRequestStream};
    use futures::StreamExt;

    use assert_matches::assert_matches;

    use crate::job::{execution, work, Signature};
    use crate::light::types::{LightGroup, LightInfo, LightState, LightType, LightValue};

    use super::*;

    #[fuchsia::test]
    fn test_response_to_vector_empty() {
        let response: Vec<fidl_fuchsia_settings::LightGroup> =
            SettingInfo::into((LightInfo { light_groups: Default::default() }).into());

        assert_eq!(response, vec![]);
    }

    #[fuchsia::test]
    fn test_response_to_vector() {
        let light_group_1 = LightGroup {
            name: "test".to_string(),
            enabled: true,
            light_type: LightType::Simple,
            lights: vec![LightState { value: Some(LightValue::Simple(true)) }],
            hardware_index: vec![],
            disable_conditions: vec![],
        };
        let light_group_2 = LightGroup {
            name: "test2".to_string(),
            enabled: false,
            light_type: LightType::Rgb,
            lights: vec![LightState { value: Some(LightValue::Brightness(0.42)) }],
            hardware_index: vec![],
            disable_conditions: vec![],
        };

        let light_groups: HashMap<_, _> = IntoIterator::into_iter([
            (String::from("test"), light_group_1.clone()),
            (String::from("test2"), light_group_2.clone()),
        ])
        .collect();

        let mut response: Vec<fidl_fuchsia_settings::LightGroup> =
            SettingInfo::into((LightInfo { light_groups }).into());

        // Sort so light groups are in a predictable order.
        response.sort_by_key(|l| l.name.clone());

        assert_eq!(response, vec![light_group_1.into(), light_group_2.into()]);
    }

    // Verify that a WatchLightGroups request is converted into a sequential job.
    #[fuchsia::test(allow_stalls = false)]
    async fn try_from_watch_light_groups_request() {
        // Connect to the Light service and make a watch request.
        let (proxy, server) = fidl::endpoints::create_proxy::<LightMarker>();
        let _fut = proxy.watch_light_groups();
        let mut request_stream: LightRequestStream = server.into_stream();
        let request = request_stream
            .next()
            .await
            .expect("should have an request before stream is closed")
            .expect("should have gotten a request");

        // Verify the request is sequential.
        let job = Job::try_from(request).expect("job conversion should succeed");
        assert_matches!(job.workload(), work::Load::Sequential(_, _));
        assert_matches!(job.execution_type(), execution::Type::Sequential(_));
    }

    /// Converts a WatchLightGroup call with the given light group name to a job and returns the
    /// signature. Also asserts that the created job is sequential.
    ///
    /// This method creates a FIDL proxy to make requests through because directly creating FIDL
    /// request objects is difficult and not intended by the API.
    async fn signature_from_watch_light_group_request(light_name: &str) -> Signature {
        let (proxy, server) = fidl::endpoints::create_proxy::<LightMarker>();
        let _fut = proxy.watch_light_group(light_name);
        let mut request_stream: LightRequestStream = server.into_stream();
        let request = request_stream
            .next()
            .await
            .expect("should have an request before stream is closed")
            .expect("should have gotten a request");
        let job = Job::try_from(request).expect("job conversion should succeed");
        assert_matches!(job.workload(), work::Load::Sequential(_, _));
        assert_matches!(job.execution_type(), execution::Type::Sequential(signature) => signature)
    }

    #[fuchsia::test(allow_stalls = false)]
    async fn try_from_watch_individual_light_group_request() {
        const TEST_LIGHT_NAME: &str = "test_light";

        // Verify that a request is transformed into a sequential job and save the signature.
        let signature = signature_from_watch_light_group_request(TEST_LIGHT_NAME).await;

        // Make another request with the same light group name and save the signature.
        let signature2 = signature_from_watch_light_group_request(TEST_LIGHT_NAME).await;

        // Verify the two requests have the same signature, since they provide the same light group
        // name as input.
        assert_eq!(signature, signature2);

        // Make a request with a different light group name and save the signature.
        let signature3 = signature_from_watch_light_group_request("different_name").await;

        // Verify that the signature of the third request differs from the other two, as it provides
        // a different light group name as input.
        assert_ne!(signature, signature3);
    }

    // Verify that a SetLightGroupValues request is converted into an independent job
    #[fuchsia::test(allow_stalls = false)]
    async fn try_from_set_light_group_values_request() {
        // Connect to the Light service and make a set request.
        let (proxy, server) = fidl::endpoints::create_proxy::<LightMarker>();
        let _fut = proxy.set_light_group_values("arbitrary name", &[]);
        let mut request_stream: LightRequestStream = server.into_stream();
        let request = request_stream
            .next()
            .await
            .expect("should have an request before stream is closed")
            .expect("should have gotten a request");

        // Verify the request is sequential.
        let job = Job::try_from(request).expect("job conversion should succeed");
        assert_matches!(job.workload(), work::Load::Independent(_));
        assert_matches!(job.execution_type(), execution::Type::Independent);
    }
}