sl4f_lib/setui/
facade.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// Copyright 2019 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 anyhow::{format_err, Error};

use serde_json::{from_value, to_value, Value};

use crate::setui::types::{IntlInfo, MicStates, NetworkType, SetUiResult};
use fidl_fuchsia_media::AudioRenderUsage;
use fidl_fuchsia_settings::{
    self as fsettings, AudioMarker, AudioStreamSettingSource, AudioStreamSettings,
    ConfigurationInterfaces, DeviceState, DisplayMarker, DisplaySettings, InputMarker, InputState,
    IntlMarker, SetupMarker, SetupSettings, Volume,
};
use fuchsia_component::client::connect_to_protocol;
use tracing::info;

/// Facade providing access to SetUi interfaces.
#[derive(Debug)]
pub struct SetUiFacade {
    /// Audio proxy that may be optionally provided for testing. The proxy is not cached during
    /// normal operation.
    audio_proxy: Option<fsettings::AudioProxy>,

    /// Optional Display proxy for testing, similar to `audio_proxy`.
    display_proxy: Option<fsettings::DisplayProxy>,

    /// Optional Input proxy for testing, similar to `audio_proxy`.
    input_proxy: Option<fsettings::InputProxy>,
}

impl SetUiFacade {
    pub fn new() -> SetUiFacade {
        SetUiFacade { audio_proxy: None, display_proxy: None, input_proxy: None }
    }

    /// Sets network option used by device setup.
    /// Same action as choosing "Setup over Ethernet [enabled|disabled]" in "Developer options"
    ///
    /// args: accepted args are "ethernet" or "wifi". ex: {"params": "ethernet"}
    pub async fn set_network(&self, args: Value) -> Result<Value, Error> {
        let network_type: NetworkType = from_value(args)?;
        info!("set_network input {:?}", network_type);
        let setup_service_proxy = match connect_to_protocol::<SetupMarker>() {
            Ok(proxy) => proxy,
            Err(e) => bail!("Failed to connect to Setup service {:?}.", e),
        };

        let mut settings = SetupSettings::default();

        match network_type {
            NetworkType::Ethernet => {
                settings.enabled_configuration_interfaces = Some(ConfigurationInterfaces::ETHERNET);
            }
            NetworkType::Wifi => {
                settings.enabled_configuration_interfaces = Some(ConfigurationInterfaces::WIFI);
            }
            _ => return Err(format_err!("Network type must either be ethernet or wifi.")),
        }
        // Update network configuration without automatic device reboot.
        // For changes to take effect, either restart basemgr component or reboot device.
        match setup_service_proxy.set(&settings, false).await? {
            Ok(_) => Ok(to_value(SetUiResult::Success)?),
            Err(err) => Err(format_err!("Update network settings failed with err {:?}", err)),
        }
    }

    /// Reports the network option used for setup
    ///
    /// Returns either "ethernet", "wifi" or "unknown".
    pub async fn get_network_setting(&self) -> Result<Value, Error> {
        let setup_service_proxy = match connect_to_protocol::<SetupMarker>() {
            Ok(proxy) => proxy,
            Err(e) => bail!("Failed to connect to Setup service {:?}.", e),
        };
        let setting = setup_service_proxy.watch().await?;
        match setting.enabled_configuration_interfaces {
            Some(ConfigurationInterfaces::ETHERNET) => Ok(to_value(NetworkType::Ethernet)?),
            Some(ConfigurationInterfaces::WIFI) => Ok(to_value(NetworkType::Wifi)?),
            _ => Ok(to_value(NetworkType::Unknown)?),
        }
    }

    /// Sets Internationalization values.
    ///
    /// Input should is expected to be type IntlInfo in json.
    /// Example:
    /// {
    ///     "hour_cycle":"H12",
    ///     "locales":[{"id":"he-FR"}],
    ///     "temperature_unit":"Celsius",
    ///     "time_zone_id":"UTC"
    /// }
    pub async fn set_intl_setting(&self, args: Value) -> Result<Value, Error> {
        let intl_info: IntlInfo = from_value(args)?;
        info!("Received Intl Settings Request {:?}", intl_info);

        let intl_service_proxy = match connect_to_protocol::<IntlMarker>() {
            Ok(proxy) => proxy,
            Err(e) => bail!("Failed to connect to Intl service {:?}.", e),
        };
        match intl_service_proxy.set(&intl_info.into()).await? {
            Ok(_) => Ok(to_value(SetUiResult::Success)?),
            Err(err) => Err(format_err!("Update intl settings failed with err {:?}", err)),
        }
    }

    /// Reads the Internationalization setting.
    ///
    /// Returns IntlInfo in json.
    pub async fn get_intl_setting(&self) -> Result<Value, Error> {
        let intl_service_proxy = match connect_to_protocol::<IntlMarker>() {
            Ok(proxy) => proxy,
            Err(e) => bail!("Failed to connect to Intl service {:?}.", e),
        };
        let intl_info: IntlInfo = intl_service_proxy.watch().await?.into();
        return Ok(to_value(&intl_info)?);
    }

    /// Reports the microphone DeviceState.
    ///
    /// Returns true if mic is muted or false if mic is unmuted.
    pub async fn is_mic_muted(&self) -> Result<Value, Error> {
        let input_proxy = match self.input_proxy.as_ref() {
            Some(proxy) => proxy.clone(),
            None => match connect_to_protocol::<InputMarker>() {
                Ok(proxy) => proxy,
                Err(e) => bail!("isMicMuted - failed to connect to Input service {:?}.", e),
            },
        };

        match input_proxy.watch().await?.devices {
            Some(input_device) => {
                let mut muted = false;
                if let Some(device) = input_device
                    .into_iter()
                    .find(|device| device.device_type == Some(fsettings::DeviceType::Microphone))
                {
                    match device.state {
                        Some(state) => {
                            muted = state.toggle_flags == Some(fsettings::ToggleStateFlags::MUTED);
                        }
                        _ => (),
                    }
                    return Ok(to_value(muted)?);
                } else {
                    // There is no microphone on device, always return unmuted
                    return Ok(to_value(false)?);
                }
            }
            _ => Err(format_err!("isMicMuted - cannot read input state.")),
        }
    }

    /// Sets the display brightness via `fuchsia.settings.Display.Set`.
    ///
    /// # Arguments
    /// * `args`: JSON value containing the desired brightness level as f32.
    pub async fn set_brightness(&self, args: Value) -> Result<Value, Error> {
        let brightness: f32 = from_value(args)?;

        // Use the test proxy if one was provided, otherwise connect to the discoverable Display
        // service.
        let display_proxy = match self.display_proxy.as_ref() {
            Some(proxy) => proxy.clone(),
            None => match connect_to_protocol::<DisplayMarker>() {
                Ok(proxy) => proxy,
                Err(e) => bail!("Failed to connect to Display service {:?}.", e),
            },
        };

        let settings = DisplaySettings {
            auto_brightness: Some(false),
            brightness_value: Some(brightness),
            ..Default::default()
        };
        match display_proxy.set(&settings).await? {
            Ok(_) => Ok(to_value(SetUiResult::Success)?),
            Err(e) => Err(format_err!("SetBrightness failed with err {:?}", e)),
        }
    }

    /// Sets the media volume level via `fuchsia.settings.Audio.Set`.
    ///
    /// # Arguments
    /// * `args`: JSON value containing the desired volume level as f32.
    pub async fn set_media_volume(&self, args: Value) -> Result<Value, Error> {
        let volume: f32 = from_value(args)?;

        // Use the test proxy if one was provided, otherwise connect to the discoverable Audio
        // service.
        let audio_proxy = match self.audio_proxy.as_ref() {
            Some(proxy) => proxy.clone(),
            None => match connect_to_protocol::<AudioMarker>() {
                Ok(proxy) => proxy,
                Err(e) => bail!("Failed to connect to Display service {:?}.", e),
            },
        };

        let stream_settings = AudioStreamSettings {
            stream: Some(AudioRenderUsage::Media),
            source: Some(AudioStreamSettingSource::User),
            user_volume: Some(Volume {
                level: Some(volume),
                muted: Some(false),
                ..Default::default()
            }),
            ..Default::default()
        };
        let settings =
            fsettings::AudioSettings { streams: Some(vec![stream_settings]), ..Default::default() };

        info!("Setting audio settings {:?}", settings);
        match audio_proxy.set(&settings).await? {
            Ok(_) => Ok(to_value(SetUiResult::Success)?),
            Err(e) => Err(format_err!("SetVolume failed with err {:?}", e)),
        }
    }

    /// Sets the AudioInput mic to (not)muted depending on input.
    ///
    /// # Arguments
    /// * args: accepted args are "muted" or "available". ex: {"params": "muted"}
    pub async fn set_mic_mute(&self, args: Value) -> Result<Value, Error> {
        let mic_state: MicStates = from_value(args)?;

        // If mic is already in desired state, then nothing left to execute.
        let is_muted = self.is_mic_muted().await?.as_bool().unwrap_or(false);
        let mut mute_mic: bool = false;
        match mic_state {
            MicStates::Muted => {
                if is_muted {
                    return Ok(to_value(SetUiResult::Success)?);
                }
                mute_mic = true;
            }
            MicStates::Available => {
                if !is_muted {
                    return Ok(to_value(SetUiResult::Success)?);
                }
            }
            _ => return Err(format_err!("Mic state must either be muted or available.")),
        }

        // Use given proxy (if possible), else connect to protocol.
        let input_proxy = match self.input_proxy.as_ref() {
            Some(proxy) => proxy.clone(),
            None => match connect_to_protocol::<InputMarker>() {
                Ok(proxy) => proxy,
                Err(e) => bail!("Failed to connect to Microphone {:?}.", e),
            },
        };

        // Initialize the InputState struct.
        let mic_device_name = "microphone";
        let mut input_states = InputState {
            name: Some(mic_device_name.to_string()),
            device_type: Some(fsettings::DeviceType::Microphone),
            state: Some(DeviceState {
                toggle_flags: Some(fsettings::ToggleStateFlags::AVAILABLE),
                ..Default::default()
            }),
            ..Default::default()
        };

        // Change DeviceState if microphone should be muted- dependent on input enum.
        if mute_mic {
            input_states.state = Some(DeviceState {
                toggle_flags: Some(fsettings::ToggleStateFlags::MUTED),
                ..Default::default()
            });
        }

        info!("SetMicMute: setting input state {:?}", input_states);
        match input_proxy.set(&[input_states]).await? {
            Ok(_) => Ok(to_value(SetUiResult::Success)?),
            Err(e) => Err(format_err!("SetMicMute failed with err {:?}", e)),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::common_utils::test::assert_value_round_trips_as;
    use crate::setui::types::MicStates::Muted;
    use crate::setui::types::{HourCycle, LocaleId, TemperatureUnit};
    use fidl::endpoints::create_proxy_and_stream;
    use fidl_fuchsia_settings::InputDevice;
    use fuchsia_async as fasync;
    use futures::TryStreamExt;
    use serde_json::json;

    fn make_intl_info() -> IntlInfo {
        return IntlInfo {
            locales: Some(vec![LocaleId { id: "en-US".into() }]),
            temperature_unit: Some(TemperatureUnit::Celsius),
            time_zone_id: Some("UTC".into()),
            hour_cycle: Some(HourCycle::H12),
        };
    }

    #[test]
    fn serde_intl_set() {
        let intl_request = make_intl_info();
        assert_value_round_trips_as(
            intl_request,
            json!(
            {
                "locales": [{"id": "en-US"}],
                "temperature_unit":"Celsius",
                "time_zone_id": "UTC",
                "hour_cycle": "H12",
            }),
        );
    }

    // Tests that `set_brightness` correctly sends a request to the Display service.
    #[fasync::run_singlethreaded(test)]
    async fn test_set_brightness() {
        let brightness = 0.5f32;
        let (proxy, mut stream) = create_proxy_and_stream::<DisplayMarker>();

        // Create a facade future that sends a request to `proxy`.
        let facade =
            SetUiFacade { audio_proxy: None, display_proxy: Some(proxy), input_proxy: None };
        let facade_fut = async move {
            assert_eq!(
                facade.set_brightness(to_value(brightness).unwrap()).await.unwrap(),
                to_value(SetUiResult::Success).unwrap()
            );
        };

        // Create a future to service the request stream.
        let stream_fut = async move {
            match stream.try_next().await {
                Ok(Some(fsettings::DisplayRequest::Set { settings, responder })) => {
                    assert_eq!(
                        settings,
                        DisplaySettings {
                            auto_brightness: Some(false),
                            brightness_value: Some(brightness),
                            ..Default::default()
                        }
                    );
                    responder.send(Ok(())).unwrap();
                }
                other => panic!("Unexpected stream item: {:?}", other),
            }
        };

        futures::future::join(facade_fut, stream_fut).await;
    }

    // Tests that `set_media_volume` correctly sends a request to the Audio service.
    #[fasync::run_singlethreaded(test)]
    async fn test_set_media_volume() {
        let volume = 0.5f32;
        let (proxy, mut stream) = create_proxy_and_stream::<AudioMarker>();

        // Create a facade future that sends a request to `proxy`.
        let facade =
            SetUiFacade { audio_proxy: Some(proxy), display_proxy: None, input_proxy: None };
        let facade_fut = async move {
            assert_eq!(
                facade.set_media_volume(to_value(volume).unwrap()).await.unwrap(),
                to_value(SetUiResult::Success).unwrap()
            );
        };

        // Create a future to service the request stream.
        let stream_fut = async move {
            match stream.try_next().await {
                Ok(Some(fsettings::AudioRequest::Set { settings, responder })) => {
                    let mut streams = settings.streams.unwrap();
                    assert_eq!(1, streams.len());
                    assert_eq!(
                        streams.pop().unwrap(),
                        AudioStreamSettings {
                            stream: Some(AudioRenderUsage::Media),
                            source: Some(AudioStreamSettingSource::User),
                            user_volume: Some(Volume {
                                level: Some(volume),
                                muted: Some(false),
                                ..Default::default()
                            }),
                            ..Default::default()
                        }
                    );
                    responder.send(Ok(())).unwrap();
                }
                other => panic!("Unexpected stream item: {:?}", other),
            }
        };

        futures::future::join(facade_fut, stream_fut).await;
    }

    // Tests that `set_mic_mute` correctly sends a request to the Input service to
    // mute the device mic.
    #[fasync::run_singlethreaded(test)]
    async fn test_set_mic_mute() {
        let mic_state: MicStates = Muted;
        let (proxy, mut stream) = create_proxy_and_stream::<InputMarker>();

        // Create a facade future that sends a request to `proxy`.
        let facade =
            SetUiFacade { audio_proxy: None, display_proxy: None, input_proxy: Some(proxy) };
        let facade_fut = async move {
            assert_eq!(
                facade.set_mic_mute(to_value(mic_state).unwrap()).await.unwrap(),
                to_value(SetUiResult::Success).unwrap()
            );
        };

        // Create a future to service the request stream.
        let input_stream_fut = async move {
            match stream.try_next().await {
                Ok(Some(fsettings::InputRequest::Watch { responder })) => {
                    let device = InputDevice {
                        device_name: None,
                        device_type: Some(fsettings::DeviceType::Microphone),
                        source_states: None,
                        mutable_toggle_state: None,
                        state: Some(DeviceState {
                            toggle_flags: Some(fsettings::ToggleStateFlags::AVAILABLE),
                            ..Default::default()
                        }),
                        ..Default::default()
                    };
                    let settings = fsettings::InputSettings {
                        devices: Some(vec![device]),
                        ..Default::default()
                    };
                    responder.send(&settings).unwrap();
                }
                other => panic!("Unexpected Watch request: {:?}", other),
            }
            match stream.try_next().await {
                Ok(Some(fsettings::InputRequest::Set { input_states, responder })) => {
                    assert_eq!(
                        input_states[0],
                        InputState {
                            name: Some("microphone".to_string()),
                            device_type: Some(fsettings::DeviceType::Microphone),
                            state: Some(DeviceState {
                                toggle_flags: Some(fsettings::ToggleStateFlags::MUTED),
                                ..Default::default()
                            }),
                            ..Default::default()
                        }
                    );
                    responder.send(Ok(())).unwrap();
                }
                other => panic!("Unexpected stream item: {:?}", other),
            }
        };

        futures::future::join(facade_fut, input_stream_fut).await;
    }

    // Tests that `set_mic_mute` does not send a request to the Input service if the mic is already in desired state.
    #[fasync::run_singlethreaded(test)]
    async fn test_set_mic_mute_in_desired_state() {
        let mic_state: MicStates = Muted;
        let (proxy, mut stream) = create_proxy_and_stream::<InputMarker>();

        // Create a facade future that sends a request to `proxy`.
        let facade =
            SetUiFacade { audio_proxy: None, display_proxy: None, input_proxy: Some(proxy) };
        let facade_fut = async move {
            assert_eq!(
                facade.set_mic_mute(to_value(mic_state).unwrap()).await.unwrap(),
                to_value(SetUiResult::Success).unwrap()
            );
        };

        // Create a future to check that the request stream using Set is never called (due to early termination).
        let input_stream_fut = async move {
            match stream.try_next().await {
                Ok(Some(fsettings::InputRequest::Watch { responder })) => {
                    let device = InputDevice {
                        device_name: None,
                        device_type: Some(fsettings::DeviceType::Microphone),
                        source_states: None,
                        mutable_toggle_state: None,
                        state: Some(DeviceState {
                            toggle_flags: Some(fsettings::ToggleStateFlags::MUTED),
                            ..Default::default()
                        }),
                        ..Default::default()
                    };
                    let settings = fsettings::InputSettings {
                        devices: Some(vec![device]),
                        ..Default::default()
                    };
                    responder.send(&settings).unwrap();
                }
                other => panic!("Unexpected Watch request: {:?}", other),
            }
            match stream.try_next().await {
                Ok(Some(fsettings::InputRequest::Set { input_states, responder: _ })) => {
                    panic!("Unexpected stream item: {:?}", input_states[0]);
                }
                _ => (),
            }
        };

        futures::future::join(facade_fut, input_stream_fut).await;
    }

    // Tests that `is_mic_muted` correctly returns the mic state.
    #[fasync::run_singlethreaded(test)]
    async fn test_is_mic_muted() {
        let is_muted = true;
        let (proxy, mut stream) = create_proxy_and_stream::<InputMarker>();

        // Create a facade future that sends a request to `proxy`.
        let facade =
            SetUiFacade { audio_proxy: None, display_proxy: None, input_proxy: Some(proxy) };
        let facade_fut = async move {
            assert_eq!(facade.is_mic_muted().await.unwrap(), to_value(is_muted).unwrap());
        };

        // Create a future to service the request stream.
        let input_stream_fut = async move {
            match stream.try_next().await {
                Ok(Some(fsettings::InputRequest::Watch { responder })) => {
                    let device = InputDevice {
                        device_name: None,
                        device_type: Some(fsettings::DeviceType::Microphone),
                        source_states: None,
                        mutable_toggle_state: None,
                        state: Some(DeviceState {
                            toggle_flags: Some(fsettings::ToggleStateFlags::MUTED),
                            ..Default::default()
                        }),
                        ..Default::default()
                    };
                    let settings = fsettings::InputSettings {
                        devices: Some(vec![device]),
                        ..Default::default()
                    };
                    responder.send(&settings).unwrap();
                }
                other => panic!("Unexpected Watch request: {:?}", other),
            }
        };

        futures::future::join(facade_fut, input_stream_fut).await;
    }
}